@@ -15,6 +15,7 @@ import * as Icon from '../icon_button/icon_button.js';
1515import { editorTheme } from './theme.js' ;
1616
1717const LINES_TO_SCAN_FOR_INDENTATION_GUESSING = 1000 ;
18+ const RECOMPUTE_INDENT_MAX_SIZE = 200 ;
1819
1920const 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
220238function matcher ( decorator : CM . MatchDecorator ) : CM . Extension {
221239 return CM . ViewPlugin . define (
0 commit comments