为 Sublime 2/3 中的语言更改/添加语法高亮 [英] Change / Add syntax highlighting for a language in Sublime 2/3

查看:43
本文介绍了为 Sublime 2/3 中的语言更改/添加语法高亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 Sublime 2/3 中的语言更改/添加语法突出显示.

例如,我希望关键字 this 在 JavaScript 中着色.

我该怎么做?

我知道在 C:\Program Files\Sublime Text 3\Packages 中有一个偏好 JavaScript 文件,但我不知道要更改什么,或者我是否必须创建一个新的 JavaScript此文件夹中某处的首选项文件 %APPDATA%\Sublime Text 3.

解决方案

语法高亮由您使用的主题控制,可通过 Preferences ->配色方案.主题通过使用范围突出显示不同的关键字、函数、变量等,范围由包含在语言目录/包中的 .tmLanguage 文件中的一系列正则表达式定义.例如,JavaScript.tmLanguage 文件将范围 source.jsvariable.language.js 分配给 this 关键字.由于 Sublime Text 3 使用 .sublime-package zip 文件格式来存储所有默认设置,因此编辑单个文件并不是很简单.

不幸的是,并非所有主题都包含所有范围,因此您需要尝试不同的范围以找到一个看起来不错的主题,并为您提供所需的突出显示.Sublime Text 包含许多主题,还有更多主题可以通过 语言此图像的定义,而不是 Sublime 附带的图像.它可以通过包控制安装.

更新

最近我发现了另一种 JavaScript 替换语言定义 - 通过包控制.它允许您无缝地查看、编辑和/或提取部分或整个 .sublime-package 包.因此,如果您选择,您可以直接编辑 Sublime 中包含的配色方案.

另一个更新

随着 Github 上几乎所有默认包的发布,变化来得又快又猛.旧的 JS 语法已完全重写,以包含 JavaScript Next ES6 语法的最佳部分,现在已尽可能完全兼容 ES6.大量 其他更改已用于涵盖角落和边缘情况,提高一致性,并且总体上使其更好.新语法已包含在(此时)最新的 dev build 3111 中.

如果您想在当前的 beta build 3103 中使用任何新语法,只需克隆Github repo 某个地方并将 JavaScript(或您想要的任何语言)链接到您的 Packages 目录 - 通过选择 Preferences 在您的系统上找到它->浏览包....然后,只需不时在原repo目录中做一个git pull来刷新任何更改,您就可以享受最新最好的!我应该注意到,repo 使用了新的 .sublime-syntax 格式而不是旧的 .tmLanguage 格式,因此它们不适用于 3084 之前的 ST3 版本或 ST2(在这两种情况下,您都应该升级到最新的测试版或开发版)无论如何).

我目前正在调整我的 Neon 配色方案以处理新 JS 语法中的所有新范围,但大多数应该已经涵盖.

I want to change / add syntax highlighting for a language in Sublime 2/3.

For example I want the keyword this colored in JavaScript.

How can I do that?

I know that there is a preference JavaScript File in C:\Program Files\Sublime Text 3\Packages, but I don't know what to change or if I have to create a new JavaScript Preference File somewhere in this folder %APPDATA%\Sublime Text 3.

解决方案

Syntax highlighting is controlled by the theme you use, accessible through Preferences -> Color Scheme. Themes highlight different keywords, functions, variables, etc. through the use of scopes, which are defined by a series of regular expressions contained in a .tmLanguage file in a language's directory/package. For example, the JavaScript.tmLanguage file assigns the scopes source.js and variable.language.js to the this keyword. Since Sublime Text 3 is using the .sublime-package zip file format to store all the default settings it's not very straightforward to edit the individual files.

Unfortunately, not all themes contain all scopes, so you'll need to play around with different ones to find one that looks good, and gives you the highlighting you're looking for. There are a number of themes that are included with Sublime Text, and many more are available through Package Control, which I highly recommend installing if you haven't already. Make sure you follow the ST3 directions.

As it so happens, I've developed the Neon Color Scheme, available through Package Control, that you might want to take a look at. My main goal, besides trying to make a broad range of languages look as good as possible, was to identify as many different scopes as I could - many more than are included in the standard themes. While the JavaScript language definition isn't as thorough as Python's, for example, Neon still has a lot more diversity than some of the defaults like Monokai or Solarized.

I should note that I used @int3h's Better JavaScript language definition for this image instead of the one that ships with Sublime. It can be installed via Package Control.

UPDATE

Of late I've discovered another JavaScript replacement language definition - JavaScriptNext - ES6 Syntax. It has more scopes than the base JavaScript or even Better JavaScript. It looks like this on the same code:

Also, since I originally wrote this answer, @skuroda has released PackageResourceViewer via Package Control. It allows you to seamlessly view, edit and/or extract parts of or entire .sublime-package packages. So, if you choose, you can directly edit the color schemes included with Sublime.

ANOTHER UPDATE

With the release of nearly all of the default packages on Github, changes have been coming fast and furiously. The old JS syntax has been completely rewritten to include the best parts of JavaScript Next ES6 Syntax, and now is as fully ES6-compatible as can be. A ton of other changes have been made to cover corner and edge cases, improve consistency, and just overall make it better. The new syntax has been included in the (at this time) latest dev build 3111.

If you'd like to use any of the new syntaxes with the current beta build 3103, simply clone the Github repo someplace and link the JavaScript (or whatever language(s) you want) into your Packages directory - find it on your system by selecting Preferences -> Browse Packages.... Then, simply do a git pull in the original repo directory from time to time to refresh any changes, and you can enjoy the latest and greatest! I should note that the repo uses the new .sublime-syntax format instead of the old .tmLanguage one, so they will not work with ST3 builds prior to 3084, or with ST2 (in both cases, you should have upgraded to the latest beta or dev build anyway).

I'm currently tweaking my Neon Color Scheme to handle all of the new scopes in the new JS syntax, but most should be covered already.

这篇关于为 Sublime 2/3 中的语言更改/添加语法高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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