我有一个Sublime插件,但我想在用户输入}
或;
我试图在on_modified
事件中处理它,但我无法让它发挥作用。
这是最好的方法吗?
答案 0 :(得分:1)
添加与您的插件捆绑在一起的新键绑定。例如,以下是触发大括号自动配对的默认键绑定:
{ "keys": ["{"], "command": "insert_snippet", "args": {"contents": "{$0}"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|$)", "match_all": true }
]
},
{ "keys": ["{"], "command": "wrap_block", "args": {"begin": "{", "end": "}"}, "context":
[
{ "key": "indented_block", "match_all": true },
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true },
]
},
{ "keys": ["{"], "command": "insert_snippet", "args": {"contents": "{${0:$SELECTION}}"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
},
请注意,通过使用单个字符作为键绑定,您将拦截按键并且它不会显示在缓冲区中。如果您希望将字符发送到缓冲区,则需要将其作为命令的一部分放在那里。
您可以在示例中看到。两个insert_snippet
命令用花括号包装任何参数:"contents": "{$0}"
,wrap_block
命令将花括号作为开头和结尾args发送:"args": {"begin": "{", "end": "}"}