python - 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 插件中以编程方式修改主题(或颜色)的方法吗?
解决方案
在 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
包中。
推荐阅读
- python - 在函数中关闭 tkinter 窗口后如何执行命令?
- android - 从 OneSignal 推送通知启动对话框
- apache - Htaccess - 如何在 url 上的最后一个斜杠后删除没有查询字符串的路径
- java - 如何从 GPS 坐标中获取道路名称或街道名称?
- rest - 尝试写入页 blob 失败并显示“错误:HTTP 标头之一的值格式不正确
- linux - SQL Developer 无法在 Linux Fedora 29 上启动
- c# - 如果用户不在 Asp.Net C# 中的 VPN 上,则访问外部服务器站点
- javascript - 如何在 React.js 中获取另一个组件的元素?
- php - 如何在 Ubuntu 上使用 Valet 进行 php-debug 扩展 - 502 Bad Gateway
- neovim - 用于预编译 nvim 二进制文件的 $VIMRUNTIME 和 $VIM 变量