Sublime Text 3 API參考

2018-10-18 17:42 更新

基本信息

基本信息

示例插件

幾個預(yù)制插件附帶Sublime Text,你可以在Default包中找到它們:

  • Packages/Default/delete_word.py 刪除光標(biāo)左側(cè)或右側(cè)的單詞
  • Packages/Default/duplicate_line.py 復(fù)制當(dāng)前行
  • Packages/Default/exec.py 使用幻影在線顯示錯誤
  • Packages/Default/font.py 顯示如何使用設(shè)置
  • Packages/Default/goto_line.py 提示用戶輸入,然后更新選擇
  • Packages/Default/mark.py 使用 add_regions()</ tt>為圖層添加圖標(biāo)
  • Packages/Default/show_scope_name.py 使用彈出窗口在插入符號處顯示范圍名稱
  • Packages/Default/trim_trailing_whitespace.py在保存緩沖區(qū)之前修改緩沖區(qū)

插件生命周期

在導(dǎo)入時,插件可能不會調(diào)用任何API函數(shù),除了sublime.version(),sublime.platform(),sublime.architecture()sublime.channel()。
如果插件定義了模塊級函數(shù)plugin_loaded(),那么當(dāng)API準(zhǔn)備好使用時,將調(diào)用它。 插件也可以定義plugin_unloaded(),以便在插件卸載之前得到通知。

線程

所有API函數(shù)都是線程安全的,但是請記住,從代碼運行在替代線程的角度來看,應(yīng)用程序狀態(tài)將在代碼運行時改變。

類型

本文檔通常指的是簡單的Python數(shù)據(jù)類型。 一些類型名稱是本文中記錄的類,但也有一些自定義類型名稱引用具有特定語義的構(gòu)造:

  • location: (str, str, (int, int))的元組,其包含關(guān)于符號的位置的信息。 第一個字符串是絕對文件路徑,第二個是相對于項目的文件路徑,第三個元素是行和列的兩元素元組。
  • point: 一個 int </ tt>,表示從編輯器緩沖區(qū)開始的偏移量。 查看</ tt>方法 text_point()</ tt>和 rowcol()</ tt>允許轉(zhuǎn)換為此格式。
  • value: 任何Python數(shù)據(jù)類型 bool </ ttttt> int </ tt>, float </ tt>, str </ tt>,列表</ tt> dict </ tt>。
  • vector: 表示 x </ tt>和 y </ tt>坐標(biāo)的(float,float)</ tt>的元組。

sublime 模塊

方法 返回值 描述
set_timeout(callback, delay) None 在給定的callback(以毫秒為單位)后,在主線程中運行delay。 具有相等延遲的回調(diào)將按照它們添加的順序運行。
set_timeout_async(callback, delay) None 在給定的callback;(以毫秒為單位)后,在備用線程上運行delay。
error_message(string) None 向用戶顯示錯誤對話框。
message_dialog(string) None 向用戶顯示消息對話框。
ok_cancel_dialog(string, <ok_title>) bool 向用戶顯示確定/取消問題對話框。 如果提供 ok_title ,則可以將其用作確定按鈕上的文本。 如果用戶按下確定按鈕,則返回 True 。
yes_no_cancel_dialog(string, <yes_title>, <no_title>) int 向用戶顯示是/否/取消問題對話框。 如果提供 yes_title 和/或 no_title ,它們將用作某些平臺上相應(yīng)按鈕上的文本。 返回 sublime.DIALOG_YES , sublime.DIALOG_NO 或 sublime.DIALOG_CANCEL 。
load_resource(name) str 加載給定資源。 名稱應(yīng)采用格式 Packages/Default/Main.sublime-menu 。
load_binary_resource(name) bytes 加載給定資源。 名稱應(yīng)采用格式 Packages/Default/Main.sublime-menu 。
find_resources(pattern) [str] 查找其文件名匹配給定pattern的資源。
encode_value(value, <pretty>) str 將JSON兼容的值編碼為字符串表示形式。 如果 pretty 設(shè)置為 True ,則字符串將包含換行符和縮進(jìn)。
decode_value(string) value 將JSON字符串解碼為對象。 如果 string 無效,則會拋出 ValueError 。
expand_variables(value, variables) value 使用字典變量中定義的變量擴(kuò)展字符串 value 中的任何變量。 value 也可以是列表或 dict ,在這種情況下,結(jié)構(gòu)將被遞歸地展開。 字符串應(yīng)使用代碼段語法,例如: expand_variables("Hello, ${name}", {"name": "Foo"})
load_settings(base_name) Settings 加載指定的設(shè)置。 名稱應(yīng)包括文件名和擴(kuò)展名,而不是路徑。 將搜索與 base_name 匹配的文件包,并將結(jié)果整理到設(shè)置對象中。 隨后使用 base_name 調(diào)用 load_settings()將返回相同的對象,而不會再次從磁盤加載設(shè)置。
save_settings(base_name) None 將任何內(nèi)存中的更改刷新到磁盤的指定設(shè)置對象。
windows() [Window] 返回所有打開的窗口的列表。
active_window() Window 返回最近使用的窗口。
packages_path() str 返回所有用戶丟失軟件包的路徑。
installed_packages_path() str 返回所有用戶所在的路徑。sublime-package 文件所在的路徑。
cache_path() str 返回Sublime Text存儲高速緩存文件的路徑。
get_clipboard(<size_limit>) str 返回剪貼板的內(nèi)容。 size_limit 是為了防止不必要的大數(shù)據(jù),默認(rèn)值為16,777,216個字符
set_clipboard(string) None 設(shè)置剪貼板的內(nèi)容。
score_selector(scope, selector) int 將選擇器與給定的范圍匹配,返回分?jǐn)?shù)。 分?jǐn)?shù) 0 表示無匹配,高于 0 表示匹配。 可以對同一范圍比較不同的選擇器:較高的分?jǐn)?shù)意味著選擇器是范圍的更好匹配。
run_command(string, <args>) None 使用給定的 args (可選)運行命名的 ApplicationCommand 。
log_commands(flag) None 控制命令日志。 如果啟用,所有命令從鍵綁定運行,菜單將記錄到控制臺。
log_input(flag) None 控制輸入記錄。 如果啟用,所有按鍵將被記錄到控制臺。
log_result_regex(flag) None 控制結(jié)果正則表達(dá)式登錄。 這對于調(diào)試構(gòu)建系統(tǒng)中使用的正則表達(dá)式很有用。
version() str 返回版本號
platform() str 返回平臺,其可以是“osx”,“l(fā)inux”或“windows”
arch() str 返回CPU體系結(jié)構(gòu),可以是“x 32”或“x64”

