如何让vim的taglist插件显示javascript的有用信息? [英] How can I make vim's taglist plugin show useful information for javascript?

查看:105
本文介绍了如何让vim的taglist插件显示javascript的有用信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近放弃了鼠标驱动的,特定于平台的GUI编辑器,并完全致力于vim。迄今为止的体验非常棒,但是当涉及到Javascript时我就陷入困境。

I've recently abandoned mouse-driven, platform-specific GUI editors and committed entirely to vim. The experience so far has been fantastic, but I'm stuck when it comes to Javascript.

这个广受欢迎的taglist实用程序(使用Exuberant Ctags)一直都很棒但是Javascript。由于语言的形式和结构过于自由,taglist只能在我打开它时才能获得一些功能 - 只有以下格式定义:

The ever-popular taglist utility (using Exuberant Ctags) has been great for everything but Javascript. With the language's overly-free form and structure, taglist could only pick up a handful of functions when I opened it up -- only those defined in the format:

function FUNCNAME (arg1, arg2) {

但没有变量或函数对象定义如下:

but no variables or function objects defined like:

var myFunc = function (arg1, arg2) {

所以我搜索了一下,发现以下为ctags设定的定义,我把它放在〜/ .ctags文件中:

So I googled a bit and found the following definition set for ctags, which I put in my ~/.ctags file:

--langdef=js
--langmap=js:.js
--regex-js=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\{/\1/,object/
--regex-js=/([A-Za-z0-9._$()]+)[ \t]*[:=][ \t]*function[ \t]*\(/\1/,function/
--regex-js=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*([^])])/\1/,function/
--regex-js=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\[/\1/,array/
--regex-js=/([^= ]+)[ \t]*=[ \t]*[^""]'[^'']*/\1/,string/
--regex-js=/([^= ]+)[ \t]*=[ \t]*[^'']"[^""]*/\1/,string/

之后,从命令行运行ctags是太棒了。它找到了我需要它找到的每个函数和对象。

After that, running ctags from the command line was fantastic. It found every function and object that I needed it to find.

问题是taglist.vim插件没有看到那些新结果。当我在vim中打开我的javascript文件并点击:TlistToggle时,我得到了与之前相同的少量功能。我点击'u'更新列表,没有效果。

The problem is that the taglist.vim plugin isn't seeing those new results. When I open my javascript file in vim and hit :TlistToggle, I get the exact same meager handful of functions I got before. I hit 'u' to update the list, with no effect.

挖掘到taglist.vim,我发现:

Digging into taglist.vim, I found this:

" java language
let s:tlist_def_java_settings = 'java;p:package;c:class;i:interface;' .
                               \ 'f:field;m:method'

" javascript language
let s:tlist_def_javascript_settings = 'javascript;f:function'

...这意味着我们只是从ctags实用程序中查看javascript的一种特定输出。不幸的是,我对taglist或vim一般都不够精明(尚未)发现我可以做出哪些改变以使所有那些精彩的ctags命令行结果显示在vim中。

...which implies we're only looking at one specific kind of output from the ctags utility for javascript. Unfortunately, I'm not savvy enough with taglist or vim in general (yet) to discover what change I can make to get all those wonderful ctags command-line results to show up in vim.

帮助赞赏!

推荐答案

知道了!我潜入taglist.vim代码一段时间了,这就是我发现的:

Got it! I dove into the taglist.vim code for awhile, and this is what I found:

taglist.vim强制ctags使用与vim正在使用的文件类型相同的文件类型。因此,即使我通过谷歌找到的〜/ .ctags片段将我急需的定义分配给新的js语言并将其应用于以.js结尾的文件,taglist也强制ctags使用JavaScript文件类型vim正在使用 - 它已经内置到ctags中。

taglist.vim forces ctags to use the same filetype that vim is using. So even though the ~/.ctags snippet I found via google is assigning my much-needed definitions to the new "js" language and applying it to files that end in .js, taglist is forcing ctags into using the "JavaScript" filetype that vim is using -- which is built right into ctags already.

解决方案是将〜/ .ctags文件从我上面发布的内容更改为:

The solution is to change the ~/.ctags file from what I've posted above to this:

--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*new[ \t]+Object\(/\1/o,object/
--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\{/\1/o,object/
--regex-JavaScript=/([A-Za-z0-9._$()]+)[ \t]*[:=][ \t]*function[ \t]*\(/\1/f,function/
--regex-JavaScript=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*\([^\]\)]*\)/\1/f,function/
--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*new[ \t]+Array\(/\1/a,array/
--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\[/\1/a,array/
--regex-JavaScript=/([^= ]+)[ \t]*=[ \t]*[^""]'[^'']*/\1/s,string/
--regex-JavaScript=/([^= ]+)[ \t]*=[ \t]*[^'']"[^""]*/\1/s,string/

直接改变预先存在的JavaScript语言定义,而不是在ctags中创建新的语言定义。现在,当taglib强制使用vim的已注册文件类型时,将使用新定义。之前发布的〜/ .ctags行中也缺少了Al在他的回答中提到的亲切字母,因此这些字母也包括在我的更新版本中。

which alters the pre-existing JavaScript language definition directly, rather than creating a new language definition within ctags. Now, when taglib forces vim's registered filetype, the new definitions are used. Also missing from the previously posted ~/.ctags lines was the "kind" letter that Al mentioned in his answer, so those are included in my updated version as well.

从那里,将以下内容放入〜/ .vimrc中以激活新类型:

From there, drop the following into your ~/.vimrc to activate the new types:

let g:tlist_javascript_settings = 'javascript;s:string;a:array;o:object;f:function'

All-in-all ,新的正则表达式线并不完美 - 他们肯定需要进行一些调整以避免大量的误报,并且分离常量等可能会很好。但现在,至少,我有能力做到这一点:)。

All-in-all, the new regex lines aren't perfect -- they'll definitely need some tweaking to avoid a lot of false positives, and it might be nice to separate out constants and such. But now, at least, I have the ability to do that :).

编辑:添加了如何激活类型而不编辑插件,并大大改进了主要的ctags函数正则表达式,以避免一些误报。

Added instructions on how to activate types without editing the plugin, and vastly improved the main ctags function regex to avoid some false-positives.

编辑2:添加更多的数组和对象定义到ctags regex。

Edit 2: Added more array and object definitions to the ctags regex.

这篇关于如何让vim的taglist插件显示javascript的有用信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