Skip to content

Commit

Permalink
Add basic support for mixed indentation style
Browse files Browse the repository at this point in the history
If editor.useSpaces=false but editor.tabSize != editor.indentSize, then
spaces will still be used when required.
  • Loading branch information
zeroimpl committed Nov 17, 2022
1 parent 0182a4b commit ba9ac4f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
20 changes: 12 additions & 8 deletions src/vs/editor/common/commands/shiftCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ export class ShiftCommand implements ICommand {
const indentCount = desiredTabStop / indentSize; // will be an integer
return cachedStringRepeat(indent, indentCount);
} else {
const indent = '\t';
const desiredTabStop = CursorColumns.prevRenderTabStop(contentStartVisibleColumn, tabSize);
const indentCount = desiredTabStop / tabSize; // will be an integer
return cachedStringRepeat(indent, indentCount);
const desiredTabStop = CursorColumns.prevRenderTabStop(contentStartVisibleColumn, indentSize);
const tabCount = Math.floor(desiredTabStop / tabSize);
const spaceCount = desiredTabStop - tabCount * tabSize;
const tabString = cachedStringRepeat('\t', tabCount);
const spaceString = cachedStringRepeat(' ', spaceCount);
return tabString + spaceString;
}
}

Expand All @@ -67,10 +69,12 @@ export class ShiftCommand implements ICommand {
const indentCount = desiredTabStop / indentSize; // will be an integer
return cachedStringRepeat(indent, indentCount);
} else {
const indent = '\t';
const desiredTabStop = CursorColumns.nextRenderTabStop(contentStartVisibleColumn, tabSize);
const indentCount = desiredTabStop / tabSize; // will be an integer
return cachedStringRepeat(indent, indentCount);
const desiredTabStop = CursorColumns.nextRenderTabStop(contentStartVisibleColumn, indentSize);
const tabCount = Math.floor(desiredTabStop / tabSize);
const spaceCount = desiredTabStop - tabCount * tabSize;
const tabString = cachedStringRepeat('\t', tabCount);
const spaceString = cachedStringRepeat(' ', spaceCount);
return tabString + spaceString;
}
}

Expand Down
20 changes: 17 additions & 3 deletions src/vs/editor/common/cursor/cursorTypeOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,32 @@ export class TypeOperations {
return indentation;
}

private static _replaceJumpToNextIndent(config: CursorConfiguration, model: ICursorSimpleModel, selection: Selection, insertsAutoWhitespace: boolean): ReplaceCommand {
private static _replaceJumpToNextIndent(config: CursorConfiguration, model: ICursorSimpleModel, selection: Selection, insertsAutoWhitespace: boolean): ICommand {
let typeText = '';

const position = selection.getStartPosition();
if (config.insertSpaces) {
const visibleColumnFromColumn = config.visibleColumnFromColumn(model, position);
const visibleColumnFromColumn = config.visibleColumnFromColumn(model, selection.getStartPosition());
const indentSize = config.indentSize;
const spacesCnt = indentSize - (visibleColumnFromColumn % indentSize);
for (let i = 0; i < spacesCnt; i++) {
typeText += ' ';
}
} else {
if (config.tabSize > config.indentSize) {
// If configured for mixed indentation and selection is at the front, do a shift instead,
// which will properly normalize existing leading whitespace
const firstNonWhitespace = model.getLineFirstNonWhitespaceColumn(selection.endLineNumber);
if (selection.endColumn <= firstNonWhitespace) {
return new ShiftCommand(selection, {
isUnshift: false,
tabSize: config.tabSize,
indentSize: config.indentSize,
insertSpaces: config.insertSpaces,
useTabStops: config.useTabStops,
autoIndent: config.autoIndent
}, config.languageConfigurationService);
}
}
typeText = '\t';
}

Expand Down

0 comments on commit ba9ac4f

Please sign in to comment.