sublime.View Class

將視圖表示為文本緩沖區(qū)。 注意,多個視圖可以引用相同的緩沖區(qū),但是它們具有它們自己的唯一選擇和幾何。

方法 返回值 描述
id() int 返回唯一標(biāo)識此視圖的數(shù)字。
buffer_id() int 返回唯一標(biāo)識此視圖下的緩沖區(qū)的數(shù)字。
is_primary() bool 如果視圖是進(jìn)入文件的主視圖。 只有 False ,如果用戶已將多個視圖打開到文件中。
file_name() str 全名文件與緩沖區(qū)關(guān)聯(lián)的文件,或None(如果磁盤上不存在)。
name() str 分配給緩沖區(qū)的名稱(如果有)
set_name(name) None 為緩沖區(qū)分配名稱
is_loading() bool 如果緩沖區(qū)仍在從磁盤加載,并且未準(zhǔn)備好使用,則返回 True 。
is_dirty() bool 如果有任何未保存的緩沖區(qū)修改,則返回 True 。
is_read_only() bool 如果緩沖區(qū)可能未被修改,則返回 True 。
set_read_only(value) None 設(shè)置緩沖區(qū)上的只讀屬性。
is_scratch() bool 如果緩沖區(qū)是暫存緩沖區(qū),則返回 True 。 劃痕緩沖區(qū)從未報告為臟。
set_scratch(value) None 設(shè)置緩沖區(qū)上的scratch屬性。
settings() Settings 返回對視圖的設(shè)置對象的引用。 對此設(shè)置對象的任何更改將對此視圖為私有。
window() Window 返回對包含視圖的窗口的引用。
run_command(string, <args>) None 使用給定的參數(shù)(可選)運行命名的 TextCommand
size() int 返回文件中的字符數(shù)。
substr(region) str 以字符串形式返回區(qū)域的內(nèi)容。
substr(point) str 返回點右側(cè)的字符。
insert(edit, point, string) int 在指定的點在緩沖區(qū)中插入給定的字符串。 返回插入的字符數(shù):如果將標(biāo)簽翻譯為當(dāng)前緩沖區(qū)中的空格,則可能會有所不同。
erase(edit, region) None 從緩沖區(qū)中刪除區(qū)域的內(nèi)容。
replace(edit, region, string) None 用給定的字符串</ tt>替換區(qū)域的內(nèi)容。
sel() Selection 返回對選擇的引用。
line(point) Region 返回包含點的行。
line(region) Region 返回 region 的修改副本,使其從一行的開始處開始,并在一行的結(jié)尾處結(jié)束。 注意,它可以跨越幾行。
full_line(point) Region 作為 line(),但是該區(qū)域包括尾隨換行符(如果有)。
full_line(region) Region 作為 line(),但是該區(qū)域包括尾隨換行符(如果有)。
lines(region) [Region] 返回與區(qū)域相交的線列表(以排序順序)。
split_by_newlines(region) [Region] 拆分區(qū)域,使得返回的每個區(qū)域都存在于一條線上。
word(point) Region 返回包含點的單詞。
word(region) Region 返回 region 的修改副本,以使其從單詞的開頭開始,并在單詞的結(jié)尾處結(jié)束。 注意,它可以跨越幾個單詞。
classify(point) int

