Skip to content

Commit a94a559

Browse files
committed
Fix Vim section movements for standard Rust style.
(Expressed another way: make `[[` et al. work with the curly brace at the end of a line as is standard Rust style, not just at the start is it is by default in Vim, from K&R style.) This came out of #11492, where a simpler but less effective technique was initially proposed; some discussion of the techniques, ways and means can be found there. There are still a few caveats: - Operator-pending mode behaves differently to the standard behaviour: if inside curly braces, it should delete up to and including the closing of the outermost curly brace (that doesn't seem to me consistent with documented behaviour, but it's what it does). Actual behaviour (the more logical and consistent, in my opinion): up to the start of the next outermost curly brace. - With folding enabled (`set fdm=syntax`), `[[` and `]]` do not behave as they should: the default behaviour treats an entire closed fold as one line for these purposes while this code does not (I explicitly `set nofoldenable` in the function—the side-effects are worse with folds enabled), leading to unexpected behaviour, the worst of which is `[[` and/or `]]` not working in visual mode on a closed fold (visual mode keeps it at the extreme end of the region line of the folded region, so it's always going back to the opening line of that fold and immediately being shoved back to the end by visual mode). - `[[` and `]]` are operating inside comments, whereas the standard behaviour skips comments. - The viewport position is sometimes changed when it should not be necessary.
1 parent 66af850 commit a94a559

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

src/etc/vim/ftplugin/rust.vim

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" Vim syntax file
22
" Language: Rust
33
" Maintainer: Chris Morgan <[email protected]>
4-
" Last Change: 2013 Jul 10
4+
" Last Change: 2014 Feb 27
55

66
if exists("b:did_ftplugin")
77
finish
@@ -42,4 +42,55 @@ if exists("g:loaded_delimitMate")
4242
let b:delimitMate_excluded_regions = delimitMate#Get("excluded_regions") . ',rustLifetimeCandidate,rustGenericLifetimeCandidate'
4343
endif
4444

45-
let b:undo_ftplugin = "setlocal formatoptions< comments< commentstring< includeexpr< suffixesadd< | if exists('b:rust_original_delimitMate_excluded_regions') | let b:delimitMate_excluded_regions = b:rust_original_delimitMate_excluded_regions | unlet b:rust_original_delimitMate_excluded_regions | elseif exists('b:delimitMate_excluded_regions') | unlet b:delimitMate_excluded_regions | endif"
45+
" Bind motion commands to support hanging indents
46+
nnoremap <silent> <buffer> [[ :call <SID>Rust_Jump('n', 'Back')<CR>
47+
nnoremap <silent> <buffer> ]] :call <SID>Rust_Jump('n', 'Forward')<CR>
48+
xnoremap <silent> <buffer> [[ :call <SID>Rust_Jump('v', 'Back')<CR>
49+
xnoremap <silent> <buffer> ]] :call <SID>Rust_Jump('v', 'Forward')<CR>
50+
onoremap <silent> <buffer> [[ :call <SID>Rust_Jump('o', 'Back')<CR>
51+
onoremap <silent> <buffer> ]] :call <SID>Rust_Jump('o', 'Forward')<CR>
52+
53+
let b:undo_ftplugin = "
54+
\setlocal formatoptions< comments< commentstring< includeexpr< suffixesadd<
55+
\|if exists('b:rust_original_delimitMate_excluded_regions')
56+
\|let b:delimitMate_excluded_regions = b:rust_original_delimitMate_excluded_regions
57+
\|unlet b:rust_original_delimitMate_excluded_regions
58+
\|elseif exists('b:delimitMate_excluded_regions')
59+
\|unlet b:delimitMate_excluded_regions
60+
\|endif
61+
\|nunmap <buffer> [[
62+
\|nunmap <buffer> ]]
63+
\|xunmap <buffer> [[
64+
\|xunmap <buffer> ]]
65+
\|ounmap <buffer> [[
66+
\|ounmap <buffer> ]]
67+
\"
68+
69+
if exists('*<SID>Rust_Jump') | finish | endif
70+
71+
function! <SID>Rust_Jump(mode, function) range
72+
let cnt = v:count1
73+
normal! m'
74+
if a:mode ==# 'v'
75+
norm! gv
76+
endif
77+
let foldenable = &foldenable
78+
set nofoldenable
79+
while cnt > 0
80+
execute "call <SID>Rust_Jump_" . a:function . "()"
81+
let cnt = cnt - 1
82+
endwhile
83+
let &foldenable = foldenable
84+
endfunction
85+
86+
function! <SID>Rust_Jump_Back()
87+
call search('{', 'b')
88+
keepjumps normal! w99[{
89+
endfunction
90+
91+
function! <SID>Rust_Jump_Forward()
92+
normal! j0
93+
call search('{', 'b')
94+
keepjumps normal! w99[{%
95+
call search('{')
96+
endfunction

0 commit comments

Comments
 (0)