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.
This commit is contained in:
Chris Morgan 2014-02-27 16:54:54 +11:00 committed by Alex Crichton
parent 0a663a2d31
commit 879ac43689

View File

@ -1,7 +1,7 @@
" Vim syntax file
" Language: Rust
" Maintainer: Chris Morgan <me@chrismorgan.info>
" Last Change: 2013 Jul 10
" Last Change: 2014 Feb 27
if exists("b:did_ftplugin")
finish
@ -42,4 +42,55 @@ if exists("g:loaded_delimitMate")
let b:delimitMate_excluded_regions = delimitMate#Get("excluded_regions") . ',rustLifetimeCandidate,rustGenericLifetimeCandidate'
endif
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"
" Bind motion commands to support hanging indents
nnoremap <silent> <buffer> [[ :call <SID>Rust_Jump('n', 'Back')<CR>
nnoremap <silent> <buffer> ]] :call <SID>Rust_Jump('n', 'Forward')<CR>
xnoremap <silent> <buffer> [[ :call <SID>Rust_Jump('v', 'Back')<CR>
xnoremap <silent> <buffer> ]] :call <SID>Rust_Jump('v', 'Forward')<CR>
onoremap <silent> <buffer> [[ :call <SID>Rust_Jump('o', 'Back')<CR>
onoremap <silent> <buffer> ]] :call <SID>Rust_Jump('o', 'Forward')<CR>
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
\|nunmap <buffer> [[
\|nunmap <buffer> ]]
\|xunmap <buffer> [[
\|xunmap <buffer> ]]
\|ounmap <buffer> [[
\|ounmap <buffer> ]]
\"
if exists('*<SID>Rust_Jump') | finish | endif
function! <SID>Rust_Jump(mode, function) range
let cnt = v:count1
normal! m'
if a:mode ==# 'v'
norm! gv
endif
let foldenable = &foldenable
set nofoldenable
while cnt > 0
execute "call <SID>Rust_Jump_" . a:function . "()"
let cnt = cnt - 1
endwhile
let &foldenable = foldenable
endfunction
function! <SID>Rust_Jump_Back()
call search('{', 'b')
keepjumps normal! w99[{
endfunction
function! <SID>Rust_Jump_Forward()
normal! j0
call search('{', 'b')
keepjumps normal! w99[{%
call search('{')
endfunction