分類點,返回這些標(biāo)志中的零個或多個的按位OR:

  • sublime.CLASS_WORD_START
  • sublime.CLASS_WORD_END
  • sublime.CLASS_PUNCTUATION_START
  • sublime.CLASS_PUNCTUATION_END
  • sublime.CLASS_SUB_WORD_START
  • sublime.CLASS_SUB_WORD_END
  • sublime.CLASS_LINE_START
  • sublime.CLASS_LINE_END
  • sublime.CLASS_EMPTY_LINE
find_by_class(point, forward, classes, <separators>) Region 查找與給定類匹配的點后的下一個位置。 如果 forward 為 False ,則向后搜索而不是向前搜索。 類是 sublime.CLASS_XXX 標(biāo)志的按位或。 分隔符可以傳入,以定義應(yīng)該考慮哪些字符來分隔單詞。
expand_by_class(point, classes, <separators>) Region 向左和向右展開點,直到每個邊達(dá)到類匹配的位置。 類是 sublime.CLASS_XXX 標(biāo)志的按位或。 分隔符可以傳入,以定義應(yīng)該考慮什么字符來分隔單詞。
expand_by_class(region, classes, <separators>) Region 向左和向右擴(kuò)展區(qū)域,直到每個邊達(dá)到類匹配的位置。 類是 sublime.CLASS_XXX 標(biāo)志的按位或。 分隔符可以傳入,以定義應(yīng)該考慮哪些字符來分隔單詞。
find(pattern, start_point, <flags>) Region 如果找不到,則從 start_point 或無返回與正則表達(dá)式模式匹配的第一個區(qū)域。 可選的 flags 參數(shù)可以是 sublime.LITERAL , sublime.IGNORECASE 或兩個ORed在一起。
find_all(pattern, <flags>, <format>, <extractions>) [區(qū)域] 返回與正則表達(dá)式模式匹配的所有(非重疊)區(qū)域。 可選的 flags 參數(shù)可以是 sublime.LITERAL , sublime.IGNORECASE 或兩個ORed在一起。 如果給出了格式字符串,則所有匹配都將使用格式化的字符串格式化,并放入提取列表中。
rowcol(point) (int, int) Calculates the 0-based line and column numbers of the point.
text_point(row, col) int 計算給定的,基于0,行和 col 的字符偏移量。 請注意, col 被解釋為超過行開頭的字符數(shù)。
set_syntax_file(syntax_file) None 更改視圖使用的語法。 syntax_file 應(yīng)為 Packages/Python/Python.tmLanguage 的名稱。 要檢索當(dāng)前語法,請使用 view.settings()。get('syntax')。
extract_scope(point) Region 返回分配給給定點的字符的范圍名稱的范圍。
scope_name(point) str 返回分配給給定點的字符的語法范圍名稱。
match_selector(point, selector) bool 檢查選擇器是否符合給定點的范圍,如果匹配,則返回布爾值。
score_selector(point, selector) int 使選擇器與給定點的范圍匹配,返回分?jǐn)?shù)。 分?jǐn)?shù) 0 表示無匹配,高于 0 表示匹配。 可以對同一范圍比較不同的選擇器:較高的分?jǐn)?shù)意味著選擇器是范圍的更好匹配。
find_by_selector(selector) [Region] 查找與給定選擇器匹配的文件中的所有區(qū)域,將它們作為列表返回。
show(location, <show_surrounds>) None 滾動視圖以顯示給定的位置,可能是,區(qū)域選擇。
show_at_center(location) None 將視圖滾動到位置的中心,可能是地區(qū)。
visible_region() Region 返回視圖的當(dāng)前可見區(qū)域。
viewport_position() vector 返回布局坐標(biāo)中視口的偏移量。
set_viewport_position(vector, <animate<) None 將視口滾動到給定的布局位置。
viewport_extent() vector 返回視口的寬度和高度。
layout_extent() vector 返回布局的寬度和高度。
text_to_layout(point) vector 將文本位置轉(zhuǎn)換為布局位置
layout_to_text(vector) point 將布局位置轉(zhuǎn)換為文本位置
window_to_layout(vector) vector 將窗口位置轉(zhuǎn)換為布局位置
window_to_text(vector) point 將窗口位置轉(zhuǎn)換為文本位置
line_height() float 返回布局中使用的燈光高度
em_width() float 返回布局中使用的典型字符寬度
add_regions(key, [regions], <scope>, <icon>, <flags>) None

向視圖中添加一組區(qū)域。 如果給定鍵已存在一組區(qū)域,它們將被覆蓋。 范圍用于提供顏色來繪制區(qū)域,應(yīng)該是范圍的名稱,例如&quot; comment&quot; 或“string”。 如果 scope 為空,則不會繪制 regions 。

可選的圖標(biāo)名稱(如果給出)將在每個區(qū)域旁邊的水槽中繪制命名的圖標(biāo)。 圖標(biāo)將使用與范圍相關(guān)聯(lián)的顏色進(jìn)行著色。 有效的圖標(biāo)名稱為 dot ,圓形,書簽和交叉。 圖標(biāo)名稱也可以是完整包相對路徑,例如 Packages/Theme-Default/dot.png 。

