简体   繁体   English

Webstorm 无法识别 JavaScript 文件

[英]Webstorm not recognising JavaScript file

看

Webstorm doesn't recognize one of my .js files as a JavaScript file so I'm losing syntax highlighting and being able to add break points. Webstorm 无法将我的 .js 文件之一识别为 JavaScript 文件,因此我失去了语法高亮显示并能够添加断点。 I've looked in the workspace.xml file and the file in question seems to have similar settings to other .js files that work correctly.我查看了workspace.xml文件,有问题的文件似乎与其他正常工作的 .js 文件具有相似的设置。

If I change the name of the file it works ok.如果我更改文件的名称,它可以正常工作。 So somehow Webstorm is stuck on misinterpreting the type of a file that has this name.所以不知何故,Webstorm 被困在误解具有此名称的文件的类型上。 Where else can I edit the project?我还能在哪里编辑项目?

上下文菜单

So I see three possible reasons for the problem:所以我看到了问题的三个可能原因:

  1. The file was marked as 'Plain text'该文件被标记为“纯文本”

  2. There is a pattern for 'Text files' file type that matches this file (or back: file type 'JavaScript' exclude this file name).有一个与此文件匹配的“文本文件”文件类型模式(或返回:文件类型“JavaScript”排除此文件名)。 See image below见下图

在此处输入图片说明

  1. There is a custom plugin that overrides default behavior for files with this name (unlikely)有一个自定义插件可以覆盖具有此名称的文件的默认行为(不太可能)

Updated: after several years I've found one more reason for the behavior and most likely it the main source of the issues.更新:几年后,我找到了导致该行为的另一个原因,并且很可能是问题的主要来源。 When you create a file without any extensions the " Register New File Type Association " is appeared.当您创建一个没有任何扩展名的文件时,会出现“注册新文件类型关联”。 And you can accidentally specify a new file type, for some file name.您可能会意外地为某些文件名指定新的文件类型。 It is can be fixed with (2) but it is the reason why the pattern was added there.它可以用(2)修复,但这是在那里添加模式的原因。

注册新文件类型关联

Note: the solution works for all IDEA-based IDEs: IntelliJ IDEA, WebStorm, RubyMine, PyCharm, PhpStorm.注意:该解决方案适用于所有基于 IDEA 的 IDE:IntelliJ IDEA、WebStorm、RubyMine、PyCharm、PhpStorm。

文件 -> 设置 -> 编辑器 -> 文件类型 -> 文本文件 -> 检查那里的“ContentRepository.js”模式并将其删除。

If the file was marked as "Plain Text", then this can be fixed as follows:如果文件被标记为“纯文本”,则可以按如下方式修复:

  1. Right click on the file.右键单击该文件。

  2. One of the menu options is "Mark as Javascript" .菜单选项之一是“标记为 Javascript”。 Just below "Delete".就在“删除”下方。

  3. Click that.单击那个。 Your file is now recognized as javascript by WebStorm.您的文件现在被 WebStorm 识别为 javascript。

Since WebStorm was not allowing screenshots after right click so couldn't add it here.由于 WebStorm 不允许右键单击后截图,因此无法在此处添加。

For me this did the trick对我来说,这成功了
1) Preferences 1) 偏好
2) Editor 2) 编辑器
3) FileTypes 3) 文件类型
4) Search for javascript 4)搜索javascript
5) Add *.js to registered patterns 5) 将*.js添加到注册模式

My problem was with Auto-detect file type by context我的问题是Auto-detect file type by context

在此处输入图片说明

You can also try "Template data languages" (in file > settings) which will force a specific language synatax high-lighting on a specific file or folder.您还可以尝试“模板数据语言”(在文件 > 设置中),这将强制在特定文件或文件夹上突出显示特定语言语法。 See screenshot below看下面的截图在此处输入图片说明

For a block of code you can also use "language injection".对于代码块,您还可以使用“语言注入”。 Simply high-light the block of code, wait for the little lamp to appear then select the bottom choice and then the language you need (screenshot also attached)只需突出显示代码块,等待小灯出现,然后选择底部选项,然后选择您需要的语言(附上截图)

在此处输入图片说明

In case it is helpful elsewhere (OMG this was scary).如果它在其他地方有帮助(天哪,这太可怕了)。

For some reason IntelliJ was not recognizing any .js file in my project (even after .idea removal ( rm -rf .idea )).出于某种原因,IntelliJ 无法识别我项目中的任何.js文件(即使在.idea删除( rm -rf .idea )之后)。

I went to Webstorm (in my case RubyMine ): Webstorm > Preferences ... > Editor > File Types then went to Recognized Filed Types , picked Javascript (but whatever file type you are missing will do), then added back a Registered Patterns entry of *.js我去了Webstorm (在我的例子中是RubyMine ): Webstorm > Preferences ... > Editor > File Types然后去Recognized Filed Types ,选择了 Javascript(但无论你缺少什么文件类型都可以),然后添加回一个Registered Patterns条目*.js

Of course, press Apply and then Ok .当然,按Apply然后按Ok

Wow, scary stuff.哇,可怕的东西。 but solved my problem, hope it does for someone else.但解决了我的问题,希望对其他人有用。

I was always trying to add .我一直试图添加 . js as a recognised file types, but somehow .js was added to Ignored Files and Folders . js作为可识别的文件类型,但不知何故.js被添加到Ignored Files and Folders

I just removed .js from Ignored Files and Folders and it worked for me.我刚刚从Ignored Files and Folders删除了.js ,它对我有用。

  1. Preferences喜好
  2. Editor编辑
  3. FileTypes文件类型
  4. Ignored Files and Folders忽略的文件和文件夹
  5. Remove *.js删除*.js

WebStorm -- 忽略的文件和文件夹

在此处输入图像描述 在此处输入图像描述

2022 update! 2022更新!

Right click on file and choose JavaScript右键单击文件并选择 JavaScript

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

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