提交 33d8446d 编写于 作者: R rebornix

AutoIndent. Correct behavior of indentNextLine for Enter

上级 19ae0932
......@@ -144,7 +144,7 @@ export class TypeOperations {
}
private static _goodIndentForLine(config: CursorConfiguration, model: ITokenizedModel, lineNumber: number): string {
let expectedIndentAction = LanguageConfigurationRegistry.getGoodIndentActionForLine(model, lineNumber);
let expectedIndentAction = LanguageConfigurationRegistry.getInheritIndentForLine(model, lineNumber, false);
if (expectedIndentAction) {
if (expectedIndentAction.action) {
......@@ -265,47 +265,24 @@ export class TypeOperations {
private static _enter(config: CursorConfiguration, model: ITokenizedModel, keepPosition: boolean, range: Range): ICommand {
let r = LanguageConfigurationRegistry.getEnterAction(model, range);
if (r) {
let enterAction = r.enterAction;
let indentation = r.indentation;
let beforeText = '';
if (!r.ignoreCurrentLine) {
// textBeforeEnter doesn't match unIndentPattern.
let goodIndent = this._goodIndentForLine(config, model, range.startLineNumber);
if (goodIndent !== null && goodIndent === r.indentation) {
if (enterAction.outdentCurrentLine) {
goodIndent = TypeOperations.unshiftIndent(config, goodIndent);
}
let lineText = model.getLineContent(range.startLineNumber);
if (config.normalizeIndentation(goodIndent) !== config.normalizeIndentation(indentation)) {
beforeText = config.normalizeIndentation(goodIndent) + lineText.substring(indentation.length, range.startColumn - 1);
indentation = goodIndent;
range = new Range(range.startLineNumber, 1, range.endLineNumber, range.endColumn);
}
}
}
if (enterAction.removeText) {
indentation = indentation.substring(0, indentation.length - enterAction.removeText);
}
if (enterAction.indentAction === IndentAction.None) {
// Nothing special
return TypeOperations._typeCommand(range, beforeText + '\n' + config.normalizeIndentation(indentation + enterAction.appendText), keepPosition);
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation + enterAction.appendText), keepPosition);
} else if (enterAction.indentAction === IndentAction.Indent) {
// Indent once
return TypeOperations._typeCommand(range, beforeText + '\n' + config.normalizeIndentation(indentation + enterAction.appendText), keepPosition);
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation + enterAction.appendText), keepPosition);
} else if (enterAction.indentAction === IndentAction.IndentOutdent) {
// Ultra special
let normalIndent = config.normalizeIndentation(indentation);
let increasedIndent = config.normalizeIndentation(indentation + enterAction.appendText);
let typeText = beforeText + '\n' + increasedIndent + '\n' + normalIndent;
let typeText = '\n' + increasedIndent + '\n' + normalIndent;
if (keepPosition) {
return new ReplaceCommandWithoutChangingPosition(range, typeText, true);
......@@ -314,7 +291,69 @@ export class TypeOperations {
}
} else if (enterAction.indentAction === IndentAction.Outdent) {
let actualIndentation = TypeOperations.unshiftIndent(config, indentation);
return TypeOperations._typeCommand(range, beforeText + '\n' + config.normalizeIndentation(actualIndentation + enterAction.appendText), keepPosition);
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(actualIndentation + enterAction.appendText), keepPosition);
}
}
// no enter rules applied, we should check indentation rules then.
let ir = LanguageConfigurationRegistry.getIndentForEnter(model, range, {
unshiftIndent: (indent) => {
return TypeOperations.unshiftIndent(config, indent);
},
shiftIndent: (indent) => {
return TypeOperations.shiftIndent(config, indent);
},
normalizeIndentation: (indent) => {
return config.normalizeIndentation(indent);
}
});
let lineText = model.getLineContent(range.startLineNumber);
let indentation = strings.getLeadingWhitespace(lineText);
if (ir) {
if (/^\s+$/.test(lineText) || indentation === config.normalizeIndentation(ir.beforeEnter)) {
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(ir.afterEnter), keepPosition);
}
let beforeText = config.normalizeIndentation(ir.beforeEnter) + lineText.substring(indentation.length, range.startColumn - 1);
range = new Range(range.startLineNumber, 1, range.endLineNumber, range.endColumn);
return TypeOperations._typeCommand(range, beforeText + '\n' + config.normalizeIndentation(ir.afterEnter), keepPosition);
} else {
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation), keepPosition);
}
}
private static _runAutoIndentType(config: CursorConfiguration, model: ITokenizedModel, selections: Selection[], ch: string): ICommand {
let selection = selections[0];
let currentIndentation = LanguageConfigurationRegistry.getIndentationAtPosition(model, selection.startLineNumber, selection.startColumn);
let actualIndentation = LanguageConfigurationRegistry.getIndentActionForType(model, selections[0].startLineNumber, selections[0].startColumn, ch, {
shiftIndent: (indentation) => {
return TypeOperations.shiftIndent(config, indentation);
},
unshiftIndent: (indentation) => {
return TypeOperations.unshiftIndent(config, indentation);
},
});
if (actualIndentation === null) {
return null;
}
if (actualIndentation !== config.normalizeIndentation(currentIndentation)) {
let firstNonWhitespace = model.getLineFirstNonWhitespaceColumn(selection.startLineNumber);
if (firstNonWhitespace === 0) {
return TypeOperations._typeCommand(
new Range(selection.startLineNumber, 0, selection.startLineNumber, selection.startColumn),
actualIndentation + ch,
false
);
} else {
return TypeOperations._typeCommand(
new Range(selection.startLineNumber, 0, selection.startLineNumber, selection.startColumn),
actualIndentation +
model.getLineContent(selection.startLineNumber).substring(firstNonWhitespace - 1, selection.startColumn) + ch,
false
);
}
}
return null;
......
......@@ -7,6 +7,7 @@
import { CharacterPairSupport } from 'vs/editor/common/modes/supports/characterPair';
import { BracketElectricCharacterSupport, IElectricAction } from 'vs/editor/common/modes/supports/electricCharacter';
import { IOnEnterSupportOptions, OnEnterSupport } from 'vs/editor/common/modes/supports/onEnter';
import { IndentRulesSupport } from 'vs/editor/common/modes/supports/indentRules';
import { RichEditBrackets } from 'vs/editor/common/modes/supports/richEditBrackets';
import Event, { Emitter } from 'vs/base/common/event';
import { ITokenizedModel } from 'vs/editor/common/editorCommon';
......@@ -29,6 +30,13 @@ export interface ICommentsConfiguration {
blockCommentEndToken?: string;
}
export interface IVirtualModel {
getLineTokens(lineNumber: number): LineTokens;
getLanguageIdentifier(): LanguageIdentifier;
getLanguageIdAtPosition(lineNumber: number, column: number): LanguageId;
getLineContent(lineNumber: number): string;
}
export class RichEditSupport {
private readonly _conf: LanguageConfiguration;
......@@ -38,6 +46,7 @@ export class RichEditSupport {
public readonly characterPair: CharacterPairSupport;
public readonly wordDefinition: RegExp;
public readonly onEnter: OnEnterSupport;
public readonly indentRulesSupport: IndentRulesSupport;
public readonly brackets: RichEditBrackets;
public readonly indentationRules: IndentationRule;
......@@ -64,6 +73,9 @@ export class RichEditSupport {
this.wordDefinition = this._conf.wordPattern || DEFAULT_WORD_REGEXP;
this.indentationRules = this._conf.indentationRules;
if (this._conf.indentationRules) {
this.indentRulesSupport = new IndentRulesSupport(this._conf.indentationRules);
}
}
private static _mergeConf(prev: LanguageConfiguration, current: LanguageConfiguration): LanguageConfiguration {
......@@ -250,6 +262,236 @@ export class LanguageConfigurationRegistryImpl {
return ensureValidWordDefinition(value.wordDefinition || null);
}
// beigin Indent Rules
private _getIndentRulesSupport(languageId: LanguageId): IndentRulesSupport {
let value = this._getRichEditSupport(languageId);
if (!value) {
return null;
}
return value.indentRulesSupport || null;
}
/**
* Get nearest preceiding line which doesn't match unIndentPattern or contains all whitespace.
*/
private getPrecedingValidLine(model: IVirtualModel, lineNumber: number, indentRulesSupport: IndentRulesSupport) {
let languageID = model.getLanguageIdAtPosition(lineNumber, 0);
if (lineNumber > 1) {
let lastLineNumber = lineNumber - 1;
let resultLineNumber = -1;
for (lastLineNumber = lineNumber - 1; lastLineNumber >= 1; lastLineNumber--) {
if (model.getLanguageIdAtPosition(lastLineNumber, 0) !== languageID) {
return resultLineNumber;
}
let text = model.getLineContent(lastLineNumber);
if (indentRulesSupport.shouldIgnore(text) || text === '') {
resultLineNumber = lastLineNumber;
continue;
}
return lastLineNumber;
}
}
return -1;
}
/**
* Get inherited indentation from above lines.
* 1. Find the nearest preceding line which doesn't match unIndentedLinePattern.
* 2. If this line matches indentNextLinePattern or increaseIndentPattern, it means that the indent level of `lineNumber` should be 1 greater than this line.
* 3. If this line doesn't match any indent rules
* a. check whether the line above it matches indentNextLinePattern
* b. If not, the indent level of this line is the result
* c. If so, it means the indent of this line is *temporary*, go upward utill we find a line whose indent is not temporary (the same workflow a -> b -> c).
* 4. Otherwise, we fail to get an inherited indent from aboves. Return null and we should not touch the indent of `lineNumber`
*
* This function only return the inherited indent based on above lines, it doesn't check whether current line should decrease or not.
*/
public getInheritIndentForLine(model: IVirtualModel, lineNumber: number, honorIntentialIndent: boolean = true) {
let indentRulesSupport = this._getIndentRulesSupport(model.getLanguageIdentifier().id);
if (!indentRulesSupport) {
return null;
}
if (lineNumber <= 1) {
return null;
}
let precedingUnIgnoredLine = this.getPrecedingValidLine(model, lineNumber, indentRulesSupport);
if (precedingUnIgnoredLine < 1) {
return null;
}
let precedingUnIgnoredLineContent = model.getLineContent(precedingUnIgnoredLine);
if (indentRulesSupport.shouldIncrease(precedingUnIgnoredLineContent) || indentRulesSupport.shouldIndentNextLine(precedingUnIgnoredLineContent)) {
return {
indentation: strings.getLeadingWhitespace(precedingUnIgnoredLineContent),
action: IndentAction.Indent
};
} else if (indentRulesSupport.shouldDecrease(precedingUnIgnoredLineContent)) {
return {
indentation: strings.getLeadingWhitespace(precedingUnIgnoredLineContent),
action: null
};
} else {
// precedingUnIgnoredLine can not be ignored.
// it doesn't increase indent of following lines
// it doesn't increase just next line
// so current line is not affect by precedingUnIgnoredLine
// and then we should get a correct inheritted indentation from above lines
if (precedingUnIgnoredLine === 1) {
return {
indentation: strings.getLeadingWhitespace(model.getLineContent(precedingUnIgnoredLine)),
action: null
};
}
let previousLine = precedingUnIgnoredLine - 1;
let previousLineContent = model.getLineContent(previousLine);
if (indentRulesSupport.shouldIndentNextLine(previousLineContent)) {
let stopLine = 0;
for (let i = previousLine - 1; i > 0; i--) {
if (indentRulesSupport.shouldIndentNextLine(model.getLineContent(i))) {
continue;
}
stopLine = i;
break;
}
return {
indentation: strings.getLeadingWhitespace(model.getLineContent(stopLine + 1)),
action: null
};
}
if (honorIntentialIndent) {
return {
indentation: strings.getLeadingWhitespace(model.getLineContent(precedingUnIgnoredLine)),
action: null
};
} else {
// search from precedingUnIgnoredLine until we find one whose indent is not temporary
for (let i = precedingUnIgnoredLine; i > 0; i--) {
let lineContent = model.getLineContent(i);
if (indentRulesSupport.shouldDecrease(lineContent)) {
return {
indentation: strings.getLeadingWhitespace(lineContent),
action: null
};
} else if (indentRulesSupport.shouldIncrease(lineContent)) {
return {
indentation: strings.getLeadingWhitespace(lineContent),
action: IndentAction.Indent
};
} else if (indentRulesSupport.shouldIndentNextLine(lineContent)) {
let stopLine = 0;
for (let j = i - 1; j > 0; j--) {
if (indentRulesSupport.shouldIndentNextLine(model.getLineContent(i))) {
continue;
}
stopLine = j;
break;
}
return {
indentation: strings.getLeadingWhitespace(model.getLineContent(stopLine + 1)),
action: null
};
}
}
return {
indentation: strings.getLeadingWhitespace(model.getLineContent(1)),
action: null
};
}
}
}
public getIndentForEnter(model: ITokenizedModel, range: Range, indentConverter: any): {beforeEnter: string, afterEnter: string} {
let scopedLineTokens = this.getScopedLineTokens(model, range.startLineNumber, range.startColumn);
let scopedLineText = scopedLineTokens.getLineContent();
let beforeEnterText = scopedLineText.substr(0, range.startColumn - 1 - scopedLineTokens.firstCharOffset);
let afterEnterText;
if (range.isEmpty()) {
afterEnterText = scopedLineText.substr(range.startColumn - 1 - scopedLineTokens.firstCharOffset);
} else {
const endScopedLineTokens = this.getScopedLineTokens(model, range.endLineNumber, range.endColumn);
afterEnterText = endScopedLineTokens.getLineContent().substr(range.endColumn - 1 - scopedLineTokens.firstCharOffset);
}
let indentRulesSupport = this._getIndentRulesSupport(scopedLineTokens.languageId);
if (!indentRulesSupport) {
return null;
}
let beforeEnterIndentAction = this.getInheritIndentForLine(model, range.startLineNumber);
let beforeEnterIndent = strings.getLeadingWhitespace(beforeEnterText);
if (indentRulesSupport.shouldDecrease(beforeEnterText)) {
if (beforeEnterIndentAction) {
beforeEnterIndent = beforeEnterIndentAction.indentation;
if (beforeEnterIndentAction.action !== IndentAction.Indent) {
beforeEnterIndent = indentConverter.unshiftIndent(beforeEnterIndent);
}
}
}
let beforeEnterResult = beforeEnterIndent + strings.ltrim(strings.ltrim(beforeEnterText, ' '), '\t');
let virtualModel: IVirtualModel = {
getLineTokens: (lineNumber: number) => {
return model.getLineTokens(lineNumber);
},
getLanguageIdentifier: () => {
return model.getLanguageIdentifier();
},
getLanguageIdAtPosition: (lineNumber: number, column: number) => {
return model.getLanguageIdAtPosition(lineNumber, column);
},
getLineContent: (lineNumber: number) => {
if (lineNumber === range.startLineNumber) {
return beforeEnterResult;
} else {
return model.getLineContent(lineNumber);
}
}
};
let afterEnterAction = this.getInheritIndentForLine(virtualModel, range.startLineNumber + 1);
if (!afterEnterAction) {
return {
beforeEnter: beforeEnterIndent,
afterEnter: beforeEnterIndent
};
}
let afterEnterIndent = afterEnterAction.indentation;
if (afterEnterAction.action === IndentAction.Indent) {
afterEnterIndent = indentConverter.shiftIndent(afterEnterIndent);
}
if (indentRulesSupport.shouldDecrease(afterEnterText)) {
afterEnterIndent = indentConverter.unshiftIndent(afterEnterIndent);
}
return {
beforeEnter: beforeEnterIndent,
afterEnter: afterEnterIndent
};
}
// begin onEnter
private _getOnEnterSupport(languageId: LanguageId): OnEnterSupport {
......@@ -266,18 +508,13 @@ export class LanguageConfigurationRegistryImpl {
return r ? r.enterAction : null;
}
public getEnterAction(model: ITokenizedModel, range: Range): { enterAction: EnterAction; indentation: string; ignoreCurrentLine: boolean } {
public getEnterAction(model: ITokenizedModel, range: Range): { enterAction: EnterAction; indentation: string; } {
let indentation = this.getIndentationAtPosition(model, range.startLineNumber, range.startColumn);
let ignoreCurrentLine = false;
let scopedLineTokens = this.getScopedLineTokens(model, range.startLineNumber, range.startColumn);
let onEnterSupport = this._getOnEnterSupport(scopedLineTokens.languageId);
if (!onEnterSupport) {
return {
enterAction: { indentAction: IndentAction.None, appendText: '' },
indentation: indentation,
ignoreCurrentLine: false
};
return null;
}
let scopedLineText = scopedLineTokens.getLineContent();
......@@ -293,45 +530,18 @@ export class LanguageConfigurationRegistryImpl {
}
let lineNumber = range.startLineNumber;
// if the text before the cursor/range start position is empty or matches `unIndentedLinePattern`
// this line is actually ignored after the enter action
if (onEnterSupport.shouldIgnore(beforeEnterText)) {
ignoreCurrentLine = true;
let lastLineNumber = this.getLastValidLine(model, lineNumber, onEnterSupport);
if (lastLineNumber <= 0) {
return {
enterAction: { indentAction: IndentAction.None, appendText: '' },
indentation: '',
ignoreCurrentLine: ignoreCurrentLine
};
}
scopedLineTokens = this.getScopedLineTokens(model, lastLineNumber);
beforeEnterText = this.getLineContent(model, lastLineNumber);
lineNumber = lastLineNumber;
indentation = this.getIndentationAtPosition(model, lineNumber, model.getLineMaxColumn(lineNumber));
}
let oneLineAboveText = '';
if (lineNumber > 1 && scopedLineTokens.firstCharOffset === 0) {
// This is not the first line and the entire line belongs to this mode
let lastLineNumber = this.getLastValidLine(model, lineNumber, onEnterSupport);
if (lastLineNumber >= 1) {
// No previous line with content found
let oneLineAboveScopedLineTokens = this.getScopedLineTokens(model, lastLineNumber);
let oneLineAboveScopedLineTokens = this.getScopedLineTokens(model, lineNumber - 1);
if (oneLineAboveScopedLineTokens.languageId === scopedLineTokens.languageId) {
// The line above ends with text belonging to the same mode
oneLineAboveText = oneLineAboveScopedLineTokens.getLineContent();
}
}
}
let enterResult: EnterAction = null;
try {
enterResult = onEnterSupport.onEnter(oneLineAboveText, beforeEnterText, afterEnterText);
} catch (e) {
......@@ -339,7 +549,7 @@ export class LanguageConfigurationRegistryImpl {
}
if (!enterResult) {
enterResult = { indentAction: IndentAction.None, appendText: '' };
return null;
} else {
// Here we add `\t` to appendText first because enterAction is leveraging appendText and removeText to change indentation.
if (!enterResult.appendText) {
......@@ -351,17 +561,20 @@ export class LanguageConfigurationRegistryImpl {
} else {
enterResult.appendText = '';
}
}
}
if (enterResult.removeText) {
indentation = indentation.substring(0, indentation.length - enterResult.removeText);
}
return {
enterAction: enterResult,
indentation: indentation,
ignoreCurrentLine: ignoreCurrentLine
};
}
private getIndentationAtPosition(model: ITokenizedModel, lineNumber: number, column: number): string {
public getIndentationAtPosition(model: ITokenizedModel, lineNumber: number, column: number): string {
let lineText = model.getLineContent(lineNumber);
let indentation = strings.getLeadingWhitespace(lineText);
if (indentation.length > column - 1) {
......@@ -371,33 +584,6 @@ export class LanguageConfigurationRegistryImpl {
return indentation;
}
private getLastValidLine(model: ITokenizedModel, lineNumber: number, onEnterSupport: OnEnterSupport): number {
if (lineNumber > 1) {
let lastLineNumber = lineNumber - 1;
for (lastLineNumber = lineNumber - 1; lastLineNumber >= 1; lastLineNumber--) {
let lineText = model.getLineContent(lastLineNumber);
if (!onEnterSupport.shouldIgnore(lineText) && onEnterSupport.containNonWhitespace(lineText)) {
break;
}
}
if (lastLineNumber >= 1) {
return lastLineNumber;
}
}
return -1;
}
private getLineContent(model: ITokenizedModel, lineNumber: number): string {
let scopedLineTokens = this.getScopedLineTokens(model, lineNumber);
let column = model.getLineMaxColumn(lineNumber);
let scopedLineText = scopedLineTokens.getLineContent();
let lineText = scopedLineText.substr(0, column - 1 - scopedLineTokens.firstCharOffset);
return lineText;
}
private getScopedLineTokens(model: ITokenizedModel, lineNumber: number, columnNumber?: number) {
model.forceTokenization(lineNumber);
let lineTokens = model.getLineTokens(lineNumber);
......@@ -406,52 +592,6 @@ export class LanguageConfigurationRegistryImpl {
return scopedLineTokens;
}
public getGoodIndentActionForLine(model: ITokenizedModel, lineNumber: number) {
let onEnterSupport = this._getOnEnterSupport(model.getLanguageIdentifier().id);
if (!onEnterSupport) {
return null;
}
/**
* In order to get correct indentation for current line
* we need to loop backwards the content from current line until
* 1. a line contains non whitespace characters,
* 2. and the line doesn't match `unIndentedLinePattern` pattern
*/
let lastLineNumber = this.getLastValidLine(model, lineNumber, onEnterSupport);
if (lastLineNumber < 1) {
// No previous line with content found
return null;
}
// it's Okay that lineNumber > model.getLineCount(), a good example is guessing the indentation of next potential line
// when the cursor is at the end of file.
if (lineNumber <= model.getLineCount()) {
let currentLineScopedLineTokens = this.getScopedLineTokens(model, lineNumber);
let lastLineScopedLineTokens = this.getScopedLineTokens(model, lastLineNumber);
if (currentLineScopedLineTokens.languageId !== lastLineScopedLineTokens.languageId) {
// The language mode of last valid line is not the same as current line.
return null;
}
}
let lineText = model.getLineContent(lastLineNumber);
let oneLineAboveText: string;
if (lastLineNumber > 1) {
oneLineAboveText = model.getLineContent(lastLineNumber - 1);
}
let indentation = strings.getLeadingWhitespace(lineText);
let onEnterAction = onEnterSupport.onEnter(oneLineAboveText, lineText, '');
return {
indentation: indentation,
action: onEnterAction ? onEnterAction.indentAction : null
};
}
// end onEnter
public getBracketsSupport(languageId: LanguageId): RichEditBrackets {
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as strings from 'vs/base/common/strings';
import { IndentationRule, IndentAction } from 'vs/editor/common/modes/languageConfiguration';
export class IndentRulesSupport {
private readonly _indentationRules: IndentationRule;
constructor(indentationRules: IndentationRule) {
this._indentationRules = indentationRules;
}
public onType(text: string): IndentAction {
if (this._indentationRules) {
if (this._indentationRules.unIndentedLinePattern && this._indentationRules.unIndentedLinePattern.test(text)) {
return null;
}
if (this._indentationRules.decreaseIndentPattern && this._indentationRules.decreaseIndentPattern.test(text)) {
return IndentAction.Outdent;
}
}
return null;
}
public containNonWhitespace(text: string): boolean {
// the text doesn't contain any non-whitespace character.
let nonWhitespaceIdx = strings.lastNonWhitespaceIndex(text);
if (nonWhitespaceIdx >= 0) {
return true;
}
return false;
}
public shouldIncrease(text: string): boolean {
if (this._indentationRules) {
if (this._indentationRules.increaseIndentPattern && this._indentationRules.increaseIndentPattern.test(text)) {
return true;
}
// if (this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(text)) {
// return true;
// }
}
return false;
}
public shouldDecrease(text: string): boolean {
if (this._indentationRules && this._indentationRules.decreaseIndentPattern && this._indentationRules.decreaseIndentPattern.test(text)) {
return true;
}
return false;
}
public shouldIndentNextLine(text: string): boolean {
if (this._indentationRules && this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(text)) {
return true;
}
return false;
}
public shouldIgnore(text: string): boolean {
// the text matches `unIndentedLinePattern`
if (this._indentationRules && this._indentationRules.unIndentedLinePattern && this._indentationRules.unIndentedLinePattern.test(text)) {
return true;
}
return false;
}
}
......@@ -72,45 +72,6 @@ export class OnEnterSupport {
}
}
// (3): Indentation Support
if (this._indentationRules) {
let indentOffset: null | number = null;
let outdentCurrentLine = false;
if (this._indentationRules.increaseIndentPattern && this._indentationRules.increaseIndentPattern.test(beforeEnterText)) {
indentOffset = 1;
}
if (this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(beforeEnterText)) {
indentOffset = 1;
}
/**
* Since the indentation of `beforeEnterText` might not be correct, we still provide the correct indent action
* even if there is nothing to outdent from.
*/
if (this._indentationRules.decreaseIndentPattern && this._indentationRules.decreaseIndentPattern.test(afterEnterText)) {
indentOffset = indentOffset ? indentOffset - 1 : -1;
}
if (this._indentationRules.indentNextLinePattern && this._indentationRules.indentNextLinePattern.test(oneLineAboveText)) {
indentOffset = indentOffset ? indentOffset - 1 : -1;
}
if (this._indentationRules.decreaseIndentPattern && this._indentationRules.decreaseIndentPattern.test(beforeEnterText)) {
outdentCurrentLine = true;
}
if (indentOffset !== null || outdentCurrentLine) {
// this means at least one indentation rule is matched so we should handle it
indentOffset = indentOffset || 0;
switch (indentOffset) {
case -1:
return { indentAction: IndentAction.Outdent, outdentCurrentLine: outdentCurrentLine };
case 0:
return { indentAction: IndentAction.None, outdentCurrentLine: outdentCurrentLine };
case 1:
return { indentAction: IndentAction.Indent, outdentCurrentLine: outdentCurrentLine };
}
}
}
// (4): Open bracket based logic
if (beforeEnterText.length > 0) {
......@@ -125,26 +86,6 @@ export class OnEnterSupport {
return null;
}
public containNonWhitespace(text: string): boolean {
// the text doesn't contain any non-whitespace character.
let nonWhitespaceIdx = strings.lastNonWhitespaceIndex(text);
if (nonWhitespaceIdx >= 0) {
return true;
}
return false;
}
public shouldIgnore(text: string): boolean {
// the text matches `unIndentedLinePattern`
if (this._indentationRules && this._indentationRules.unIndentedLinePattern && this._indentationRules.unIndentedLinePattern.test(text)) {
return true;
}
return false;
}
private static _createOpenBracketRegExp(bracket: string): RegExp {
var str = strings.escapeRegExpCharacters(bracket);
if (!/\B/.test(str.charAt(0))) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册