可選的 flags 參數(shù)是以下各項的按位組合:

  • sublime.DRAW EMPTY :使用豎線繪制空白區(qū)域。 默認(rèn)情況下,它們不會繪制。
  • sublime.HIDE_ON_MINIMAP:不要在小地圖上顯示區(qū)域。
  • sublime.DRAW_EMPTY_AS_OVERWRITE: 使用水平條而不是垂直條繪制空白區(qū)域。
  • sublime.DRAW_NO_FILL:禁用填充區(qū)域,只留下輪廓。
  • sublime.DRAW_NO_OUTLINE:禁用繪制區(qū)域的輪廓。
  • sublime.DRAW_SOLID_UNDERLINE: 在區(qū)域下面繪制一個實線下劃線。
  • sublime.DRAW_STIPPLED_UNDERLINE: 在區(qū)域下方繪制點狀下劃線。
  • sublime.DRAW_SQUIGGLY_UNDERLINE: 以下地區(qū)波浪下劃線。
  • sublime.PERSISTENT: 在會話中保存區(qū)域。
  • sublime.HIDDEN: 不要繪制區(qū)域。

下劃線樣式是獨占的,應(yīng)該給出零個或一個。 如果使用下劃線,則通常應(yīng)傳入 sublime.DRAW_NO_FILL 和 sublime.DRAW_NO_OUTLINE 。

get_regions(key) [Region] 返回與給定鍵(如果有)相關(guān)聯(lián)的區(qū)域
erase_regions(key) None 刪除了命名區(qū)域
set_status(key, value) None 將狀態(tài)鍵添加到視圖。 值將顯示在狀態(tài)欄中,以逗號分隔的所有狀態(tài)值列表的形式,按鍵排序。 將值設(shè)置為空字符串將清除狀態(tài)。
get_status(key) str 返回與鍵(如果有)相關(guān)聯(lián)的先前分配的值。
erase_status(key) None 清除命名狀態(tài)。
command_history(index, <modifying_only>) (str, dict, int)

返回存儲在undo/redo堆棧中的給定歷史記錄條目的命令名稱,命令參數(shù)和重復(fù)計數(shù)。

索引 0 對應(yīng)于最近的命令, -1 之前的命令,等等。 索引的正值表示在重做堆棧中查找命令。 如果撤消/重做歷史記錄不足夠延伸,則將返回(None,None,0)。

將 modifying_only 設(shè)置為 True (默認(rèn)值為 False )將只返回修改緩沖區(qū)的條目。

change_count() int 返回當(dāng)前更改計數(shù)。 每次修改緩沖區(qū)時,增量更改計數(shù)。 更改計數(shù)可用于確定自上次檢查后緩沖區(qū)是否已更改。
fold([regions]) bool 折疊給定的區(qū)域,如果它們已折疊,則返回 False
fold(region) bool 折疊給定的區(qū)域,如果它已折疊,則返回 False
unfold(region) [Region] 展開區(qū)域中的所有文本,返回展開的區(qū)域
unfold([regions]) [Region] 展開地區(qū)中的所有文字,返回展開的區(qū)域
encoding() str 返回當(dāng)前與文件關(guān)聯(lián)的編碼
set_encoding(encoding) None 對文件應(yīng)用新的編碼。 此編碼將在下次保存文件時使用。
line_endings() str 返回當(dāng)前文件使用的行尾。
set_line_endings(line_endings) None 設(shè)置下次保存時將應(yīng)用的行結(jié)束。
overwrite_status() bool 返回覆蓋狀態(tài),用戶通常通過插入鍵切換。
set_overwrite_status(enabled) None 設(shè)置覆蓋狀態(tài)。
symbols() [(Region, str)] 提取在緩沖區(qū)中定義的所有符號。
show_popup_menu(items, on_done, <flags>) None

在插入符處顯示彈出式菜單,以選擇列表中的項目。 on_done 將使用所選項目的索引調(diào)用一次。 如果彈出式菜單已取消,將使用參數(shù) -1 調(diào)用 on_done 。

items is a list of strings.

flags it currently unused.

show_popup(content, <flags>, <location>, <max_width>, <max_height>, <on_navigate>, <on_hide>) None

顯示顯示HTML內(nèi)容的彈出窗口。

標(biāo)志是以下各項的按位組合:

  • sublime.COOPERATE_WITH_AUTO_COMPLETE. 導(dǎo)致彈出式窗口顯示在自動完成菜單旁邊
  • sublime.HIDE_ON_MOUSE_MOVE. 當(dāng)鼠標(biāo)移動,點擊或滾動時,導(dǎo)致彈出窗口隱藏
  • sublime.HIDE_ON_MOUSE_MOVE_AWAY.當(dāng)鼠標(biāo)移動(除非朝向彈出窗口),或單擊或滾動時,導(dǎo)致彈出窗口隱藏

-1 的默認(rèn)位置將在光標(biāo)處顯示彈出窗口,否則應(yīng)傳遞文本點。

