久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

linux中怎么利用CTags開發一個Sublime Text代碼補完插件

201次閱讀
沒有評論

共計 4425 個字符,預計需要花費 12 分鐘才能閱讀完成。

自動寫代碼機器人,免費開通

丸趣 TV 小編給大家分享一下 linux 中怎么利用 CTags 開發一個 Sublime Text 代碼補完插件,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

開始編寫新建插件

剛開始接觸 Sublime Text 插件的編寫,當然需要先了解 Sublime Text 提供的各種接口,為此,我去 Sublime Text 的官網找到了相關文檔:How to Create a Sublime Text Plugin,以及 Sublime Text Unofficial Documentation。

首先,在 Sublime Text  中選擇“Tools – Developer – New Plugin”新建一個最基本的插件文檔:

import sublimeimport sublime_plugin class ExampleCommand(sublime_plugin.TextCommand): def run(self, edit): self.view.insert(edit, 0,  Hello, World!)

這里的 sublime 和 sublime_plugin 是 Sublime 必需的模塊,其中具體的類和方法可以參考官方的 API Reference。

接著,把這個文件保存到 Package 文件夾(默認的保存位置 User 文件夾的上一層)的 CTagsAutoComplete 文件夾(新建)下,并命名為 CTagsAutoComplete.py。盡管命名并沒有什么限制,但 *** 還是以插件的名稱來統一命名。

然后回到 Sublime Text 中,通過快捷鍵 Ctrl+` 進入 Sublime Text 的 Command Console,然后輸入 view.run_command(example),如果下方顯示“Hello World”,說明插件已經正常加載。

這里之所以直接用 example,是因為 Command 命令的名稱是根據大寫字符進行拆分的,例子中的 ExampleCommand 在 Command 中 為 example_command,直接輸入 example 也可以訪問。

文中的術語

Window:Sublime Text 的當前窗口對象

View:Sublime Text 當前窗口中打開的視圖對象

Command Palette:Sublime Text 中通過快捷鍵 Ctrl+Shift+P 打開的交互式列表

確定插件接口類型

Sublime Text 下的插件命令有 3 種命令類型(都來自于 sublime_plugin 模塊):

TextCommand Class:通過 View 對象提供對選定文件 / 緩沖區的內容的訪問。

WindowCommand Class:通過 Window 對象提供當前窗口的引用

ApplicationCommand Class:這個類沒有引用任何特定窗口或文件 / 緩沖區,因此很少使用

2 種事件監聽類型:

EventListener Class:監聽 Sublime Text 中各種事件并執行一次命令

ViewEventListener Class:為 EventListener 提供類似事件處理的類,但綁定到特定的 view。

2 種輸入處理程序:

TextInputHandler Class:可用于接受 Command Palette 中的文本輸入。

ListInputHandler Class:可用于接受來自 Command Palette 中列表項的選擇輸入。

因為我要實現的功能比較簡單,只需要監聽輸入事件并觸發自動完成功能,因此需要用到 EventListener Class。在該類下面找到了 on_query_completions 方法用來處理觸發自動完成時執行的命令。接著修改一下剛才的代碼:

import sublimeimport sublime_plugin class CTagsAutoComplete(sublime_plugin.EventListener): def on_query_completions(self, view, prefix, locations):

view:當前視圖

prefix:觸發自動完成時輸入的文字

locations: 觸發自動完成時輸入在緩存區中的位置,可以通過這個參數判斷語言來執行不同命令

返回類型:

return None

return [[trigger \t hint , contents]…],其中 \t hint 為可選內容,給自動完成的函數名稱添加一個提示

return (results, flag),其中 results 是包含自動完成語句的 list,如上;flag 是一個額外參數,可用來控制是否顯示 Sublime Text 自帶的自動完成功能

讀取 CTags 文件

為了讀取 .tag 文件,首先得判斷當前項目是否打開,同時 .tag 文件是否存在,然后讀取 .tag 文件中的所有內容:

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

通過上述操作,即可讀取當前項目下所有的 .tag 文件中的內容。

分析 CTags 文件

首先是獲取 .tags 文件中,包含 prefix 的行:

for rows in ctags_rows: target = re.findall(^  + prefix +  .* , rows)

一旦找到,就通過正則表達式對該行數據進行處理:

if target: matched = re.split(\t , str(target[0])) trigger = matched[0] #  返回的 *** 個參數,函數名稱  trigger +=  \t(%s)  %  CTags  #  給函數名稱后加上標識   CTags  contents = re.findall(prefix +  [0-9a-zA-Z_]*\(.*\) , str(matched[2])) #  返回的第二個參數,函數的具體定義  if (len(matched)   1 and contents): results.append((trigger, contents[0])) results = list(set(results)) #  去除重復的函數  results.sort() #  排序 

處理完成之后就可以返回了,考慮到 *** 只顯示 .tags 中的函數,我不需要顯示 Sublime Text 自帶的自動完成功能(提取當前頁面中的變量和函數),因此我的返回結果如下:

return (results, sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)

添加配置文件

考慮到能夠關閉插件的功能,因此需要添加一個配置文件,用來指定不開啟插件功能的語言,這里我參考了“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 []

這里用到的配置文件需要添加到插件所在的文件夾中,名稱為 CTagsAutoComplete.sublime-settings,其內容為:

{ // An array of syntax names to exclude from being autocompleted.  exclude_from_completion : [  css ,  html  ]}

添加設置文件

有了配置文件,還需要在 Sublime Text 的“Preferences – Package settings”下添加相應的設置,同樣也是放在插件所在文件夾中,名稱為 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  } ] } ] } ] }]

以上是“linux 中怎么利用 CTags 開發一個 Sublime Text 代碼補完插件”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!

向 AI 問一下細節

丸趣 TV 網 – 提供最優質的資源集合!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-07發表,共計4425字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 措美县| 澜沧| 专栏| 临颍县| 白山市| 海兴县| 阿鲁科尔沁旗| 扎囊县| 牟定县| 西安市| 罗田县| 金坛市| 大厂| 凤阳县| 嘉义县| 元谋县| 河东区| 平顶山市| 沈阳市| 灵宝市| 美姑县| 迁西县| 盐山县| 新安县| 余庆县| 洛隆县| 和硕县| 翼城县| 霍林郭勒市| 岑巩县| 社旗县| 凤阳县| 昆山市| 安达市| 浮山县| 历史| 林周县| 鸡西市| 琼中| 平潭县| 镇赉县|