Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Left shift fix 2299 #2300

Merged
merged 7 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/actions/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ class OutdentOperator extends BaseOperator {
keys = ['<'];

public async run(vimState: VimState, start: Position, end: Position): Promise<VimState> {
vimState.editor.selection = new vscode.Selection(start, end);
vimState.editor.selection = new vscode.Selection(start, end.getLineEnd());

await vscode.commands.executeCommand('editor.action.outdentLines');
vimState.currentMode = ModeName.Normal;
Expand Down
52 changes: 52 additions & 0 deletions test/operator/shift.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getAndUpdateModeHandler } from '../../extension';
import { ModeHandler } from '../../src/mode/modeHandler';
import { getTestingFunctions } from '../testSimplifier';
import { cleanUpWorkspace, setupWorkspace } from '../testUtils';

suite('shift operator', () => {
let modeHandler: ModeHandler;

let { newTest, newTestOnly } = getTestingFunctions();

setup(async () => {
await setupWorkspace();
modeHandler = await getAndUpdateModeHandler();
});

teardown(cleanUpWorkspace);

newTest({
title: 'basic shift left test',
start: [' |zxcv', ' zxcv', ' zxcv'],
keysPressed: '<<',
end: ['|zxcv', ' zxcv', ' zxcv'],
});

newTest({
title: 'shift left goto end test',
start: [' |zxcv', ' zxcv', ' zxcv'],
keysPressed: '<G',
end: ['|zxcv', 'zxcv', 'zxcv'],
});

newTest({
title: 'shift left goto line test',
start: [' |zxcv', ' zxcv', ' zxcv'],
keysPressed: '<2G',
end: ['|zxcv', 'zxcv', ' zxcv'],
});

newTest({
title: 'shift right goto end test',
start: ['|zxcv', 'zxcv', 'zxcv'],
keysPressed: '>G',
end: [' |zxcv', ' zxcv', ' zxcv'],
});

newTest({
title: 'shift right goto line test',
start: ['|zxcv', 'zxcv', 'zxcv'],
keysPressed: '>2G',
end: [' |zxcv', ' zxcv', 'zxcv'],
});
});
19 changes: 18 additions & 1 deletion test/testSimplifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,24 @@ function tokenizeKeySequence(sequence: string): string[] {
let key = '';
let result: string[] = [];

// no close bracket, probably trying to do a left shift, take literal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried walking through this a couple of times, it's probably cause it's the end of the day, but I can't grok this code.

The code might be simpler if instead we walk the string, if we see <, find the matching >. If there's no matching <, then it's a left shift.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what I'm doing is sort of the opposite, I'm looking for another < following a <, and then working back instead of looking ahead.

If I see a second < (i.e. isBracketedKey is already truthy when I hit a <) I know whatever was previous was a left shift and subsequent literal character inputs, so I take the currently built key and tokenize it one character per token.

After the tokenizing loop has run, if isBracketedKey is truthy I know we also saw an unclosed < so again I tokenize the currently built key as one character per token.

I don't mind looking ahead for closing > instead if you still prefer that. I didn't do that because it's a bit more complicated in the case of keysPressed like <<<ESC> where I would have to start counting open <s and looking for matches or recursing which seemed overcomplicated for a convenience test helper function. That would be starting to get into serious parsing territory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although now that I've explained this I've found a bug, I will update.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks for the explanation.

// char sequence
function rawTokenize(characters: string): void {
for (const char of characters) {
result.push(char);
}
}

for (const char of sequence) {
key += char;

if (char === '<') {
isBracketedKey = true;
if (isBracketedKey) {
rawTokenize(key.slice(0, key.length - 1));
key = '<';
} else {
isBracketedKey = true;
}
}

if (char === '>') {
Expand All @@ -190,6 +203,10 @@ function tokenizeKeySequence(sequence: string): string[] {
key = '';
}

if (isBracketedKey) {
rawTokenize(key);
}

return result;
}

Expand Down