最大寬度和最大高度設(shè)置彈出窗口的最大尺寸,之后將顯示滾動條。

on_navigate 是一個回調(diào),應(yīng)該接受用戶點擊的鏈接上的 href 屬性的字符串內(nèi)容。

on_hide 在隱藏彈出式窗口時調(diào)用。

update_popup(content) None 更新當(dāng)前可見彈出窗口的內(nèi)容。
is_popup_visible() bool 如果彈出窗口當(dāng)前正在顯示,則返回。
hide_popup() None 隱藏彈出窗口。
is_auto_complete_visible() bool 目前可以看到自動完成菜單的返回。

sublime.Selection

保持一組區(qū)域,確保沒有重疊。 區(qū)域按照排序順序保存。

方法 返回值 描述
clear() None 刪除所有區(qū)域。
add(region) None 添加給定的區(qū)域。 它將與已經(jīng)包含在集合內(nèi)的任何相交區(qū)域合并。
add_all(regions) None 添加給定列表或元組中的所有區(qū)域。
subtract(region) None 從集合中的所有區(qū)域中減去區(qū)域。
contains(region) bool 如果給定的區(qū)域是子集,則返回 True 。

sublime.Region

表示緩沖區(qū)的面積。空區(qū)域,其中 a==b 有效。

構(gòu)造函數(shù) 描述
Region(a, b) 創(chuàng)建具有初始值a和b的區(qū)域。
屬性 類型 描述
a int 區(qū)域的第一端。
b int 區(qū)域的第二端。 可能小于a,在這種情況下,該區(qū)域是相反的。
xpos int 區(qū)域的目標(biāo)水平位置,如果未定義,則 -1 。 按下向上或向下鍵時的效果行為。
方法返回值描述
begin() int 返回a和b的最小值。
end() int 返回a和b的最大值。
size() int 返回由區(qū)域跨越的字符數(shù)。 始終&gt; = 0。
empty() bool 返回 True iff begin() == 和()。
cover(region) Region 返回橫跨此區(qū)域和給定區(qū)域的區(qū)域。
intersection(region) Region 返回兩個區(qū)域的集合交集。
intersects(region) bool 返回 Truett> off self == region 或兩者包括一個或多個共同位置。
contains(region) bool 如果給定的區(qū)域是子集,則返回 True 。
contains(point) bool 返回 True iff begin() <=tt>指向&lt; = 和()。

sublime.Phantom

表示基于HTML的裝飾,用于顯示散布在視圖中的不可編輯內(nèi)容。 與PhantomSet一起使用實際將幻像添加到視圖。 一旦Phantom被構(gòu)造并添加到視圖中,對屬性的更改將不起作用。

構(gòu)造函數(shù) 描述
Phantom(region, content, layout, <on_navigate>)

創(chuàng)建附加到區(qū)域的幻像</ tt>。 內(nèi)容</ tt>是要由主html 處理的HTML。

layout must be one of:

  • sublime.LAYOUT_INLINE: Display the phantom in between the region and the point following.
  • sublime.LAYOUT_BELOW: Display the phantom in space below the current line, left-aligned with the region.
  • sublime.LAYOUT_BLOCK: Display the phantom in space below the current line, left-aligned with the beginning of the line.

on_navigate is an optional callback that should accept a single string parameter, that is the href attribute of the link clicked.

sublime.PhantomSet Class

A collection that manages Phantoms and the process of adding them, updating them and removing them from the View.

Constructors Description
PhantomSet(view, <key>) Creates a PhantomSet attached to a view. key is a string to group Phantoms together.
MethodsReturn ValueDescription
update(phantoms) None

phantoms should be a list of phantoms.

The .region attribute of each existing phantom in the set will be updated. New phantoms will be added to the view and phantoms not in phantoms list will be deleted.

sublime.Edit Class

Edit objects have no functions, they exist to group buffer modifications.

Edit objects are passed to TextCommands, and can not be created by the user. Using an invalid Edit object, or an Edit object from a different View, will cause the functions that require them to fail.

Methods Return Value Description
(no methods)    

sublime.Window Class

Methods Return Value Description
id() int Returns a number that uniquely identifies this window.
new_file() View Creates a new file. The returned view will be empty, and its is_loaded() method will return True.
open_file(file_name, <flags>) View

Opens the named file, and returns the corresponding view. If the file is already opened, it will be brought to the front. Note that as file loading is asynchronous, operations on the returned view won't be possible until its is_loading() method returns False.

The optional flags parameter is a bitwise combination of:

  • sublime.ENCODED_POSITION: Indicates the file_name should be searched for a :row or :row:col suffix
  • sublime.TRANSIENT: Open the file as a preview only: it won't have a tab assigned it until modified
