您好,登錄后才能下訂單哦!
小編給大家分享一下linux中怎么利用CTags開(kāi)發(fā)一個(gè)Sublime Text代碼補(bǔ)完插件,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
剛開(kāi)始接觸 Sublime Text 插件的編寫(xiě),當(dāng)然需要先了解 Sublime Text 提供的各種接口,為此,我去 Sublime Text 的官網(wǎng)找到了相關(guān)文檔:How to Create a Sublime Text Plugin,以及 Sublime Text Unofficial Documentation。
首先,在 Sublime Text 中選擇 “Tools -> Developer -> New Plugin” 新建一個(gè)最基本的插件文檔:
import sublimeimport sublime_plugin class ExampleCommand(sublime_plugin.TextCommand): def run(self, edit): self.view.insert(edit, 0, "Hello, World!")
這里的 sublime
和 sublime_plugin
是 Sublime 必需的模塊,其中具體的類(lèi)和方法可以參考官方的 API Reference。
接著,把這個(gè)文件保存到 Package
文件夾(默認(rèn)的保存位置 User
文件夾的上一層)的 CTagsAutoComplete
文件夾(新建)下,并命名為 CTagsAutoComplete.py
。盡管命名并沒(méi)有什么限制,但***還是以插件的名稱(chēng)來(lái)統(tǒng)一命名。
然后回到 Sublime Text 中,通過(guò)快捷鍵 Ctrl+`
進(jìn)入 Sublime Text 的 Command Console,然后輸入 view.run_command('example')
,如果下方顯示 “Hello World”,說(shuō)明插件已經(jīng)正常加載。
這里之所以直接用
'example'
,是因?yàn)?Command 命令的名稱(chēng)是根據(jù)大寫(xiě)字符進(jìn)行拆分的,例子中的ExampleCommand
在 Command 中 為'example_command'
,直接輸入'example'
也可以訪(fǎng)問(wèn)。
Window
:Sublime Text 的當(dāng)前窗口對(duì)象
View
:Sublime Text 當(dāng)前窗口中打開(kāi)的視圖對(duì)象
Command Palette
:Sublime Text 中通過(guò)快捷鍵 Ctrl+Shift+P
打開(kāi)的交互式列表
Sublime Text 下的插件命令有 3 種命令類(lèi)型(都來(lái)自于 sublime_plugin
模塊):
TextCommand Class:通過(guò) View
對(duì)象提供對(duì)選定文件/緩沖區(qū)的內(nèi)容的訪(fǎng)問(wèn)。
WindowCommand Class:通過(guò) Window
對(duì)象提供當(dāng)前窗口的引用
ApplicationCommand Class:這個(gè)類(lèi)沒(méi)有引用任何特定窗口或文件/緩沖區(qū),因此很少使用
2 種事件監(jiān)聽(tīng)類(lèi)型:
EventListener Class:監(jiān)聽(tīng) Sublime Text 中各種事件并執(zhí)行一次命令
ViewEventListener Class:為 EventListener
提供類(lèi)似事件處理的類(lèi),但綁定到特定的 view。
2 種輸入處理程序:
TextInputHandler Class:可用于接受 Command Palette 中的文本輸入。
ListInputHandler Class:可用于接受來(lái)自 Command Palette 中列表項(xiàng)的選擇輸入。
因?yàn)槲乙獙?shí)現(xiàn)的功能比較簡(jiǎn)單,只需要監(jiān)聽(tīng)輸入事件并觸發(fā)自動(dòng)完成功能,因此需要用到 EventListener Class
。在該類(lèi)下面找到了 on_query_completions
方法用來(lái)處理觸發(fā)自動(dòng)完成時(shí)執(zhí)行的命令。接著修改一下剛才的代碼:
import sublimeimport sublime_plugin class CTagsAutoComplete(sublime_plugin.EventListener): def on_query_completions(self, view, prefix, locations):
view
:當(dāng)前視圖
prefix
:觸發(fā)自動(dòng)完成時(shí)輸入的文字
locations
: 觸發(fā)自動(dòng)完成時(shí)輸入在緩存區(qū)中的位置,可以通過(guò)這個(gè)參數(shù)判斷語(yǔ)言來(lái)執(zhí)行不同命令
返回類(lèi)型:
return None
return [["trigger \t hint", "contents"]...]
,其中 \t hint
為可選內(nèi)容,給自動(dòng)完成的函數(shù)名稱(chēng)添加一個(gè)提示
return (results, flag)
,其中 results
是包含自動(dòng)完成語(yǔ)句的 list,如上;flag
是一個(gè)額外參數(shù),可用來(lái)控制是否顯示 Sublime Text 自帶的自動(dòng)完成功能
為了讀取 .tag 文件,首先得判斷當(dāng)前項(xiàng)目是否打開(kāi),同時(shí) .tag 文件是否存在,然后讀取 .tag 文件中的所有內(nèi)容:
import sublimeimport sublime_pluginimport osimport re class CTagsAutoComplete(sublime_plugin.EventListener): def on_query_completions(self, view, prefix, locations): results = [] ctags_paths = [folder + '\.tags' for folder in view.window().folders()] ctags_rows = [] for ctags_path in ctags_paths: if not is_file_exist(view, ctags_path): return [] ctags_path = str(ctags_path) ctags_file = open(ctags_path, encoding = 'utf-8') ctags_rows += ctags_file.readlines() ctags_file.close() def is_file_exist(view, file): if (not view.window().folders() or not os.path.exists(file)): return False return True
通過(guò)上述操作,即可讀取當(dāng)前項(xiàng)目下所有的 .tag 文件中的內(nèi)容。
首先是獲取 .tags 文件中,包含 prefix
的行:
for rows in ctags_rows: target = re.findall('^' + prefix + '.*', rows)
一旦找到,就通過(guò)正則表達(dá)式對(duì)該行數(shù)據(jù)進(jìn)行處理:
if target: matched = re.split('\t', str(target[0])) trigger = matched[0] # 返回的***個(gè)參數(shù),函數(shù)名稱(chēng) trigger += '\t(%s)' % 'CTags' # 給函數(shù)名稱(chēng)后加上標(biāo)識(shí) 'CTags' contents = re.findall(prefix + '[0-9a-zA-Z_]*\(.*\)', str(matched[2])) # 返回的第二個(gè)參數(shù),函數(shù)的具體定義 if (len(matched) > 1 and contents): results.append((trigger, contents[0])) results = list(set(results)) # 去除重復(fù)的函數(shù) results.sort() # 排序
處理完成之后就可以返回了,考慮到***只顯示 .tags 中的函數(shù),我不需要顯示 Sublime Text 自帶的自動(dòng)完成功能(提取當(dāng)前頁(yè)面中的變量和函數(shù)),因此我的返回結(jié)果如下:
return (results, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
考慮到能夠關(guān)閉插件的功能,因此需要添加一個(gè)配置文件,用來(lái)指定不開(kāi)啟插件功能的語(yǔ)言,這里我參考了 “All AutoComplete” 的代碼:
def plugin_loaded(): global settings settings = sublime.load_settings('CTagsAutoComplete.sublime-settings') def is_disabled_in(scope): excluded_scopes = settings.get("exclude_from_completion", []) for excluded_scope in excluded_scopes: if scope.find(excluded_scope) != -1: return True return False if is_disabled_in(view.scope_name(locations[0])): return []
這里用到的配置文件需要添加到插件所在的文件夾中,名稱(chēng)為 CTagsAutoComplete.sublime-settings
,其內(nèi)容為:
{ // An array of syntax names to exclude from being autocompleted. "exclude_from_completion": [ "css", "html" ]}
有了配置文件,還需要在 Sublime Text 的 “Preferences -> Package settings” 下添加相應(yīng)的設(shè)置,同樣也是放在插件所在文件夾中,名稱(chēng)為 Main.sublime-menu
:
[ { "caption": "Preferences", "mnemonic": "n", "id": "preferences", "children": [ { "caption": "Package Settings", "mnemonic": "P", "id": "package-settings", "children": [ { "caption": "CTagsAutoComplete", "children": [ { "command": "open_file", "args": { "file": "${packages}/CTagsAutoComplete/CTagsAutoComplete.sublime-settings" }, "caption": "Settings" } ] } ] } ] }]
以上是“l(fā)inux中怎么利用CTags開(kāi)發(fā)一個(gè)Sublime Text代碼補(bǔ)完插件”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。