Skip to content

Commit 2c208a9

Browse files
committed
Introduce and adapt CompleteEnterAction
1 parent 0789507 commit 2c208a9

5 files changed

Lines changed: 64 additions & 49 deletions

File tree

src/vs/editor/common/commands/shiftCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class ShiftCommand implements ICommand {
139139
// The current line is "miss-aligned", so let's see if this is expected...
140140
// This can only happen when it has trailing commas in the indent
141141
if (model.isCheapToTokenize(lineNumber - 1)) {
142-
let enterAction = LanguageConfigurationRegistry.getRawEnterActionAtPosition(this._opts.autoIndent, model, lineNumber - 1, model.getLineMaxColumn(lineNumber - 1));
142+
let enterAction = LanguageConfigurationRegistry.getEnterAction(this._opts.autoIndent, model, new Range(lineNumber - 1, model.getLineMaxColumn(lineNumber - 1), lineNumber - 1, model.getLineMaxColumn(lineNumber - 1)));
143143
if (enterAction) {
144144
extraSpaces = previousLineExtraSpaces;
145145
if (enterAction.appendText) {

src/vs/editor/common/controller/cursorTypeOperations.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,7 @@ export class TypeOperations {
174174
const maxColumn = model.getLineMaxColumn(lastLineNumber);
175175
const expectedEnterAction = LanguageConfigurationRegistry.getEnterAction(config.autoIndent, model, new Range(lastLineNumber, maxColumn, lastLineNumber, maxColumn));
176176
if (expectedEnterAction) {
177-
indentation = expectedEnterAction.indentation;
178-
action = expectedEnterAction.enterAction;
179-
if (action) {
180-
indentation += action.appendText;
181-
}
177+
indentation = expectedEnterAction.indentation + expectedEnterAction.appendText;
182178
}
183179
}
184180

@@ -304,21 +300,18 @@ export class TypeOperations {
304300

305301
const r = LanguageConfigurationRegistry.getEnterAction(config.autoIndent, model, range);
306302
if (r) {
307-
const enterAction = r.enterAction;
308-
const indentation = r.indentation;
309-
310-
if (enterAction.indentAction === IndentAction.None) {
303+
if (r.indentAction === IndentAction.None) {
311304
// Nothing special
312-
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation + enterAction.appendText), keepPosition);
305+
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(r.indentation + r.appendText), keepPosition);
313306

314-
} else if (enterAction.indentAction === IndentAction.Indent) {
307+
} else if (r.indentAction === IndentAction.Indent) {
315308
// Indent once
316-
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation + enterAction.appendText), keepPosition);
309+
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(r.indentation + r.appendText), keepPosition);
317310

318-
} else if (enterAction.indentAction === IndentAction.IndentOutdent) {
311+
} else if (r.indentAction === IndentAction.IndentOutdent) {
319312
// Ultra special
320-
const normalIndent = config.normalizeIndentation(indentation);
321-
const increasedIndent = config.normalizeIndentation(indentation + enterAction.appendText);
313+
const normalIndent = config.normalizeIndentation(r.indentation);
314+
const increasedIndent = config.normalizeIndentation(r.indentation + r.appendText);
322315

323316
const typeText = '\n' + increasedIndent + '\n' + normalIndent;
324317

@@ -327,9 +320,9 @@ export class TypeOperations {
327320
} else {
328321
return new ReplaceCommandWithOffsetCursorState(range, typeText, -1, increasedIndent.length - normalIndent.length, true);
329322
}
330-
} else if (enterAction.indentAction === IndentAction.Outdent) {
331-
const actualIndentation = TypeOperations.unshiftIndent(config, indentation);
332-
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(actualIndentation + enterAction.appendText), keepPosition);
323+
} else if (r.indentAction === IndentAction.Outdent) {
324+
const actualIndentation = TypeOperations.unshiftIndent(config, r.indentation);
325+
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(actualIndentation + r.appendText), keepPosition);
333326
}
334327
}
335328

src/vs/editor/common/modes/languageConfiguration.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,28 @@ export interface EnterAction {
228228
removeText?: number;
229229
}
230230

231+
/**
232+
* @internal
233+
*/
234+
export interface CompleteEnterAction {
235+
/**
236+
* Describe what to do with the indentation.
237+
*/
238+
indentAction: IndentAction;
239+
/**
240+
* Describes text to be appended after the new line and after the indentation.
241+
*/
242+
appendText: string;
243+
/**
244+
* Describes the number of characters to remove from the new line's indentation.
245+
*/
246+
removeText: number;
247+
/**
248+
* The line's indentation minus removeText
249+
*/
250+
indentation: string;
251+
}
252+
231253
/**
232254
* @internal
233255
*/

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Range } from 'vs/editor/common/core/range';
1212
import { ITextModel } from 'vs/editor/common/model';
1313
import { DEFAULT_WORD_REGEXP, ensureValidWordDefinition } from 'vs/editor/common/model/wordHelper';
1414
import { LanguageId, LanguageIdentifier } from 'vs/editor/common/modes';
15-
import { EnterAction, FoldingRules, IAutoClosingPair, IndentAction, IndentationRule, LanguageConfiguration, StandardAutoClosingPairConditional } from 'vs/editor/common/modes/languageConfiguration';
15+
import { EnterAction, FoldingRules, IAutoClosingPair, IndentAction, IndentationRule, LanguageConfiguration, StandardAutoClosingPairConditional, CompleteEnterAction } from 'vs/editor/common/modes/languageConfiguration';
1616
import { createScopedLineTokens, ScopedLineTokens } from 'vs/editor/common/modes/supports';
1717
import { CharacterPairSupport } from 'vs/editor/common/modes/supports/characterPair';
1818
import { BracketElectricCharacterSupport, IElectricAction } from 'vs/editor/common/modes/supports/electricCharacter';
@@ -697,12 +697,7 @@ export class LanguageConfigurationRegistryImpl {
697697
return value.onEnter || null;
698698
}
699699

