简体   繁体   English

如何在空行上下文中运行sublime-text命令?

[英]How to run a sublime-text command in an empty line context?

I'd like to open a "sbcl" repl when I type 'repl' on an empty line in a lisp file. 当我在Lisp文件的空白行中键入'repl'时,我想打开一个“ sbcl” repl。

I've got this keybinding so far, 到目前为止,我已经完成了这些绑定,

[
    {
        "keys": ["r", "e", "p", "l"],
        "command": "run_existing_window_command",
        "args": {
            "id": "repl_sbcl",
            "file": "config/CommonLisp/Main.sublime-menu"
        },
        "context": [
            { "key": "selector", "operator": "equal", "operand": "source.lisp"},
        ]
    }
]

It works fine, except that it triggers in cases where I don't want it to trigger, ie, within code 它工作正常,除了它在我不想触发的情况下(即在代码内)触发

(defun (bla) repl)

Or within comments 或在评论中

; repl

I only want it to trigger on empty lines, denoted with {HERE} (example code taken from the 'practical common lisp' book) 我只希望它在用{HERE}表示的空行上触发(示例代码摘自“实践通用Lisp”书)

(defun dump-db ()
  (progn
    (format t "~%") ; print a newline, prettier in a repl
    (dolist (cd *songs*)
      (format t "~{~a:~10t~a~%~}~%" cd))))
{HERE}
(add-record
  (make-cd "Butterflies (On Luci's Way) - Demo" "*Shels" 8 nil))
{HERE}

How can I express this empty line constraint using Sublime's context ? 如何使用Sublime的context来表达此空行约束?

Edit: despite being the one asking this questions, I figured out that this is a really bad idea . 编辑:尽管有人问这个问题,但我发现这是一个非常糟糕的主意 Using bindings like "keys": ["r", "e", "p", "l"] will mess with your undo history. 使用诸如"keys": ["r", "e", "p", "l"]绑定"keys": ["r", "e", "p", "l"]会破坏您的撤消历史记录。 The interaction between Sublime's keybinding listeners and the history is that if add a handle like this, every single letter you type will be added to the undo history Very, very bad . Sublime的绑定监听器和历史记录之间的相互作用是,如果添加这样的句柄,则您键入的每个字母都会被添加到撤消历史记录中 ,非常糟糕 Ctrl-z becomes a keyboard-mash-fest. Ctrl-z变成键盘mash-fest。 :( :(

Just add a context to check that the preceding text on the line is empty (or consists only of rep or repl , depending whether the keys you press are added to the document or not before the keybinding operates - I'm not at a computer to check right now) and a context to check that the following text on the line is empty: 只需添加上下文以检查该行上的前面的文本是否为空(或仅由reprepl ,具体取决于您在进行绑定操作之前是否将按下的键添加到了文档中-我不在计算机上立即检查)和上下文来检查该行中的以下文本是否为空:

{ "key": "preceding_text", "operator": "regex_match", "operand": "^$|^repl?$", "match_all": true },
{ "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true },

You can take a look at the default keybindings for more examples of using the preceding_text and following_text contexts. 你可以看看使用的多个实例的默认键绑定preceding_textfollowing_text上下文。 There are also some more details on the unofficial docs . 有关非官方文档的更多详细信息。

To exclude the key binding from operating in multi-line comments, you could change your selector to source.lisp - comment . 要从多行注释中排除键绑定,可以将选择器更改为source.lisp - comment

Based on the help from https://stackoverflow.com/a/45875699/2302759 , I came up with this configuration: 基于https://stackoverflow.com/a/45875699/2302759的帮助,我想到了以下配置:

// opens an sbcl repl when you type 'repl' in a lisp context
{
    "keys": ["r", "e", "p", "l"],
    "command": "run_existing_window_command",
    "args": {
        "id": "repl_sbcl",
        "file": "config/CommonLisp/Main.sublime-menu"
    },
    "context": [
        { "key": "following_text", "operator": "regex_match", "operand": "^$", "match_all": true },
        { "key": "selector", "operator": "equal", "operand": "source.lisp"},
        { "key": "preceding_text", "operator": "regex_match", "operand": "^[repl]*", "match_all": true }
    ]
}

The first context matches end of line, the second context matches .lisp files only, lastly, the third context matches that there is nothing before the line except for potentially lingering 'repl' characters 第一个上下文匹配行尾,第二个上下文仅匹配.lisp文件,最后,第三个上下文匹配该行之前没有任何内容,除了潜在的'repl'字符

The configuration matches the test-case perfectly now :) 现在,配置与测试用例完美匹配:)

(defun dump-db ()
  (progn
    (format t "~%") ; print a newline, prettier in a repl
    (dolist (cd *songs*)
      (format t "~{~a:~10t~a~%~}~%" cd))))
{HERE}
(add-record
  (make-cd "Butterflies (On Luci's Way) - Demo" "*Shels" 8 nil))
{HERE}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM