Skip to content

Commit bf65dba

Browse files
committed
using on enter rules to calculate indent
1 parent b126f58 commit bf65dba

File tree

16 files changed

+125
-130
lines changed

16 files changed

+125
-130
lines changed

src/vs/editor/common/config/editorOptions.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ export interface IEditorOptions {
790790
/**
791791
* When the preceding line is a blank line, use OnEnterRule on the previous non-blank line to determine the indentation.
792792
*/
793-
considerOnEnterRulesForInheritedIndentAfterBlankLine?: boolean;
793+
useOnEnterRulesForInheritedIndent?: boolean;
794794
}
795795

796796
/**
@@ -5653,7 +5653,7 @@ export const enum EditorOption {
56535653
colorDecoratorsActivatedOn,
56545654
inlineCompletionsAccessibilityVerbose,
56555655
effectiveExperimentalEditContextEnabled,
5656-
considerOnEnterRulesForInheritedIndentAfterBlankLine,
5656+
useOnEnterRulesForInheritedIndent,
56575657
}
56585658

56595659
export const EditorOptions = {
@@ -6487,9 +6487,9 @@ export const EditorOptions = {
64876487
wrappingIndent: register(new WrappingIndentOption()),
64886488
wrappingStrategy: register(new WrappingStrategy()),
64896489
effectiveExperimentalEditContextEnabled: register(new EffectiveExperimentalEditContextEnabled()),
6490-
considerOnEnterRulesForInheritedIndentAfterBlankLine: register(new EditorBooleanOption(
6491-
EditorOption.considerOnEnterRulesForInheritedIndentAfterBlankLine, 'considerOnEnterRulesForInheritedIndentAfterBlankLine', false,
6492-
{ description: nls.localize('considerOnEnterRulesForInheritedIndentAfterBlankLine', "When the preceding line is a blank line, use OnEnterRule on the previous non-blank line to determine the indentation.") }
6490+
useOnEnterRulesForInheritedIndent: register(new EditorBooleanOption(
6491+
EditorOption.useOnEnterRulesForInheritedIndent, 'useOnEnterRulesForInheritedIndent', false,
6492+
{ description: nls.localize('useOnEnterRulesForInheritedIndent', "When the preceding line is a blank line, use OnEnterRule on the previous non-blank line to determine the indentation.") }
64936493
)),
64946494
};
64956495

src/vs/editor/common/cursor/cursorTypeEditOperations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ export class EnterOperation {
570570
const indentation = strings.getLeadingWhitespace(lineText).substring(0, range.startColumn - 1);
571571

572572
if (config.autoIndent >= EditorAutoIndentStrategy.Full) {
573-
const ir = getIndentForEnter(config.autoIndent, config.considerOnEnterRulesForInheritedIndentAfterBlankLine, model, range, {
573+
const ir = getIndentForEnter(config, model, range, {
574574
unshiftIndent: (indent) => {
575575
return unshiftIndent(config, indent);
576576
},
@@ -815,7 +815,7 @@ export class TabOperation {
815815
private static _goodIndentForLine(config: CursorConfiguration, model: ITextModel, lineNumber: number): string | null {
816816
let action: IndentAction | EnterAction | null = null;
817817
let indentation: string = '';
818-
const expectedIndentAction = getInheritIndentForLine(config.autoIndent, config.considerOnEnterRulesForInheritedIndentAfterBlankLine, model, lineNumber, false, config.languageConfigurationService, undefined);
818+
const expectedIndentAction = getInheritIndentForLine(config.autoIndent, config.useOnEnterRulesForInheritedIndent, model, lineNumber, false, config.languageConfigurationService);
819819
if (expectedIndentAction) {
820820
action = expectedIndentAction.action;
821821
indentation = expectedIndentAction.indentation;

src/vs/editor/common/cursorCommon.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class CursorConfiguration {
7979
public readonly shouldAutoCloseBefore: { quote: (ch: string) => boolean; bracket: (ch: string) => boolean; comment: (ch: string) => boolean };
8080
public readonly wordSegmenterLocales: string[];
8181
public readonly overtypeOnPaste: boolean;
82-
public readonly considerOnEnterRulesForInheritedIndentAfterBlankLine: boolean;
82+
public readonly useOnEnterRulesForInheritedIndent: boolean;
8383

8484
private readonly _languageId: string;
8585
private _electricChars: { [key: string]: boolean } | null;
@@ -103,7 +103,6 @@ export class CursorConfiguration {
103103
|| e.hasChanged(EditorOption.readOnly)
104104
|| e.hasChanged(EditorOption.wordSegmenterLocales)
105105
|| e.hasChanged(EditorOption.overtypeOnPaste)
106-
|| e.hasChanged(EditorOption.considerOnEnterRulesForInheritedIndentAfterBlankLine)
107106
);
108107
}
109108

@@ -143,7 +142,7 @@ export class CursorConfiguration {
143142
this.autoIndent = options.get(EditorOption.autoIndent);
144143
this.wordSegmenterLocales = options.get(EditorOption.wordSegmenterLocales);
145144
this.overtypeOnPaste = options.get(EditorOption.overtypeOnPaste);
146-
this.considerOnEnterRulesForInheritedIndentAfterBlankLine = options.get(EditorOption.considerOnEnterRulesForInheritedIndentAfterBlankLine);
145+
this.useOnEnterRulesForInheritedIndent = options.get(EditorOption.useOnEnterRulesForInheritedIndent);
147146

148147
this.surroundingPairs = {};
149148
this._electricChars = null;

src/vs/editor/common/languages/autoIndent.ts

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -60,47 +60,6 @@ function getPrecedingValidLine(model: IVirtualModel, lineNumber: number, process
6060
return -1;
6161
}
6262

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-
10463
/**
10564
* Get inherited indentation from above lines.
10665
* 1. Find the nearest preceding line which doesn't match unIndentedLinePattern.
@@ -115,12 +74,11 @@ function getIndentationForLineFromReferenceLine(
11574
*/
11675
export function getInheritIndentForLine(
11776
autoIndent: EditorAutoIndentStrategy,
118-
considerOnEnterRulesForInheritedIndentAfterBlankLine: boolean,
77+
useOnEnterRulesForInheritedIndent: boolean,
11978
model: IVirtualModel,
12079
lineNumber: number,
12180
honorIntentialIndent: boolean = true,
122-
languageConfigurationService: ILanguageConfigurationService,
123-
indentConverter: IIndentConverter | undefined
81+
languageConfigurationService: ILanguageConfigurationService
12482
): { indentation: string; action: IndentAction | null; line?: number } | null {
12583
if (autoIndent < EditorAutoIndentStrategy.Full) {
12684
return null;
@@ -130,7 +88,7 @@ export function getInheritIndentForLine(
13088
if (!indentRulesSupport) {
13189
return null;
13290
}
133-
const processedIndentRulesSupport = new ProcessedIndentRulesSupport(model, indentRulesSupport, languageConfigurationService);
91+
const processedIndentRulesSupport = new ProcessedIndentRulesSupport(model, autoIndent, useOnEnterRulesForInheritedIndent, indentRulesSupport, languageConfigurationService);
13492

13593
if (lineNumber <= 1) {
13694
return {
@@ -192,7 +150,7 @@ export function getInheritIndentForLine(
192150

193151
const previousLine = precedingUnIgnoredLine - 1;
194152

195-
const previousLineIndentMetadata = indentRulesSupport.getIndentMetadata(model.getLineContent(previousLine));
153+
const previousLineIndentMetadata = indentRulesSupport.getIndentMetadata(autoIndent, useOnEnterRulesForInheritedIndent, model.getLineContent(previousLine));
196154
if (!(previousLineIndentMetadata & (IndentConsts.INCREASE_MASK | IndentConsts.DECREASE_MASK)) &&
197155
(previousLineIndentMetadata & IndentConsts.INDENT_NEXTLINE_MASK)) {
198156
let stopLine = 0;
@@ -213,7 +171,7 @@ export function getInheritIndentForLine(
213171

214172
if (honorIntentialIndent) {
215173
return {
216-
indentation: getIndentationForLineFromReferenceLine(autoIndent, model, languageConfigurationService, considerOnEnterRulesForInheritedIndentAfterBlankLine, indentConverter, precedingUnIgnoredLine, lineNumber),
174+
indentation: strings.getLeadingWhitespace(model.getLineContent(precedingUnIgnoredLine)),
217175
action: null,
218176
line: precedingUnIgnoredLine
219177
};
@@ -261,7 +219,7 @@ export function getInheritIndentForLine(
261219

262220
export function getGoodIndentForLine(
263221
autoIndent: EditorAutoIndentStrategy,
264-
considerOnEnterRulesForInheritedIndentAfterBlankLine: boolean,
222+
useOnEnterRulesForInheritedIndent: boolean,
265223
virtualModel: IVirtualModel,
266224
languageId: string,
267225
lineNumber: number,
@@ -282,8 +240,8 @@ export function getGoodIndentForLine(
282240
return null;
283241
}
284242

285-
const processedIndentRulesSupport = new ProcessedIndentRulesSupport(virtualModel, indentRulesSupport, languageConfigurationService);
286-
const indent = getInheritIndentForLine(autoIndent, considerOnEnterRulesForInheritedIndentAfterBlankLine, virtualModel, lineNumber, undefined, languageConfigurationService, indentConverter);
243+
const processedIndentRulesSupport = new ProcessedIndentRulesSupport(virtualModel, autoIndent, useOnEnterRulesForInheritedIndent, indentRulesSupport, languageConfigurationService);
244+
const indent = getInheritIndentForLine(autoIndent, useOnEnterRulesForInheritedIndent, virtualModel, lineNumber, undefined, languageConfigurationService);
287245

288246
if (indent) {
289247
const inheritLine = indent.line;
@@ -346,13 +304,13 @@ export function getGoodIndentForLine(
346304
}
347305

348306
export function getIndentForEnter(
349-
autoIndent: EditorAutoIndentStrategy,
350-
considerOnEnterRulesForInheritedIndentAfterBlankLine: boolean,
307+
cursorConfig: CursorConfiguration,
351308
model: ITextModel,
352309
range: Range,
353310
indentConverter: IIndentConverter,
354311
languageConfigurationService: ILanguageConfigurationService
355312
): { beforeEnter: string; afterEnter: string } | null {
313+
const autoIndent = cursorConfig.autoIndent;
356314
if (autoIndent < EditorAutoIndentStrategy.Full) {
357315
return null;
358316
}
@@ -373,7 +331,7 @@ export function getIndentForEnter(
373331
const languageIsDifferentFromLineStart = isLanguageDifferentFromLineStart(model, range.getStartPosition());
374332
const currentLine = model.getLineContent(range.startLineNumber);
375333
const currentLineIndent = strings.getLeadingWhitespace(currentLine);
376-
const afterEnterAction = getInheritIndentForLine(autoIndent, considerOnEnterRulesForInheritedIndentAfterBlankLine, virtualModel, range.startLineNumber + 1, undefined, languageConfigurationService, indentConverter);
334+
const afterEnterAction = getInheritIndentForLine(cursorConfig.autoIndent, cursorConfig.useOnEnterRulesForInheritedIndent, virtualModel, range.startLineNumber + 1, undefined, languageConfigurationService);
377335
if (!afterEnterAction) {
378336
const beforeEnter = languageIsDifferentFromLineStart ? currentLineIndent : beforeEnterIndent;
379337
return {
@@ -388,7 +346,7 @@ export function getIndentForEnter(
388346
afterEnterIndent = indentConverter.shiftIndent(afterEnterIndent);
389347
}
390348

391-
if (indentRulesSupport.shouldDecrease(afterEnterProcessedTokens.getLineContent())) {
349+
if (indentRulesSupport.shouldDecrease(cursorConfig.autoIndent, cursorConfig.useOnEnterRulesForInheritedIndent, afterEnterProcessedTokens.getLineContent())) {
392350
afterEnterIndent = indentConverter.unshiftIndent(afterEnterIndent);
393351
}
394352

@@ -414,7 +372,6 @@ export function getIndentActionForType(
414372
if (autoIndent < EditorAutoIndentStrategy.Full) {
415373
return null;
416374
}
417-
const considerOnEnterRulesForInheritedIndentAfterBlankLine = cursorConfig.considerOnEnterRulesForInheritedIndentAfterBlankLine;
418375
const languageIsDifferentFromLineStart = isLanguageDifferentFromLineStart(model, range.getStartPosition());
419376
if (languageIsDifferentFromLineStart) {
420377
// this line has mixed languages and indentation rules will not work
@@ -436,10 +393,10 @@ export function getIndentActionForType(
436393

437394
// If previous content already matches decreaseIndentPattern, it means indentation of this line should already be adjusted
438395
// Users might change the indentation by purpose and we should honor that instead of readjusting.
439-
if (!indentRulesSupport.shouldDecrease(textAroundRange) && indentRulesSupport.shouldDecrease(textAroundRangeWithCharacter)) {
396+
if (!indentRulesSupport.shouldDecrease(cursorConfig.autoIndent, cursorConfig.useOnEnterRulesForInheritedIndent, textAroundRange) && indentRulesSupport.shouldDecrease(cursorConfig.autoIndent, cursorConfig.useOnEnterRulesForInheritedIndent, textAroundRangeWithCharacter)) {
440397
// after typing `ch`, the content matches decreaseIndentPattern, we should adjust the indent to a good manner.
441398
// 1. Get inherited indent action
442-
const r = getInheritIndentForLine(autoIndent, considerOnEnterRulesForInheritedIndentAfterBlankLine, model, range.startLineNumber, false, languageConfigurationService, indentConverter);
399+
const r = getInheritIndentForLine(cursorConfig.autoIndent, cursorConfig.useOnEnterRulesForInheritedIndent, model, range.startLineNumber, false, languageConfigurationService);
443400
if (!r) {
444401
return null;
445402
}
@@ -455,8 +412,8 @@ export function getIndentActionForType(
455412
const previousLineNumber = range.startLineNumber - 1;
456413
if (previousLineNumber > 0) {
457414
const previousLine = model.getLineContent(previousLineNumber);
458-
if (indentRulesSupport.shouldIndentNextLine(previousLine) && indentRulesSupport.shouldIncrease(textAroundRangeWithCharacter)) {
459-
const inheritedIndentationData = getInheritIndentForLine(autoIndent, considerOnEnterRulesForInheritedIndentAfterBlankLine, model, range.startLineNumber, false, languageConfigurationService, undefined);
415+
if (indentRulesSupport.shouldIndentNextLine(cursorConfig.autoIndent, cursorConfig.useOnEnterRulesForInheritedIndent, previousLine) && indentRulesSupport.shouldIncrease(cursorConfig.autoIndent, cursorConfig.useOnEnterRulesForInheritedIndent, textAroundRangeWithCharacter)) {
416+
const inheritedIndentationData = getInheritIndentForLine(cursorConfig.autoIndent, cursorConfig.useOnEnterRulesForInheritedIndent, model, range.startLineNumber, false, languageConfigurationService);
460417
const inheritedIndentation = inheritedIndentationData?.indentation;
461418
if (inheritedIndentation !== undefined) {
462419
const currentLine = model.getLineContent(range.startLineNumber);
@@ -480,6 +437,8 @@ export function getIndentActionForType(
480437

481438
export function getIndentMetadata(
482439
model: ITextModel,
440+
autoIndent: EditorAutoIndentStrategy,
441+
useOnEnterRulesForInheritedIndent: boolean,
483442
lineNumber: number,
484443
languageConfigurationService: ILanguageConfigurationService
485444
): number | null {
@@ -490,7 +449,7 @@ export function getIndentMetadata(
490449
if (lineNumber < 1 || lineNumber > model.getLineCount()) {
491450
return null;
492451
}
493-
return indentRulesSupport.getIndentMetadata(model.getLineContent(lineNumber));
452+
return indentRulesSupport.getIndentMetadata(autoIndent, useOnEnterRulesForInheritedIndent, model.getLineContent(lineNumber));
494453
}
495454

496455
function createVirtualModelWithModifiedTokensAtLine(model: ITextModel, modifiedLineNumber: number, modifiedTokens: IViewLineTokens): IVirtualModel {

src/vs/editor/common/languages/languageConfigurationRegistry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ export class ResolvedLanguageConfiguration {
379379
this.indentationRules = this.underlyingConfig.indentationRules;
380380
if (this.underlyingConfig.indentationRules) {
381381
this.indentRulesSupport = new IndentRulesSupport(
382-
this.underlyingConfig.indentationRules
382+
this.underlyingConfig.indentationRules,
383+
this._onEnterSupport
383384
);
384385
} else {
385386
this.indentRulesSupport = null;

0 commit comments

Comments
 (0)