neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

rust.vim (10366B)


      1 " Vim indent file
      2 " Language:         Rust
      3 " Author:           Chris Morgan <me@chrismorgan.info>
      4 " Last Change:      2023-09-11
      5 " 2024 Jul 04 by Vim Project: use shiftwidth() instead of hard-coding shifted values #15138
      6 " 2025 Dec 29 by Vim Project: clean up
      7 " 2025 Dec 31 by Vim Project: correcly indent after nested array literal #19042
      8 " 2026 Jan 28 by Vim Project: fix indentation when a string literal contains 'if' #19265
      9 
     10 " For bugs, patches and license go to https://github.com/rust-lang/rust.vim
     11 " Note: upstream seems umaintained: https://github.com/rust-lang/rust.vim/issues/502
     12 
     13 " Only load this indent file when no other was loaded.
     14 if exists("b:did_indent")
     15    finish
     16 endif
     17 let b:did_indent = 1
     18 
     19 setlocal cindent
     20 setlocal cinoptions=L0,(s,Ws,J1,j1,m1
     21 setlocal cinkeys=0{,0},!^F,o,O,0[,0],0(,0)
     22 " Don't think cinwords will actually do anything at all... never mind
     23 setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern,macro
     24 
     25 " Some preliminary settings
     26 setlocal nolisp		" Make sure lisp indenting doesn't supersede us
     27 setlocal autoindent	" indentexpr isn't much help otherwise
     28 " Also do indentkeys, otherwise # gets shoved to column 0 :-/
     29 setlocal indentkeys=0{,0},!^F,o,O,0[,0],0(,0)
     30 
     31 setlocal indentexpr=GetRustIndent(v:lnum)
     32 
     33 let b:undo_indent = "setlocal cindent< cinoptions< cinkeys< cinwords< lisp< autoindent< indentkeys< indentexpr<"
     34 
     35 " Only define the function once.
     36 if exists("*GetRustIndent")
     37    finish
     38 endif
     39 
     40 " vint: -ProhibitAbbreviationOption
     41 let s:save_cpo = &cpo
     42 set cpo&vim
     43 " vint: +ProhibitAbbreviationOption
     44 
     45 " Come here when loading the script the first time.
     46 
     47 function! s:get_line_trimmed(lnum)
     48    " Get the line and remove a trailing comment.
     49    " Use syntax highlighting attributes when possible.
     50    " NOTE: this is not accurate; /* */ or a line continuation could trick it
     51    let line = getline(a:lnum)
     52    let line_len = strlen(line)
     53    if has('syntax_items')
     54        " If the last character in the line is a comment, do a binary search for
     55        " the start of the comment.  synID() is slow, a linear search would take
     56        " too long on a long line.
     57        if synIDattr(synID(a:lnum, line_len, 1), "name") =~? 'Comment\|Todo'
     58            let min = 1
     59            let max = line_len
     60            while min < max
     61                let col = (min + max) / 2
     62                if synIDattr(synID(a:lnum, col, 1), "name") =~? 'Comment\|Todo'
     63                    let max = col
     64                else
     65                    let min = col + 1
     66                endif
     67            endwhile
     68            let line = strpart(line, 0, min - 1)
     69        endif
     70        return substitute(line, "\s*$", "", "")
     71    else
     72        " Sorry, this is not complete, nor fully correct (e.g. string "//").
     73        " Such is life.
     74        return substitute(line, "\s*//.*$", "", "")
     75    endif
     76 endfunction
     77 
     78 function! s:is_string_comment(lnum, col)
     79    if has('syntax_items')
     80        for id in synstack(a:lnum, a:col)
     81            let synname = synIDattr(id, "name")
     82            if synname ==# "rustString" || synname =~# "^rustComment"
     83                return 1
     84            endif
     85        endfor
     86    else
     87        " without syntax, let's not even try
     88        return 0
     89    endif
     90 endfunction
     91 
     92 function GetRustIndent(lnum)
     93    " Starting assumption: cindent (called at the end) will do it right
     94    " normally. We just want to fix up a few cases.
     95 
     96    let line = getline(a:lnum)
     97 
     98    if has('syntax_items')
     99        let synname = synIDattr(synID(a:lnum, 1, 1), "name")
    100        if synname ==# "rustString"
    101            " If the start of the line is in a string, don't change the indent
    102            return -1
    103        elseif synname =~? '\(Comment\|Todo\)'
    104                    \ && line !~# '^\s*/\*'  " not /* opening line
    105            if synname =~? "CommentML" " multi-line
    106                if line !~# '^\s*\*' && getline(a:lnum - 1) =~# '^\s*/\*'
    107                    " This is (hopefully) the line after a /*, and it has no
    108                    " leader, so the correct indentation is that of the
    109                    " previous line.
    110                    return GetRustIndent(a:lnum - 1)
    111                endif
    112            endif
    113            " If it's in a comment, let cindent take care of it now. This is
    114            " for cases like "/*" where the next line should start " * ", not
    115            " "* " as the code below would otherwise cause for module scope
    116            " Fun fact: "  /*\n*\n*/" takes two calls to get right!
    117            return cindent(a:lnum)
    118        endif
    119    endif
    120 
    121    " cindent gets second and subsequent match patterns/struct members wrong,
    122    " as it treats the comma as indicating an unfinished statement::
    123    "
    124    " match a {
    125    "     b => c,
    126    "         d => e,
    127    "         f => g,
    128    " };
    129 
    130    " Search backwards for the previous non-empty line.
    131    let prevlinenum = prevnonblank(a:lnum - 1)
    132    let prevline = s:get_line_trimmed(prevlinenum)
    133    while prevlinenum > 1 && prevline !~# '[^[:blank:]]'
    134        let prevlinenum = prevnonblank(prevlinenum - 1)
    135        let prevline = s:get_line_trimmed(prevlinenum)
    136    endwhile
    137 
    138    " A standalone '{', '}', or 'where'
    139    let l:standalone_open = line =~# '\V\^\s\*{\s\*\$'
    140    let l:standalone_close = line =~# '\V\^\s\*}\s\*\$'
    141    let l:standalone_where = line =~# '\V\^\s\*where\s\*\$'
    142    if l:standalone_open || l:standalone_close || l:standalone_where
    143        let l:orig_line = line('.')
    144        let l:orig_col = col('.')
    145        let l:i = 0
    146        while 1
    147            " ToDo: we can search for more items than 'fn' and 'if'.
    148            let [l:found_line, l:col, l:submatch] =
    149                        \ searchpos('\<\(fn\|if\)\>', 'bWp')
    150            if l:found_line ==# 0 || !s:is_string_comment(l:found_line, l:col)
    151                break
    152            endif
    153            let l:i += 1
    154            " Limit to 10 iterations as a failsafe against endless looping.
    155            if l:i >= 10
    156                let l:found_line = 0
    157                break
    158            endif
    159        endwhile
    160        call cursor(l:orig_line, l:orig_col)
    161        if l:found_line !=# 0
    162            " Now we count the number of '{' and '}' in between the match
    163            " locations and the current line (there is probably a better
    164            " way to compute this).
    165            let l:i = l:found_line
    166            let l:search_line = strpart(getline(l:i), l:col - 1)
    167            let l:opens = 0
    168            let l:closes = 0
    169            while l:i < a:lnum
    170                let l:search_line2 = substitute(l:search_line, '\V{', '', 'g')
    171                let l:opens += strlen(l:search_line) - strlen(l:search_line2)
    172                let l:search_line3 = substitute(l:search_line2, '\V}', '', 'g')
    173                let l:closes += strlen(l:search_line2) - strlen(l:search_line3)
    174                let l:i += 1
    175                let l:search_line = getline(l:i)
    176            endwhile
    177            if l:standalone_open || l:standalone_where
    178                if l:opens ==# l:closes
    179                    return indent(l:found_line)
    180                endif
    181            else
    182                " Expect to find just one more close than an open
    183                if l:opens ==# l:closes + 1
    184                    return indent(l:found_line)
    185                endif
    186            endif
    187        endif
    188    endif
    189 
    190    " A standalone 'where' adds a shift.
    191    let l:standalone_prevline_where = prevline =~# '\V\^\s\*where\s\*\$'
    192    if l:standalone_prevline_where
    193        return indent(prevlinenum) + shiftwidth()
    194    endif
    195 
    196    " Handle where clauses nicely: subsequent values should line up nicely.
    197    if prevline[len(prevline) - 1] ==# ","
    198                \ && prevline =~# '^\s*where\s'
    199        return indent(prevlinenum) + 6
    200    endif
    201 
    202    let l:last_prevline_character = prevline[len(prevline) - 1]
    203 
    204    " A line that ends with '.<expr>;' is probably an end of a long list
    205    " of method operations.
    206    if prevline =~# '\V\^\s\*.' && l:last_prevline_character ==# ';'
    207        call cursor(a:lnum - 1, 1)
    208        let l:scope_start = searchpair('{\|(', '', '}\|)', 'nbW',
    209                    \ 's:is_string_comment(line("."), col("."))')
    210        if l:scope_start != 0 && l:scope_start < a:lnum
    211            return indent(l:scope_start) + shiftwidth()
    212        endif
    213    endif
    214 
    215    " Prevent cindent from becoming confused when pairing square brackets, as
    216    " in
    217    "
    218    " let arr = [[u8; 4]; 2] = [
    219    "     [0; 4],
    220    "     [1, 3, 5, 9],
    221    " ];
    222    "     | ← indentation placed here
    223    "
    224    " for which it calculates too much indentation in the line following the
    225    " close of the array.
    226    if prevline =~# '^\s*\]' && l:last_prevline_character ==# ';'
    227                \ && line !~# '^\s*}'
    228        return indent(prevlinenum)
    229    endif
    230 
    231    if l:last_prevline_character ==# ","
    232                \ && s:get_line_trimmed(a:lnum) !~# '^\s*[\[\]{})]'
    233                \ && prevline !~# '^\s*fn\s'
    234                \ && prevline !~# '([^()]\+,$'
    235                \ && s:get_line_trimmed(a:lnum) !~# '^\s*\S\+\s*=>'
    236        " Oh ho! The previous line ended in a comma! I bet cindent will try to
    237        " take this too far... For now, let's normally use the previous line's
    238        " indent.
    239 
    240        " One case where this doesn't work out is where *this* line contains
    241        " square or curly brackets; then we normally *do* want to be indenting
    242        " further.
    243        "
    244        " Another case where we don't want to is one like a function
    245        " definition with arguments spread over multiple lines:
    246        "
    247        " fn foo(baz: Baz,
    248        "        baz: Baz) // <-- cindent gets this right by itself
    249        "
    250        " Another case is similar to the previous, except calling a function
    251        " instead of defining it, or any conditional expression that leaves
    252        " an open paren:
    253        "
    254        " foo(baz,
    255        "     baz);
    256        "
    257        " if baz && (foo ||
    258        "            bar) {
    259        "
    260        " Another case is when the current line is a new match arm.
    261        "
    262        " There are probably other cases where we don't want to do this as
    263        " well. Add them as needed.
    264        return indent(prevlinenum)
    265    endif
    266 
    267    " Fall back on cindent, which does it mostly right
    268    return cindent(a:lnum)
    269 endfunction
    270 
    271 " vint: -ProhibitAbbreviationOption
    272 let &cpo = s:save_cpo
    273 unlet s:save_cpo
    274 " vint: +ProhibitAbbreviationOption
    275 
    276 " vim: set et sw=4 sts=4 ts=8: