您鏈接到的解決方案應該同時適用於Sublime 2和3(假設提供repl_transfer_current
命令的插件適用於兩者),但由於宏格式不正確而無法工作。
[編輯] 由崇高本身提供不把你的微距拍攝時使用的參數的move
命令。據推測,如果這對你有效,這也是由一些包提供的。如果是這樣,您可能需要相應地調整下面的示例代碼。 [/編輯]
既然這樣,問題是,在崇高差不多(但不是全部)的配置文件是在JSON格式(略放鬆,允許評論),以及宏代碼上面和概述鏈接的解決方案不是有效的JSON,因爲它中的第一個和第二個命令不以逗號分隔。
類似下面應該工作:
[
{"command": "repl_transfer_current", "args": {"scope": "lines"}},
{"command": "move", "args": {"mode": "lines", "amount": 1}}
]
我認爲你鏈接到上面是崇高2崇高的文檔。一個很好的資源是Unofficial Documentation,它還包含list of commands(加上很多其他好東西)。
爲了做這樣的事情,並把它保持,直到它到達的第一個非空白,非註釋行你需要一個簡單的插件
具體那就要向下移動光標向下移動(使用現有的move
命令),然後檢查當前行以查看它是空白還是註釋,如果是則再次移動。然後,您可以使用該命令代替宏中的move
命令。
對於獎勵標記,下面是一個插件的例子,它做了這樣的事情。它比需要的更冗長,以至於它更具啓發性,並且可能需要額外的調整(R不是我使用/知道的語言之一),但它應該讓你開始。
有關如何工作的更多信息,可以查看API Reference以查看可以在插件中使用的所有內部命令。
要使用此,從菜單中選擇Tools > Developer > New Plugin...
,然後替換爲這裏呈現的插件代碼顯示的存根代碼的整體,並與.py
擴展名保存(名稱並不重要):
import sublime
import sublime_plugin
import re
# A regex that matches a line that's blank or contains a comment.
# Adjust as needed
_r_blank = re.compile("^\s*(#.*)?$")
class RAdvanceNextCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Get the count of lines in the buffer so we know when to stop
last_line = self.line_at(self.view.size())
while True:
# Move to the next line
self.view.run_command("move", {"by": "lines", "forward": True})
# Get the current cursor position in the file
caret = self.view.sel()[0].begin()
# Get the new current line number
cur_line = self.line_at(caret)
# Get the contents of the current line
content = self.view.substr(self.view.line(caret))
# If the current line is the last line, or the contents of
# the current line does not match the regex, break out now.
if cur_line == last_line or not _r_blank.match(content):
break
# Jump to the start of the line
self.view.run_command("move_to", {"to": "bol"})
# Convert a 0 based offset into the file into a 0 based line in
# the file.
def line_at(self, point):
return self.view.rowcol(point)[0]
這實現了一個名爲r_advance_next
的新命令,它將光標向下移動通過文件,跳過完全是空格或包含行註釋的行(假設我的正則表達式已達到snuff)。在地方
有了這個,您的宏應該是這樣的:
[
{"command": "repl_transfer_current", "args": {"scope": "lines"}},
{"command": "r_advance_next"}
]
此外,可以使用一鍵綁定,如以下。既然你提到RStudio使用控制+輸入,這就是我在這裏使用的。此綁定具有應用於其的上下文,以便它僅適用於當前文件是R文件時,以便在不適當時觸發它。
{ "keys": ["ctrl+enter"], "command": "run_macro_file",
"args": {"file": "Packages/User/geditlike_lineeval.sublime-macro"},
"context": [
{ "key": "selector", "operator": "equal", "operand": "source.r"}
]
}
對於BONUS加分,你可以直接從這裏介紹的插件命令,在這種情況下,你並不需要在所有使用宏內運行repl_transfer_current
命令,你可以只綁定的關鍵直接從插件的命令。在這種情況下,您可能想要以不同的方式命名類(例如RTransferAndAdvanceCommand
或某些類型),以便命令名更有意義。
是的,這是逗號。第一次使用JSON。鑑於JSON基本上沒有作爲語言的功能(從我收集的內容),我該如何編寫一個涉及「while」和「regex」的小程序 - 看起來我需要實現您的建議? –
Sublime中的插件是用Python編寫的(只是配置的東西是JSON,不包括與TextMate兼容的一些XML格式的文件)。我可以提供一個簡單的例子(目前不在計算機上)。 – OdatNurd
示例插件和用法已添加到我的答案中。 – OdatNurd