vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

Switch to using setTextDocumentLanguage instead of adding to files.associations

Open sean-mcmanus opened this issue 6 years ago • 18 comments

The Septemember 2018 release added vscode.languages.setTextDocumentLanguage, so our extension can try using that instead of adding files to files.associations.

The previous thread is https://github.com/microsoft/vscode-cpptools/issues/722 .

sean-mcmanus avatar Aug 13 '19 19:08 sean-mcmanus

Actually, this is problematic, because setTextDocumentLanguage is not persistent like files.associations is...

sean-mcmanus avatar Sep 09 '19 21:09 sean-mcmanus

@sean-mcmanus did you try this and verify that it does not persist? We may need to re-engage with the VS Code team on this and come up with a solution that works for us.

bobbrow avatar Mar 12 '20 21:03 bobbrow

The VS Code issue on this: microsoft/vscode#78968

bobbrow avatar Mar 12 '20 21:03 bobbrow

I think this needs to be spec'd out. The set document language doesn't persist. We could persist it in a "hidden" way, but then users wouldn't have any ability to set those settings. Maybe we could add a setting for users who strong don't want the files.associations in settings.json, and maybe we could change the "hidden" setting after the user manually changes the language via the status bar (not sure).

sean-mcmanus avatar Mar 13 '20 20:03 sean-mcmanus

@sean-mcmanus @bobbrow I am wondering whether there is any progress on this issue? I am hitting exactly the same issue, where I don't want the C++ plugin to modify my settings, which can be achieved by "C_Cpp.autoAddFileAssociations": false, but then the opened STL headers through code navigation are opened as plain text. Thanks.

ahojnnes avatar Aug 12 '21 07:08 ahojnnes

@ahojnnes We could dynamically setTextDocumentLanguage, but the problem that needs to be solved is how to decide when to call that -- did you have a proposed solution? I believe we would need language type auto-detection for extensionless files from VS Code: https://github.com/microsoft/vscode/issues/130642

sean-mcmanus avatar Aug 13 '21 20:08 sean-mcmanus

Unless I am missing something, shouldn't the language always be C++ when following headers from existing C++ code? I guess one could end up in a external C file but then C++ should still be good enough?

ahojnnes avatar Aug 14 '21 11:08 ahojnnes

Yeah, if we're opening a file from a header, then the setTextDocumentLanguage will work, but then after a Reload Window or any other case where the header happens to be open again, the language would be reset unless the association is saved somehow.

We could fix it to call setTextDocumentLanguage on go to definition when the autoAddFileAssociaitons is set to false.

sean-mcmanus avatar Aug 16 '21 17:08 sean-mcmanus

We could fix it to call setTextDocumentLanguage on go to definition when the autoAddFileAssociaitons is set to false.

I would say that this would already be an improvement over the current behavior.

ahojnnes avatar Aug 16 '21 21:08 ahojnnes

Any update on this issue? Running into the same file association issue described in #722.

ngunderson avatar Dec 06 '21 15:12 ngunderson

@ngunderson No update.

sean-mcmanus avatar Dec 06 '21 15:12 sean-mcmanus

What about persisting this information in workspaceStorage for example ?

amurzeau avatar Jun 18 '22 13:06 amurzeau

But how would the user see/modify the value in the workspaceStorage? I mention this in my comment https://github.com/microsoft/vscode-cpptools/issues/4077#issuecomment-598905870

sean-mcmanus avatar Jun 20 '22 19:06 sean-mcmanus

Any update on this issue? It requires me to edit my .vscode/settings.json file before every commit.

geertj avatar Jul 22 '23 22:07 geertj

@geertj No update. A potential workaround is to not commit .vscode/settings.json and instead commit a .code-workspace file with the shared settings for a prjoect. Then you can make a local copy of that file and open that workspace file so that files.associations don't get added to the checked in version. You could also potentially check in the files.associations with either settings (well, that may not work if the ordering is not stable, or there could be an issue with the list growing).

sean-mcmanus avatar Jul 25 '23 22:07 sean-mcmanus

Why not to just make "C_Cpp.autoAddFileAssociations": false default behavior and generate those associations only if user manually create settings.json and set this value to true? I (and probably many other users) have never experienced any need for those autogenerated associations, meanwhile bad UX from irritating settings spamming is a thing I experience all the time from that extension.

Divelix avatar Jan 26 '24 16:01 Divelix

This kind of all-consuming glob pattern helped me:

"files.associations": {
	"**/Microsoft Visual Studio/**/MSVC/**/include/**": "cpp"
}

Challanger524 avatar Apr 08 '24 16:04 Challanger524

@Divelix The main reason is the system headers end up not being recognized as C++ files when they're opened, but maybe that could be fixed via using setTextDocumentLanguage for files located under a system include header path.

sean-mcmanus avatar Apr 08 '24 18:04 sean-mcmanus