Skip to content

Commit dbb9e9b

Browse files
marijnhDevtools-frontend LUCI CQ
authored andcommitted
Re-detect indentation in editor when the unchanged content is <200 chars
[email protected] Fixed: 377820803 Change-Id: I0a52c209fd04a076270776516972efa917bc6d77 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6049060 Commit-Queue: Benedikt Meurer <[email protected]> Reviewed-by: Benedikt Meurer <[email protected]> Reviewed-by: Simon Zünd <[email protected]>
1 parent f367239 commit dbb9e9b

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

front_end/ui/components/text_editor/config.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as Icon from '../icon_button/icon_button.js';
1515
import {editorTheme} from './theme.js';
1616

1717
const LINES_TO_SCAN_FOR_INDENTATION_GUESSING = 1000;
18+
const RECOMPUTE_INDENT_MAX_SIZE = 200;
1819

1920
const UIStrings = {
2021
/**
@@ -209,13 +210,30 @@ export const codeFolding = DynamicSetting.bool('text-editor-code-folding', [
209210
CM.keymap.of(CM.foldKeymap),
210211
]);
211212

212-
const deriveIndentUnit = CM.Prec.highest(CM.indentUnit.compute([], (state: CM.EditorState) => {
213-
const lines = state.doc.iterLines(1, Math.min(state.doc.lines + 1, LINES_TO_SCAN_FOR_INDENTATION_GUESSING));
213+
const AutoDetectIndent = CM.StateField.define<string>({
214+
create: state => detectIndentation(state.doc),
215+
update: (indent, tr) => {
216+
return tr.docChanged && preservedLength(tr.changes) <= RECOMPUTE_INDENT_MAX_SIZE ? detectIndentation(tr.state.doc) :
217+
indent;
218+
},
219+
provide: f => CM.Prec.highest(CM.indentUnit.from(f)),
220+
});
221+
222+
function preservedLength(ch: CM.ChangeDesc): number {
223+
let len = 0;
224+
ch.iterGaps((from, to, l) => {
225+
len += l;
226+
});
227+
return len;
228+
}
229+
230+
function detectIndentation(doc: CM.Text): string {
231+
const lines = doc.iterLines(1, Math.min(doc.lines + 1, LINES_TO_SCAN_FOR_INDENTATION_GUESSING));
214232
const indentUnit = TextUtils.TextUtils.detectIndentation(lines);
215233
return indentUnit ?? Common.Settings.Settings.instance().moduleSetting('text-editor-indent').get();
216-
}));
234+
}
217235

218-
export const autoDetectIndent = DynamicSetting.bool('text-editor-auto-detect-indent', deriveIndentUnit);
236+
export const autoDetectIndent = DynamicSetting.bool('text-editor-auto-detect-indent', AutoDetectIndent);
219237

220238
function matcher(decorator: CM.MatchDecorator): CM.Extension {
221239
return CM.ViewPlugin.define(

0 commit comments

Comments
 (0)