Skip to content

Commit

Permalink
Merge pull request #2307 from jpotterm/sneak
Browse files Browse the repository at this point in the history
Sneak plugin
  • Loading branch information
Chillee authored Feb 10, 2018
2 parents f9ffe42 + a765407 commit c8d6d34
Show file tree
Hide file tree
Showing 13 changed files with 367 additions and 55 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Please report missing features/bugs on [GitHub](https://github.com/VSCodeVim/Vim
* [vim-surround](#vim-surround)
* [vim-commentary](#vim-commentary)
* [vim-indent-object](#vim-indent-object)
* [vim-sneak](#vim-sneak)
* [VSCodeVim tricks](#vscodevim-tricks)
* [F.A.Q / Troubleshooting](#faq)
* [Contributing](#contributing)
Expand Down Expand Up @@ -72,6 +73,7 @@ Below is an example of a [settings.json](https://code.visualstudio.com/Docs/cust
```json
{
"vim.easymotion": true,
"vim.sneak": true,
"vim.incsearch": true,
"vim.useSystemClipboard": true,
"vim.useCtrlKeys": true,
Expand Down Expand Up @@ -484,6 +486,21 @@ Command | Description
`<operator>ai`|This indentation level and the line above (think `if` statements in Python)
`<operator>aI`|This indentation level, the line above, and the line after (think `if` statements in C/C++/Java/etc)


### vim-sneak

Based on [vim-sneak](https://github.com/justinmk/vim-sneak). To activate sneak, you need to make sure that `sneak` is set to `true` in settings.json (default is `false`).

Once sneak is active, initiate motions using the following commands. For operators sneak uses `z` instead of `s` because `s` is already taken by the surround plugin.

Motion Command | Description
---|--------
`s<char><char>`|Move forward to the first occurence of `<char><char>`
`S<char><char>`|Move backward to the first occurence of `<char><char>`
`<operator>z<char><char>`|Perform `<operator>` forward to the first occurence of `<char><char>`
`<operator>Z<char><char>`|Perform `<operator>` backward to the first occurence of `<char><char>`


## VSCodeVim tricks!

Vim has a lot of nifty tricks and we try to preserve some of them:
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@
"description": "Set the characters used for jump marker name",
"default": "hklyuiopnm,qwertzxcvbasdgjf;"
},
"vim.sneak": {
"type": "boolean",
"description": "Enable the Sneak plugin for Vim.",
"default": false
},
"vim.surround": {
"type": "boolean",
"description": "Enable the Surround plugin for Vim.",
Expand Down
72 changes: 69 additions & 3 deletions src/actions/commands/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,13 @@ abstract class CommandFold extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine];
commandName: string;

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
// Don't run if there's an operator because the Sneak plugin uses <operator>z
return (
super.doesActionApply(vimState, keysPressed) && vimState.recordedState.operator === undefined
);
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
await vscode.commands.executeCommand(this.commandName);
vimState.currentMode = ModeName.Normal;
Expand Down Expand Up @@ -1716,6 +1723,13 @@ class CommandCenterScroll extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ['z', 'z'];

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
// Don't run if there's an operator because the Sneak plugin uses <operator>z
return (
super.doesActionApply(vimState, keysPressed) && vimState.recordedState.operator === undefined
);
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
// In these modes you want to center on the cursor position
vimState.editor.revealRange(
Expand All @@ -1732,6 +1746,13 @@ class CommandCenterScrollFirstChar extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ['z', '.'];

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
// Don't run if there's an operator because the Sneak plugin uses <operator>z
return (
super.doesActionApply(vimState, keysPressed) && vimState.recordedState.operator === undefined
);
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
// In these modes you want to center on the cursor position
// This particular one moves cursor to first non blank char though
Expand All @@ -1752,6 +1773,13 @@ class CommandTopScroll extends BaseCommand {
modes = [ModeName.Normal];
keys = ['z', 't'];

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
// Don't run if there's an operator because the Sneak plugin uses <operator>z
return (
super.doesActionApply(vimState, keysPressed) && vimState.recordedState.operator === undefined
);
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.postponedCodeViewChanges.push({
command: 'revealLine',
Expand All @@ -1769,6 +1797,13 @@ class CommandTopScrollFirstChar extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ['z', '\n'];

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
// Don't run if there's an operator because the Sneak plugin uses <operator>z
return (
super.doesActionApply(vimState, keysPressed) && vimState.recordedState.operator === undefined
);
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
// In these modes you want to center on the cursor position
// This particular one moves cursor to first non blank char though
Expand All @@ -1792,6 +1827,13 @@ class CommandBottomScroll extends BaseCommand {
modes = [ModeName.Normal];
keys = ['z', 'b'];

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
// Don't run if there's an operator because the Sneak plugin uses <operator>z
return (
super.doesActionApply(vimState, keysPressed) && vimState.recordedState.operator === undefined
);
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
vimState.postponedCodeViewChanges.push({
command: 'revealLine',
Expand All @@ -1809,6 +1851,13 @@ class CommandBottomScrollFirstChar extends BaseCommand {
modes = [ModeName.Normal, ModeName.Visual, ModeName.VisualLine, ModeName.VisualBlock];
keys = ['z', '-'];

public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
// Don't run if there's an operator because the Sneak plugin uses <operator>z
return (
super.doesActionApply(vimState, keysPressed) && vimState.recordedState.operator === undefined
);
}

public async exec(position: Position, vimState: VimState): Promise<VimState> {
// In these modes you want to center on the cursor position
// This particular one moves cursor to first non blank char though
Expand Down Expand Up @@ -1981,6 +2030,15 @@ class CommandClearLine extends BaseCommand {
end
);
}

// Don't clash with sneak
public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
return super.doesActionApply(vimState, keysPressed) && !configuration.sneak;
}

public couldActionApply(vimState: VimState, keysPressed: string[]): boolean {
return super.couldActionApply(vimState, keysPressed) && !configuration.sneak;
}
}

@RegisterAction
Expand Down Expand Up @@ -3332,13 +3390,21 @@ class ActionChangeChar extends BaseCommand {
return state;
}

// Don't clash with surround mode!
// Don't clash with surround or sneak modes!
public doesActionApply(vimState: VimState, keysPressed: string[]): boolean {
return super.doesActionApply(vimState, keysPressed) && !vimState.recordedState.operator;
return (
super.doesActionApply(vimState, keysPressed) &&
!configuration.sneak &&
!vimState.recordedState.operator
);
}

public couldActionApply(vimState: VimState, keysPressed: string[]): boolean {
return super.doesActionApply(vimState, keysPressed) && !vimState.recordedState.operator;
return (
super.doesActionApply(vimState, keysPressed) &&
!configuration.sneak &&
!vimState.recordedState.operator
);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/actions/include-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import './commands/actions';
// plugin
import './plugins/easymotion/easymotion.cmd';
import './plugins/easymotion/registerMoveActions';
import './plugins/sneak';
import './plugins/surround';
Loading

0 comments on commit c8d6d34

Please sign in to comment.