代码之家  ›  专栏  ›  技术社区  ›  Samuel Dauzon

Sublime文本自定义插件:更改样式、选项卡颜色或背景色

  •  0
  • Samuel Dauzon  · 技术社区  · 7 年前

    有时,我会直接处理生产文件(我知道它很难看,而且会带来一些风险,但目前,我别无选择)。我想要一种容易识别我在制作文件。我可以处理文件名,因为生产文件在生产文件夹(或等效文件夹)中。因此,我开始了一个崇高的文本插件,以改变标签背景或代码背景的另一种颜色。

    我可以显示样式信息,但我不知道如何改变这种样式…

    早期插件:

    import sublime, sublime_plugin
    class TestStCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            if "production" in str(self.view.file_name()):
                print("===== self.view.style() =====")
                print(self.view.style())
    

    插件输出:

    ===== self.view.style() =====
    {'active_guide': '#7a4815', 'find_highlight': '#ffe894', 'inactive_selection_foreground': '#f8f8f2', 'background': '#282923', 'selection_foreground': '#f8f8f2', 'highlight': '#c4c4bd', 'selection': '#48473d', 'rulers': '#5c5c56', 'selection_border': '#212117', 'shadow': '#141411', 'accent': '#67d8ef', 'misspelling': '#f92472', 'gutter': '#282923', 'guide': '#474842', 'stack_guide': '#7a4815', 'line_highlight': '#3e3d32', 'foreground': '#f8f8f2', 'gutter_foreground': '#90908a', 'minimap_border': '#90908a', 'caret': '#f8f8f1', 'invisibles': '#70716b', 'inactive_selection': '#383830', 'find_highlight_foreground': '#000000'}
    

    你能给我一个在Sublime文本插件中编程修改主题(或颜色)的方法吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   OdatNurd    7 年前

    在Sublime中,文件标签的颜色总是跟在文件背景的颜色后面,唯一可以改变的是 color_scheme 设置。

    特别是,即使api允许您查看正在用于特定样式(如您在问题中指出的样式)的颜色,但是api函数没有直接的模拟来直接更改这些样式之一。

    一般策略是通过更改 配色方案 将该文件设置为其他文件以应用所需的颜色更改。

    这可以通过您在问题中概述的命令手动完成,也可以使用 EventListener 监视文件事件,以便为您执行检查,以便无缝地更改颜色,或进行某些组合。

    这种插件的一个例子是:

    import sublime
    import sublime_plugin
    
    # A global list of potential path fragments that indicate that a file is a
    # production file.
    _prod_paths = ["/production/", "/prod/"]
    
    # The color scheme to apply to files that are production files.
    #
    # If the color scheme you use is a `tmTheme` format, the value here needs to
    # be a full package resource path. For sublime-color-scheme, only the name of
    # the file should be used.
    _prod_scheme = "Monokai-Production.sublime-color-scheme"
    # _prod_scheme = "Packages/Color Scheme - Legacy/Blackboard.tmTheme"
    
    class ProductionEventListener(sublime_plugin.EventListener):
        """
        Listen for files to be loaded or saved and alter their color scheme if
        any of the _production_path path fragments appear in their paths.
        """
        def alter_color_scheme(self, view):
            if view.file_name():
                # Get the current color scheme and an indication if this file
                # contains a production path fragment,.
                scheme = view.settings().get("color_scheme")
                is_prod = any(p for p in _prod_paths if p in view.file_name())
    
                # If this file is a production file and the color scheme is not the
                # production scheme, change it now.
                if is_prod and scheme != _prod_scheme:
                    view.settings().set("color_scheme", _prod_scheme)
    
                # If the file is not production but has the production color scheme
                # remove our custom setting; this can happen if the path has
                # changed, for example.
                if not is_prod and scheme == _prod_scheme:
                    view.settings().erase("color_scheme")
    
        # Use our method to handle file loads and saves
        on_load = on_post_save = alter_color_scheme
    

    每个视图都有自己的本地视图 settings 对象,该对象继承默认设置,但也允许您提供每视图设置。在这里插件应用 配色方案 当检测到文件包含生产路径段时重写继承版本的设置,如果您 Save As 文件名不再是生产路径。

    剩下的难题是如何确定要在这里使用什么颜色方案。在上面的例子中,我手动创建了 Monokai.sublime-color-scheme 带着崇高和改良 background 属性更改显示的颜色。

    或者,您可以选择其他颜色方案作为生产方案,或者甚至生成 sublime-color-scheme 在飞行中。

    在这种情况下,您可能需要使用 sublime.load_resource() sublime.decode_value() 加载并解码 高级配色方案 进入一个json对象,然后操作颜色并将文件另存为一个新的 高级配色方案 进入你的 User 包裹。