提交 3eb4e2be 编写于 作者: R rebornix

get expected indentation from above lines

上级 6267d9f5
......@@ -103,16 +103,16 @@ export class TypeOperations {
}
private static _goodIndentForLine(config: CursorConfiguration, model: ITokenizedModel, lineNumber: number): string {
let inheritIndentAction = LanguageConfigurationRegistry.getInheritedIndentActionForCurrentLine(model, lineNumber);
let expectedIndentAction = LanguageConfigurationRegistry.getExpectedIndentActionAtPosition(model, lineNumber);
if (inheritIndentAction && inheritIndentAction.action) {
let indentation = inheritIndentAction.indentation;
if (expectedIndentAction && expectedIndentAction.action) {
let indentation = expectedIndentAction.indentation;
if (inheritIndentAction.action === IndentAction.Indent) {
if (expectedIndentAction.action === IndentAction.Indent) {
indentation = TypeOperations.shiftIndent(config, indentation);
}
if (inheritIndentAction.action === IndentAction.Outdent) {
if (expectedIndentAction.action === IndentAction.Outdent) {
indentation = TypeOperations.unshiftIndent(config, indentation);
}
......@@ -213,37 +213,32 @@ export class TypeOperations {
}
let enterAction = LanguageConfigurationRegistry.getEnterActionAtPosition(model, range.startLineNumber, range.startColumn);
let beforeText = '';
let inheritedIntentationAction = LanguageConfigurationRegistry.getInheritedIndentActionForCurrentLine(model, range.startLineNumber);
let currentLineIndentationRule = LanguageConfigurationRegistry.getIndentationRuleForCurrentLine(model, range.startLineNumber, range.startColumn);
let expectedIndentAction = LanguageConfigurationRegistry.getExpectedIndentActionAtPosition(model, range.startLineNumber);
let expectedIndentationBeforeEnter = indentation;
if (inheritedIntentationAction || currentLineIndentationRule) {
// The indentation of current line may be adjusted
let expectedIndentation = indentation;
if (expectedIndentAction && expectedIndentAction.action) {
expectedIndentationBeforeEnter = expectedIndentAction.indentation;
if (inheritedIntentationAction && inheritedIntentationAction.action) {
if (inheritedIntentationAction.action === IndentAction.Indent) {
expectedIndentation = TypeOperations.shiftIndent(config, inheritedIntentationAction.indentation);
}
if (inheritedIntentationAction.action === IndentAction.Outdent) {
expectedIndentation = TypeOperations.unshiftIndent(config, inheritedIntentationAction.indentation);
}
if (expectedIndentAction.action === IndentAction.Indent) {
expectedIndentationBeforeEnter = TypeOperations.shiftIndent(config, expectedIndentationBeforeEnter);
}
if (currentLineIndentationRule && currentLineIndentationRule === IndentAction.Outdent) {
expectedIndentation = TypeOperations.unshiftIndent(config, expectedIndentation);
if (expectedIndentAction.action === IndentAction.Outdent) {
expectedIndentationBeforeEnter = TypeOperations.unshiftIndent(config, expectedIndentationBeforeEnter);
}
}
expectedIndentation = config.normalizeIndentation(expectedIndentation);
if (enterAction.outdentCurrentLine) {
expectedIndentationBeforeEnter = TypeOperations.unshiftIndent(config, expectedIndentationBeforeEnter);
}
if (expectedIndentation !== indentation) {
// adjust indentation of current line.
beforeText = expectedIndentation + lineText.substring(indentation.length, range.startColumn - 1);
indentation = expectedIndentation;
range = new Range(range.startLineNumber, 1, range.endLineNumber, range.endColumn);
}
expectedIndentationBeforeEnter = config.normalizeIndentation(expectedIndentationBeforeEnter);
let beforeText = '';
if (expectedIndentationBeforeEnter !== indentation) {
beforeText = expectedIndentationBeforeEnter + lineText.substring(indentation.length, range.startColumn - 1);
indentation = expectedIndentationBeforeEnter;
range = new Range(range.startLineNumber, 1, range.endLineNumber, range.endColumn);
}
// compute indentation of following lines
......
......@@ -174,6 +174,10 @@ export interface EnterAction {
* Describe what to do with the indentation.
*/
indentAction: IndentAction;
/**
* Describe whether to outdent current line.
*/
outdentCurrentLine?: boolean;
/**
* Describes text to be appended after the new line and after the indentation.
*/
......
......@@ -279,16 +279,28 @@ export class LanguageConfigurationRegistryImpl {
return result;
}
public getInheritedIndentActionForCurrentLine(model: ITokenizedModel, lineNumber: number): { indentation: string, action: IndentAction } {
public getEnterActionAtPosition(model: ITokenizedModel, lineNumber: number, column: number): EnterAction {
let enterAction = this.getRawEnterActionAtPosition(model, lineNumber, column);
if (!enterAction) {
enterAction = {
indentAction: IndentAction.None,
appendText: '',
};
} else {
if (!enterAction.appendText) {
enterAction.appendText = '';
}
}
return enterAction;
}
public getExpectedIndentActionAtPosition(model: ITokenizedModel, lineNumber: number) {
/**
* In order to get correct indentation for current line
* we need to loop backwards all the lines from current line until
* 1. a line is not empty
* 2. and the line doesn't match `unIndentedLinePattern` pattern
* we need to loop backwards all the lines from current line until a line contains non whitespace characters.
*/
// TODO: how about a line filled with only spaces?
let lastLineNumber = lineNumber - 1;
for (lastLineNumber = lineNumber - 1; lastLineNumber >= 1; lastLineNumber--) {
......@@ -313,18 +325,17 @@ export class LanguageConfigurationRegistryImpl {
}
let lineText = model.getLineContent(lastLineNumber);
let lineTextAbove: string;
let oneLineAboveText: string;
if (lastLineNumber > 1) {
lineTextAbove = model.getLineContent(lastLineNumber - 1);
oneLineAboveText = model.getLineContent(lastLineNumber - 1);
}
let indentation = strings.getLeadingWhitespace(lineText);
let inheritedIntentationRule = onEnterSupport.getInheritedIndentationRules(lineText, lineTextAbove);
let expectedIntentAction = onEnterSupport.getExpectedIndentActionAtPosition(lineText, oneLineAboveText);
return {
indentation: indentation,
action: inheritedIntentationRule
action: expectedIntentAction
};
}
......@@ -342,22 +353,6 @@ export class LanguageConfigurationRegistryImpl {
return indentAction;
}
public getEnterActionAtPosition(model: ITokenizedModel, lineNumber: number, column: number): EnterAction {
let enterAction = this.getRawEnterActionAtPosition(model, lineNumber, column);
if (!enterAction) {
enterAction = {
indentAction: IndentAction.None,
appendText: '',
};
} else {
if (!enterAction.appendText) {
enterAction.appendText = '';
}
}
return enterAction;
}
// end onEnter
public getBracketsSupport(languageId: LanguageId): RichEditBrackets {
......
......@@ -75,7 +75,6 @@ export class OnEnterSupport {
// (3): Indentation Support
if (this._indentationRules) {
let enterAction: EnterAction = null;
if (this._indentationRules.increaseIndentPattern && this._indentationRules.increaseIndentPattern.test(beforeEnterText)) {
enterAction = { indentAction: IndentAction.Indent };
}
......@@ -90,6 +89,13 @@ export class OnEnterSupport {
if (this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(oneLineAboveText)) {
enterAction = { indentAction: IndentAction.Outdent };
}
if (this._indentationRules.decreaseIndentPattern && this._indentationRules.decreaseIndentPattern.test(beforeEnterText)) {
if (enterAction === null) {
enterAction = { indentAction: IndentAction.None, outdentCurrentLine: true };
} else {
enterAction = { indentAction: enterAction.indentAction, outdentCurrentLine: true };
}
}
}
if (enterAction !== null) {
......@@ -110,10 +116,10 @@ export class OnEnterSupport {
return null;
}
public getInheritedIndentationRules(text: string, textOneLineAbove?: string): IndentAction {
public getExpectedIndentActionAtPosition(text: string, oneLineAboveText?: string): IndentAction {
let offset = 0;
if (this._indentationRules) {
if (textOneLineAbove && this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(textOneLineAbove)) {
if (oneLineAboveText && this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(oneLineAboveText)) {
offset -= 1;
}
......@@ -144,17 +150,6 @@ export class OnEnterSupport {
return null;
}
public getIndentActionForContent(text: string): IndentAction {
if (this._indentationRules && /^\s/.test(text)) {
// No reason to run regular expressions if there is nothing to outdent from
if (this._indentationRules.decreaseIndentPattern && this._indentationRules.decreaseIndentPattern.test(text)) {
return IndentAction.Outdent;
}
}
return null;
}
private static _createOpenBracketRegExp(bracket: string): RegExp {
var str = strings.escapeRegExpCharacters(bracket);
if (!/\B/.test(str.charAt(0))) {
......
......@@ -1142,7 +1142,8 @@ class OnEnterMode extends MockMode {
onEnterRules: [{
beforeText: /.*/,
action: {
indentAction: indentAction
indentAction: indentAction,
outdentCurrentLine: outdentCurrentLine
}
}]
}));
......
......@@ -4298,6 +4298,10 @@ declare module monaco.languages {
* Describe what to do with the indentation.
*/
indentAction: IndentAction;
/**
* Describe whether to outdent current line.
*/
outdentCurrentLine?: boolean;
/**
* Describes text to be appended after the new line and after the indentation.
*/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册