提交 e82a1bef 编写于 作者: R rebornix

AutoIndent: Type supports selection.

上级 557f010f
...@@ -322,10 +322,9 @@ export class TypeOperations { ...@@ -322,10 +322,9 @@ export class TypeOperations {
} }
} }
private static _runAutoIndentType(config: CursorConfiguration, model: ITokenizedModel, selections: Selection[], ch: string): ICommand { private static _runAutoIndentType(config: CursorConfiguration, model: ITokenizedModel, range: Range, ch: string): ICommand {
let selection = selections[0]; let currentIndentation = LanguageConfigurationRegistry.getIndentationAtPosition(model, range.startLineNumber, range.startColumn);
let currentIndentation = LanguageConfigurationRegistry.getIndentationAtPosition(model, selection.startLineNumber, selection.startColumn); let actualIndentation = LanguageConfigurationRegistry.getIndentActionForType(model, range, ch, {
let actualIndentation = LanguageConfigurationRegistry.getIndentActionForType(model, selections[0].startLineNumber, selections[0].startColumn, ch, {
shiftIndent: (indentation) => { shiftIndent: (indentation) => {
return TypeOperations.shiftIndent(config, indentation); return TypeOperations.shiftIndent(config, indentation);
}, },
...@@ -339,18 +338,18 @@ export class TypeOperations { ...@@ -339,18 +338,18 @@ export class TypeOperations {
} }
if (actualIndentation !== config.normalizeIndentation(currentIndentation)) { if (actualIndentation !== config.normalizeIndentation(currentIndentation)) {
let firstNonWhitespace = model.getLineFirstNonWhitespaceColumn(selection.startLineNumber); let firstNonWhitespace = model.getLineFirstNonWhitespaceColumn(range.startLineNumber);
if (firstNonWhitespace === 0) { if (firstNonWhitespace === 0) {
return TypeOperations._typeCommand( return TypeOperations._typeCommand(
new Range(selection.startLineNumber, 0, selection.startLineNumber, selection.startColumn), new Range(range.startLineNumber, 0, range.endLineNumber, range.endColumn),
actualIndentation + ch, actualIndentation + ch,
false false
); );
} else { } else {
return TypeOperations._typeCommand( return TypeOperations._typeCommand(
new Range(selection.startLineNumber, 0, selection.startLineNumber, selection.startColumn), new Range(range.startLineNumber, 0, range.endLineNumber, range.endColumn),
actualIndentation + actualIndentation +
model.getLineContent(selection.startLineNumber).substring(firstNonWhitespace - 1, selection.startColumn) + ch, model.getLineContent(range.startLineNumber).substring(firstNonWhitespace - 1, range.startColumn - 1) + ch,
false false
); );
} }
...@@ -596,7 +595,7 @@ export class TypeOperations { ...@@ -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) { if (indentCommand) {
return new EditOperationResult([indentCommand], { return new EditOperationResult([indentCommand], {
shouldPushStackElementBefore: true, shouldPushStackElementBefore: true,
......
...@@ -497,24 +497,29 @@ export class LanguageConfigurationRegistryImpl { ...@@ -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 * 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. * 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 { public getIndentActionForType(model: ITokenizedModel, range: Range, ch: string, indentConverter: any): string {
let maxColumn = model.getLineMaxColumn(lineNumber); let scopedLineTokens = this.getScopedLineTokens(model, range.startLineNumber, range.startColumn);
// let indentation = this.getIndentationAtPosition(model, lineNumber, column);
let scopedLineTokens = this.getScopedLineTokens(model, lineNumber, maxColumn);
let indentRulesSupport = this._getIndentRulesSupport(scopedLineTokens.languageId); let indentRulesSupport = this._getIndentRulesSupport(scopedLineTokens.languageId);
if (!indentRulesSupport) { if (!indentRulesSupport) {
return null; return null;
} }
let scopedLineText = scopedLineTokens.getLineContent(); let scopedLineText = scopedLineTokens.getLineContent();
let beforeTypeText = scopedLineText.substr(0, column - 1); let beforeTypeText = scopedLineText.substr(0, range.startColumn - 1 - scopedLineTokens.firstCharOffset);
let afterTypeText = scopedLineText.substr(column - 1); 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)) { if (indentRulesSupport.shouldDecrease(beforeTypeText + ch + afterTypeText)) {
// after typing `ch`, the content matches decreaseIndentPattern, we should adjust the indent to a good manner. // after typing `ch`, the content matches decreaseIndentPattern, we should adjust the indent to a good manner.
// 1. Get inherited indent action // 1. Get inherited indent action
let r = this.getInheritIndentForLine(model, lineNumber, false); let r = this.getInheritIndentForLine(model, range.startLineNumber, false);
if (!r) { if (!r) {
return null; return null;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册