Skip to content

Commit

Permalink
Fix autoindent on cc/S #2497
Browse files Browse the repository at this point in the history
Many VSCodeVim commands already respect VS Code's build-in indentation, so I thought it should stay that way
`cc` and `S` now act like when vim's filetype indent is turned on
  • Loading branch information
dqsully committed Jun 13, 2018
1 parent d6c61bc commit 32e6121
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
11 changes: 1 addition & 10 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2130,16 +2130,7 @@ class CommandClearLine extends BaseCommand {
runsOnceForEachCountPrefix = false;

public async exec(position: Position, vimState: VimState): Promise<VimState> {
let count = vimState.recordedState.count || 1;
let end = position
.getDownByCount(Math.max(0, count - 1))
.getLineEnd()
.getLeft();
return new operator.ChangeOperator().run(
vimState,
position.getLineBeginRespectingIndent(),
end
);
return new operator.ChangeOperator(this.multicursorIndex).runRepeat(vimState, position, 1);
}

// Don't clash with sneak
Expand Down
28 changes: 15 additions & 13 deletions src/actions/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,21 +566,23 @@ export class ChangeOperator extends BaseOperator {
}

public async runRepeat(vimState: VimState, position: Position, count: number): Promise<VimState> {
const lineIsAllWhitespace = TextEditor.getLineAt(position).text.trim() === '';
vimState.currentRegisterMode = RegisterMode.LineWise;
if (lineIsAllWhitespace) {
return this.run(
vimState,
position.getLineBegin(),
position.getDownByCount(Math.max(0, count - 1)).getLineEnd()
);
} else {
return this.run(
vimState,
position.getLineBeginRespectingIndent(),
position.getDownByCount(Math.max(0, count - 1)).getLineEnd()
);

vimState = await this.run(
vimState,
position.getLineBegin(),
position.getDownByCount(Math.max(0, count - 1)).getLineEnd()
);

if (configuration.autoindent) {
vimState.recordedState.transformations.push({
type: 'reindent',
cursorIndex: this.multicursorIndex,
diff: new PositionDiff(0, 1), // Handle transition from Normal to Insert modes
});
}

return vimState;
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,20 @@ export class ModeHandler implements vscode.Disposable {
accumulatedPositionDifferences[command.cursorIndex].push(command.diff);
}
break;
case 'reindent':
await vscode.commands.executeCommand('editor.action.reindentselectedlines');
if (command.diff) {
if (command.cursorIndex === undefined) {
throw new Error('No cursor index - this should never ever happen!');
}

if (!accumulatedPositionDifferences[command.cursorIndex]) {
accumulatedPositionDifferences[command.cursorIndex] = [];
}

accumulatedPositionDifferences[command.cursorIndex].push(command.diff);
}
break;
default:
console.warn(`Unhandled text transformation type: ${command.type}.`);
break;
Expand Down
16 changes: 15 additions & 1 deletion src/transformations/transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ export interface Tab {
diff?: PositionDiff;
}

/**
* Represents reindenting the selected line
*/
export interface Reindent {
type: 'reindent';
cursorIndex?: number;

/**
* Move the cursor this much.
*/
diff?: PositionDiff;
}

/**
* Represents macro
*/
Expand Down Expand Up @@ -246,7 +259,8 @@ export type Transformation =
| Macro
| ContentChangeTransformation
| DeleteTextTransformation
| Tab;
| Tab
| Reindent;

/**
* Text Transformations
Expand Down

0 comments on commit 32e6121

Please sign in to comment.