find_open_file(file_name) View Finds the named file in the list of open files, and returns the corresponding View, or None if no such file is open.
active_view() View Returns the currently edited view.
active_view_in_group(group) View Returns the currently edited view in the given group.
views() [View] Returns all open views in the window.
views_in_group(group) [View] Returns all open views in the given group.
num_groups() int Returns the number of view groups in the window.
active_group() int Returns the index of the currently selected group.
focus_group(group) None Makes the given group active.
focus_view(view) None Switches to the given view.
get_view_index(view) (int, int) Returns the group, and index within the group of the view. Returns -1 if not found.
set_view_index(view, group, index) None Moves the view to the given group and index.
status_message(string) None Show a message in the status bar.
is_menu_visible() bool Returns True if the menu is visible.
set_menu_visible(flag) None Controls if the menu is visible.
is_sidebar_visible() bool Returns True if the sidebar will be shown when contents are available.
set_sidebar_visible(flag) None Sets the sidebar to be shown or hidden when contents are available.
get_tabs_visible() bool Returns True if tabs will be shown for open files.
set_tabs_visible(flag) None Controls if tabs will be shown for open files.
is_minimap_visible() bool Returns True if the minimap is enabled.
set_minimap_visible(flag) None Controls the visibility of the minimap.
is_status_bar_visible() bool Returns True if the status bar will be shown.
set_status_bar_visible(flag) None Controls the visibility of the status bar.
folders() [str] Returns a list of the currently open folders.
project_file_name() str Returns name of the currently opened project file, if any.
project_data() dict Returns the project data associated with the current window. The data is in the same format as the contents of a .sublime-project file.
set_project_data(data) None Updates the project data associated with the current window. If the window is associated with a .sublime-project file, the project file will be updated on disk, otherwise the window will store the data internally.
run_command(string, <args>) None Runs the named WindowCommand with the (optional) given args. This method is able to run any sort of command, dispatching the command via input focus.
show_quick_panel(items, on_done, <flags>, <selected_index>, <on_highlighted>) None

Shows a quick panel, to select an item in a list. on_done will be called once, with the index of the selected item. If the quick panel was cancelled, on_done will be called with an argument of -1.

items may be a list of strings, or a list of string lists. In the latter case, each entry in the quick panel will show multiple rows.

flags is a bitwise OR of sublime.MONOSPACE_FONT and sublime.KEEP_OPEN_ON_FOCUS_LOST

on_highlighted, if given, will be called every time the highlighted item in the quick panel is changed.

show_input_panel(caption, initial_text, on_done, on_change, on_cancel) View Shows the input panel, to collect a line of input from the user. on_done and on_change, if not None, should both be functions that expect a single string argument. on_cancel should be a function that expects no arguments. The view used for the input widget is returned.
create_output_panel(name, <unlisted>) View

Returns the view associated with the named output panel, creating it if required. The output panel can be shown by running the show_panel window command, with the panel argument set to the name with an "output." prefix.

The optional unlisted parameter is a boolean to control if the output panel should be listed in the panel switcher.

find_output_panel(name) View or None Returns the view associated with the named output panel, or None if the output panel does not exist.
destroy_output_panel(name) None Destroys the named output panel, hiding it if currently open.
active_panel() str or None Returns the name of the currently open panel, or None if no panel is open. Will return built-in panel names (e.g. "console", "find", etc) in addition to output panels.
panels() [str] Returns a list of the names of all panels that have not been marked as unlisted. Includes certain built-in panels in addition to output panels.
lookup_symbol_in_index(symbol) [location] Returns all locations where the symbol is defined across files in the current project.
lookup_symbol_in_open_files(symbol) [location] Returns all locations where the symbol is defined across open files.
extract_variables() dict

Returns a dictionary of strings populated with contextual keys:

packages, platform, file, file_path, file_name, file_base_name, file_extension, folder, project, project_path, project_name, project_base_name, project_extension. This dict is suitable for passing to sublime.expand_variables().

sublime.Settings Class

Methods Return Value Description
get(name, <default>) value Returns the named setting, or default if it's not defined. If not passed, default will have a value of None.
set(name, value) None Sets the named setting. Only primitive types, lists, and dicts are accepted.
erase(name) None Removes the named setting. Does not remove it from any parent Settings.
has(name) bool Returns True iff the named option exists in this set of Settings or one of its parents.
add_on_change(key, on_change) None Register a callback to be run whenever a setting in this object is changed.
clear_on_change(key) None Remove all callbacks registered with the given key.

sublime_plugin Module

Methods Return Value Description
(no methods)    

sublime_plugin.EventListener Class

Note that many of these events are triggered by the buffer underlying the view, and thus the method is only called once, with the first view as the parameter.

