Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make VS code auto detect the language what I type and change language mode to it?

I'm creating a project with HTML , CSS , JAVASCRIPT, PHP , MYSQL

And now I'm using VScode to after using Aptana Studio for 3 years.

So how can I make VScode detect what language I type and automatically change mode to it ?

example:

I use php scripts inside html code, but the extension of the file is .php how can I make vscode when I'm coding inside <?php ?> area automatically detect that and change language mode to php, and when I'm coding outside <?php ?> area automatically detect that and change language mode to html, and for example when I type a tag like <style></style> and begin to type inside it, automatically detect that and change language mode to css.

Is there any extension for this or any solution ?

btw, this feature was in aptana studio

like image 913
Ameer Ahmed Avatar asked Jun 05 '18 04:06

Ameer Ahmed


2 Answers

Coming in vscode 1.59 is automatic language detection:

Automatic language detection of Untitled files

We're excited to announce the initial preview of automatic language detection of Untitled files which uses machine learning to detect which language you are coding in and automatically sets the language mode of the untitled file. This feature leverages the open source ML library, Tensorflow.js, and the ML model from Guesslang by GitHub user @yoeo.

The setting is "workbench.editor.languageDetection": true, (true = default)

Note: If language detection isn't confident enough, then you will stay in your current language mode and no results will show in the language picker until language detection has more confidence. The setting also allows you to provide language overrides that can be used to specify language modes you don't want to be automatically switched off from. Given this example:


 "[markdown]": {
   "workbench.editor.languageDetection": false 
} 

Or Workbench > Editor: Language Detection


And in v1.65 more work and another setting on automatic language detection:

Improved automatic language detection

When the new setting workbench.editor.historyBasedLanguageDetection is enabled, untitled editors will now use an improved automatic language detection algorithm that factors in your editor history and the contents of the current workspace to provide detection results with much less input text required

  • from v1.65 Release Notes
like image 119
Mark Avatar answered Oct 23 '22 11:10

Mark


It seems that what you are requesting does not exist as per the documentation:

In VS Code, we default the language support for a file based on its filename extension. However, at times you may wish to change language modes, to do this click on the language indicator - which is located on the right hand of the Status Bar. This will bring up the Select Language Mode drop-down where you can select another language for the current file.

Based on that, you can understand that VS Code's language detection is file extension based. However you can associate languages with file extensions. As per the documentation:

For example, the setting below adds the .myphp file extension to the php language identifier:

"files.associations": {
    "*.myphp": "php"
}

Like that, you can associate your common languages to your common file extensions.

like image 3
Script47 Avatar answered Oct 23 '22 12:10

Script47