Skip to content

Commit

Permalink
Improve b:match_words pattern (#1288)
Browse files Browse the repository at this point in the history
This PR makes editing tabstops in snippets easier esp. with the plugin, [vim-matchup](https://github.com/andymass/vim-matchup), which is a replacement of classic [matchit](http://ftp.vim.org/pub/vim/runtime/macros/matchit.txt).

## Example

```snippets
${1:fo|o} # "|" is cursor position.
```

then, `ci%`,

```snippets
${1:|}
```

Or,

```snippets                                                             
`!p snip.rv = b|ar`
```                                                                     
                                                            
then, `ci%`,
                                                                        
```snippets                                                             
`!p |`                                                                  
```                                                                     

                                                                        
## Note
It seems undesirable to add `snip.rv` to `b:match_words` as a middle pattern of `!p`-interpolation because it often disturbs to `ci%` to edit an interpolation, or even to `da%` to delete/move a whole interpolation, i.e., no middle pattern is included in `b:match_words` on this PR.
  • Loading branch information
aileot authored Nov 9, 2020
1 parent e647887 commit b837416
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion ftplugin/snippets.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,28 @@ augroup END
" http://www.vim.org/scripts/script.php?script_id=39
if exists("loaded_matchit") && !exists("b:match_words")
let b:match_ignorecase = 0
let b:match_words = '^snippet\>:^endsnippet\>,^global\>:^endglobal\>,\${:}'
function! s:set_match_words() abort
let pairs = [
\ ['^snippet\>', '^endsnippet\>'],
\ ['^global\>', '^endglobal\>'],
\ ]

" Note: Keep the related patterns into a pattern
" Because tabstop-patterns such as ${1}, ${1:foo}, ${VISUAL}, ..., end with
" the same pattern, '}', matchit could fail to get corresponding '}' in
" nested patterns like ${1:${VISUAL:bar}} when the end-pattern is simply
" set to '}'.
call add(pairs, ['\${\%(\d\|VISUAL\)|\ze.*\\\@<!|}', '\\\@<!|}']) " ${1|baz,qux|}
call add(pairs, ['\${\%(\d\|VISUAL\)\/\ze.*\\\@<!\/[gima]*}', '\\\@<!\/[gima]*}']) " ${1/garply/waldo/g}
call add(pairs, ['\${\%(\%(\d\|VISUAL\)\:\ze\|\ze\%(\d\|VISUAL\)\).*\\\@<!}', '\\\@<!}']) " ${1:foo}, ${VISUAL:bar}, ... or ${1}, ${VISUAL}, ...
call add(pairs, ['\\\@<!`\%(![pv]\|#!\/\f\+\)\%( \|$\)', '\\\@<!`']) " `!p quux`, `!v corge`, `#!/usr/bin/bash grault`, ... (indicators includes a whitespace or end-of-line)

let pats = map(deepcopy(pairs), 'join(v:val, ":")')
let match_words = join(pats, ',')
return match_words
endfunction
let b:match_words = s:set_match_words()
delfunction s:set_match_words
let s:set_match_words = 1
endif

Expand Down

0 comments on commit b837416

Please sign in to comment.