Sublime Text - C++ 高亮 [英] Sublime Text - C++ Highlight

查看:65
本文介绍了Sublime Text - C++ 高亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让我的类作为普通的原始类型(如 int 或 double)突出显示?

How can i make my class be highlighted as a normal primitive type like the int or double?

看一个例子:

我希望在我声明 Test aloha; 测试时以与 int a 相同的方式突出显示.

I want that when i declare the Test aloha; Test be highlighted of the same way that int a.

推荐答案

语法高亮由 tmLanguage 文件控制.在 ST3 中,它们保存在 .cache 文件中.

The syntax highlighting is controlled by the tmLanguage file. In ST3 these are held in .cache files.

请按照以下步骤操作.

  • 打开工具 -> 命令托盘并使用 Package Manager 安装 PackageResourceViewer
  • 在命令托盘中选择 PackageResourceViewer: Open Resource
  • 在出现的面板中选择C++
  • 在出现的面板中选择C++.tmLanguage
  • Open Tools -> Command Pallet and using Package Manager install PackageResourceViewer
  • In the Command Pallet select PackageResourceViewer: Open Resource
  • In the presented panel choose C++
  • In the presented panel choose C++.tmLanguage

现在您已经为您显示了语言定义文件.

Now you have the language definition file displayed for you.

看看如何定义语言部分的形式.如果我们搜索 class,我们会找到以下匹配项之一:

Have a look at the form of how the parts of a language are defined. If we search for class we find the following as one of the matches:

<dict>
    <key>match</key>
    <string>\b(class|struct)\s+([_A-Za-z][_A-Za-z0-9]*\b);</string>
    <key>name</key>
    <string>meta.class-struct-block.c++</string>
    <key>captures</key>
    <dict>
        . . .
    . . . 
</dict>

我选择那个是因为它涉及到一些正则表达式 (regex) 匹配,这是做你想做的事情的提示.

I chose that one because it involves some regular expresssion (regex) matching and that's the hint to do what you want to do.

因为 ST 有一个可爱的开放框架,我们任何懂一点编程的人都可以将我们自己的语言组件定义添加到这些 .tmLanguage 文件中.这种灵活性是我选择 ST 的部分原因.

Because ST has a lovely open framework any of us who know a little programming can add our own language component definitions to these .tmLanguage files. This flexibility is part of why ST is my choice.

那么让我们看看如何做到这一点.

So let's see how to do that.

首先将我们在编辑器中打开的 C++.tmLanguage 文件保存到我们的 User 文件夹中.

First thing is to save the C++.tmLanguage file we have open in the editor into our User folder.

我们这样做是为了在出现问题时保留原始版本作为回滚,因为这意味着我们在 User 中的版本将不会每次 ST 更新时都会被覆盖.ST 加载文件的方式意味着 User 文件夹中的重复项会覆盖默认位置中的值,因此当我们使用 ST 时,我们的 User 版本优先于正在运行的内容.酷嘿?

We do this both to keep the original as a rollback if things go wrong and because it means that our version in User will not get overwritten each time ST updates. The way ST loads files means that duplicates in the User folder overwrite values in the default location so our User versions take precedence in what is running when we use ST. Cool hey?

现在我们有了自己版本的 C++.tmLanguage 文件,让我们来试试吧.

So now we have our own version of the C++.tmLanguage file let's play with it.

找到一个看起来有点像我们想要做的定义并复制它.

Find a definition that looks a bit like what we want to do and duplicate it.

我要复制这个:

<dict>
    <key>match</key>
    <string>\b(class|wchar_t|nullptr_t)\b</string>
    <key>name</key>
    <string>storage.type.c++</string>
</dict>

我要做的第一件事是决定我要为我的新语言组件命名什么.

First thing I am going to do is decide what I am going to name my new language component.

我可以简单地给它一个现有语言组件的名称,该组件具有我想要的颜色.这样做的好处是,如果该名称已经存在于现有的配色方案中,我将免费获得突出显示.随意做出这个选择.

I could simply give it the name of an existing language component that has the colouring I want to have. This has the advantage that if that name is already in place in the existing colour schemes I will get the highlighting for free. Feel free to make this choice.