700-
public getRawEnterActionAtPosition(autoIndent: EditorAutoIndentStrategy, model: ITextModel, lineNumber: number, column: number): EnterAction | null {
701-
const r = this.getEnterAction(autoIndent, model, new Range(lineNumber, column, lineNumber, column));
702-
return r ? r.enterAction : null;
703-
}
704-
705-
public getEnterAction(autoIndent: EditorAutoIndentStrategy, model: ITextModel, range: Range): { enterAction: EnterAction; indentation: string; } | null {
700+
public getEnterAction(autoIndent: EditorAutoIndentStrategy, model: ITextModel, range: Range): CompleteEnterAction | null {
706701
const scopedLineTokens = this.getScopedLineTokens(model, range.startLineNumber, range.startColumn);
707702
const onEnterSupport = this._getOnEnterSupport(scopedLineTokens.languageId);
708703
if (!onEnterSupport) {
@@ -740,28 +735,34 @@ export class LanguageConfigurationRegistryImpl {
740735

741736
if (!enterResult) {
742737
return null;
743-
} else {
744-
// Here we add `\t` to appendText first because enterAction is leveraging appendText and removeText to change indentation.
745-
if (!enterResult.appendText) {
746-
if (
747-
(enterResult.indentAction === IndentAction.Indent) ||
748-
(enterResult.indentAction === IndentAction.IndentOutdent)
749-
) {
750-
enterResult.appendText = '\t';
751-
} else {
752-
enterResult.appendText = '';
753-
}
738+
}
739+
740+
const indentAction = enterResult.indentAction;
741+
let appendText = enterResult.appendText;
742+
const removeText = enterResult.removeText || 0;
743+
744+
// Here we add `\t` to appendText first because enterAction is leveraging appendText and removeText to change indentation.
745+
if (!appendText) {
746+
if (
747+
(indentAction === IndentAction.Indent) ||
748+
(indentAction === IndentAction.IndentOutdent)
749+
) {
750+
appendText = '\t';
751+
} else {
752+
appendText = '';
754753
}
755754
}
756755

757756
let indentation = this.getIndentationAtPosition(model, range.startLineNumber, range.startColumn);
758-
if (enterResult.removeText) {
759-
indentation = indentation.substring(0, indentation.length - enterResult.removeText);
757+
if (removeText) {
758+
indentation = indentation.substring(0, indentation.length - removeText);
760759
}
761760

762761
return {
763-
enterAction: enterResult,
764-
indentation: indentation,
762+
indentAction: indentAction,
763+
appendText: appendText,
764+
removeText: removeText,
765+
indentation: indentation
765766
};
766767
}
767768

src/vs/editor/contrib/linesOperations/moveLinesCommand.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,15 @@ export class MoveLinesCommand implements ICommand {
256256

257257
if (enter) {
258258
let enterPrefix = enter.indentation;
259-
let enterAction = enter.enterAction;
260259

261-
if (enterAction.indentAction === IndentAction.None) {
262-
enterPrefix = enter.indentation + enterAction.appendText;
263-
} else if (enterAction.indentAction === IndentAction.Indent) {
264-
enterPrefix = enter.indentation + enterAction.appendText;
265-
} else if (enterAction.indentAction === IndentAction.IndentOutdent) {
260+
if (enter.indentAction === IndentAction.None) {
261+
enterPrefix = enter.indentation + enter.appendText;
262+
} else if (enter.indentAction === IndentAction.Indent) {
263+
enterPrefix = enter.indentation + enter.appendText;
264+
} else if (enter.indentAction === IndentAction.IndentOutdent) {
266265
enterPrefix = enter.indentation;
267-
} else if (enterAction.indentAction === IndentAction.Outdent) {
268-
enterPrefix = indentConverter.unshiftIndent(enter.indentation) + enterAction.appendText;
266+
} else if (enter.indentAction === IndentAction.Outdent) {
267+
enterPrefix = indentConverter.unshiftIndent(enter.indentation) + enter.appendText;
269268
}
270269
let movingLineText = model.getLineContent(line);
271270
if (this.trimLeft(movingLineText).indexOf(this.trimLeft(enterPrefix)) >= 0) {

0 commit comments

Comments
 (0)