From e82a1bef993a7499af90d2a1aa1735fd524af348 Mon Sep 17 00:00:00 2001 From: rebornix Date: Thu, 8 Jun 2017 18:16:53 -0700 Subject: [PATCH] AutoIndent: Type supports selection. --- .../common/controller/cursorTypeOperations.ts | 17 +++++++-------- .../modes/languageConfigurationRegistry.ts | 21 ++++++++++++------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/vs/editor/common/controller/cursorTypeOperations.ts b/src/vs/editor/common/controller/cursorTypeOperations.ts index d10b0d5951c..7f57c9f51f8 100644 --- a/src/vs/editor/common/controller/cursorTypeOperations.ts +++ b/src/vs/editor/common/controller/cursorTypeOperations.ts @@ -322,10 +322,9 @@ export class TypeOperations { } } - 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, { + private static _runAutoIndentType(config: CursorConfiguration, model: ITokenizedModel, range: Range, ch: string): ICommand { + let currentIndentation = LanguageConfigurationRegistry.getIndentationAtPosition(model, range.startLineNumber, range.startColumn); + let actualIndentation = LanguageConfigurationRegistry.getIndentActionForType(model, range, ch, { shiftIndent: (indentation) => { return TypeOperations.shiftIndent(config, indentation); }, @@ -339,18 +338,18 @@ export class TypeOperations { } if (actualIndentation !== config.normalizeIndentation(currentIndentation)) { - let firstNonWhitespace = model.getLineFirstNonWhitespaceColumn(selection.startLineNumber); + let firstNonWhitespace = model.getLineFirstNonWhitespaceColumn(range.startLineNumber); if (firstNonWhitespace === 0) { return TypeOperations._typeCommand( - new Range(selection.startLineNumber, 0, selection.startLineNumber, selection.startColumn), + new Range(range.startLineNumber, 0, range.endLineNumber, range.endColumn), actualIndentation + ch, false ); } else { return TypeOperations._typeCommand( - new Range(selection.startLineNumber, 0, selection.startLineNumber, selection.startColumn), + new Range(range.startLineNumber, 0, range.endLineNumber, range.endColumn), actualIndentation + - model.getLineContent(selection.startLineNumber).substring(firstNonWhitespace - 1, selection.startColumn) + ch, + model.getLineContent(range.startLineNumber).substring(firstNonWhitespace - 1, range.startColumn - 1) + ch, false ); } @@ -596,7 +595,7 @@ export class TypeOperations { }); } - let indentCommand = this._runAutoIndentType(config, model, selections, ch); + let indentCommand = this._runAutoIndentType(config, model, selections[0], ch); if (indentCommand) { return new EditOperationResult([indentCommand], { shouldPushStackElementBefore: true, diff --git a/src/vs/editor/common/modes/languageConfigurationRegistry.ts b/src/vs/editor/common/modes/languageConfigurationRegistry.ts index c15da62eb22..2c9d960febc 100644 --- a/src/vs/editor/common/modes/languageConfigurationRegistry.ts +++ b/src/vs/editor/common/modes/languageConfigurationRegistry.ts @@ -497,24 +497,29 @@ export class LanguageConfigurationRegistryImpl { * We should always allow intentional indentation. It means, if users change the indentation of `lineNumber` and the content of * this line doesn't match decreaseIndentPattern, we should not adjust the indentation. */ - public getIndentActionForType(model: ITokenizedModel, lineNumber: number, column: number, ch: string, indentConverter: any): string { - let maxColumn = model.getLineMaxColumn(lineNumber); - // let indentation = this.getIndentationAtPosition(model, lineNumber, column); - - let scopedLineTokens = this.getScopedLineTokens(model, lineNumber, maxColumn); + public getIndentActionForType(model: ITokenizedModel, range: Range, ch: string, indentConverter: any): string { + let scopedLineTokens = this.getScopedLineTokens(model, range.startLineNumber, range.startColumn); let indentRulesSupport = this._getIndentRulesSupport(scopedLineTokens.languageId); if (!indentRulesSupport) { return null; } let scopedLineText = scopedLineTokens.getLineContent(); - let beforeTypeText = scopedLineText.substr(0, column - 1); - let afterTypeText = scopedLineText.substr(column - 1); + let beforeTypeText = scopedLineText.substr(0, range.startColumn - 1 - scopedLineTokens.firstCharOffset); + let afterTypeText; + + // selection support + if (range.isEmpty()) { + afterTypeText = scopedLineText.substr(range.startColumn - 1 - scopedLineTokens.firstCharOffset); + } else { + const endScopedLineTokens = this.getScopedLineTokens(model, range.endLineNumber, range.endColumn); + afterTypeText = endScopedLineTokens.getLineContent().substr(range.endColumn - 1 - scopedLineTokens.firstCharOffset); + } if (indentRulesSupport.shouldDecrease(beforeTypeText + ch + afterTypeText)) { // after typing `ch`, the content matches decreaseIndentPattern, we should adjust the indent to a good manner. // 1. Get inherited indent action - let r = this.getInheritIndentForLine(model, lineNumber, false); + let r = this.getInheritIndentForLine(model, range.startLineNumber, false); if (!r) { return null; } -- GitLab