IntelliJ:如何强制编辑器将文件视为 javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5790402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:20:45  来源:igfitidea点击:

IntelliJ: how to force editor to treat a file as javascript?

javascriptintellij-ideacode-editor

提问by Dean Moses

I'm creating a Grails app in IntelliJ 10 and have a javascript file that is created dynamically, as a Grails view.

我正在 IntelliJ 10 中创建一个 Grails 应用程序,并有一个动态创建的 javascript 文件,作为 Grails 视图。

For that reason the javascript file doesn't end in '.js'. It ends in '.gsp' because it's a Groovy Server Page that spits out javascript.

因此,javascript 文件不会以“.js”结尾。它以“.gsp”结尾,因为它是一个吐出 javascript 的 Groovy 服务器页面。

IntelliJ doesn't know it's a javascript file so it doesn't give me code completion or warnings.

IntelliJ 不知道它是一个 javascript 文件,因此它不会给我代码完成或警告。

I want my code complete and syntax checking! Is there a way to force IntelliJ to treat this specific file as Javascript, WITHOUT messing up the IntelliJ's correct treatment of .gsp files everywhere else?

我想要我的代码完整和语法检查!有没有办法强制 IntelliJ 将此特定文件视为 Javascript,而不会在其他地方搞乱 IntelliJ 对 .gsp 文件的正确处理?

Thanks!

谢谢!

回答by Monarch Wadia

The above answer is slightly outdated. You can do the same in IntelliJ 14 by doing the following:

上面的答案有点过时了。您可以通过执行以下操作在 IntelliJ 14 中执行相同的操作:

  1. Go to Preferences(Mac OSX : Cmd + Comma)
  2. Under Editor -> File Types
  3. In the Recognized File TypesHighlight JavaScript files (JavaScript)
  4. Under Registered Patternspress the +button
  5. Enter your regex pattern (for example: *.js.scala.html) and press OK
  1. 转到Preferences(Mac OSX:Cmd + 逗号)
  2. 在下面 Editor -> File Types
  3. Recognized File Types亮点JavaScript files (JavaScript)
  4. Registered Patterns按下+按钮
  5. 输入您的正则表达式模式(例如:)*.js.scala.html并按 OK

Cheers!

干杯!

回答by CrazyCoder

You can add additional extensions to the JavaScriptin Settings(Preferenceson Mac) | File Types.

您可以在(在 Mac 上) | 中向JavaScript添加其他扩展。.SettingsPreferencesFile Types

enter image description here

在此处输入图片说明

See also Settings| Template Data Languageswhere you can configure certain files in the project as templates producing specified language to get proper code completion.

另见Settings| Template Data Languages您可以在其中将项目中的某些文件配置为生成指定语言的模板,以获得正确的代码完成。