Methods Return Value Description
on_new(view) None Called when a new buffer is created.
on_new_async(view) None Called when a new buffer is created. Runs in a separate thread, and does not block the application.
on_clone(view) None Called when a view is cloned from an existing one.
on_clone_async(view) None Called when a view is cloned from an existing one. Runs in a separate thread, and does not block the application.
on_load(view) None Called when the file is finished loading.
on_load_async(view) None Called when the file is finished loading. Runs in a separate thread, and does not block the application.
on_pre_close(view) None Called when a view is about to be closed. The view will still be in the window at this point.
on_close(view) None Called when a view is closed (note, there may still be other views into the same buffer).
on_pre_save(view) None Called just before a view is saved.
on_pre_save_async(view) None Called just before a view is saved. Runs in a separate thread, and does not block the application.
on_post_save(view) None Called after a view has been saved.
on_post_save_async(view) None Called after a view has been saved. Runs in a separate thread, and does not block the application.
on_modified(view) None Called after changes have been made to a view.
on_modified_async(view) None Called after changes have been made to a view. Runs in a separate thread, and does not block the application.
on_selection_modified(view) None Called after the selection has been modified in a view.
on_selection_modified_async(view) None Called after the selection has been modified in a view. Runs in a separate thread, and does not block the application.
on_activated(view) None Called when a view gains input focus.
on_activated_async(view) None Called when a view gains input focus. Runs in a separate thread, and does not block the application.
on_deactivated(view) None Called when a view loses input focus.
on_deactivated_async(view) None Called when a view loses input focus. Runs in a separate thread, and does not block the application.
on_hover(view, point, hover_zone) None

Called when the user's mouse hovers over a view for a short period.

point is the closest point in the view to the mouse location. The mouse may not actually be located adjacent based on the value of hover_zone:

  • sublime.HOVER_TEXT: When the mouse is hovered over text.
  • sublime.HOVER_GUTTER: When the mouse is hovered over the gutter.
  • sublime.HOVER_MARGIN: When the mouse is hovered in whitespace to the right of a line.
on_query_context(view, key, operator, operand, match_all) bool or None

Called when determining to trigger a key binding with the given context key. If the plugin knows how to respond to the context, it should return either True of False. If the context is unknown, it should return None.

operator is one of:

  • sublime.OP_EQUAL: Is the value of the context equal to the operand?
  • sublime.OP_NOT_EQUAL: Is the value of the context not equal to the operand?
  • sublime.OP_REGEX_MATCH: Does the value of the context match the regex given in operand?
  • sublime.OP_NOT_REGEX_MATCH: Does the value of the context not match the regex given in operand?
  • sublime.OP_REGEX_CONTAINS: Does the value of the context contain a substring matching the regex given in operand?
  • sublime.OP_NOT_REGEX_CONTAINS: Does the value of the context not contain a substring matching the regex given in operand?

match_all should be used if the context relates to the selections: does every selection have to match (match_all == True), or is at least one matching enough (match_all == False)?

on_query_completions(view, prefix, locations) list, tuple or None

Called whenever completions are to be presented to the user. The prefix is a unicode string of the text to complete.

locations is a list of points. Since this method is called for all completions in every view no matter the syntax, view.match_selector(point, relevant_scope) should be called to determine if the point is relevant.

The return value must be one of the following formats:

  • None: no completions are provided

    return None

  • A list of 2-element lists/tuples. The first element is a unicode string of the completion trigger, the second is the unicode replacement text.

    return [["me1", "method1()"], ["me2", "method2()"]]

    The trigger may contain a tab character (\t) followed by a hint to display in the right-hand side of the completion box.

    return [
        ["me1\tmethod", "method1()"],
        ["me2\tmethod", "method2()"]
    ]

    The replacement text may contain dollar-numeric fields such as a snippet does, e.g. $0, $1.

    return [
        ["fn", "def ${1:name}($2) { $0 }"],
        ["for", "for ($1; $2; $3) { $0 }"]
    ]

  • A 2-element tuple with the first element being the list format documented above, and the second element being bit flags from the following list:

    • sublime.INHIBIT_WORD_COMPLETIONS: prevent Sublime Text from showing completions based on the contents of the view
    • sublime.INHIBIT_EXPLICIT_COMPLETIONS: prevent Sublime Text from showing completions based on .sublime-completions files

    return (
        [
            ["me1", "method1()"],
            ["me2", "method2()"]
        ],
        sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS
    )

on_text_command(view, command_name, args) (str, dict) Called when a text command is issued. The listener may return a (command, arguments) tuple to rewrite the command, or None to run the command unmodified.
on_window_command(window, command_name, args) (str, dict) Called when a window command is issued. The listener may return a (command, arguments) tuple to rewrite the command, or None to run the command unmodified.
on_post_text_command(view, command_name, args) None Called after a text command has been executed.
on_post_window_command(window, command_name, args) None Called after a window command has been executed.

sublime_plugin.ViewEventListener Class

A class that provides similar event handling to EventListener, but bound to a specific view. Provides class method-based filtering to control what views objects are created for.

The view is passed as a single parameter to the constructor. The default implementation makes the view available via self.view.

Class Methods Return Value Description
is_applicable(settings) bool A @classmethod that receives a Settings object and should return a bool indicating if this class applies to a view with those settings
applies_to_primary_view_only() bool A @classmethod that should return a bool indicating if this class applies only to the primary view for a file. A view is considered primary if it is the only, or first, view into a file.
Methods Return Value Description
on_modified() None Called after changes have been made to the view.
on_modified_async() None Called after changes have been made to the view. Runs in a separate thread, and does not block the application.
on_selection_modified() None Called after the selection has been modified in the view.
on_selection_modified_async() None Called after the selection has been modified in the view. Runs in a separate thread, and does not block the application.
on_activated() None Called when a view gains input focus.
on_activated_async() None Called when the view gains input focus. Runs in a separate thread, and does not block the application.
on_deactivated() None Called when the view loses input focus.
on_deactivated_async() None Called when the view loses input focus. Runs in a separate thread, and does not block the application.
on_hover(point, hover_zone) None

