【问题标题】:Sublime Text 3 does not pair quotes before semicolonSublime Text 3 不在分号前配对引号
【发布时间】:2014-09-25 13:22:26
【问题描述】:

Sublime Text 3 有引号自动配对功能,效果很好。但是,由于某种原因,当后面的字符是分号时,它会停止工作。所以,在这里输入单引号或双引号:

echo ^ 

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

echo ^;

将导致 Sublime Text 3 只输入单引号或双引号。

我在用 PHP 编程时有一个小故障,我通常先输入分号,然后我回到那一行并编写实际代码。许多 sn-ps 和宏也输入分号。所以,在这种情况下,ST3 的这种行为有点烦人。

有什么解释,为什么 Sublime Text 3 中的自动配对被限制在分号之前不起作用?而且——最重要的是——有什么办法可以解决这种有问题的行为吗?

【问题讨论】:

    标签: autocomplete sublimetext3 sublimetext


    【解决方案1】:

    默认情况下,Sublime 仅在以下字符为\t(制表符)、(空格)、)]}>、或在行尾。幸运的是,可以通过基于默认键创建自定义键绑定轻松修改此规则。打开 Preferences -> Key Bindings-User 并添加以下内容(如果文件为空,则在开头用左方括号 [ 将所有内容括起来,在末尾用 ] 关闭 [ <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 },
    

    如果您发现您想在另一个字符之前自动配对,只需在 "operand" 的分号后放置一个管道 | 并添加您想要的字符。

    我应该注意,这在 Sublime Text 2 和 3 中都可以使用。

    【讨论】:

    • @trejder 很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2023-04-02
    • 1970-01-01
    • 2015-09-09
    • 2015-05-26
    • 2016-04-12
    • 1970-01-01
    相关资源
    最近更新 更多