Introduce and adapt CompleteEnterAction

上级 0789507d
......@@ -139,7 +139,7 @@ export class ShiftCommand implements ICommand {
// The current line is "miss-aligned", so let's see if this is expected...
// This can only happen when it has trailing commas in the indent
if (model.isCheapToTokenize(lineNumber - 1)) {
let enterAction = LanguageConfigurationRegistry.getRawEnterActionAtPosition(this._opts.autoIndent, model, lineNumber - 1, model.getLineMaxColumn(lineNumber - 1));
let enterAction = LanguageConfigurationRegistry.getEnterAction(this._opts.autoIndent, model, new Range(lineNumber - 1, model.getLineMaxColumn(lineNumber - 1), lineNumber - 1, model.getLineMaxColumn(lineNumber - 1)));
if (enterAction) {
extraSpaces = previousLineExtraSpaces;
if (enterAction.appendText) {
......
......@@ -174,11 +174,7 @@ export class TypeOperations {
const maxColumn = model.getLineMaxColumn(lastLineNumber);
const expectedEnterAction = LanguageConfigurationRegistry.getEnterAction(config.autoIndent, model, new Range(lastLineNumber, maxColumn, lastLineNumber, maxColumn));
if (expectedEnterAction) {
indentation = expectedEnterAction.indentation;
action = expectedEnterAction.enterAction;
if (action) {
indentation += action.appendText;
}
indentation = expectedEnterAction.indentation + expectedEnterAction.appendText;
}
}
......@@ -304,21 +300,18 @@ export class TypeOperations {
const r = LanguageConfigurationRegistry.getEnterAction(config.autoIndent, model, range);
if (r) {
const enterAction = r.enterAction;
const indentation = r.indentation;
if (enterAction.indentAction === IndentAction.None) {
if (r.indentAction === IndentAction.None) {
// Nothing special
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation + enterAction.appendText), keepPosition);
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(r.indentation + r.appendText), keepPosition);
} else if (enterAction.indentAction === IndentAction.Indent) {
} else if (r.indentAction === IndentAction.Indent) {
// Indent once
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation + enterAction.appendText), keepPosition);
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(r.indentation + r.appendText), keepPosition);
} else if (enterAction.indentAction === IndentAction.IndentOutdent) {
} else if (r.indentAction === IndentAction.IndentOutdent) {
// Ultra special
const normalIndent = config.normalizeIndentation(indentation);
const increasedIndent = config.normalizeIndentation(indentation + enterAction.appendText);
const normalIndent = config.normalizeIndentation(r.indentation);
const increasedIndent = config.normalizeIndentation(r.indentation + r.appendText);
const typeText = '\n' + increasedIndent + '\n' + normalIndent;
......@@ -327,9 +320,9 @@ export class TypeOperations {
} else {
return new ReplaceCommandWithOffsetCursorState(range, typeText, -1, increasedIndent.length - normalIndent.length, true);
}
} else if (enterAction.indentAction === IndentAction.Outdent) {
const actualIndentation = TypeOperations.unshiftIndent(config, indentation);
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(actualIndentation + enterAction.appendText), keepPosition);
} else if (r.indentAction === IndentAction.Outdent) {
const actualIndentation = TypeOperations.unshiftIndent(config, r.indentation);
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(actualIndentation + r.appendText), keepPosition);
}
}
......
......@@ -228,6 +228,28 @@ export interface EnterAction {
removeText?: number;
}
/**
* @internal
*/
export interface CompleteEnterAction {
/**
* Describe what to do with the indentation.
*/
indentAction: IndentAction;
/**
* Describes text to be appended after the new line and after the indentation.
*/
appendText: string;
/**
* Describes the number of characters to remove from the new line's indentation.
*/
removeText: number;
/**
* The line's indentation minus removeText
*/
indentation: string;
}
/**
* @internal
*/
......
......@@ -12,7 +12,7 @@ import { Range } from 'vs/editor/common/core/range';
import { ITextModel } from 'vs/editor/common/model';
import { DEFAULT_WORD_REGEXP, ensureValidWordDefinition } from 'vs/editor/common/model/wordHelper';
import { LanguageId, LanguageIdentifier } from 'vs/editor/common/modes';
import { EnterAction, FoldingRules, IAutoClosingPair, IndentAction, IndentationRule, LanguageConfiguration, StandardAutoClosingPairConditional } from 'vs/editor/common/modes/languageConfiguration';
import { EnterAction, FoldingRules, IAutoClosingPair, IndentAction, IndentationRule, LanguageConfiguration, StandardAutoClosingPairConditional, CompleteEnterAction } from 'vs/editor/common/modes/languageConfiguration';
import { createScopedLineTokens, ScopedLineTokens } from 'vs/editor/common/modes/supports';
import { CharacterPairSupport } from 'vs/editor/common/modes/supports/characterPair';
import { BracketElectricCharacterSupport, IElectricAction } from 'vs/editor/common/modes/supports/electricCharacter';
......@@ -697,12 +697,7 @@ export class LanguageConfigurationRegistryImpl {
return value.onEnter || null;
}
public getRawEnterActionAtPosition(autoIndent: EditorAutoIndentStrategy, model: ITextModel, lineNumber: number, column: number): EnterAction | null {
const r = this.getEnterAction(autoIndent, model, new Range(lineNumber, column, lineNumber, column));
return r ? r.enterAction : null;
}
public getEnterAction(autoIndent: EditorAutoIndentStrategy, model: ITextModel, range: Range): { enterAction: EnterAction; indentation: string; } | null {
public getEnterAction(autoIndent: EditorAutoIndentStrategy, model: ITextModel, range: Range): CompleteEnterAction | null {
const scopedLineTokens = this.getScopedLineTokens(model, range.startLineNumber, range.startColumn);
const onEnterSupport = this._getOnEnterSupport(scopedLineTokens.languageId);
if (!onEnterSupport) {
......@@ -740,28 +735,34 @@ export class LanguageConfigurationRegistryImpl {
if (!enterResult) {
return null;
} else {
// Here we add `\t` to appendText first because enterAction is leveraging appendText and removeText to change indentation.
if (!enterResult.appendText) {
if (
(enterResult.indentAction === IndentAction.Indent) ||
(enterResult.indentAction === IndentAction.IndentOutdent)
) {
enterResult.appendText = '\t';
} else {
enterResult.appendText = '';
}
}
const indentAction = enterResult.indentAction;
let appendText = enterResult.appendText;
const removeText = enterResult.removeText || 0;
// Here we add `\t` to appendText first because enterAction is leveraging appendText and removeText to change indentation.
if (!appendText) {
if (
(indentAction === IndentAction.Indent) ||
(indentAction === IndentAction.IndentOutdent)
) {
appendText = '\t';
} else {
appendText = '';
}
}
let indentation = this.getIndentationAtPosition(model, range.startLineNumber, range.startColumn);
if (enterResult.removeText) {
indentation = indentation.substring(0, indentation.length - enterResult.removeText);
if (removeText) {
indentation = indentation.substring(0, indentation.length - removeText);
}
return {
enterAction: enterResult,
indentation: indentation,
indentAction: indentAction,
appendText: appendText,
removeText: removeText,
indentation: indentation
};
}
......
......@@ -256,16 +256,15 @@ export class MoveLinesCommand implements ICommand {
if (enter) {
let enterPrefix = enter.indentation;
let enterAction = enter.enterAction;
if (enterAction.indentAction === IndentAction.None) {
enterPrefix = enter.indentation + enterAction.appendText;
} else if (enterAction.indentAction === IndentAction.Indent) {
enterPrefix = enter.indentation + enterAction.appendText;
} else if (enterAction.indentAction === IndentAction.IndentOutdent) {
if (enter.indentAction === IndentAction.None) {
enterPrefix = enter.indentation + enter.appendText;
} else if (enter.indentAction === IndentAction.Indent) {
enterPrefix = enter.indentation + enter.appendText;
} else if (enter.indentAction === IndentAction.IndentOutdent) {
enterPrefix = enter.indentation;
} else if (enterAction.indentAction === IndentAction.Outdent) {
enterPrefix = indentConverter.unshiftIndent(enter.indentation) + enterAction.appendText;
} else if (enter.indentAction === IndentAction.Outdent) {
enterPrefix = indentConverter.unshiftIndent(enter.indentation) + enter.appendText;
}
let movingLineText = model.getLineContent(line);
if (this.trimLeft(movingLineText).indexOf(this.trimLeft(enterPrefix)) >= 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册