1

在 Sublime Text 3 中,我尝试使用相同的快捷方式更改设置的值,但使用不同的上下文。基本上,我想draw_white_space在其三个可能值之间交替设置:noneselectionall

我使用三个单独的快捷键/键盘映射很容易更改设置。这是代码(工作):

{
    "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>\Data\Packages\User\Default (Windows).sublime-keymap文件。

有任何想法吗?我做错了什么或错过了什么?

4

1 回答 1

2

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

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

于 2013-08-30T03:16:58.707 回答