提交 e82a1bef 编写于 作者: R rebornix

AutoIndent: Type supports selection.

上级 557f010f
......@@ -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,
......
......@@ -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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册