microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.54k stars 29k forks source link

Trigger language auto detection mechanism #11838

Open XVincentX opened 8 years ago

XVincentX commented 8 years ago

Some times I'm fetching some content from the web and pasting it into the current active editor. So far, I didn't find any way to set the current language for the window. Is there any particular API call for that?

Even better, would it be possible to trigger the auto detection mechanism with the API?

ramya-rao-a commented 8 years ago

See Changing language for current file

Please ask 'how-to' questions about VS Code on Stack Overflow using the tag vscode. The VS Code development team together with the VS Code Community will answer question your question over there. Here is how the development team tracks feedback on the different channels: Feedback Channels.

XVincentX commented 8 years ago

@ramya-rao-a ?????? Hmm...I am not talking the IDE itself, I'm talking from an extension context. I'm missing an API function to make that happen. Does that clarify?

XVincentX commented 8 years ago

My point is that - it seems that TextEditor class is missing methods to set current language type, or eventually trigger the auto detect mechanism

ramya-rao-a commented 8 years ago

Sorry, I thought you were talking from a user's perspective rather than an extension author. Will look into it and get back to you

jrieken commented 8 years ago

@XVincentX This mostly depends on the filename and 'pasting it into the editor' will miss that information I guess.

XVincentX commented 8 years ago

@jrieken Is there any plan to provide an API call to trigger the auto detection? In my use cases, giving I'm providing regexp in languages I support, it would really solve my problem here.

Thanks!

XVincentX commented 8 years ago

@jrieken I started contributing #12035 and I liked it.

Would you be open to an user contribution here? If you could point out where I should start look, I'd love to give it a try!

mbana commented 6 years ago

Hi,

I am faced with a similar problem/scenario as the original poster. Let's say you're faced with this scenario:

  1. Open new a editor.
  2. Paste in some content, say, it's JSON.
  3. I want the VSCode to auto-detect that it is JSON only and set the langauge accordingly.

Is it this possible, or is it at least something in the pipeline?

From an implementation point of view, I would do:

  1. Loop through all the languages.
  2. Paste content there and see if it's a match.
  3. If match is found, set that as the language.

This is quite crude, but I hope that gets the point across.


Regardless +1 for this feature.

zlzdp commented 6 years ago

I want auto-detect too. when I copy a json(or other) from chrome dev tool(or webpage), paste in vscode, I hope vscode auto-detect program language, and highlight.

JonasKru commented 3 years ago

I need that feature as well, please.

gjsjohnmurray commented 1 year ago

@jrieken is the idea to provide new API vscode.languages.detectLanguage(resource: Uri, supportedLanguageIds?: string[]): Thenable<string | undefined> which would get wired through to the detectLanguage method of LanguageDetectionService?

https://github.com/microsoft/vscode/blob/d23edd26b1068cdce81fa2d1e381197db57a3941/src/vs/workbench/services/languageDetection/browser/languageDetectionWorkerServiceImpl.ts#L142-L150

mikeburgh commented 4 months ago

Adding that new API would be great!

I found you can also call vscode.commands.executeCommand('editor.detectLanguage') which will run the detection on the active editor.. but it's not silent, and if it does not find a match a warning notification is displayed. Would it be easier to add a silent option to that command ?