Skip to content

Commit 16009d4

Browse files
committed
fix: optimize _formatCodeOnTypeInTextEditor
1 parent 29b83d3 commit 16009d4

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/CodeFormatManager.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export default class CodeFormatManager {
5353
const editorSubs = new CompositeDisposable(
5454
// Format on typing in the editor
5555
editor.getBuffer().onDidStopChanging(async (event) => {
56+
if (!getFormatOnType()) {
57+
return
58+
}
5659
try {
5760
await this._formatCodeOnTypeInTextEditor(editor, event)
5861
} catch (err) {
@@ -148,26 +151,26 @@ export default class CodeFormatManager {
148151

149152
async _formatCodeOnTypeInTextEditor(
150153
editor: TextEditor,
151-
aggregatedEvent: BufferStoppedChangingEvent
154+
{ changes }: BufferStoppedChangingEvent
152155
): Promise<Array<TextEdit>> {
153156
// Don't try to format changes with multiple cursors.
154-
if (aggregatedEvent.changes.length !== 1) {
157+
if (changes.length !== 1) {
158+
return []
159+
}
160+
// if no provider return immediately
161+
const providers = [...this._onTypeProviders.getAllProvidersForEditor(editor)]
162+
if (providers.length === 0) {
155163
return []
156164
}
157-
const event = aggregatedEvent.changes[0]
165+
const event = changes[0]
158166
// This also ensures the non-emptiness of event.newText for below.
159-
if (!shouldFormatOnType(event) || !getFormatOnType()) {
167+
if (!shouldFormatOnType(event)) {
160168
return []
161169
}
162170
// In the case of bracket-matching, we use the last character because that's
163171
// the character that will usually cause a reformat (i.e. `}` instead of `{`).
164172
const character = event.newText[event.newText.length - 1]
165173

166-
const providers = [...this._onTypeProviders.getAllProvidersForEditor(editor)]
167-
if (providers.length === 0) {
168-
return []
169-
}
170-
171174
const contents = editor.getText()
172175
const cursorPosition = editor.getCursorBufferPosition().copy()
173176

0 commit comments

Comments
 (0)