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

时间:2013-04-06 00:12:30

标签: clojure lisp sublimetext2 quotes sublimetext

有没有办法获取语法类型来定义键盘快捷键,或者设置键盘快捷键以取决于语法类型(可能在"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 }
    ]
},

2 个答案:

答案 0 :(得分:14)

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

// Singlequote for lisp
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'"}, "context":
    [
        { "key": "selector", "operator": "equal", "operand": "source.lisp"}
    ]
},
用户密钥绑定中的


提升一步:

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


要解决此问题,请将添加到用户密钥键绑定

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

重要提示:

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

"source.lisp"

"source.<lisp-of-your-choice>"
或者更确切地说 "source.<name-of-syntax-of-your-choice>"
甚至更确切地说:
"source.<name-of-scope-the-syntax-you're-using-is-using>"


ScopeAlways 是一个插件,可以显示您的语法正在使用的范围的正确名称。对于CL来说很可能会是"source.lisp",对于clojure来说可能是"source.clojure",如果你和那些很酷的孩子一起出去玩,可能会"source.scheme"等......

答案 1 :(得分:8)

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

"auto_match_enabled": false

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

"my_auto_match_enabled": false

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

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

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

说了这么多,我没有完全测试所有这些,所以如果你遇到问题请评论。

<强>解释

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

Sublime Text 3:

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