【问题标题】:Sublime Text custom plugin : change style, tab color or back-ground colorSublime Text 自定义插件:更改样式、标签颜色或背景颜色
【发布时间】:2018-06-26 11:57:50
【问题描述】:

有时,我直接在生产文件上工作(我知道它非常丑陋并且存在一些风险,但目前我别无选择)。我想要一种方法来轻松识别我正在处理生产文件。我可以处理 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 插件中以编程方式修改主题(或颜色)的方法吗?

【问题讨论】:

    标签: python ide sublimetext3


    【解决方案1】:

    在 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 文件的名称删除,则删除该自定义设置(恢复到继承的版本)更长的生产路径。

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

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

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

    【讨论】:

    • 非常感谢您的精彩回答...非常详细!完美的 !我借此机会感谢您的 overrideAudit !太棒了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    相关资源
    最近更新 更多