Properly Tokenize R language in VS Code Vim

50 Views Asked by At

In the VSCode Vim plugin, there are various normal mode commands that depend on the boundaries of the word/token under the cursor (e.g. *, w, e, etc). These don't work well with the R language which uses the dot (.) as an ordinary token character and not as an operator as most languages do.

This means that, for example, if you search (*) for a variable under the cursor like my.data, the vim plugin will either search for my or data, but not my.data as it should.

Curiously, with the R plugin installed (which has the correct syntax definitions), built in VSCode features like the Rename Symbol Command work as they should (e.g. hitting F2 when the cursor is over my.data will replace the whole variable and not just my or data).

Is there a way to get VSCode Vim to respect the R syntax via user configuration or would this require an update to the R or VSCode vim plugins?

1

There are 1 best solutions below

2
starball On

The VS Code Vim extension is supposed to honour its vim.iskeyword setting, which is really just a higher-priority alias of the editor.wordSeparators setting for the Vim extension. If you don't set vim.iskeyword, it will fall back to editor.wordSeparators, and if neither are set, it defaults to using /\\()"\':,.;<>~!@#$%^&*|+=[]{}`?-. The source code for this behaviour is in https://github.com/VSCodeVim/Vim/blob/master/src/configuration/configuration.ts. Interestingly, vim.iskeyword isn't registered as a language-overridable setting, but doing something like this should work:

"[r]": {
  "editor.wordSeparators": "", // <- put whatever you want in here that doesn't include ".".
},

I've tested that it works for selecting the inner word in visual mode (viw). Weird things happen with forward word search (*, but I think that's probably just a bug, in which case I'd suggest that you raise a bug ticket). If you want to trace the source code for this, you can go through the following: