Sublime Text 3不会在分号前配对引号

时间:2014-09-25 13:22:27

标签: autocomplete sublimetext3 sublimetext

Sublime Text 3引用了自动对功能,完美运行。但是,出于某种原因,当后续字符是分号时,它会停止工作。所以,在这里输入单引号或双引号:

echo ^ 

将输入两个引号并将光标置于它们之间,同时在此处执行相同操作:

echo ^;

将导致Sublime Text 3仅输入打开单引号或双引号。

我在PHP中编程有一个小故障,我经常先输入分号,然后我回到那一行并编写实际代码。许多片段和宏也会以分号形式出现。因此,在这种情况下,ST3的这种行为有点烦人。

有没有任何解释,为什么Sublime Text 3中的自动配对限制在分号之前不起作用?并且 - 最重要的是 - 有没有办法解决这个有问题的行为?

1 个答案:

答案 0 :(得分:1)

默认情况下,只有当以下字符属于\t(标签),(空格),)]时,Sublime才会自动配对引号, }>或在行尾。幸运的是,可以通过基于默认键绑定创建自定义键绑定来轻松修改此规则。打开 Preferences -> Key Bindings-User 并添加以下内容(如果文件为空,请在开头围绕所有内容使用左方括号[并在末尾]结束{ {1}}):

[ <content> ]

每条规则的关键是:

// allow matched quotes before semi-colon
// double quotes
{ "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 },
        { "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\"a-zA-Z0-9_]$", "match_all": true },
        { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double", "match_all": true }
    ]
},

// single quotes
{ "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 },
        { "key": "preceding_text", "operator": "not_regex_contains", "operand": "['a-zA-Z0-9_]$", "match_all": true },
        { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single", "match_all": true }
    ]
},

// curly brackets
// parentheses and square brackets already work
{ "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 }
    ]
}

如果您发现自己想要在另一个角色之前自动配对,只需在{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$|;)", "match_all": true }, 中的分号后面添加一个|管道,然后添加所需的字符。

我应该注意,这将适用于Sublime Text 2和3。