简体   繁体   English

如何在Sublime Text(Textmate)中突出显示PHP方法调用

[英]How to Highlight PHP Method Calls in Sublime Text (textmate)

I am using Sublime Text 3 as a PHP editor, I've been customizing the PHP.tmLanguage file to include more syntax scopes, right now I cannot figure out how to capture class method invocations. 我将Sublime Text 3用作PHP编辑器,我一直在自定义PHP.tmLanguage文件以包含更多语法范围,现在我不知道如何捕获类方法调用。

$html = new HTML("hr");
$html->output_ml("moo");

output_ml is currently declared as scope variable.other.property.php I would like to add a scope to pertain to specifically to class method calls, but I am having trouble defining the regex in the tmLanguage file. 目前将output_ml声明为作用域variable.other.property.php。我想添加一个范围,专门用于类方法调用,但是我在定义tmLanguage文件中的正则表达式时遇到了麻烦。

I've tried 我试过了

<dict>
    <key>match</key>
    <string>(?i)\$[a-z_][a-z0-9_]*-&gt;([a-z_][a-z_0-9]*)\s*\(</string>
    <key>name</key>
    <string>meta.method-call.php</string>
</dict>

You were pretty close, I just tweaked a few things: 您非常接近,我只是调整了几件事:

(?i:(?!\$[a-z_][a-z0-9_]*-&gt;)([a-z_][a-z_0-9]*)\s*\()

First thing I did was wrap the entire expression in the "case-insensitive" modifier, just so nothing got missed. 我要做的第一件事是将整个表达式包装在“不区分大小写”修饰符中,以至于没有遗漏任何东西。 Probably not necessary, but whatever. 可能不是必需的,但无论如何。 Second, and more importantly, I took your first group that was outside of parentheses, and made them a negative lookahead. 其次,更重要的是,我选择了第一个不在括号内的组,并使其成为负面的前瞻性。 This way, they match the class name and the arrow, but don't report it - basically saying "match whatever is ahead of us as long as we exist in front of it, but don't match us." 这样,它们匹配类名和箭头,但不报告它-基本上是说“只要存在于我们前面,就匹配我们前面的任何东西,但不匹配我们”。

Now, you'll have a scope that matches just the method's name. 现在,您将拥有一个仅与方法名称匹配的范围。

Demo 演示


Now, here's what I don't get - why you should have to do this. 现在,这是我没有得到的-为什么您必须这样做。 I maintain both a language syntax and a color scheme , so I have a lot of experience with scoping in Sublime Text and TextMate. 我同时维护语言语法配色方案 ,因此我在Sublime Text和TextMate的作用域定义方面有很多经验。 And to my knowledge, the PHP .tmLanguage already includes scopes for function calls. 据我所知,PHP .tmLanguage已经包含了函数调用的范围。 I'm not a PHP expert by any means, but I know the basics, so I made three examples of different PHP function calls: 无论如何我都不是PHP专家,但是我知道基础知识,因此我举了三个不同的PHP函数调用示例:

PHP函数调用

Green highlights a large variety of function calls in different languages. 绿色突出显示了多种不同语言的函数调用。 In the top example, baz_quux is scoped as meta.function-call.object.php , in the middle example it's meta.function-call.static.php , and in the bottom it's just meta.function-call.php . 在上面的示例中, baz_quux的范围为meta.function-call.object.php ,在中间的示例中为meta.function-call.static.php ,在底部的meta.function-call.php I don't understand where you're getting variable.other.property.php from. 我不明白您从哪里得到variable.other.property.php

However, if I take away the parentheses after the function calls above: 但是,如果我在上面的函数调用之后删除括号:

PHP函数调用,无括号

I get the following scopes, from top down: variable.other.property.php ( aha! ), constant.other.class.php , and constant.other.php . 我从上到下获得了以下范围: variable.other.property.phpaha! ), constant.other.class.phpconstant.other.php If I put the parens back, and add a space or three after the end of the function name, they are still highlighted as functions, in green. 如果我放回括号,并在函数名称的末尾添加一个或三个空格,它们仍会以绿色突出显示为函数。


So, while we had some fun with regexes today, ultimately your work has already been done for you. 因此,尽管我们今天对正则表达式感到满意,但最终您的工作已经为您完成。 If you're going to be doing more scope-hunting, I highly recommend the ScopeAlways plugin, which, like its name implies, always lists the full scope of the current cursor position in the bottom bar (you can turn it off via the Command Palette if you want). 如果您打算进行更多的范围ScopeAlways ,我强烈建议您使用ScopeAlways插件,该插件顾名思义,总是在底部栏中列出当前光标位置的全部范围(您可以通过Command将其关闭调色板(如果需要)。 If I get a request to expand my color scheme's highlighting to a new language, I just open up as much code as I can find and poke around with my mouse, looking at the different scopes, then editing my theme to correct colors as needed, or add new ones for brand-new scopes. 如果我要求将配色方案的重点突出显示为一种新的语言,则只需打开我能找到的尽可能多的代码并用鼠标四处浏览,查看不同的范围,然后根据需要编辑主题以更正颜色,或为全新的示波器添加新的示波器。 I then scan through the .tmLanguage file (after converting it to YAML ) and see if I missed anything. 然后,我浏览.tmLanguage文件( 将其转换为YAML之后 ),看是否错过了任何内容。

Good luck with your work! 祝您工作顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM