neovim

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

r.vim (15106B)


      1 " Vim syntax file
      2 " Language:	      R (GNU S)
      3 " Maintainer:	      This runtime file is looking for a new maintainer.
      4 " Former Maintainers: Jakson Aquino <jalvesaq@gmail.com>
      5 "                     Vaidotas Zemlys <zemlys@gmail.com>
      6 "                     Tom Payne <tom@tompayne.org>
      7 " Contributor:        Johannes Ranke <jranke@uni-bremen.de>
      8 " Former Repository:  https://github.com/jalvesaq/R-Vim-runtime
      9 " Filenames:          *.R *.r *.Rhistory *.Rt
     10 " Last Change:        2023 Dec 24  08:05AM
     11 "   2024 Feb 19 by Vim Project (announce adoption)
     12 "
     13 " NOTE: The highlighting of R functions might be defined in
     14 " runtime files created by a filetype plugin, if installed.
     15 "
     16 " CONFIGURATION:
     17 "   Syntax folding can be turned on by
     18 "
     19 "      let r_syntax_folding = 1
     20 "
     21 "   ROxygen highlighting can be turned off by
     22 "
     23 "      let r_syntax_hl_roxygen = 0
     24 "
     25 " Some lines of code were borrowed from Zhuojun Chen.
     26 
     27 if exists("b:current_syntax")
     28  finish
     29 endif
     30 
     31 syn iskeyword @,48-57,_,.
     32 
     33 " The variables g:r_hl_roxygen and g:r_syn_minlines were renamed on April 8, 2017.
     34 if exists("g:r_hl_roxygen")
     35  let g:r_syntax_hl_roxygen = g:r_hl_roxygen
     36 endif
     37 if exists("g:r_syn_minlines")
     38  let g:r_syntax_minlines = g:r_syn_minlines
     39 endif
     40 
     41 if exists("g:r_syntax_folding") && g:r_syntax_folding
     42  setlocal foldmethod=syntax
     43 endif
     44 
     45 let g:r_syntax_hl_roxygen = get(g:, 'r_syntax_hl_roxygen', 1)
     46 
     47 syn case match
     48 
     49 " Comment
     50 syn match rCommentTodo contained "\(BUG\|FIXME\|NOTE\|TODO\):"
     51 syn match rTodoParen contained "\(BUG\|FIXME\|NOTE\|TODO\)\s*(.\{-})\s*:" contains=rTodoKeyw,rTodoInfo transparent
     52 syn keyword rTodoKeyw BUG FIXME NOTE TODO contained
     53 syn match rTodoInfo "(\zs.\{-}\ze)" contained
     54 syn match rComment contains=@Spell,rCommentTodo,rTodoParen "#.*"
     55 
     56 " Roxygen
     57 if g:r_syntax_hl_roxygen
     58  " A roxygen block can start at the beginning of a file (first version) and
     59  " after a blank line (second version). It ends when a line appears that does not
     60  " contain a roxygen comment. In the following comments, any line containing
     61  " a roxygen comment marker (one or two hash signs # followed by a single
     62  " quote ' and preceded only by whitespace) is called a roxygen line. A
     63  " roxygen line containing only a roxygen comment marker, optionally followed
     64  " by whitespace is called an empty roxygen line.
     65 
     66  syn match rOCommentKey "^\s*#\{1,2}'" contained
     67  syn region rOExamples start="^\s*#\{1,2}' @examples.*"rs=e+1,hs=e+1 end="^\(#\{1,2}' @.*\)\@=" end="^\(#\{1,2}'\)\@!" contained contains=rOTag fold
     68  
     69  " R6 classes may contain roxygen lines independent of roxygen blocks
     70  syn region rOR6Class start=/R6Class(/ end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError fold
     71  syn match rOR6Block "#\{1,2}'.*" contains=rOTag,rOExamples,@Spell containedin=rOR6Class contained
     72  syn match rOR6Block "^\s*#\{1,2}'.*" contains=rOTag,rOExamples,@Spell containedin=rOR6Class contained
     73 
     74  " First we match all roxygen blocks as containing only a title. In case an
     75  " empty roxygen line ending the title or a tag is found, this will be
     76  " overridden later by the definitions of rOBlock.
     77  syn match rOTitleBlock "\(\%^\|^\s*\n\)\@<=\(\s*#\{1,2}' .*\n\)\{1,}" contains=rOCommentKey,rOTitleTag
     78 
     79  " A title as part of a block is always at the beginning of the block, i.e.
     80  " either at the start of a file or after a completely empty line.
     81  syn match rOTitle "\(\%^\|^\s*\n\)\@<=\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*$" contained contains=rOCommentKey,rOTitleTag
     82  syn match rOTitleTag contained "@title"
     83 
     84  " When a roxygen block has a title and additional content, the title
     85  " consists of one or more roxygen lines (as little as possible are matched),
     86  " followed either by an empty roxygen line
     87  syn region rOBlock start="\(\%^\|^\s*\n\)\@<=\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*$" end="^\s*\(#\{1,2}'\)\@!" contains=rOTitle,rOTag,rOExamples,@Spell keepend fold
     88 
     89  " or by a roxygen tag (we match everything starting with @ but not @@ which is used as escape sequence for a literal @).
     90  syn region rOBlock start="\(\%^\|^\s*\n\)\@<=\(\s*#\{1,2}' .*\n\)\{-}\s*#\{1,2}' @\(@\)\@!" end="^\s*\(#\{1,2}'\)\@!" contains=rOTitle,rOTag,rOExamples,@Spell keepend fold
     91 
     92  " If a block contains an @rdname, @describeIn tag, it may have paragraph breaks, but does not have a title
     93  syn region rOBlockNoTitle start="\(\%^\|^\s*\n\)\@<=\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*\n\(\s*#\{1,2}'.*\n\)\{-}\s*#\{1,2}' @rdname" end="^\s*\(#\{1,2}'\)\@!" contains=rOTag,rOExamples,@Spell keepend fold
     94  syn region rOBlockNoTitle start="\(\%^\|^\s*\n\)\@<=\(\s*#\{1,2}' .*\n\)\{-1,}\s*#\{1,2}'\s*\n\(\s*#\{1,2}'.*\n\)\{-}\s*#\{1,2}' @describeIn" end="^\s*\(#\{1,2}'\)\@!" contains=rOTag,rOExamples,@Spell keepend fold
     95 
     96  " rOTag list originally generated from the lists that were available in
     97  " https://github.com/klutometis/roxygen/R/rd.R and
     98  " https://github.com/klutometis/roxygen/R/namespace.R
     99  " using s/^    \([A-Za-z0-9]*\) = .*/  syn match rOTag contained "@\1"/
    100  " Plus we need the @include tag
    101 
    102  " rd.R
    103  syn match rOTag contained "@aliases"
    104  syn match rOTag contained "@author"
    105  syn match rOTag contained "@backref"
    106  syn match rOTag contained "@concept"
    107  syn match rOTag contained "@describeIn"
    108  syn match rOTag contained "@description"
    109  syn match rOTag contained "@details"
    110  syn match rOTag contained "@docType"
    111  syn match rOTag contained "@encoding"
    112  syn match rOTag contained "@evalRd"
    113  syn match rOTag contained "@example"
    114  syn match rOTag contained "@examples"
    115  syn match rOTag contained "@family"
    116  syn match rOTag contained "@field"
    117  syn match rOTag contained "@format"
    118  syn match rOTag contained "@inherit"
    119  syn match rOTag contained "@inheritParams"
    120  syn match rOTag contained "@inheritDotParams"
    121  syn match rOTag contained "@inheritSection"
    122  syn match rOTag contained "@keywords"
    123  syn match rOTag contained "@method"
    124  syn match rOTag contained "@name"
    125  syn match rOTag contained "@md"
    126  syn match rOTag contained "@noMd"
    127  syn match rOTag contained "@noRd"
    128  syn match rOTag contained "@note"
    129  syn match rOTag contained "@param"
    130  syn match rOTag contained "@rdname"
    131  syn match rOTag contained "@rawRd"
    132  syn match rOTag contained "@references"
    133  syn match rOTag contained "@return"
    134  syn match rOTag contained "@section"
    135  syn match rOTag contained "@seealso"
    136  syn match rOTag contained "@slot"
    137  syn match rOTag contained "@source"
    138  syn match rOTag contained "@template"
    139  syn match rOTag contained "@templateVar"
    140  syn match rOTag contained "@title"
    141  syn match rOTag contained "@usage"
    142  " namespace.R
    143  syn match rOTag contained "@export"
    144  syn match rOTag contained "@exportClass"
    145  syn match rOTag contained "@exportMethod"
    146  syn match rOTag contained "@exportPattern"
    147  syn match rOTag contained "@import"
    148  syn match rOTag contained "@importClassesFrom"
    149  syn match rOTag contained "@importFrom"
    150  syn match rOTag contained "@importMethodsFrom"
    151  syn match rOTag contained "@rawNamespace"
    152  syn match rOTag contained "@S3method"
    153  syn match rOTag contained "@useDynLib"
    154  " other
    155  syn match rOTag contained "@eval"
    156  syn match rOTag contained "@include"
    157  syn match rOTag contained "@includeRmd"
    158  syn match rOTag contained "@order"
    159 endif
    160 
    161 
    162 if &filetype == "rhelp"
    163  " string enclosed in double quotes
    164  syn region rString contains=rSpecial,@Spell start=/"/ skip=/\\\\\|\\"/ end=/"/
    165  " string enclosed in single quotes
    166  syn region rString contains=rSpecial,@Spell start=/'/ skip=/\\\\\|\\'/ end=/'/
    167 else
    168  " string enclosed in double quotes
    169  syn region rString contains=rSpecial,rStrError,@Spell start=/"/ skip=/\\\\\|\\"/ end=/"/
    170  " string enclosed in single quotes
    171  syn region rString contains=rSpecial,rStrError,@Spell start=/'/ skip=/\\\\\|\\'/ end=/'/
    172 endif
    173 
    174 syn match rStrError display contained "\\."
    175 
    176 
    177 " New line, carriage return, tab, backspace, bell, feed, vertical tab, backslash
    178 syn match rSpecial display contained "\\\(n\|r\|t\|b\|a\|f\|v\|'\|\"\)\|\\\\"
    179 
    180 " Hexadecimal and Octal digits
    181 syn match rSpecial display contained "\\\(x\x\{1,2}\|[0-8]\{1,3}\)"
    182 
    183 " Unicode characters
    184 syn match rSpecial display contained "\\u\x\{1,4}"
    185 syn match rSpecial display contained "\\U\x\{1,8}"
    186 syn match rSpecial display contained "\\u{\x\{1,4}}"
    187 syn match rSpecial display contained "\\U{\x\{1,8}}"
    188 
    189 " Raw string
    190 syn region rRawString matchgroup=rRawStrDelim start=/[rR]\z(['"]\)\z(-*\)(/ end=/)\z2\z1/ keepend
    191 syn region rRawString matchgroup=rRawStrDelim start=/[rR]\z(['"]\)\z(-*\){/ end=/}\z2\z1/ keepend
    192 syn region rRawString matchgroup=rRawStrDelim start=/[rR]\z(['"]\)\z(-*\)\[/ end=/\]\z2\z1/ keepend
    193 
    194 " Statement
    195 syn keyword rStatement   break next return
    196 syn keyword rConditional if else
    197 syn keyword rRepeat      for in repeat while
    198 
    199 " Constant (not really)
    200 syn keyword rConstant T F LETTERS letters month.abb month.name pi
    201 syn keyword rConstant R.version.string
    202 
    203 syn keyword rNumber   NA_integer_ NA_real_ NA_complex_ NA_character_
    204 
    205 " Constants
    206 syn keyword rConstant NULL
    207 syn keyword rBoolean  FALSE TRUE
    208 syn keyword rNumber   NA Inf NaN
    209 
    210 " integer
    211 syn match rInteger "\<\d\+L"
    212 syn match rInteger "\<0x\([0-9]\|[a-f]\|[A-F]\)\+L"
    213 syn match rInteger "\<\d\+[Ee]+\=\d\+L"
    214 
    215 " number with no fractional part or exponent
    216 syn match rNumber "\<\d\+\>"
    217 " hexadecimal number
    218 syn match rNumber "\<0x\([0-9]\|[a-f]\|[A-F]\)\+"
    219 
    220 " floating point number with integer and fractional parts and optional exponent
    221 syn match rFloat "\<\d\+\.\d*\([Ee][-+]\=\d\+\)\="
    222 " floating point number with no integer part and optional exponent
    223 syn match rFloat "\<\.\d\+\([Ee][-+]\=\d\+\)\="
    224 " floating point number with no fractional part and optional exponent
    225 syn match rFloat "\<\d\+[Ee][-+]\=\d\+"
    226 
    227 " complex number
    228 syn match rComplex "\<\d\+i"
    229 syn match rComplex "\<\d\++\d\+i"
    230 syn match rComplex "\<0x\([0-9]\|[a-f]\|[A-F]\)\+i"
    231 syn match rComplex "\<\d\+\.\d*\([Ee][-+]\=\d\+\)\=i"
    232 syn match rComplex "\<\.\d\+\([Ee][-+]\=\d\+\)\=i"
    233 syn match rComplex "\<\d\+[Ee][-+]\=\d\+i"
    234 
    235 syn match rAssign    '='
    236 syn match rOperator    "&"
    237 syn match rOperator    '-'
    238 syn match rOperator    '\*'
    239 syn match rOperator    '+'
    240 if &filetype == "quarto" || &filetype == "rmd" || &filetype == "rrst"
    241  syn match rOperator    "[|!<>^~`/:]"
    242 else
    243  syn match rOperator    "[|!<>^~/:]"
    244 endif
    245 syn match rOperator    "%\{2}\|%\S\{-}%"
    246 syn match rOperator '\([!><]\)\@<=='
    247 syn match rOperator '=='
    248 syn match rOperator '|>'
    249 syn match rOpError  '\*\{3}'
    250 syn match rOpError  '//'
    251 syn match rOpError  '&&&'
    252 syn match rOpError  '|||'
    253 syn match rOpError  '<<'
    254 syn match rOpError  '>>'
    255 
    256 syn match rAssign "<\{1,2}-"
    257 syn match rAssign "->\{1,2}"
    258 
    259 " Special
    260 syn match rDelimiter "[,;:]"
    261 
    262 " Error
    263 if exists("g:r_syntax_folding")
    264  syn region rRegion matchgroup=Delimiter start=/(/ matchgroup=Delimiter end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError fold
    265  syn region rRegion matchgroup=Delimiter start=/{/ matchgroup=Delimiter end=/}/ transparent contains=ALLBUT,rError,rBraceError,rParenError fold
    266  syn region rRegion matchgroup=Delimiter start=/\[/ matchgroup=Delimiter end=/]/ transparent contains=ALLBUT,rError,rCurlyError,rParenError fold
    267  syn region rSection matchgroup=Title start=/^#.*[-=#]\{4,}/ end=/^#.*[-=#]\{4,}/ms=s-2,me=s-1 transparent contains=ALL fold
    268 else
    269  syn region rRegion matchgroup=Delimiter start=/(/ matchgroup=Delimiter end=/)/ transparent contains=ALLBUT,rError,rBraceError,rCurlyError
    270  syn region rRegion matchgroup=Delimiter start=/{/ matchgroup=Delimiter end=/}/ transparent contains=ALLBUT,rError,rBraceError,rParenError
    271  syn region rRegion matchgroup=Delimiter start=/\[/ matchgroup=Delimiter end=/]/ transparent contains=ALLBUT,rError,rCurlyError,rParenError
    272 endif
    273 
    274 syn match rError      "[)\]}]"
    275 syn match rBraceError "[)}]" contained
    276 syn match rCurlyError "[)\]]" contained
    277 syn match rParenError "[\]}]" contained
    278 
    279 " Use Nvim-R to highlight functions dynamically if it is installed
    280 if !exists("g:r_syntax_fun_pattern")
    281  let s:ff = split(substitute(globpath(&rtp, "R/functions.vim"), "functions.vim", "", "g"), "\n")
    282  if len(s:ff) > 0
    283    let g:r_syntax_fun_pattern = 0
    284  else
    285    let g:r_syntax_fun_pattern = 1
    286  endif
    287 endif
    288 
    289 " Only use Nvim-R to highlight functions if they should not be highlighted
    290 " according to a generic pattern
    291 if g:r_syntax_fun_pattern == 1
    292  syn match rFunction '[0-9a-zA-Z_\.]\+\s*\ze('
    293 else
    294  " Nvim-R:
    295  runtime R/functions.vim
    296 endif
    297 
    298 syn match rDollar display contained "\$"
    299 syn match rDollar display contained "@"
    300 
    301 " List elements will not be highlighted as functions:
    302 syn match rLstElmt "\$[a-zA-Z0-9\\._]*" contains=rDollar
    303 syn match rLstElmt "@[a-zA-Z0-9\\._]*" contains=rDollar
    304 
    305 " Functions that may add new objects
    306 syn keyword rPreProc     library require attach detach source
    307 
    308 if &filetype == "rhelp"
    309  syn match rHelpIdent '\\method'
    310  syn match rHelpIdent '\\S4method'
    311 endif
    312 
    313 " Type
    314 syn match rType "\\"
    315 syn keyword rType array category character complex double function integer list logical matrix numeric vector data.frame
    316 
    317 " Name of object with spaces
    318 if &filetype == "rmd" || &filetype == "rrst" || &filetype == "quarto"
    319  syn region rNameWSpace start="`" end="`" contains=rSpaceFun containedin=rmdrChunk
    320 else
    321  syn region rNameWSpace start="`" end="`" contains=rSpaceFun
    322 endif
    323 
    324 if &filetype == "rhelp"
    325  syn match rhPreProc "^#ifdef.*"
    326  syn match rhPreProc "^#endif.*"
    327  syn match rhSection "\\dontrun\>"
    328 endif
    329 
    330 if exists("r_syntax_minlines")
    331  exe "syn sync minlines=" . r_syntax_minlines
    332 else
    333  syn sync minlines=40
    334 endif
    335 
    336 " Define the default highlighting.
    337 hi def link rAssign      Statement
    338 hi def link rBoolean     Boolean
    339 hi def link rBraceError  Error
    340 hi def link rComment     Comment
    341 hi def link rTodoParen   Comment
    342 hi def link rTodoInfo    SpecialComment
    343 hi def link rCommentTodo Todo
    344 hi def link rTodoKeyw    Todo
    345 hi def link rComplex     Number
    346 hi def link rConditional Conditional
    347 hi def link rConstant    Constant
    348 hi def link rCurlyError  Error
    349 hi def link rDelimiter   Delimiter
    350 hi def link rDollar      SpecialChar
    351 hi def link rError       Error
    352 hi def link rFloat       Float
    353 hi def link rFunction    Function
    354 hi def link rSpaceFun    Function
    355 hi def link rHelpIdent   Identifier
    356 hi def link rhPreProc    PreProc
    357 hi def link rhSection    PreCondit
    358 hi def link rInteger     Number
    359 hi def link rLstElmt     Normal
    360 hi def link rNameWSpace  Normal
    361 hi def link rNumber      Number
    362 hi def link rOperator    Operator
    363 hi def link rOpError     Error
    364 hi def link rParenError  Error
    365 hi def link rPreProc     PreProc
    366 hi def link rRawString   String
    367 hi def link rRawStrDelim Delimiter
    368 hi def link rRepeat      Repeat
    369 hi def link rSpecial     SpecialChar
    370 hi def link rStatement   Statement
    371 hi def link rString      String
    372 hi def link rStrError    Error
    373 hi def link rType        Type
    374 if g:r_syntax_hl_roxygen
    375  hi def link rOTitleTag   Operator
    376  hi def link rOTag        Operator
    377  hi def link rOTitleBlock Title
    378  hi def link rOBlock         Comment
    379  hi def link rOBlockNoTitle  Comment
    380  hi def link rOR6Block         Comment
    381  hi def link rOTitle      Title
    382  hi def link rOCommentKey Comment
    383  hi def link rOExamples   SpecialComment
    384 endif
    385 
    386 let b:current_syntax="r"
    387 
    388 " vim: ts=8 sw=2