neovim

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

rhelp.vim (3025B)


      1 " Vim indent file
      2 " Language:	R Documentation (Help), *.Rd
      3 " Maintainer: This runtime file is looking for a new maintainer.
      4 " Former Maintainer: Jakson Alves de Aquino <jalvesaq@gmail.com>
      5 " Former Repository: https://github.com/jalvesaq/R-Vim-runtime
      6 " Last Change:	2023 Feb 27  07:01PM
      7 "		2024 Feb 19 by Vim Project (announce adoption)
      8 
      9 
     10 " Only load this indent file when no other was loaded.
     11 if exists("b:did_indent")
     12  finish
     13 endif
     14 runtime indent/r.vim
     15 let s:RIndent = function(substitute(&indentexpr, "()", "", ""))
     16 let b:did_indent = 1
     17 
     18 setlocal noautoindent
     19 setlocal nocindent
     20 setlocal nosmartindent
     21 setlocal nolisp
     22 setlocal indentkeys=0{,0},:,!^F,o,O,e
     23 setlocal indentexpr=GetCorrectRHelpIndent()
     24 
     25 let b:undo_indent = "setl ai< cin< inde< indk< lisp< si<"
     26 
     27 " Only define the functions once.
     28 if exists("*GetRHelpIndent")
     29  finish
     30 endif
     31 
     32 function s:SanitizeRHelpLine(line)
     33  let newline = substitute(a:line, '\\\\', "x", "g")
     34  let newline = substitute(newline, '\\{', "x", "g")
     35  let newline = substitute(newline, '\\}', "x", "g")
     36  let newline = substitute(newline, '\\%', "x", "g")
     37  let newline = substitute(newline, '%.*', "", "")
     38  let newline = substitute(newline, '\s*$', "", "")
     39  return newline
     40 endfunction
     41 
     42 function GetRHelpIndent()
     43 
     44  let clnum = line(".")    " current line
     45  if clnum == 1
     46    return 0
     47  endif
     48  let cline = getline(clnum)
     49 
     50  if cline =~ '^\s*}\s*$'
     51    let i = clnum
     52    let bb = -1
     53    while bb != 0 && i > 1
     54      let i -= 1
     55      let line = s:SanitizeRHelpLine(getline(i))
     56      let line2 = substitute(line, "{", "", "g")
     57      let openb = strlen(line) - strlen(line2)
     58      let line3 = substitute(line2, "}", "", "g")
     59      let closeb = strlen(line2) - strlen(line3)
     60      let bb += openb - closeb
     61    endwhile
     62    return indent(i)
     63  endif
     64 
     65  if cline =~ '^\s*#ifdef\>' || cline =~ '^\s*#endif\>'
     66    return 0
     67  endif
     68 
     69  let lnum = clnum - 1
     70  let line = getline(lnum)
     71  if line =~ '^\s*#ifdef\>' || line =~ '^\s*#endif\>'
     72    let lnum -= 1
     73    let line = getline(lnum)
     74  endif
     75  while lnum > 1 && (line =~ '^\s*$' || line =~ '^#ifdef' || line =~ '^#endif')
     76    let lnum -= 1
     77    let line = getline(lnum)
     78  endwhile
     79  if lnum == 1
     80    return 0
     81  endif
     82  let line = s:SanitizeRHelpLine(line)
     83  let line2 = substitute(line, "{", "", "g")
     84  let openb = strlen(line) - strlen(line2)
     85  let line3 = substitute(line2, "}", "", "g")
     86  let closeb = strlen(line2) - strlen(line3)
     87  let bb = openb - closeb
     88 
     89  let ind = indent(lnum) + (bb * shiftwidth())
     90 
     91  if line =~ '^\s*}\s*$'
     92    let ind = indent(lnum)
     93  endif
     94 
     95  if ind < 0
     96    return 0
     97  endif
     98 
     99  return ind
    100 endfunction
    101 
    102 function GetCorrectRHelpIndent()
    103  let lastsection = search('^\\[a-z]*{', "bncW")
    104  let secname = getline(lastsection)
    105  if secname =~ '^\\usage{' || secname =~ '^\\examples{' || secname =~ '^\\dontshow{' || secname =~ '^\\dontrun{' || secname =~ '^\\donttest{' || secname =~ '^\\testonly{' || secname =~ '^\\method{.*}{.*}('
    106    return s:RIndent()
    107  else
    108    return GetRHelpIndent()
    109  endif
    110 endfunction
    111 
    112 " vim: sw=2