【问题标题】:List of colors for HighlightWords Sublime pluginHighlightWords Sublime 插件的颜色列表
【发布时间】:2018-01-25 19:41:12
【问题描述】:

我知道如何向HighlightWords 插件列表添加新颜色。但是,我无法在其中添加比默认选项更多的选项。

您能帮我在列表中添加更多颜色吗?

这是我目前拥有的:

"colors_by_scope": [
    "string",
    "entity.name.class",
    "variable.parameter",
    "invalid.deprecated",
    "invalid",
    "support.function",
    "source.json meta.structure.dictionary.json comment.line.double-slash.js ",
    "source.json meta.structure.dictionary.json meta.structure.dictionary.value.json constant.language.json "
],

【问题讨论】:

  • 您想添加特定颜色还是只知道如何在列表中查找更多现有颜色?
  • 我只是希望你给我新颜色的范围变量,这样我就可以制作更大的颜色列表。

标签: plugins sublimetext3 sublimetext syntax-highlighting highlight


【解决方案1】:

简答

供您使用的可用范围列表由您当前使用的配色方案决定,因此您需要查看配色方案文件以了解您当前可用的范围。

为此,您可以参考您的用户设置以查看 color_scheme 首选项的设置,然后使用 PackageResourceViewer 打开该文件并查看它定义的范围。

或者,我的答案底部有一个示例插件代码,它尝试加载您当前的配色方案并按名称告诉您所有唯一范围。


旁注:将您的Theme 视为您的Color Scheme 是一个常见的陷阱,但它们是不同的东西; Theme 设置 Sublime 的整体外观(例如选项卡的形状),而 Color Scheme 设置语法高亮颜色。


长答案

范围与 Sublime 中语法高亮的工作方式相关联,类似于:

  • 当前文件的语法规范使用规则根据文件内容在该类型文件中的表示将文件内容分成几部分
  • 文件的不同部分被分配一个或多个scopes 以唯一标识它们
  • 配色方案通过将颜色分配给范围来为您的代码分配颜色

例如,在 HTML 文件中,HTML 语法表明 <html> 中的 html 具有范围 entity.name.tag,但您的配色方案表明范围 entity.name.tag 应该是红色。同样,<> 具有指示它们是标点符号的范围,并且配色方案表明标点符号应该是白色的。

Sublime 使用tmTheme 格式(取自 TextMate)来指定哪些颜色与哪些范围相关联。它是 Plist 格式的 XML 格式文件。如果您使用上面提到的 PackageResourceViewer 打开您当前使用的配色方案,您将看到(除其他外)一堆看起来像这样的部分:

<dict>
    <key>name</key>
    <string>Comment</string>
    <key>scope</key>
    <string>comment</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#75715E</string>
    </dict>
</dict>

这表示对于范围comment,文本的前景色应为#75715E。也可以更改字体样式(粗体或斜体)以及背景颜色。

为了省去尝试从tmTheme 文件的XML 中挖掘正确信息的麻烦,您可以从菜单中选择Tools &gt; Developer &gt; New Plugin...,然后将您看到的存根代码替换为以下python 代码和在Sublime默认的位置保存为explore_scopes.py

import sublime
import sublime_plugin
import xml.etree.ElementTree as ET

class ExploreScopesCommand(sublime_plugin.WindowCommand):
    def run(self):
        try:
            settings = self.window.active_view().settings()
            scheme = settings.get("color_scheme")
            xml = sublime.load_resource(scheme)
            self.process_scheme(ET.fromstring(xml))
        except:
            sublime.status_message("Error loading color scheme")
            raise

    def process_scheme(self, color_scheme):
        settings = color_scheme.find("./dict/array")
        if settings is None:
            return sublime.status_message("No color scheme settings found")

        scopes = list()
        for child in settings:
            self.get_scope(scopes, child)

        new_view = self.window.new_file()
        new_view.set_scratch(True)
        new_view.set_name("Available Scopes")
        new_view.run_command("append", {"characters": "\n".join(scopes)})

    def get_scope(self, scopes, setting):
        for i in range(0, len(setting), 2):
            if setting[i].tag == "key" and setting[i].text == "scope":
                scopes.append(setting[i + 1].text)

有了它,你可以将一个键绑定到explore_scopes 命令,或者使用View &gt; Show ConsoleCtrl+` 打开 Sublime 控制台并输入以下内容:

window.run_command("explore_scopes")

这将在当前窗口中打开一个名为Available Scopes 的新选项卡,它将显示当前设置的配色方案中存在的所有范围。您看到的内容取决于您安装的 Sublime 版本以及您设置的配色方案。

正如所写,这支持从窗口中当前聚焦的任何文件(如果有)中提取颜色方案。因此,如果您为不同类型的文件设置了不同的配色方案,请在运行命令之前选择正确的文件类型。

【讨论】:

    猜你喜欢
    • 2015-06-26
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多