这是根据Henrico的回答修改的,可以通过复制并粘贴以下代码到自己的~/.ctags文件中实现。 Henrico的代码无法处理缩进的函数,但以下代码可以。
--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/
这允许变量被识别为ctags以及函数。如果您正在使用taglist vim插件,则可以区分全局变量和其他变量。此外,如果您正在使用taglist,则需要将以下内容粘贴到vimrc中。
let tlist_r_settings = 'R;f:Functions;g:GlobalVariables;v:FunctionVariables'
这是我在家目录中的.ctags文件内容。我在互联网上找到了它。使用它,您可以为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/
--langdef=Splus
更改为 --langdef=R
。 - Paul Hiemstra就我目前所见,rtags()是生成R代码标签的最佳方式,因为它将考虑所有不同的赋值方式,例如:
ok = c("<-", "=", "<<-", "assign",
"setGeneric", "setGroupGeneric", "setMethod",
"setClass", "setClassUnion")
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)
rtags
输出与[tag:vim]集成? - Konrad也许你可以阅读如何为ctags添加对新语言的支持。
实际上,我认为 rtags()
在 vim 中可以使用。
我使用 nvim-r 插件(可在 https://www.vim.org/scripts/script.php?script_id=2628 获取),并在我的 ~/.vimrc
文件中添加了以下内容:
:command! Rtags :!Rscript -e 'rtags(path="./", recursive=TRUE, ofile="TAGS")'
:Rtags
来重建标签,然后当鼠标悬停在一个单词上时,使用g]
可以找到该单词的定义。如其他人所提到的,rtags()
+etags2ctags()
可以为vim生成一个tagsfile
(请参见:h tags-and-searches
)。您可以为vim创建一个自定义命令,以便使用Rscript
在R中运行rtags()
。要这样做,请将以下内容放入您的.vimrc
文件中:
:command! Rtags :!Rscript -e 'rtags(path="./", recursive=TRUE, ofile="RTAGS")' -e 'etags2ctags("RTAGS", "rtags")' -e 'unlink("RTAGS")'
set tags+=rtags
:Rtags
时,vim将运行外部进程Rscript
(它必须在PATH
中),并评估rtags(path="./", recursive=TRUE, ofile="RTAGS");etags2ctags("RTAGS", "rtags");unlink("RTAGS")
。 rtags()
会生成一个Emacs tags format格式的RTAGS
文件,然后etags2ctags()
将其转换为vim可以读取的rtags
文件。unlink()
删除了不需要的RTAGS
文件。
set tags+=rtags
使vim除了通常的tags
文件外还会搜索rtags
文件(参见:h tags-and-searches
)
Universal-ctags 有一个 R 解析器。
"$ 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