Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose 'change language' as command #1800

Closed
ctlajoie opened this issue Jan 5, 2016 · 35 comments
Closed

Expose 'change language' as command #1800

ctlajoie opened this issue Jan 5, 2016 · 35 comments
Assignees
Labels
api api-finalization feature-request Request for new features or functionality release-notes Release notes issues verification-needed Verification of issue is requested verified Verification succeeded

Comments

@ctlajoie
Copy link

ctlajoie commented Jan 5, 2016

I have a modelines plugin but it currently lacks the ability to set the language of the file. In order to implement this I would need an API to allow it to be set.

As an example, a Vagrantfile is a ruby script, but Code opens it as plain text. The API should allow plugins to determine the type of file by arbitrary means and set the language appropriately.

@joaomoreno joaomoreno added the feature-request Request for new features or functionality label Jan 6, 2016
@joaomoreno
Copy link
Member

@alexandrudima @jrieken ping

@jrieken
Copy link
Member

jrieken commented Jan 6, 2016

there should be a command that allows to change the mine, like from the status bar.

@jrieken jrieken added this to the Backlog milestone Jan 6, 2016
@jrieken jrieken self-assigned this Jan 6, 2016
@ctlajoie
Copy link
Author

ctlajoie commented Jan 6, 2016

@jrieken Are you saying there is already a way to do it by sending a command?

@jrieken
Copy link
Member

jrieken commented Jan 7, 2016

yes and no. there is command but I believe it won't work the way it should.

@jrieken jrieken assigned bpasero and unassigned jrieken Jan 7, 2016
@jrieken jrieken changed the title Provide API to set language of a TextEditor Expose 'change language' as command Jan 7, 2016
@jrieken
Copy link
Member

jrieken commented Jan 7, 2016

It should be like vscode.editor.changeLanguage with uri, languageid or so. basically whatever gets executed when a selection in language picker is made

@bpasero
Copy link
Member

bpasero commented Jan 7, 2016

I think this action does not exist the way wanted, it just opens the mode picker. Having a way to change the mode on a opened document is maybe something that should be exposed as API?

@bpasero bpasero added the api label Jan 7, 2016
@jrieken
Copy link
Member

jrieken commented Jan 7, 2016

we cannot make it a setting because by the logic of the document API and synching it will remove that document and create a new one. like calling document.languageId = 'foo' will make it disappear.

@bpasero bpasero removed their assignment Jan 8, 2016
@markeissler
Copy link

Any update on this issue? I just started using vscode after many years on sublime and the lack of modeline support is hugely annoying...enough to switch back.

Also, this issue ought to be merged with:

#1651 Detect Language Mode from Modeline Comment

@jrieken
Copy link
Member

jrieken commented Oct 20, 2016

@markeissler This is about adding API to allow extensions to change the mode. I believe you are looking for a user interaction, right? There is the mode switcher at the bottom of the editor

@markeissler
Copy link

No, this is exactly about the API. It's standard practice to use vi/vim/emacs modeline indicators in header comments of a file so that an editor can override whatever it's best "guess" is to select the correct language. Usually, the guess is based on the file extension but it has become common for certain files not to carry a filename extension (the OP started this thread with the example of a Vagrantfile, which is actually a ruby file but without an extension). I'm facing the same issue with large shell utilities whose library files don't tend to carry an extension. It's a huge drag to have to manually select the correct language each time I open a file...especially when you are working on a project with dozens of extension-less files.

Sublime Text, for example, has a couple of plugins that implement this functionality (reading the first X number of lines of a file in search of a modeline, if it exists select the language from the modeline, otherwise try and use the file extension, otherwise fall back to plain text). The OP (@ctlajoie) created such a plugin for vscode but is unable to implement language selection because of the lack of support by the vscode API.

@mcastany
Copy link

mcastany commented Dec 4, 2016

Any update on this issue ?

@jitcoder
Copy link

writing my first extension and have spent the past 12 hours trying to figure out a work around for this. Need this ability desperately.., I don't want to use the previewHtml.

created a textdocumentcontentprovider, the contents of which are JSON and I'd like to set the languageId to json so the user doesn't have to do it manually..

@d-akara
Copy link

d-akara commented Mar 20, 2017

I need this as well for some extensions I'm working with

@snewell92
Copy link

I'd like this to work with .asp files that are all JScript, so that when I switch to an include file that is pure JScript I can quickly press a chord and get javascript syntax while still tabbing over to mixed files. This is exactly what was brought up in #21826.

Example keybindings.json (which I just tried to make and use but failed 😢)

{
  "key": "ctrl+alt+shift+j",
  "command": "workbench.action.editor.changeLanguageMode",
  "args": { "langauge": "javascript" }
}

Thanks for reading!

@bmewburn
Copy link

This could be a solution to #670 for php. Could hook into the selection changed event when in and out of php tags to automatically toggle active language.

@mrdevon
Copy link

mrdevon commented Nov 17, 2017

Would love to see this added either as an arg we can pass or as a separate command.

@jrieken jrieken added this to the September 2018 milestone Aug 27, 2018
@artrz
Copy link

artrz commented Aug 27, 2018

Is there a way to get an extension id from a filename string, say 'file.js'?

@jrieken
Copy link
Member

jrieken commented Sep 3, 2018

@alexandrudima Thinking about the API for this and I wonder why we decided that changing a mode is expressed as a close/open-event-sequence. See https://github.com/Microsoft/vscode/blob/a69c03e4ca08f58b67ed4b91eaa57e2db44592e6/src/vs/workbench/api/node/extHostDocuments.ts#L103
Seems like we have done since day one but I don't remember why? Us just being pragmatic or not? My idea is that we could simply make TextDocument#languageId writable (like editor selections et al) instead of adding a separate function like changeLanguage

@jrieken
Copy link
Member

jrieken commented Sep 3, 2018

Ok, one advantage of sending a close/open-sequence is that validation participants have an easy way to 'follow' documents of a certain language

@jrieken
Copy link
Member

jrieken commented Sep 19, 2018

Current finalisation proposal

/**
 * Set (and change) the [language](#TextDocument.languageId) that is associated
 * with the given document.
 *
 * *Note* that calling this function will trigger the [`onDidCloseTextDocument`](#languages.onDidCloseTextDocument) event
 * followed by the [`onDidOpenTextDocument`](#languages.onDidOpenTextDocument) event.
 *
 * @param document The document which language is to be changed
 * @param languageId The new language identifier.
 * @returns A thenable that resolves with the updated document.
 */
export function setTextDocumentLanguage(document: TextDocument, languageId: string): Thenable<TextDocument>;

@jrieken
Copy link
Member

jrieken commented Sep 20, 2018

This is API now

@jrieken jrieken closed this as completed Sep 20, 2018
jrieken added a commit that referenced this issue Sep 20, 2018
jrieken added a commit that referenced this issue Sep 20, 2018
jrieken added a commit that referenced this issue Sep 20, 2018
@jrieken jrieken added the verification-needed Verification of issue is requested label Sep 24, 2018
@sbatten sbatten added the verified Verification succeeded label Sep 25, 2018
@jrieken jrieken added the release-notes Release notes issues label Sep 27, 2018
@bbrendon
Copy link

How do you use this? is there documentation for it?

@mechatroner
Copy link
Contributor

@bbrendon you can take a look at "Rainbow CSV" or "vscode-modelines" source code for usage examples.

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api api-finalization feature-request Request for new features or functionality release-notes Release notes issues verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

No branches or pull requests