Sublime Text and Clojure:不要配对单引号

Ste*_* Lu 15 lisp quotes clojure sublimetext sublimetext2

有没有办法获得语法类型来定义键盘快捷键,或设置键盘快捷键以依赖于语法类型(可能在"context")设置?

我引用的列表'(1 2 3)就像这样输入:'(1 2 3)'因为Sublime应用了这个有用的(但不是在这种情况下)行为.

这是Default (OSX).sublime-keymap文件的相关位

// Auto-pair 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 }
    ]
},
{ "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 }
    ]
},
Run Code Online (Sandbox Code Playgroud)

enr*_*rey 14

我做了这么简单的事:
这个:

// Singlequote for lisp
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'"}, "context":
    [
        { "key": "selector", "operator": "equal", "operand": "source.lisp"}
    ]
},
Run Code Online (Sandbox Code Playgroud)

在您的用户密钥绑定中.


提升一个档次:

在这种情况下还有一个不寻常的情况:( '|'管道代表插入符号),按下退格键后,它仍然会删除两个单引号.


为了解决这个问题,添加这个到您的用户键按键绑定:

{ "keys": ["backspace"], "command": "left_delete", "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 },
        { "key": "selector", "operator": "equal", "operand": "source.lisp"}
    ]
},
Run Code Online (Sandbox Code Playgroud)

重要的提示:

正如匹克威克在评论中所说,你当然应该改变

"source.lisp"

"source.<lisp-of-your-choice>"
或宁可
"source.<name-of-syntax-of-your-choice>"
甚至相当-ER:
"source.<name-of-scope-the-syntax-you're-using-is-using>".


ScopeAlways是一个插件,可以显示语法正确使用的作用域名称.它很可能会像"source.lisp"CL一样,"source.clojure"对于clojure,也许"source.scheme"如果你和酷孩子一起出去等等......

  • 将"source.lisp"更改为"source.clojure"后,它对我有用.谢谢 (2认同)

sku*_*oda 8

你应该能够,虽然这是一种痛苦.首先,你必须禁用内置的自动配对(不用担心,当我们完成时,其他一切的自动配对应该工作).为此,请在用户设置中进行以下设置.

"auto_match_enabled": false
Run Code Online (Sandbox Code Playgroud)

然后将以下内容添加到您的设置中.

"my_auto_match_enabled": false
Run Code Online (Sandbox Code Playgroud)

接下来,我们需要添加一组新的键绑定.我只根据你发布的内容做了一个,但我会解释我的所作所为,因为它可能并不明显.

{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'${0:$SELECTION}'"}, "context":
    [
        { "key": "setting.my_auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
        { "key": "selector", "operator": "not_equal", "operand": "source.clojure", "match_all": true }
    ]
}
Run Code Online (Sandbox Code Playgroud)

首先,请注意我将第一个上下文键切换setting.auto_match_enabledsetting.my_auto_match_enabled.接下来,我添加了最后一个限制范围的上下文条目.这样,只有当您不在source.clojure范围内时,代码段才会运行.你可能不得不修改它,因为我不知道clojure中的范围名称是什么,我只是猜到了=).您必须为所有单引号条目执行此操作.现在,因为我们禁用了内置的自动配对,我们也必须读取所有这些条目.在这些条目中,我们将再次更改setting.auto_match_enabledsetting.my_auto_match_enabled.在那之后,一切都应该有效.注意它实际上并不是必须的my_auto_match_enabled,这正是我选择的.您可以根据需要更改它.

尽管如此,我并没有完全测试所有这些,所以如果你遇到问题就要评论.

说明:

所以现在你可能会问自己,为什么我需要禁用内置的自动匹配代码?那么这就是答案.即使我们在用户设置中阻止自动完成,使用类似的范围规则,它仍然会回退到默认值,因此无论我们在用户文件夹中执行什么操作,都会插入自动配对的引号.现在你可能会想,但是如果我们修改默认设置文件会怎样.虽然我们可以这样做,再次插入相同的上下文设置,我们必须确保在任何后续更新上恢复该文件.所以我想最终取决于你.如果您编辑默认文件,请记住,如果您需要还原,则必须再次更改文件.

崇高文本3:

在ST3中,您可以执行实际修改"默认"文件.这是由于ST3中加载包的方式发生了变化.插件不是需要解压缩到Packages文件夹,而是直接从.sublime-package文件(只是重命名的zip文件)运行.此外,Packages目录中的任何文件都优先于.sublime-package文件.因此,在ST3中,您将创建Packages/Default/Default (your os here).sublime-keymap并添加上下文行.由于更新不再修改Packages目录,因此对任何后续更新都是安全的.