首页 > 解决方案 > Sublime Text 自定义插件:更改样式、标签颜色或背景颜色

问题描述

有时,我直接在生产文件上工作(我知道它非常丑陋并且存在一些风险,但目前我别无选择)。我想要一种方法来轻松识别我正在处理生产文件。我可以处理 file_name 因为生产文件位于生产文件夹(或等效文件夹)中。所以我开始了一个 Sublime Text 插件来将标签背景或代码背景更改为另一种颜色。

我可以显示样式信息,但我不知道如何更改此样式...

早期插件:

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 Text 插件中以编程方式修改主题(或颜色)的方法吗?

标签: pythonidesublimetext3

解决方案


在 Sublime 中,文件选项卡的颜色始终遵循文件背景的颜色,唯一可以改变的是color_scheme设置。

特别是,即使 API 允许您查看用于特定样式的颜色,例如您在问题中指出的,API 函数也没有直接的类似物来直接更改其中一种样式。

然后,一般策略是响应文件是生产文件的信息,方法是将该文件的color_scheme设置更改为其他内容以应用您想要的颜色更改。

这可以通过您在问题中概述的命令手动完成,或者您可以使用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对象,该对象继承默认设置,但也允许您提供每个视图的设置。在这里,插件应用一个color_scheme设置,当它检测到文件包含生产路径段时覆盖继承的版本,如果Save As文件名称不再是生产路径,则删除该自定义设置(恢复到继承的版本)。

剩下的难题是如何确定要在此处使用的配色方案。Monokai.sublime-color-scheme对于上面的示例,我手动创建了Sublime 附带的副本,并修改了background属性以稍微改变显示的颜色。

或者,您可以选择其他一些配色方案作为生产的配色方案,甚至可以sublime-color-scheme即时生成。

在这种情况下,您可能希望使用sublime.load_resource()并将sublime.decode_value()a 加载和解码sublime-color-scheme为 JSON 对象,然后操作颜色并将文件作为新文件保存sublime-color-scheme到您的User包中。


推荐阅读