Skip to content

Commit 83032f9

Browse files
committed
fix: remove rxjs from _formatCodeInTextEditor
1 parent 84459d6 commit 83032f9

File tree

1 file changed

+50
-59
lines changed

1 file changed

+50
-59
lines changed

src/CodeFormatManager.ts

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -126,66 +126,57 @@ export default class CodeFormatManager {
126126
}
127127

128128
// Return the text edits used to format code in the editor specified.
129-
_formatCodeInTextEditor(editor: TextEditor, range?: Range): Observable<Array<TextEdit>> {
130-
return Observable.defer(() => {
131-
const buffer = editor.getBuffer()
132-
const selectionRange = range || editor.getSelectedBufferRange()
133-
const { start: selectionStart, end: selectionEnd } = selectionRange
134-
let formatRange: Range
135-
if (selectionRange.isEmpty()) {
136-
// If no selection is done, then, the whole file is wanted to be formatted.
137-
formatRange = buffer.getRange()
138-
} else {
139-
// Format selections should start at the beginning of the line,
140-
// and include the last selected line end.
141-
// (If the user has already selected complete rows, then depending on how they
142-
// did it, their caret might be either (1) at the end of their last selected line
143-
// or (2) at the first column of the line AFTER their selection. In both cases
144-
// we snap the formatRange to end at the first column of the line after their
145-
// selection.)
146-
formatRange = new Range(
147-
[selectionStart.row, 0],
148-
selectionEnd.column === 0 ? selectionEnd : [selectionEnd.row + 1, 0]
149-
)
150-
}
151-
const rangeProviders = [...this._rangeProviders.getAllProvidersForEditor(editor)]
152-
const fileProviders = [...this._fileProviders.getAllProvidersForEditor(editor)]
153-
const contents = editor.getText()
154-
const rangeEdits = Observable.defer(() =>
155-
this._reportBusy(editor, Promise.all(rangeProviders.map((p) => p.formatCode(editor, formatRange))))
156-
).switchMap((allEdits) => {
157-
const firstNonEmpty = allEdits.find((edits) => edits.length > 0)
158-
if (firstNonEmpty == null) {
159-
return Observable.empty()
160-
} else {
161-
return Observable.of(firstNonEmpty)
162-
}
163-
})
164-
const fileEdits = Observable.defer(() =>
165-
this._reportBusy(editor, Promise.all(fileProviders.map((p) => p.formatEntireFile(editor, formatRange))))
129+
async _formatCodeInTextEditor(editor: TextEditor, range?: Range): Promise<Array<TextEdit>> {
130+
const buffer = editor.getBuffer()
131+
const selectionRange = range || editor.getSelectedBufferRange()
132+
const { start: selectionStart, end: selectionEnd } = selectionRange
133+
let formatRange: Range
134+
if (selectionRange.isEmpty()) {
135+
// If no selection is done, then, the whole file is wanted to be formatted.
136+
formatRange = buffer.getRange()
137+
} else {
138+
// Format selections should start at the beginning of the line,
139+
// and include the last selected line end.
140+
// (If the user has already selected complete rows, then depending on how they
141+
// did it, their caret might be either (1) at the end of their last selected line
142+
// or (2) at the first column of the line AFTER their selection. In both cases
143+
// we snap the formatRange to end at the first column of the line after their
144+
// selection.)
145+
formatRange = new Range(
146+
[selectionStart.row, 0],
147+
selectionEnd.column === 0 ? selectionEnd : [selectionEnd.row + 1, 0]
166148
)
167-
.switchMap((allResults) => {
168-
const firstNonNull = allResults.find((result) => result != null)
169-
if (firstNonNull == null) {
170-
return Observable.empty()
171-
} else {
172-
return Observable.of(firstNonNull)
173-
}
174-
})
175-
.map(({ formatted }) => {
176-
return [
177-
{
178-
oldRange: editor.getBuffer().getRange(),
179-
newText: formatted,
180-
oldText: contents,
181-
},
182-
]
183-
})
184-
// When formatting the entire file, prefer file-based providers.
185-
const preferFileEdits = formatRange.isEqual(buffer.getRange())
186-
const edits = preferFileEdits ? fileEdits.concat(rangeEdits) : rangeEdits.concat(fileEdits)
187-
return edits.first(Boolean, [])
188-
})
149+
}
150+
const rangeProviders = [...this._rangeProviders.getAllProvidersForEditor(editor)]
151+
const fileProviders = [...this._fileProviders.getAllProvidersForEditor(editor)]
152+
const contents = editor.getText()
153+
154+
const allEdits = await this._reportBusy(
155+
editor,
156+
Promise.all(rangeProviders.map((p) => p.formatCode(editor, formatRange)))
157+
)
158+
const rangeEdits = allEdits.filter((edits) => edits.length > 0)
159+
160+
const allResults = await this._reportBusy(
161+
editor,
162+
Promise.all(fileProviders.map((p) => p.formatEntireFile(editor, formatRange)))
163+
)
164+
const nonNullResults = allResults.filter((result) => result !== null && result !== undefined) as {
165+
newCursor?: number
166+
formatted: string
167+
}[]
168+
const fileEdits = nonNullResults.map(({ formatted }) => [
169+
{
170+
oldRange: editor.getBuffer().getRange(),
171+
newText: formatted,
172+
oldText: contents,
173+
} as TextEdit,
174+
])
175+
176+
// When formatting the entire file, prefer file-based providers.
177+
const preferFileEdits = formatRange.isEqual(buffer.getRange())
178+
const edits = preferFileEdits ? fileEdits.concat(rangeEdits) : rangeEdits.concat(fileEdits)
179+
return edits.flat() // TODO or [0]?
189180
}
190181

191182
_formatCodeOnTypeInTextEditor(

0 commit comments

Comments
 (0)