Skip to content

Surround fixup #6860

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,14 @@ Some examples:
- `"test"` with cursor inside quotes type `ds"` to end up with `test`
- `"test"` with cursor inside quotes type `cs"t` and enter `123>` to end up with `<123>test</123>`

Surround mappings:
since 1.20, we internally use special key notation for surround ( `<plugys>`, `<plugcs>`, `<plugds>` ), for which we create default mappings. This has two consequences:

- custom mappings need to use these too.\
Example: `nnoremap s" <plugys>iw"`
- if you use a custom keyboard layout (workman, dvorak, etc.), the default mappings will not fit for you.
You need to disable by settting `vim.enableDefaultPluginMappings` to false and then create a mapping for the 3 key sequences above, like so:\
Example: `nnoremap ys <plugys>` where you replace `ys` with what fits for your layout.
### vim-commentary

Similar to [vim-commentary](https://github.com/tpope/vim-commentary), but uses the VS Code native _Toggle Line Comment_ and _Toggle Block Comment_ features.
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,11 @@
"markdownDescription": "Enable the [Surround](https://github.com/tpope/vim-surround) plugin for Vim.",
"default": true
},
"vim.enableDefaultPluginMappings": {
"type": "boolean",
"markdownDescription": "For users of custom keyboard layouts. Disable default keymappings starting with c/d/y. At present only affects surround.",
"default": true
},
"vim.argumentObjectSeparators": {
"type": "array",
"items": {
Expand Down
25 changes: 22 additions & 3 deletions src/actions/plugins/pluginDefaultMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,30 @@ export class PluginDefaultMappings {
configSwitch: 'surround',
mapping: { before: ['d', 's'], after: ['<plugds>'] },
},
// support some special cases with mappings
// see: https://github.com/tpope/vim-surround/blob/master/doc/surround.txt, TARGETS w,W,s
{
mode: 'normalModeKeyBindingsNonRecursive',
configSwitch: 'surround',
mapping: { before: ['c', 's', 'w'], after: ['<plugys>', 'i', 'w'] },
},
{
mode: 'normalModeKeyBindingsNonRecursive',
configSwitch: 'surround',
mapping: { before: ['c', 's', 'W'], after: ['<plugys>', 'i', 'W'] },
},
{
mode: 'normalModeKeyBindingsNonRecursive',
configSwitch: 'surround',
mapping: { before: ['c', 's', 's'], after: ['<plugys>', 'i', 's'] },
},
];

public static getPluginDefaultMappings(mode: string, config: IConfiguration): IKeyRemapping[] {
return this.defaultMappings
.filter((m) => m.mode === mode && config[m.configSwitch])
.map((m) => m.mapping);
return config.enableDefaultPluginMappings
? this.defaultMappings
.filter((m) => m.mode === mode && config[m.configSwitch])
.map((m) => m.mapping)
: [];
}
}
2 changes: 2 additions & 0 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ class Configuration implements IConfiguration {

surround = true;

enableDefaultPluginMappings = true;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

about this switch: I want to do one switch for all cases (right now there is only surround. there might be coerce cr or exchange cx).
Not one per possible plugin; that would bloat the code and config.
the only real use case I see, is the few users with custom keyboard layouts. For that, mappings like cr or cx will be wrong same as cs.

argumentObjectSeparators = [','];
argumentObjectOpeningDelimiters = ['(', '['];
argumentObjectClosingDelimiters = [')', ']'];
Expand Down
4 changes: 4 additions & 0 deletions src/configuration/iconfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export interface IConfiguration {
*/
surround: boolean;

/**
* create default mappings for surround plugin
*/
enableDefaultPluginMappings: boolean;
/**
* Customize argument textobject delimiter and separator characters
*/
Expand Down
7 changes: 7 additions & 0 deletions test/plugins/surround.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ suite('surround plugin', () => {
end: ['first |( line ) test'],
});

newTest({
title: "'csw' as shortform for ysiw",
start: ['first li|ne test'],
keysPressed: 'csw(',
end: ['first |( line ) test'],
});

newTest({
title: "'ysw)' surrounds word without space",
start: ['first |line test'],
Expand Down
1 change: 1 addition & 0 deletions test/testConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Configuration implements IConfiguration {
sneakUseIgnorecaseAndSmartcase = false;
sneakReplacesF = false;
surround = false;
enableDefaultPluginMappings = true;
argumentObjectSeparators = [','];
argumentObjectOpeningDelimiters = ['(', '['];
argumentObjectClosingDelimiters = [')', ']'];
Expand Down