在同一文件中切换语言 [英] switching between languages in same file

查看:178
本文介绍了在同一文件中切换语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近参加了一个用户组会议,其中IntelliJ代表演示了版本13.



他演示了如何切换文件的代码完成视图。我不完全记得这个特定文件的文件扩展名是什么,可能是java。



这个概念是,如果文件是带有嵌入式javascript的html,他可以用快捷方式在html和javascript之间切换代码完成。如果他说将文件视为html,则文件中的所有代码都被视为代码完成目的为html,反之亦然为javascript。



有人知道他可能用来启用语言切换的快捷方式吗?

解决方案

听起来您可能指的是,如果您使用注释而未附加注释,IDEA将提供将其作为库附加。



IntelliLang和 @Language 注释支持大量语言。输入 @Language()后,只需在引号内使用代码完成即可查看列表。 (内联搜索也在列表中工作。)最有用的一个是Regexp。例如,如果你有一个方法,希望传入的字符串是一个有效的正则表达式,那么注释它将给任何人调用它正则代码完成和错误突出显示,如果他们传入一个无效的正则表达式模式。即使是使用其他IDE的开发人员,它也可以作为一种文档形式。



至于快速更改代码完成语言的快捷方式,我唯一的事情就是可以认为你可能指的是注入语言意图。如果我输入字符串值,并通过 Alt + Enter 调出快速修复/意图菜单,我可以选择注入语言:





如果我选择它,IntelliJ IDEA会问我想用什么语言:





在做出选择后,IntelliJ IDEA会给我临时所选语言的语言注入(包括代码完成)。





它还为我提供了一个选项,可以为永久注射添加 @Language 注释。





到据我所知(作为10年的IntelliJ IDEA用户),这是切换代码完成语言类型的唯一方法。所以希望这就是你要找的东西。对我而言,IntelliLang是IntelliJ中最酷的功能之一。 (它实际上是作为第三方插件开始的,然后JetBrains将其吸收到产品中。)


I recently attended a user group meeting where the IntelliJ representative was demonstrating version 13.

He demonstrated how to switch the code completion view of a file. I do not exactly remember what the file extension of this particular file was, probably java.

The concept was that if the file is html with embedded javascript he could then switch the code completion between html and javascript with a shortcut. If he says treat the file as html then all code in file was treated for code completion purposes as html, and vice versa for javascript.

Does anybody know what shortcut he might have been using to enable the language switch?

解决方案

Sounds like you may be referring to the IntelliLang feature. IntelliJ IDEA can be aware of other languages embedded within a file.

A simple example is in an HTML file that has CSS and JavaScript.

Notice when I am inside the HTML markup:

or inside an HTML element:

The code complete shows HTML completion options. However, when I am inside the style attribute, I get CSS code completion:

I also get CSS code completion if I am inside a <style> element. So even though I am in an HTML file, I see CSS code completion because of my location.

Same case with JavaScript. When I invoke code completion inside a <script> element, I get JavaScript completion, even though I am in an HTML file.

Anytime IntelliJ IDEA can determine that another embedded language is present, it provides, via IntelliLang, the appropriate syntax highlighting, error highlighting, and code completion. The same holds true for Java. Notice here that IDEA knows the method I am competing takes an SQL statement and therefore highlights the String value using SQL highlighting, and provides SQL code completion:

So even though I am in a .java file, I get SQL code completion. The reason is that IntelliLang comes pre-configured knowing the embedded language of some methods. You modify them, or add more, in File > Settings > [Project Settings] > Language Injections.

In addition, you can use an annotation to tell IntelliJ IDEA (as well as developers looking at the code) that a String must be valid in a particular language. For example, I can annotate a String field, variable, or parameter, to indicate it must be valid HTML:

Notice I get HTML syntax highlighting, HTML code completions, and the CSS color shows in the left gutter. If I annotate a method parameter, then any time I call the method, I get the appropriate syntax highlighting, code completion, and error/warning highlighting:

The @Language annotation is inside the annotations.jar that is contained in the redist directory inside the IntelliJ IDEA installation directory. It is also available in maven central, or IDEA will offer to attach it as a Library if you use the annotation without it being attached.

IntelliLang and the @Language annotation supports a large number of languages. Just use code Completion inside the quotes after typing @Language("") to see a list. (Inline search works in the list as well.) One of the most useful is Regexp. For example, if you have a method that expects the string passed in to be a valid Regular Expression, annotating it as such will give anyone that calls it Regex code completion and error highlighting if they are passing in an invalid Regex pattern. Even for developers using other IDEs it is useful as a form of documentation.

As for a shortcut to change the the language on the fly for code completion, the only thing I can think that you might be referring to is the "Inject Language" intention. If I am entering a String value, and I bring up the quick-fix/intention menu via Alt+Enter, I am given an option to inject a language:

If I select that, IntelliJ IDEA will ask me what language I want to use:

After making my selection, IntelliJ IDEA will give me temporary language injection (including code completion) for the selected language.

It also gives me an option to add the @Language annotation for permanent injection.

To the best of my knowledge (as a 10 year IntelliJ IDEA user) that is the only way to switch code completion language types. So hopefully that is what you are looking for. To me, IntelliLang is one of the coolest features in IntelliJ. (It actually started as a third party plug-in and JetBrains then absorbed it into the product.)

这篇关于在同一文件中切换语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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