Called when the user's mouse hovers over the view for a short period.

point is the closest point in the view to the mouse location. The mouse may not actually be located adjacent based on the value of hover_zone:

  • sublime.HOVER_TEXT: When the mouse is hovered over text.
  • sublime.HOVER_GUTTER: When the mouse is hovered over the gutter.
  • sublime.HOVER_MARGIN: When the mouse is hovered in whitespace to the right of a line.
on_query_context(key, operator, operand, match_all) bool or None

Called when determining to trigger a key binding with the given context key. If the plugin knows how to respond to the context, it should return either True of False. If the context is unknown, it should return None.

operator is one of:

  • sublime.OP_EQUAL: Is the value of the context equal to the operand?
  • sublime.OP_NOT_EQUAL: Is the value of the context not equal to the operand?
  • sublime.OP_REGEX_MATCH: Does the value of the context match the regex given in operand?
  • sublime.OP_NOT_REGEX_MATCH: Does the value of the context not match the regex given in operand?
  • sublime.OP_REGEX_CONTAINS: Does the value of the context contain a substring matching the regex given in operand?
  • sublime.OP_NOT_REGEX_CONTAINS: Does the value of the context not contain a substring matching the regex given in operand?

match_all should be used if the context relates to the selections: does every selection have to match (match_all == True), or is at least one matching enough (match_all == False)?

on_query_completions(prefix, locations) list, tuple or None

Called whenever completions are to be presented to the user. The prefix is a unicode string of the text to complete.

locations is a list of points. Since this method is called for all completions no matter the syntax, self.view.match_selector(point, relevant_scope) should be called to determine if the point is relevant.

The return value must be one of the following formats:

  • None: no completions are provided

    return None

  • A list of 2-element lists/tuples. The first element is a unicode string of the completion trigger, the second is the unicode replacement text.

    return [["me1", "method1()"], ["me2", "method2()"]]

    The trigger may contain a tab character (\t) followed by a hint to display in the right-hand side of the completion box.

    return [
        ["me1\tmethod", "method1()"],
        ["me2\tmethod", "method2()"]
    ]

    The replacement text may contain dollar-numeric fields such as a snippet does, e.g. $0, $1.

    return [
        ["fn", "def ${1:name}($2) { $0 }"],
        ["for", "for ($1; $2; $3) { $0 }"]
    ]

  • A 2-element tuple with the first element being the list format documented above, and the second element being bit flags from the following list:

    • sublime.INHIBIT_WORD_COMPLETIONS: prevent Sublime Text from showing completions based on the contents of the view
    • sublime.INHIBIT_EXPLICIT_COMPLETIONS: prevent Sublime Text from showing completions based on .sublime-completions files

    return (
        [
            ["me1", "method1()"],
            ["me2", "method2()"]
        ],
        sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS
    )

sublime_plugin.ApplicationCommand Class

Methods Return Value Description
run(<args>) None Called when the command is run.
is_enabled(<args>) bool Returns True if the command is able to be run at this time. The default implementation simply always returns True.
is_visible(<args>) bool Returns True if the command should be shown in the menu at this time. The default implementation always returns True.
is_checked(<args>) bool Returns True if a checkbox should be shown next to the menu item. The .sublime-menu file must have the checkbox attribute set to true for this to be used.
description(<args>) str Returns a description of the command with the given arguments. Used in the menu, if no caption is provided. Return None to get the default description.

sublime_plugin.WindowCommandClass

WindowCommands are instantiated once per window. The Window object may be retrieved via self.window

Methods Return Value Description
run(<args>) None Called when the command is run.
is_enabled(<args>) bool Returns True if the command is able to be run at this time. The default implementation simply always returns True.
is_visible(<args>) bool Returns True if the command should be shown in the menu at this time. The default implementation always returns True.
description(<args>) str Returns a description of the command with the given arguments. Used in the menu, if no caption is provided. Return None to get the default description.

sublime_plugin.TextCommand Class

TextCommands are instantiated once per view. The View object may be retrieved via self.view

Methods Return Value Description
run(edit, <args>) None Called when the command is run.
is_enabled(<args>) bool Returns True if the command is able to be run at this time. The default implementation simply always returns True.
is_visible(<args>) bool Returns True if the command should be shown in the menu at this time. The default implementation always returns True.
description(<args>) str Returns a description of the command with the given arguments. Used in the menus, and for Undo / Redo descriptions. Return None to get the default description.
want_event() bool Return True to receive an event argument when the command is triggered by a mouse action. The event information allows commands to determine which portion of the view was clicked on. The default implementation returns False.
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號