@@ -60,6 +60,47 @@ function getPrecedingValidLine(model: IVirtualModel, lineNumber: number, process
60
60
return - 1 ;
61
61
}
62
62
63
+ /**
64
+ * Get indentation for line from reference line
65
+ */
66
+ function getIndentationForLineFromReferenceLine (
67
+ autoIndent : EditorAutoIndentStrategy ,
68
+ model : IVirtualModel ,
69
+ languageConfigurationService : ILanguageConfigurationService ,
70
+ considerOnEnterRulesForInheritedIndentAfterBlankLine : boolean ,
71
+ indentConverter : IIndentConverter | undefined ,
72
+ referencePrevLineNum : number ,
73
+ currentLineNumber : number
74
+ ) : string {
75
+ let indentation = strings . getLeadingWhitespace ( model . getLineContent ( referencePrevLineNum ) ) ;
76
+ // Check for onEnter rules that should decrease the indent
77
+ if ( indentConverter && considerOnEnterRulesForInheritedIndentAfterBlankLine ) {
78
+ const richEditSupport = languageConfigurationService . getLanguageConfiguration ( model . tokenization . getLanguageId ( ) ) ;
79
+ if ( richEditSupport ) {
80
+ const previousLineText = referencePrevLineNum < 1 ? '' : model . getLineContent ( referencePrevLineNum - 1 ) ;
81
+ let afterEnterText = "" ;
82
+ try {
83
+ afterEnterText = model . getLineContent ( currentLineNumber ) ;
84
+ } catch ( e ) {
85
+ // Probably after the end of file
86
+ }
87
+ const referencePrevLineNumContent = model . getLineContent ( referencePrevLineNum ) ;
88
+ const enterResult = richEditSupport . onEnter ( autoIndent , previousLineText , referencePrevLineNumContent , afterEnterText ) ;
89
+ if ( enterResult ) {
90
+ if ( enterResult . indentAction === IndentAction . Outdent ) {
91
+ indentation = indentConverter . unshiftIndent ( indentation ) ;
92
+ } else if ( enterResult . indentAction === IndentAction . Indent ) {
93
+ indentation = indentConverter . shiftIndent ( indentation ) ;
94
+ } else if ( enterResult . removeText && indentation . length >= enterResult . removeText ) {
95
+ indentation = indentation . substring ( 0 , indentation . length - enterResult . removeText - 1 ) ;
96
+ }
97
+ }
98
+ }
99
+ }
100
+
101
+ return indentation ;
102
+ }
103
+
63
104
/**
64
105
* Get inherited indentation from above lines.
65
106
* 1. Find the nearest preceding line which doesn't match unIndentedLinePattern.
@@ -74,11 +115,12 @@ function getPrecedingValidLine(model: IVirtualModel, lineNumber: number, process
74
115
*/
75
116
export function getInheritIndentForLine (
76
117
autoIndent : EditorAutoIndentStrategy ,
118
+ considerOnEnterRulesForInheritedIndentAfterBlankLine : boolean ,
77
119
model : IVirtualModel ,
78
120
lineNumber : number ,
79
121
honorIntentialIndent : boolean = true ,
80
122
languageConfigurationService : ILanguageConfigurationService ,
81
- indentConverter : IIndentConverter | undefined = undefined
123
+ indentConverter : IIndentConverter | undefined
82
124
) : { indentation : string ; action : IndentAction | null ; line ?: number } | null {
83
125
if ( autoIndent < EditorAutoIndentStrategy . Full ) {
84
126
return null ;
@@ -170,26 +212,8 @@ export function getInheritIndentForLine(
170
212
}
171
213
172
214
if ( honorIntentialIndent ) {
173
- let indentation = strings . getLeadingWhitespace ( model . getLineContent ( precedingUnIgnoredLine ) ) ;
174
- // Check for onEnter rules that should decrease the indent
175
- if ( indentConverter ) {
176
- const richEditSupport = languageConfigurationService . getLanguageConfiguration ( model . tokenization . getLanguageId ( ) ) ;
177
- if ( richEditSupport ) {
178
- const previousLineText = precedingUnIgnoredLine < 1 ? '' : model . getLineContent ( precedingUnIgnoredLine - 1 ) ;
179
- const afterEnterText = model . getLineContent ( lineNumber ) ;
180
- const enterResult = richEditSupport . onEnter ( autoIndent , previousLineText , precedingUnIgnoredLineContent , afterEnterText ) ;
181
- if ( enterResult ) {
182
- if ( enterResult . indentAction === IndentAction . Outdent ) {
183
- indentation = indentConverter . unshiftIndent ( indentation ) ;
184
- } else if ( enterResult . removeText && indentation . length >= enterResult . removeText ) {
185
- indentation = indentation . substring ( 0 , indentation . length - enterResult . removeText - 1 ) ;
186
- }
187
- }
188
- }
189
- }
190
-
191
215
return {
192
- indentation : indentation ,
216
+ indentation : getIndentationForLineFromReferenceLine ( autoIndent , model , languageConfigurationService , considerOnEnterRulesForInheritedIndentAfterBlankLine , indentConverter , precedingUnIgnoredLine , lineNumber ) ,
193
217
action : null ,
194
218
line : precedingUnIgnoredLine
195
219
} ;
@@ -237,6 +261,7 @@ export function getInheritIndentForLine(
237
261
238
262
export function getGoodIndentForLine (
239
263
autoIndent : EditorAutoIndentStrategy ,
264
+ considerOnEnterRulesForInheritedIndentAfterBlankLine : boolean ,
240
265
virtualModel : IVirtualModel ,
241
266
languageId : string ,
242
267
lineNumber : number ,
@@ -258,7 +283,7 @@ export function getGoodIndentForLine(
258
283
}
259
284
260
285
const processedIndentRulesSupport = new ProcessedIndentRulesSupport ( virtualModel , indentRulesSupport , languageConfigurationService ) ;
261
- const indent = getInheritIndentForLine ( autoIndent , virtualModel , lineNumber , undefined , languageConfigurationService , indentConverter ) ;
286
+ const indent = getInheritIndentForLine ( autoIndent , considerOnEnterRulesForInheritedIndentAfterBlankLine , virtualModel , lineNumber , undefined , languageConfigurationService , indentConverter ) ;
262
287
263
288
if ( indent ) {
264
289
const inheritLine = indent . line ;
@@ -322,6 +347,7 @@ export function getGoodIndentForLine(
322
347
323
348
export function getIndentForEnter (
324
349
autoIndent : EditorAutoIndentStrategy ,
350
+ considerOnEnterRulesForInheritedIndentAfterBlankLine : boolean ,
325
351
model : ITextModel ,
326
352
range : Range ,
327
353
indentConverter : IIndentConverter ,
@@ -347,7 +373,7 @@ export function getIndentForEnter(
347
373
const languageIsDifferentFromLineStart = isLanguageDifferentFromLineStart ( model , range . getStartPosition ( ) ) ;
348
374
const currentLine = model . getLineContent ( range . startLineNumber ) ;
349
375
const currentLineIndent = strings . getLeadingWhitespace ( currentLine ) ;
350
- const afterEnterAction = getInheritIndentForLine ( autoIndent , virtualModel , range . startLineNumber + 1 , undefined , languageConfigurationService , indentConverter ) ;
376
+ const afterEnterAction = getInheritIndentForLine ( autoIndent , considerOnEnterRulesForInheritedIndentAfterBlankLine , virtualModel , range . startLineNumber + 1 , undefined , languageConfigurationService , indentConverter ) ;
351
377
if ( ! afterEnterAction ) {
352
378
const beforeEnter = languageIsDifferentFromLineStart ? currentLineIndent : beforeEnterIndent ;
353
379
return {
@@ -388,6 +414,7 @@ export function getIndentActionForType(
388
414
if ( autoIndent < EditorAutoIndentStrategy . Full ) {
389
415
return null ;
390
416
}
417
+ const considerOnEnterRulesForInheritedIndentAfterBlankLine = cursorConfig . considerOnEnterRulesForInheritedIndentAfterBlankLine ;
391
418
const languageIsDifferentFromLineStart = isLanguageDifferentFromLineStart ( model , range . getStartPosition ( ) ) ;
392
419
if ( languageIsDifferentFromLineStart ) {
393
420
// this line has mixed languages and indentation rules will not work
@@ -412,7 +439,7 @@ export function getIndentActionForType(
412
439
if ( ! indentRulesSupport . shouldDecrease ( textAroundRange ) && indentRulesSupport . shouldDecrease ( textAroundRangeWithCharacter ) ) {
413
440
// after typing `ch`, the content matches decreaseIndentPattern, we should adjust the indent to a good manner.
414
441
// 1. Get inherited indent action
415
- const r = getInheritIndentForLine ( autoIndent , model , range . startLineNumber , false , languageConfigurationService , indentConverter ) ;
442
+ const r = getInheritIndentForLine ( autoIndent , considerOnEnterRulesForInheritedIndentAfterBlankLine , model , range . startLineNumber , false , languageConfigurationService , indentConverter ) ;
416
443
if ( ! r ) {
417
444
return null ;
418
445
}
@@ -429,7 +456,7 @@ export function getIndentActionForType(
429
456
if ( previousLineNumber > 0 ) {
430
457
const previousLine = model . getLineContent ( previousLineNumber ) ;
431
458
if ( indentRulesSupport . shouldIndentNextLine ( previousLine ) && indentRulesSupport . shouldIncrease ( textAroundRangeWithCharacter ) ) {
432
- const inheritedIndentationData = getInheritIndentForLine ( autoIndent , model , range . startLineNumber , false , languageConfigurationService ) ;
459
+ const inheritedIndentationData = getInheritIndentForLine ( autoIndent , considerOnEnterRulesForInheritedIndentAfterBlankLine , model , range . startLineNumber , false , languageConfigurationService , undefined ) ;
433
460
const inheritedIndentation = inheritedIndentationData ?. indentation ;
434
461
if ( inheritedIndentation !== undefined ) {
435
462
const currentLine = model . getLineContent ( range . startLineNumber ) ;
0 commit comments