PyCharm:突出显示自定义文件类型 [英] PyCharm: highlighting for custom filetype

查看:61
本文介绍了PyCharm:突出显示自定义文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PyCharm 中,是否可以为文件定义语法突出显示的自定义规则,这些规则结合了两种不同语言的语法?

In PyCharm, is it possible to define custom rules for syntax highlighting for files, which combine syntax from two different languages?

一个有点人为的例子:下面的文件应该被视为 HTML,除了脚本标签中的文本,它应该被视为 Python 代码.

A bit contrived example: following file should be treated as HTML, except for text in script tags, which should be treated as Python code.

<html>
<p></p>
<script type="text/python">
  v = "something"
</script>
<br />
<script type="text/python">
  print v
</python>
</html>

使代码完成也能正常工作的奖励积分.

Bonus points for getting code completion to work as well.

请注意,IntelliLang"扩展功能不适合,因为在第二个块中,它不会看到在前一个块中声明的变量.

Note that "IntelliLang" extension features don't fit, as in second block it wouldn't see variables declared in previous blocks.

推荐答案

我发现了一个来自 jetbrains 的博客,详细介绍了如何做到这一点,它适用于其他语言,也适用于 PyCharm他们的IDE).这是在 javascript 中使用 PHP 的示例,但应该也适用于您.

I found a blog from jetbrains that details how to do it, and it will work in other languages and inside PyCharm as well (It is noted to work in most of their IDEs). This is an example of having PHP inside of javascript, but should work for you as well.

语法突出显示 - 这有一个关于如何做的简要说明,还有一个链接,指向一个关于他们的融合的完整教程:完整教程

Syntax Highlighting - This has a brief explanation of what to do, and also has a link that goes to a full blown tutorial on their confluence: Full tutorial

完整教程中的一个小片段,您必须为其创建自定义文件扩展名才能读取两种/多种语言 说:

A small snipet from the full tutorial, which you will have to create a custom file extension for it to read both/multiple languages says:

步骤 2. 将新文件类型与 PHP(或其他语言)关联

Step 2. Associate new file type with PHP (or other language)

我们需要将新的文件类型 (*.js.php) 与 PHP(在我们的案例)在设置 |文件类型.如果您不想使用此类自定义扩展名,那么您必须将完整的文件名指定为模式,这是不推荐的.

We need to associate the new file type (*.js.php) with PHP (in our case) in Settings | File Types. If you do not want to use such custom extension, then you'll have to assign the full file name as a pattern, which is not recommended.

请注意,此设置是 IDE 范围的,因此此文件类型关联将影响所有项目.因此,有一个习俗扩展是最好的方法.

Please note that this setting is IDE-wide, so this file type association will affect all projects. Therefore, having a custom extension is the best way to proceed with it.

这篇关于PyCharm:突出显示自定义文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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