Skip to content

Commit

Permalink
Format operator fixes and tests
Browse files Browse the repository at this point in the history
Make format operator (=) consistent with Vim8/NeoVim.
- Format operator now operates on complete lines.
- Cursor ends on first non-blank character instead of column 0.

fix VSCodeVim#1574
  • Loading branch information
watsoncj committed Dec 9, 2018
1 parent 0151438 commit f9ef65c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/actions/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ export class FormatOperator extends BaseOperator {
public modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];

public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
// = operates on complete lines
start = new Position(start.line, 0);
end = end.getLineEnd();
vimState.editor.selection = new vscode.Selection(start, end);
await vscode.commands.executeCommand('editor.action.formatSelection');
let line = vimState.cursorStartPosition.line;
Expand All @@ -351,7 +354,7 @@ export class FormatOperator extends BaseOperator {
line = vimState.cursorPosition.line;
}

let newCursorPosition = new Position(line, 0);
let newCursorPosition = new Position(line, 0).getFirstLineNonBlankChar();
vimState.cursorPosition = newCursorPosition;
vimState.cursorStartPosition = newCursorPosition;
await vimState.setCurrentMode(ModeName.Normal);
Expand Down
69 changes: 69 additions & 0 deletions test/operator/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ModeName } from '../../src/mode/mode';
import { getTestingFunctions, ITestObject } from '../testSimplifier';
import { cleanUpWorkspace, setupWorkspace } from './../testUtils';

suite('format operator', () => {
let { newTest, newTestOnly } = getTestingFunctions();

setup(async () => {
await setupWorkspace(undefined, '.ts');
});

teardown(cleanUpWorkspace);

newTest({
title: '== formats current line',
start: [' |let a;', ' let b;'],
keysPressed: '==',
end: ['|let a;', ' let b;'],
});

newTest({
title: '=$ formats entire line',
start: [' function f() {|let a;', 'let b;', '}'],
keysPressed: '=$',
end: ['|function f() {', ' let a;', 'let b;', '}'],
});

newTest({
title: '=j formats two lines',
start: [' |let a;', ' let b;', ' let c;'],
keysPressed: '=j',
end: ['|let a;', 'let b;', ' let c;'],
});

newTest({
title: '3=k formats three lines',
start: [' let a;', ' let b;', '| let c;'],
keysPressed: '3=k',
end: ['|let a;', 'let b;', 'let c;'],
});

newTest({
title: '=gg formats to top of file',
start: [' let a;', ' let b;', '| let c;'],
keysPressed: '=gg',
end: ['|let a;', 'let b;', 'let c;'],
});

newTest({
title: '=G formats to bottom of file',
start: ['| let a;', ' let b;', ' let c;'],
keysPressed: '=G',
end: ['|let a;', 'let b;', 'let c;'],
});

newTest({
title: '=ip formats paragraph',
start: [' function f() {', '|let a;', ' }', '', ' let b;'],
keysPressed: '=ip',
end: ['|function f() {', ' let a;', '}', '', ' let b;'],
});

newTest({
title: 'format in visual mode',
start: [' function f() {', 'let a;', '| }', '', ' let b;'],
keysPressed: 'vkk=',
end: ['|function f() {', ' let a;', '}', '', ' let b;'],
});
});

0 comments on commit f9ef65c

Please sign in to comment.