使用KeymapManager管理Sublime Text2插件的快捷键

1879次阅读  |  发布于5年以前

使用Sublime Text2编辑器的同学应该都知道,几乎所有的功能都可以通过快捷键进行。系统默认的快捷键可以通过Preferences -> Key Bindings - Default查看。但对于插件的快捷键查看就没这么简单了,有些插件将一些信息写到Sublime-Menu下还可以方便查看,但很多插件都没写Sublime Menu的信息,这个时候如果忘记了快捷键,那只能找到对应的插件目录,打开对应的文件查看了。

为了解决这个问题,让所有安装的插件可以很方便的管理,开发了KeymapManager这个插件。

安装KeymapManager

可以通过Packages Control进行安装,搜索keymapmanager即可。

如果你还没有安装Packages Control,建议先安装Packages Control在安装KeymapManager。或者你也可以从https://github.com/welefen/keymapmanager下载安装。

使用方式

通过ctrl + alt + k 或者通过顶部菜单 View -> Keymap Manager打开Pannel。如:

这样就可以很快搜索到你需要使用的插件了,并且enter就执行那个插件的命令。

源代码简单分析

import sublime, sublime_plugin
    import os
    import json
    settings = sublime.load_settings("KeymapManager.sublime-settings")
    class KeymapManagerCommand(sublime_plugin.TextCommand):
        """
        keymap manager for plugins
        """
        #add some default very usefull commands
        defaultCommand = [
            {"name": "Goto Anything...", "keys": ["ctrl+p"], "command": "show_overlay", "args": {"overlay": "goto", "show_files": True} },
            {"name": "Command Palette", "keys": ["ctrl+shift+p"], "command": "show_overlay", "args": {"overlay": "command_palette"} },
            {"name": "Goto Symbol...", "keys": ["ctrl+r"], "command": "show_overlay", "args": {"overlay": "goto", "text": "@"} },
            {"name": "Goto Line...",  "keys": ["ctrl+g"], "command": "show_overlay", "args": {"overlay": "goto", "text": ":"} },
            {"name": "Search Keywords", "keys": ["ctrl+;"], "command": "show_overlay", "args": {"overlay": "goto", "text": "#"} },
            {"name": "Show Console",  "keys": ["ctrl+`"],  "command": "show_panel", "args": {"panel": "console", "toggle": True} }
        ]
        #installed plugins list
        plugins = None
        def run(self, edit):
            self.defaultCommand.sort(key=lambda x: x["name"].lower())
            if self.plugins == None:
                self.plugins = []
            path = sublime.packages_path()
            dirs = os.listdir(path)
            #sort with insensitive
            dirs.sort(key=lambda x: x.lower())
            plugins = []
            ignored_packages = settings.get("ignored_packages")
            for name in dirs:
                if name in ignored_packages:
                    continue
                dir = path + '/' + name + '/'
                if not os.path.isdir(dir):
                    continue
                platform = sublime.platform()
                platform = platform[0].upper() + platform[1:].lower()
                keymapFile = dir + "Default (" + platform + ").sublime-keymap"
                if not os.path.isfile(keymapFile):
                    continue
                #plugins.append(keymapFile)
                with open(keymapFile) as f:
                    content = open(keymapFile).read()
                try:
                    jsonData = json.loads(content)
                except (ValueError):
                    continue
                if not isinstance(jsonData, list):
                    continue
                i = 0
                for item in jsonData:
                    #only show 3 items if num max than 3
                    if i >= 3:
                        break
                    if "keys" not in item or "command" not in item:
                        continue
                    keys = item["keys"]
                    if isinstance(keys, list):
                        keys = ", " . join(keys)
                    command = item["command"]
                    item["name"] = name
                    plugins.append([name, command + " : " +  keys])
                    self.plugins.append(item)
                    i += 1
            for item in self.defaultCommand:
                plugins.append([item['name'], item['command'] + " : " +  ",".join(item['keys'])])
                self.plugins.append(item)
            self.view.window().show_quick_panel(plugins, self.panel_done)
        #panel done
        def panel_done(self, picked):
            if picked == -1:
                return
            item = self.plugins[picked]
            if self.checkContext(item) == False:
                return
            args = {}
            if "args" in item:
                args = item['args']
            #thanks wuliang
            self.view.run_command(item['command'], args)
            self.view.window().run_command(item['command'], args)
            sublime.run_command(item['command'], args)
        #check context condition
        def checkContext(self, plugin):
            return True
            if "context" not in plugin:
                return True
            if "window" in plugin and plugin["window"]:
                return True
            context = plugin["context"]
            name = plugin["name"]
            path = path = sublime.packages_path() + '/' + name + '/'
            import glob
            pyFiles = glob.glob("*.py")
            sublime.status_message(",".join(pyFiles))
            return True

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8