我无法让它工作:在崇高文本 (3) 中,我尝试使用相同的快捷方式/键盘映射和上下文替换设置的值



在Sublime Text 3中,我试图使用相同的快捷方式,但使用不同的上下文来替换设置的值。基本上,我想在noneselectionall这三个可能的值之间替换draw_white_space设置。

我可以通过三个单独的快捷键/关键点映射轻松地更改设置。这是代码(工作):

{
    "keys": ["ctrl+e", "ctrl+w"],
    "command": "set_setting",
    "args": {
        "setting": "draw_white_space",
        "value": "all",
    }
},
{
    "keys": ["ctrl+e", "ctrl+q"],
    "command": "set_setting",
    "args": {
        "setting": "draw_white_space",
        "value": "none",
    }
},
{
    "keys": ["ctrl+e", "ctrl+s"],
    "command": "set_setting",
    "args": {
        "setting": "draw_white_space",
        "value": "selection",
    }
}

但是,我真正想要的是能够按下["ctrl+e", "ctrl+w"],并让它在每个可能的值之间交替。是的,这是我习惯的Visual Studio快捷方式!

我创造了在我看来应该有效的东西,但事实并非如此。至少不是我想要的。这是代码(坏了):

{
    "keys": ["ctrl+e", "ctrl+w"],
    "command": "set_setting",
    "args": {
        "setting": "draw_white_space",
        "value": "none",
    },
    "context": [
        { "key": "setting.draw_white_space", 
          "operator": "equal", "operand": "all" }
    ]
},
{
    "keys": ["ctrl+e", "ctrl+w"],
    "command": "set_setting",
    "args": {
        "setting": "draw_white_space",
        "value": "selection",
    },
    "context": [
        { "key": "setting.draw_white_space",
          "operator": "equal", "operand": "none" }
    ]
},
{
    "keys": ["ctrl+e", "ctrl+w"],
    "command": "set_setting",
    "args": {
        "setting": "draw_white_space",
        "value": "all",
    },
    "context": [
        { "key": "setting.draw_white_space",
          "operator": "equal", "operand": "selection" }
    ]
}

我已经测试了我的上下文,所以我知道它们是有效的。例如,我可以在设置文件中手动设置all,然后检查all的快捷方式将仅在第一次工作。在那之后,它和其他人都不会工作。

我注意到的另一件事是,当快捷方式(包括三个单独的快捷方式)工作时,我的设置文件不会更改draw_white_space值。我认为这可能是默认行为——设置更改可能是每个会话——这也没关系。但是,我完全删除了设置,它仍然是原来的行为。

我正在更改通过Preferences|Key Bindings - User菜单打开的文件,该菜单打开了<Sublime Text>DataPackagesUserDefault (Windows).sublime-keymap文件。

有什么想法吗?我是做错了什么还是错过了什么?

也许不是你想要的,但你可以用一个非常简单的插件来获得这种行为。

import sublime
import sublime_plugin
class CycleDrawWhiteSpaceCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        white_space_type = view.settings().get("draw_white_space")
        if white_space_type == "all":
            view.settings().set("draw_white_space", "none")
        elif white_space_type == "none":
            view.settings().set("draw_white_space", "selection")
        else:
            view.settings().set("draw_white_space", "all")

保存插件后,将密钥绑定绑定到cycle_draw_white_space

最新更新