开发工具 怎样理解 SublimeText 关于括号的配置?

jiyinyiyong · 2012年11月14日 · 最后由 jiyinyiyong 回复于 2012年11月14日 · 4178 次阅读

想交换圆括号和方括号的。打开配置看了半天看不懂.. 哪位大侠给讲解一下吗?

// Auto-pair square brackets
{ "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": "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 }
  ]
},
{ "keys": ["]"], "command": "move", "args": {"by": "characters", "forward": true}, "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": "^\\]", "match_all": true }
  ]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "Packages/Default/Delete Left Right.sublime-macro"}, "context":
  [
    { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
    { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
    { "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true },
    { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
  ]
}

没太看明白,你想做什么?

{ "keys": ["["], "command": "insert_snippet", "args": {"contents": "[$0]"}, "context":

那这个讲吧... 当下面 3 个条件满足时 按下"["的时候插入 snippet "[$0]"

{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },

是说 setting.auto_match_enabled 这个要设定为 true

{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },

当前没有任何选中 (selection_empty)

{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true }

光标后面的内容要配到这个 regex

卤煮是想说按方括号的时候实际出圆括号?我不知道 sublime 能不能直接把一个键 map 到另一个 好像 key setting 里都是去触发 command 的

这里是文档 http://docs.sublimetext.info/en/latest/reference/key_bindings.html#

#2 楼 @leozwa 这么一说瞬间被大败了... Sublime 应该直接用 JS 代码写配置的 像楼上的解释的,下面的 JSON:

{ "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 }
  ]
}

实际上意思是,如果用伪代码表示结构才很清晰..

view.on '[', do
  if setting.auto_match_enabled == true
    if selection_empty == true and match_all
      if following_text ~= "^(?:\t| |\\)|]|;|\\}|$)" and match_all
        insert_snippet $0

这么想配置为什么有这么多的重复的 key 就明白了.. 我再看看

#1 楼 @chenge 想做两件事情,一是按下 [ 的时候代码里用 ), 相当于调换 注意是要写在 Package 的配置里面,而不能直接写在 Default 和 User 的配置里 第二件上边没写,, 我想在 Package 里干掉单引号的自动补全

需要 登录 后方可回复, 如果你还没有账号请 注册新账号