您链接到的解决方案应该在 Sublime 2 和 3 中都可以使用(假设提供 repl_transfer_current 命令的插件适用于两者),但由于宏格式不正确而无法正常工作。
[编辑]
Sublime 原生提供的move 命令不采用您的宏正在使用的参数。如果这对您有用,大概也是某些软件包提供的东西。如果是这样,您可能需要相应地调整下面的示例代码。
[/编辑]
就目前而言,问题在于 Sublime 中的几乎(但不是全部)配置文件都是 JSON 格式(稍微放宽以允许 cmets),并且上面概述的宏代码和链接的解决方案不是有效的 JSON因为其中的第一个和第二个命令没有用逗号分隔。
类似下面的东西应该可以工作:
[
{"command": "repl_transfer_current", "args": {"scope": "lines"}},
{"command": "move", "args": {"mode": "lines", "amount": 1}}
]
我认为您上面链接到的 Sublime 文档适用于 Sublime 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 的新命令,该命令在文件中向下移动光标,跳过完全为空白或包含行注释的行(假设我的正则表达式符合标准)。
有了这个,您的宏将如下所示:
[
{"command": "repl_transfer_current", "args": {"scope": "lines"}},
{"command": "r_advance_next"}
]
此外,您可以使用如下键绑定。由于您提到使用 Control+Enter 的 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 或类似名称),以便命令名称更有意义。