Skip to content

Commit b4702d5

Browse files
committed
Adopt EditorAutoIndentStrategy
1 parent e625e61 commit b4702d5

9 files changed

Lines changed: 164 additions & 165 deletions

File tree

src/vs/editor/common/commands/shiftCommand.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import { Selection, SelectionDirection } from 'vs/editor/common/core/selection';
1111
import { ICommand, ICursorStateComputerData, IEditOperationBuilder } from 'vs/editor/common/editorCommon';
1212
import { ITextModel } from 'vs/editor/common/model';
1313
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
14+
import { EditorAutoIndentStrategy } from 'vs/editor/common/config/editorOptions';
1415

1516
export interface IShiftCommandOpts {
1617
isUnshift: boolean;
1718
tabSize: number;
1819
indentSize: number;
1920
insertSpaces: boolean;
2021
useTabStops: boolean;
22+
autoIndent: EditorAutoIndentStrategy;
2123
}
2224

2325
const repeatCache: { [str: string]: string[]; } = Object.create(null);
@@ -137,7 +139,7 @@ export class ShiftCommand implements ICommand {
137139
// The current line is "miss-aligned", so let's see if this is expected...
138140
// This can only happen when it has trailing commas in the indent
139141
if (model.isCheapToTokenize(lineNumber - 1)) {
140-
let enterAction = LanguageConfigurationRegistry.getRawEnterActionAtPosition(model, lineNumber - 1, model.getLineMaxColumn(lineNumber - 1));
142+
let enterAction = LanguageConfigurationRegistry.getRawEnterActionAtPosition(this._opts.autoIndent, model, lineNumber - 1, model.getLineMaxColumn(lineNumber - 1));
141143
if (enterAction) {
142144
extraSpaces = previousLineExtraSpaces;
143145
if (enterAction.appendText) {

src/vs/editor/common/controller/cursorCommon.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class CursorConfiguration {
103103
public readonly autoClosingQuotes: EditorAutoClosingStrategy;
104104
public readonly autoClosingOvertype: EditorAutoClosingOvertypeStrategy;
105105
public readonly autoSurround: EditorAutoSurroundStrategy;
106-
public readonly autoIndent2: EditorAutoIndentStrategy;
106+
public readonly autoIndent: EditorAutoIndentStrategy;
107107
public readonly autoClosingPairsOpen2: Map<string, StandardAutoClosingPairConditional[]>;
108108
public readonly autoClosingPairsClose2: Map<string, StandardAutoClosingPairConditional[]>;
109109
public readonly surroundingPairs: CharacterMap;
@@ -155,7 +155,7 @@ export class CursorConfiguration {
155155
this.autoClosingQuotes = options.get(EditorOption.autoClosingQuotes);
156156
this.autoClosingOvertype = options.get(EditorOption.autoClosingOvertype);
157157
this.autoSurround = options.get(EditorOption.autoSurround);
158-
this.autoIndent2 = options.get(EditorOption.autoIndent);
158+
this.autoIndent = options.get(EditorOption.autoIndent);
159159

160160
this.autoClosingPairsOpen2 = new Map<string, StandardAutoClosingPairConditional[]>();
161161
this.autoClosingPairsClose2 = new Map<string, StandardAutoClosingPairConditional[]>();

src/vs/editor/common/controller/cursorTypeOperations.ts

Lines changed: 65 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export class TypeOperations {
3535
tabSize: config.tabSize,
3636
indentSize: config.indentSize,
3737
insertSpaces: config.insertSpaces,
38-
useTabStops: config.useTabStops
38+
useTabStops: config.useTabStops,
39+
autoIndent: config.autoIndent
3940
});
4041
}
4142
return commands;
@@ -49,7 +50,8 @@ export class TypeOperations {
4950
tabSize: config.tabSize,
5051
indentSize: config.indentSize,
5152
insertSpaces: config.insertSpaces,
52-
useTabStops: config.useTabStops
53+
useTabStops: config.useTabStops,
54+
autoIndent: config.autoIndent
5355
});
5456
}
5557
return commands;
@@ -150,15 +152,15 @@ export class TypeOperations {
150152
let action: IndentAction | EnterAction | null = null;
151153
let indentation: string = '';
152154

153-
let expectedIndentAction = config.autoIndent ? LanguageConfigurationRegistry.getInheritIndentForLine(model, lineNumber, false) : null;
155+
const expectedIndentAction = LanguageConfigurationRegistry.getInheritIndentForLine(config.autoIndent, model, lineNumber, false);
154156
if (expectedIndentAction) {
155157
action = expectedIndentAction.action;
156158
indentation = expectedIndentAction.indentation;
157159
} else if (lineNumber > 1) {
158160
let lastLineNumber: number;
159161
for (lastLineNumber = lineNumber - 1; lastLineNumber >= 1; lastLineNumber--) {
160-
let lineText = model.getLineContent(lastLineNumber);
161-
let nonWhitespaceIdx = strings.lastNonWhitespaceIndex(lineText);
162+
const lineText = model.getLineContent(lastLineNumber);
163+
const nonWhitespaceIdx = strings.lastNonWhitespaceIndex(lineText);
162164
if (nonWhitespaceIdx >= 0) {
163165
break;
164166
}
@@ -169,8 +171,8 @@ export class TypeOperations {
169171
return null;
170172
}
171173

172-
let maxColumn = model.getLineMaxColumn(lastLineNumber);
173-
let expectedEnterAction = LanguageConfigurationRegistry.getEnterAction(model, new Range(lastLineNumber, maxColumn, lastLineNumber, maxColumn));
174+
const maxColumn = model.getLineMaxColumn(lastLineNumber);
175+
const expectedEnterAction = LanguageConfigurationRegistry.getEnterAction(config.autoIndent, model, new Range(lastLineNumber, maxColumn, lastLineNumber, maxColumn));
174176
if (expectedEnterAction) {
175177
indentation = expectedEnterAction.indentation;
176178
action = expectedEnterAction.enterAction;
@@ -252,7 +254,8 @@ export class TypeOperations {
252254
tabSize: config.tabSize,
253255
indentSize: config.indentSize,
254256
insertSpaces: config.insertSpaces,
255-
useTabStops: config.useTabStops
257+
useTabStops: config.useTabStops,
258+
autoIndent: config.autoIndent
256259
});
257260
}
258261
}
@@ -290,21 +293,19 @@ export class TypeOperations {
290293
}
291294

292295
private static _enter(config: CursorConfiguration, model: ITextModel, keepPosition: boolean, range: Range): ICommand {
293-
if (config.autoIndent2 === EditorAutoIndentStrategy.None) {
296+
if (config.autoIndent === EditorAutoIndentStrategy.None) {
294297
return TypeOperations._typeCommand(range, '\n', keepPosition);
295298
}
296-
if (!model.isCheapToTokenize(range.getStartPosition().lineNumber) || config.autoIndent2 === EditorAutoIndentStrategy.Keep) {
299+
if (!model.isCheapToTokenize(range.getStartPosition().lineNumber) || config.autoIndent === EditorAutoIndentStrategy.Keep) {
297300
let lineText = model.getLineContent(range.startLineNumber);
298301
let indentation = strings.getLeadingWhitespace(lineText).substring(0, range.startColumn - 1);
299302
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation), keepPosition);
300303
}
301304

302-
const autoIndent = config.autoIndent2;
303-
304-
let r = LanguageConfigurationRegistry.getEnterAction(model, range);
305+
const r = LanguageConfigurationRegistry.getEnterAction(config.autoIndent, model, range);
305306
if (r) {
306-
let enterAction = r.enterAction;
307-
let indentation = r.indentation;
307+
const enterAction = r.enterAction;
308+
const indentation = r.indentation;
308309

309310
if (enterAction.indentAction === IndentAction.None) {
310311
// Nothing special
@@ -316,83 +317,76 @@ export class TypeOperations {
316317

317318
} else if (enterAction.indentAction === IndentAction.IndentOutdent) {
318319
// Ultra special
319-
let normalIndent = config.normalizeIndentation(indentation);
320-
let increasedIndent = config.normalizeIndentation(indentation + enterAction.appendText);
320+
const normalIndent = config.normalizeIndentation(indentation);
321+
const increasedIndent = config.normalizeIndentation(indentation + enterAction.appendText);
321322

322-
let typeText = '\n' + increasedIndent + '\n' + normalIndent;
323+
const typeText = '\n' + increasedIndent + '\n' + normalIndent;
323324

324325
if (keepPosition) {
325326
return new ReplaceCommandWithoutChangingPosition(range, typeText, true);
326327
} else {
327328
return new ReplaceCommandWithOffsetCursorState(range, typeText, -1, increasedIndent.length - normalIndent.length, true);
328329
}
329330
} else if (enterAction.indentAction === IndentAction.Outdent) {
330-
let actualIndentation = TypeOperations.unshiftIndent(config, indentation);
331+
const actualIndentation = TypeOperations.unshiftIndent(config, indentation);
331332
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(actualIndentation + enterAction.appendText), keepPosition);
332333
}
333334
}
334335

335-
// no enter rules applied, we should check indentation rules then.
336-
if (!config.autoIndent) {
337-
// Nothing special
338-
let lineText = model.getLineContent(range.startLineNumber);
339-
let indentation = strings.getLeadingWhitespace(lineText).substring(0, range.startColumn - 1);
340-
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation), keepPosition);
341-
}
342-
343-
let ir = LanguageConfigurationRegistry.getIndentForEnter(model, range, {
344-
unshiftIndent: (indent) => {
345-
return TypeOperations.unshiftIndent(config, indent);
346-
},
347-
shiftIndent: (indent) => {
348-
return TypeOperations.shiftIndent(config, indent);
349-
},
350-
normalizeIndentation: (indent) => {
351-
return config.normalizeIndentation(indent);
352-
}
353-
}, config.autoIndent);
336+
const lineText = model.getLineContent(range.startLineNumber);
337+
const indentation = strings.getLeadingWhitespace(lineText).substring(0, range.startColumn - 1);
354338

355-
let lineText = model.getLineContent(range.startLineNumber);
356-
let indentation = strings.getLeadingWhitespace(lineText).substring(0, range.startColumn - 1);
339+
if (config.autoIndent >= EditorAutoIndentStrategy.Full) {
340+
const ir = LanguageConfigurationRegistry.getIndentForEnter(config.autoIndent, model, range, {
341+
unshiftIndent: (indent) => {
342+
return TypeOperations.unshiftIndent(config, indent);
343+
},
344+
shiftIndent: (indent) => {
345+
return TypeOperations.shiftIndent(config, indent);
346+
},
347+
normalizeIndentation: (indent) => {
348+
return config.normalizeIndentation(indent);
349+
}
350+
});
357351

358-
if (ir) {
359-
let oldEndViewColumn = CursorColumns.visibleColumnFromColumn2(config, model, range.getEndPosition());
360-
let oldEndColumn = range.endColumn;
352+
if (ir) {
353+
let oldEndViewColumn = CursorColumns.visibleColumnFromColumn2(config, model, range.getEndPosition());
354+
const oldEndColumn = range.endColumn;
361355

362-
let beforeText = '\n';
363-
if (indentation !== config.normalizeIndentation(ir.beforeEnter)) {
364-
beforeText = config.normalizeIndentation(ir.beforeEnter) + lineText.substring(indentation.length, range.startColumn - 1) + '\n';
365-
range = new Range(range.startLineNumber, 1, range.endLineNumber, range.endColumn);
366-
}
356+
let beforeText = '\n';
357+
if (indentation !== config.normalizeIndentation(ir.beforeEnter)) {
358+
beforeText = config.normalizeIndentation(ir.beforeEnter) + lineText.substring(indentation.length, range.startColumn - 1) + '\n';
359+
range = new Range(range.startLineNumber, 1, range.endLineNumber, range.endColumn);
360+
}
367361

368-
let newLineContent = model.getLineContent(range.endLineNumber);
369-
let firstNonWhitespace = strings.firstNonWhitespaceIndex(newLineContent);
370-
if (firstNonWhitespace >= 0) {
371-
range = range.setEndPosition(range.endLineNumber, Math.max(range.endColumn, firstNonWhitespace + 1));
372-
} else {
373-
range = range.setEndPosition(range.endLineNumber, model.getLineMaxColumn(range.endLineNumber));
374-
}
362+
const newLineContent = model.getLineContent(range.endLineNumber);
363+
const firstNonWhitespace = strings.firstNonWhitespaceIndex(newLineContent);
364+
if (firstNonWhitespace >= 0) {
365+
range = range.setEndPosition(range.endLineNumber, Math.max(range.endColumn, firstNonWhitespace + 1));
366+
} else {
367+
range = range.setEndPosition(range.endLineNumber, model.getLineMaxColumn(range.endLineNumber));
368+
}
375369

376-
if (keepPosition) {
377-
return new ReplaceCommandWithoutChangingPosition(range, beforeText + config.normalizeIndentation(ir.afterEnter), true);
378-
} else {
379-
let offset = 0;
380-
if (oldEndColumn <= firstNonWhitespace + 1) {
381-
if (!config.insertSpaces) {
382-
oldEndViewColumn = Math.ceil(oldEndViewColumn / config.indentSize);
370+
if (keepPosition) {
371+
return new ReplaceCommandWithoutChangingPosition(range, beforeText + config.normalizeIndentation(ir.afterEnter), true);
372+
} else {
373+
let offset = 0;
374+
if (oldEndColumn <= firstNonWhitespace + 1) {
375+
if (!config.insertSpaces) {
376+
oldEndViewColumn = Math.ceil(oldEndViewColumn / config.indentSize);
377+
}
378+
offset = Math.min(oldEndViewColumn + 1 - config.normalizeIndentation(ir.afterEnter).length - 1, 0);
383379
}
384-
offset = Math.min(oldEndViewColumn + 1 - config.normalizeIndentation(ir.afterEnter).length - 1, 0);
380+
return new ReplaceCommandWithOffsetCursorState(range, beforeText + config.normalizeIndentation(ir.afterEnter), 0, offset, true);
385381
}
386-
return new ReplaceCommandWithOffsetCursorState(range, beforeText + config.normalizeIndentation(ir.afterEnter), 0, offset, true);
387382
}
388-
389-
} else {
390-
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation), keepPosition);
391383
}
384+
385+
return TypeOperations._typeCommand(range, '\n' + config.normalizeIndentation(indentation), keepPosition);
392386
}
393387

394388
private static _isAutoIndentType(config: CursorConfiguration, model: ITextModel, selections: Selection[]): boolean {
395-
if (!config.autoIndent) {
389+
if (config.autoIndent < EditorAutoIndentStrategy.Full) {
396390
return false;
397391
}
398392

@@ -406,8 +400,8 @@ export class TypeOperations {
406400
}
407401

408402
private static _runAutoIndentType(config: CursorConfiguration, model: ITextModel, range: Range, ch: string): ICommand | null {
409-
let currentIndentation = LanguageConfigurationRegistry.getIndentationAtPosition(model, range.startLineNumber, range.startColumn);
410-
let actualIndentation = LanguageConfigurationRegistry.getIndentActionForType(model, range, ch, {
403+
const currentIndentation = LanguageConfigurationRegistry.getIndentationAtPosition(model, range.startLineNumber, range.startColumn);
404+
const actualIndentation = LanguageConfigurationRegistry.getIndentActionForType(config.autoIndent, model, range, ch, {
411405
shiftIndent: (indentation) => {
412406
return TypeOperations.shiftIndent(config, indentation);
413407
},
@@ -421,7 +415,7 @@ export class TypeOperations {
421415
}
422416

423417
if (actualIndentation !== config.normalizeIndentation(currentIndentation)) {
424-
let firstNonWhitespace = model.getLineFirstNonWhitespaceColumn(range.startLineNumber);
418+
const firstNonWhitespace = model.getLineFirstNonWhitespaceColumn(range.startLineNumber);
425419
if (firstNonWhitespace === 0) {
426420
return TypeOperations._typeCommand(
427421
new Range(range.startLineNumber, 0, range.endLineNumber, range.endColumn),

0 commit comments

Comments
 (0)