但是为了探索更多的系统,我将创造自己的名字并将其范围限定在我自己的范围内.所以我称之为duncan.name.class.您可以使用任何与现有名称不冲突的名称.

But to explore more of the system I am going to invent my own name and scope it to myself. So I will call it duncan.name.class. You can use any name you like that does not clash with an existing name.

所以我将在重复中进行更改:

So I will make that change in the duplicate:

<dict>
    <key>match</key>
    <string>\b(class|wchar_t|nullptr_t)\b</string>
    <key>name</key>
    <string>duncan.name.class</string>
</dict>

现在我需要编写正则表达式来识别我的新语言组件.这很容易,因为我将假设我们都遵循相当标准的做法,即使用前导大写字母命名我们的类,而不是将其用于其他语言组件,因此我的正则表达式只需要找到任何以大写字母开头的字符串,即很容易定义为 [AZ] 后跟任何字母数字字符.请注意,这排除了标点符号(如下划线 _),因此如果您在类名中使用标点符号,则需要扩展正则表达式以包含这些字符.

Now I need to write the regex to identify my new language component. This is pretty easy since I am going to assume we are all following the fairly standard practice of naming our classes with leading Caps and not using that for other language components so my regex just needs to find any string that starts with an UPPERCASE alpha, which is easily defined as [A-Z] followed by any alphanumeric character/s. Note that this precludes punctuation (like underscore _) so if you use those in your class names you'll need to expand the regex to include those characters.

所以让我们更改匹配条件以使用该正则表达式:

So let's change the match condition to use that regex:

<dict>
    <key>match</key>
    <string>\b([A-Z][a-zA-Z0-9]+)\b</string>        
    <key>name</key>
    <string>duncan.name.class</string>
</dict>

现在我有了一个新的语言组件,我需要告诉我的主题如何处理它.我将在此示例中处理 Amy 主题.

Now I have a new language component I need to tell my theme how to handle it. I am going to work on the Amy theme for this example.

回到有用的PackageResourceViewer,就像这样:

So back to the ever helpful PackageResourceViewer like this:

  • 在命令托盘中选择 PackageResourceViewer: Open Resource
  • 在显示的面板中选择Color Scheme - Default
  • 在显示的面板中选择 Amy.tmTheme

现在您已经为您显示了主题文件.

Now you have the theme file displayed for you.

我想再次将我的副本保存到 User 文件夹中,然后复制一个现有的示例.我将复制 String 但任何吸引你的想法都很好.

Again I want to save my copy into the User folder and then duplicate an existing example. I am going to duplicate String but anything that grabs your fancy is fine.

<dict>
    <key>name</key>
    <string>String</string>
    <key>scope</key>
    <string>string</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#999999</string>
    </dict>
</dict>

现在我将更改 Stringduplicate 来为我的新语言组件着色.

Now I'll change the duplicate of String to do colouring for my new language component.

<dict>
    <key>name</key>
    <string>Class Names :: Duncan</string>
    <key>scope</key>
    <string>duncan.name.class</string>
    <key>settings</key>
    <dict>
        <key>foreground</key>
        <string>#999999</string>
        <key>background</key>
        <string>#FFFFFF</string>
        <key>fontStyle</key>
        <string>bold</string>
    </dict>
</dict>

我希望你从我们上面所做的工作中认出我使用的名字?

I hope you recognise the names I am using from the work we did above?

我选择了一组略显疯狂的语法高亮首选项,只是为了显示您可以使用的一些选项.在这里尽情发挥创意.

I have chosen a slightly mad set of syntax highlighting preferences just to show some of the options you have available. Knock yourself out playing with ideas here.

这一切有意义吗?

有了这些想法,您可以修复 ST3 中的任何语法高亮小问题,以完全按照您的喜好工作.也许我上面的正则表达式不太好?我没有想太多,所以你可能会发现你需要在那里做更多的工作才能得到你想要的.

With these ideas in place you can fix any syntax highlighting niggles you have in ST3 to work just exactly as you like. Perhaps my regex above is not too great? I didn't think about it a whole lot so you may find you need to do more work there to get just what you want.

如果其中一些没有意义,请告诉我,我会努力做得更好.

Let me know if some of this doesn't make sense and I'll try and do better.

这篇关于Sublime Text - C++ 高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