如何在不编辑的情况下向 Sublime Text 中的语法定义添加功能? [英] How do I add features to a syntax definition in Sublime Text without editing it?

查看:35
本文介绍了如何在不编辑的情况下向 Sublime Text 中的语法定义添加功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想在我的 Python 文档字符串中突出显示 @param@type@return(即 epytext 声明).我已经想出了如何通过简单地编辑 Python/Python.tmLanguage 来做到这一点;但是,出于以下几个原因,我真的很想将其放入自己的文件中:

For example, I would like to highlight @param, @type, and @return (i.e. epytext declarations) in my Python docstrings. I have figured out how to do this by simply editing Python/Python.tmLanguage; however, I would really like to put this in its own file, for a couple of reasons:

  1. 我不总是想应用这种突出显示.它仅适用于使用 epydoc 作为其文档工具的项目;在其他项目中,我想突出显示 reStructuredText.
  2. 当 Sublime Text 更新时,我希望有自己的语法作为包含功能的叠加.Sublime 的作者可能会选择在未来版本中包含 Python 的一些功能,我不想每次更新修改基本 Python 语法定义时都手动找出我更改的内容.
  3. 我在版本控制中保留我自己的编辑器配置,但我希望这是我自己的原创;我不想承担必须携带 Sublime 附带的语法定义的副本(可能是专有的?我不知道,它似乎没有独立许可)的负担.
  1. I don't always want to apply this highlighting. It's only for projects that use epydoc as their documentation tool; in other projects, I would want to highlight reStructuredText instead.
  2. When Sublime Text updates, I'd like to have my own syntax as an overlay onto the included features. there are several features of Python that Sublime's author may choose to include in future versions and I don't want to have to manually figure out what I changed each time an update modifies the base Python syntax definition.
  3. I keep my own editor configuration in version control, but I want that to be my own original stuff; I don't want to be burdened with having to carry copies (possibly proprietary? I don't know, it doesn't seem to have an independent license) of syntax definitions that come with Sublime.

有没有办法把高亮规则放到一个单独的文件中,也许只适用于特定的范围?

Is there a way to put highlighting rules into a separate file, perhaps to only be applied to a particular scope?

推荐答案

在我看来,您想创建自己的语法文件,从 Python 语法文件 (source.python) 继承,并且在那里进行更改和自定义.Sublime Text 2 使用 .tmLanguage 格式的语法文件,有点复杂,不过好在大部分工作已经为你完成了,只需继承source.python即可.

Sounds to me like you want to create your own syntax file, inherit from the Python syntax file (source.python), and make your changes and customizations there. Sublime Text 2 uses the .tmLanguage format for syntax files, which is a bit complex, but fortunately most of the work has already been done for you, just by inheriting from source.python.

您要做的是设置一个正则表达式模式来匹配您要突出显示的子字符串,并为该模式命名;类似于 punctuation.definition.comment.epydoc,遵循 Java/JavaDoc.tmLanguage 中设置的约定.然后,检查您的配色方案文件(以 .tmTheme 结尾)并确保为您选择的范围或其中一个父级设置了设置,因为 ST2 应该使用正确的范围规则对于这种事情.

What you want to do is set up a regex pattern to match the substrings that you want to highlight, and give that pattern a name; something like punctuation.definition.comment.epydoc, to follow the convention set in Java/JavaDoc.tmLanguage. Then, check your color scheme file (ending in .tmTheme) and make sure there are settings for the scope that you've chosen -- or for one of its parents, since ST2 should use proper scoping rules for this sort of thing.

要使用您创建的语法文件,请将其粘贴在 Packages/User(以便 ST2 不会在升级时覆盖它),在编辑器中打开一个 Python 文件,然后选择窗口右下角下拉列表中的新语法.如果您已正确设置所有内容,则 epydoc 字符串应更改为您在配色方案中设置的任何颜色.

To use the syntax file you've created, stick it in Packages/User (so that ST2 doesn't overwrite it on upgrades), open a Python file in the editor, and then select your new syntax from the dropdown list in the bottom right corner of the window. If you've set up everything correctly, the epydoc strings should change to be whatever color you've set in your color scheme.

祝你好运!当你有一个有效的语法文件时,在某处张贴一个链接,这样其他人也可以看到和使用它!

Good luck! And post a link somewhere when you've got a syntax file that works, so others can see and use it, as well!

这篇关于如何在不编辑的情况下向 Sublime Text 中的语法定义添加功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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