Exuberant Ctags with R

前端 未结 6 824
北荒
北荒 2021-02-05 07:54

Is there any documented use of ctags with R? Would this be useful? Would it be difficult to implement?

Specifically, I\'ve just started using Vim. It would be cool to be

相关标签:
6条回答
  • 2021-02-05 08:13

    Probably you can read how to add support for a new language to ctags.

    0 讨论(0)
  • 2021-02-05 08:17

    Universal-ctags has an R parser.

    $ cat input.r
    add <- function (x, y) {
        3 -> z
        return(x + y + z)
    }
    add (3, 4)
    $ ./ctags --quiet --options=NONE -o - --kinds-R='*' --fields=+S input.r
    add input.r /^add <- function (x, y) {$/;"  f   signature:(x, y)
    x   input.r /^add <- function (x, y) {$/;"  z   function:add
    y   input.r /^add <- function (x, y) {$/;"  z   function:add
    z   input.r /^    3 -> z$/;"    v   function:add
    

    In addition, the ctags implementation supports R6Class and S4Class.

    0 讨论(0)
  • 2021-02-05 08:22

    This is a modification of Henrico's answer, and may be implemented by copying and pasting the following code into one's ~/.ctags files. Henrico's code did not work for indented functions, but the following code does.

    --langdef=R
    --langmap=r:.R.r
    --regex-R=/^[ \t]*"?([.A-Za-z][.A-Za-z0-9_]*)"?[ \t]*<-[ \t]function/\1/f,Functions/
    --regex-R=/^"?([.A-Za-z][.A-Za-z0-9_]*)"?[ \t]*<-[ \t][^f][^u][^n][^c][^t][^i][^o][^n]/\1/g,GlobalVars/ 
    --regex-R=/[ \t]"?([.A-Za-z][.A-Za-z0-9_]*)"?[ \t]*<-[ \t][^f][^u][^n][^c][^t][^i][^o][^n]/\1/v,FunctionVariables/
    

    This allows variables to be recognized with ctags as well as functions. If you're using the taglist vim addon, then, it allows you to distinguish between global variables and other variables. Also, if you're using taglist, then you will need to paste the following in your vimrc.

    let tlist_r_settings = 'R;f:Functions;g:GlobalVariables;v:FunctionVariables'
    
    0 讨论(0)
  • 2021-02-05 08:22

    As mentioned by other rtags() + etags2ctags() can generate a tagsfile for vim (see :h tags-and-searches). You can create a custom command for vim so that it run rtags() in R using Rscript. To do so put this in your .vimrc:

    :command! Rtags :!Rscript -e 'rtags(path="./", recursive=TRUE, ofile="RTAGS")' -e 'etags2ctags("RTAGS", "rtags")' -e 'unlink("RTAGS")'
    set tags+=rtags
    

    Now when you do :Rtags vim will run the external process Rscript (it has to be in the PATH of course) and evaluate rtags(path="./", recursive=TRUE, ofile="RTAGS");etags2ctags("RTAGS", "rtags");unlink("RTAGS"). rtags() will generate an RTAGS file in Emacs tags format then etags2ctags() will transform that into an rtags file which vim can read. unlink() deletes the RTAGS file since it's not needed for vim.

    The set tags+=rtags makes vim to search for an rtags file in addition to the usual tags file (See :h tags-and-searches)

    0 讨论(0)
  • 2021-02-05 08:25

    rtags() is the best to way to generate tags for R codes from what I see so far, since it will take all different ways of assignments in consideration, such as:

    ok = c("<-", "=", "<<-", "assign",
           "setGeneric", "setGroupGeneric", "setMethod",
           "setClass", "setClassUnion")
    

    An example of using rtags():

    path <- "~/path/to/project"
    rtags(path=path,
          pattern = "[.]*\\.[RrSs]$",
          keep.re = ".", # the value ('/R/') in the help page will only run through the codes in R/ folder.
          verbose = TRUE,
          ofile = file.path(path, 'TAGS'),
          append = FALSE,
          recursive = TRUE)
    
    0 讨论(0)
  • 2021-02-05 08:27

    This is the content of my .ctags file in my home directory. I found it somewhere on the internet. Using this you can generate a tags-file for vim.

     --langdef=Splus
     --langmap=Splus:.s.S.R.r.q
     --regex-Splus=/^[ \t]+"?([.A-Za-z][.A-Za-z0-9_]*)"?[\t]*<-[\t]*function/\1/
     --regex-Splus=/^"?([.A-Za-z][.A-Za-z0-9_]*)"?[ \t]*<-/\1/
    
    0 讨论(0)
提交回复
热议问题