Skip to content

Commit 0795945

Browse files
committed
fix: remove rxjs from code-format:format-code
1 parent a92d37d commit 0795945

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

src/CodeFormatManager.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type FormatEvent =
4141
}
4242

4343
export default class CodeFormatManager {
44-
_subscriptions: UniversalDisposable
44+
_subscriptions = new UniversalDisposable()
4545
_rangeProviders: ProviderRegistry<RangeCodeFormatProvider>
4646
_fileProviders: ProviderRegistry<FileCodeFormatProvider>
4747
_onTypeProviders: ProviderRegistry<OnTypeCodeFormatProvider>
@@ -50,7 +50,6 @@ export default class CodeFormatManager {
5050

5151
constructor() {
5252
this._subscriptions = new UniversalDisposable(
53-
this._subscribeToEvents(),
5453
registerOnWillSave(this._onWillSaveProvider())
5554
)
5655
this._rangeProviders = new ProviderRegistry()
@@ -65,23 +64,35 @@ export default class CodeFormatManager {
6564
*/
6665
_subscribeToEvents(): Subscription {
6766
// Events from the explicit Atom command.
68-
const commandEvents = observableFromSubscribeFunction((callback) =>
69-
atom.commands.add("atom-text-editor", "code-format:format-code", callback)
70-
).switchMap(() => {
71-
const editor = atom.workspace.getActiveTextEditor()
72-
if (!editor) {
73-
return Observable.empty()
74-
}
75-
return Observable.of({ type: "command", editor })
76-
})
67+
this._subscriptions.add(
68+
atom.commands.add("atom-text-editor", "code-format:format-code", async (event) => {
69+
const editor = atom.workspace.getActiveTextEditor()
70+
if (!editor) {
71+
return
72+
}
73+
// Make sure we halt everything when the editor gets destroyed.
74+
const edits = await this._formatCodeInTextEditor(editor)
75+
try {
76+
applyTextEditsToBuffer(editor.getBuffer(), edits).forEach((result) => {
77+
if (!result) {
78+
throw new Error("No code formatting providers found!")
79+
}
80+
})
81+
} catch (err) {
82+
atom.notifications.addError(`Failed to format code: ${err.message}`, {
83+
detail: err.detail,
84+
})
85+
}
86+
})
87+
)
7788

7889
// Events from editor actions (saving, typing).
7990
const editorEvents = observableFromSubscribeFunction((cb) => atom.workspace.observeTextEditors(cb)).mergeMap(
8091
(editor) => _getEditorEventStream(editor)
8192
)
8293

8394
return (
84-
Observable.merge(commandEvents, editorEvents)
95+
editorEvents
8596
// Group events by buffer to prevent simultaneous formatting operations.
8697
.groupBy(
8798
(event) => event.editor.getBuffer(),
@@ -99,21 +110,6 @@ export default class CodeFormatManager {
99110
async _handleEvent(event: FormatEvent) {
100111
const { editor } = event
101112
switch (event.type) {
102-
case "command": {
103-
const edits = await this._formatCodeInTextEditor(editor)
104-
try {
105-
applyTextEditsToBuffer(editor.getBuffer(), edits).forEach((result) => {
106-
if (!result) {
107-
throw new Error("No code formatting providers found!")
108-
}
109-
})
110-
} catch (err) {
111-
atom.notifications.addError(`Failed to format code: ${err.message}`, {
112-
detail: err.detail,
113-
})
114-
}
115-
break
116-
}
117113
case "type":
118114
return this._formatCodeOnTypeInTextEditor(editor, event.edit).catch((err) => {
119115
getLogger("code-format").warn("Failed to format code on type:", err)

0 commit comments

Comments
 (0)