Ctags自動補全快捷鍵版(不使用grep)

Ctags自動補全快捷鍵版(不使用grep)

之前的插件是在需要補全時讀取.tags文件,當.tags文件較大時,補全比較消耗時間,這裏通過快捷鍵觸發讀取.tags文件內容,需要補全時直接從內存中查找符號,節省了時間和資源。

# CTags based autocompletion plugin for Sublime Text 2
# You can add the file to the User Package in ~/Library/Application Support/Sublime Text 2/Packages and restart Sublime Text 2.
# Need 'ctags' in you PATH. You can donwload Windows 'ctags' here: URL=http://sourceforge.net/projects/ctags/
# generate the .tags file in your project root with "ctags -R -f .tags"

import sublime, sublime_plugin, os, re

gtagLines = []

class ctagsautocompleteCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        del gtagLines[:]
        tags_paths = [folder + '/.tags' for folder in self.view.window().folders()]
        for tags_path in tags_paths:
            if (not self.view.window().folders() or not os.path.exists(tags_path)): #check if a project is open and the .tags file exists
                return results
        tags_paths = ' '.join([str(x) for x in tags_paths]) 
        ftags = open(tags_paths, 'rU', encoding='utf-8')
        tagLines = ftags.readlines()
        gtagLines.extend(tagLines)
        ftags.close()

class CTagsAutoComplete(sublime_plugin.EventListener):
    def on_query_completions(self, view, prefix, locations):
        results=[]
        for tagsLine in gtagLines:
            rightLint = re.findall('^'+prefix+'.*', tagsLine)
            if rightLint:
                matchStr = re.split('\/\^', rightLint[0])
                if(1<len(matchStr)):
                    matchStr = matchStr[1]
                    matchStr = re.findall('.*\)', matchStr)
                    results.append(matchStr)
                    results = [(item,item) for sublist in results for item in sublist] #flatten
                    results = list(set(results)) # make unique
                    results.sort() # sort
        return results

Default (Linux).sublime-keymap/Default (OSX).sublime-keymap/Default (Windows).sublime-keymap
快捷鍵配置文件內容如下

[
    { "keys": ["ctrl+t", "ctrl+g"], "command": "ctagsautocomplete"  }
]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章