插件可以做这种事情。基本上我们想要的是覆盖 enter 的正常行为,当行的开头包含 n*= 时,* 是一个数字。为此,我们需要一个自定义的 EventListener 来实现 on_query_context 方法和一个在满足上下文时运行的自定义命令。
import re
import sublime
import sublime_plugin
class MrcScriptEventListener(sublime_plugin.EventListener):
""" A custom event listener that implements an on_query_context method which checks to see if
the start of the line if of the form n*= where * = number.
"""
def on_query_context(self, view, key, operator, operand, match_all):
current_pt = view.sel()[0].begin()
desired = view.substr(view.line(view.sel()[0].begin()))
if key != "mrc_script":
return None
if operator != sublime.OP_REGEX_MATCH:
return None
if operator == sublime.OP_REGEX_MATCH:
return re.search(operand, desired)
return None
class MrcScriptCommand(sublime_plugin.TextCommand):
""" A custom command that is executed when the context set by the MrcScript event listener
is fulfilled.
"""
def run(self, edit):
current_line = self.view.substr(self.view.line(self.view.sel()[0].begin()))
match_pattern = r"^(n\d+=)"
if re.search(match_pattern, current_line):
num = int(re.match(match_pattern, current_line).groups()[0][1:-1]) + 1
self.view.run_command("insert", {
"characters": "\nn{}=".format(num)
})
else:
return
键绑定如下:-
{
"keys": ["enter"],
"command": "mrc_script",
"context": [
{
"key": "mrc_script",
"operator": "regex_match",
"operand": "^(n\\d+=)"
}
],
}
我不会详细介绍这个插件的工作原理。完成这项工作所需要做的就是遵循gist 中给出的说明。
这是它的动图:-
警告是:-
- 它不尊重您请求中的
[ips] 部分,因为我认为这会使插件变得不必要地复杂。
- 它只查看当前行,查看
n 和= 之间的数字,并为下一行相应地增加它。所以这条线是否已经存在并不明智。
希望这符合您的要求。