VSCode - clangd extension does not format/lint on save - visual-studio-code

I am using VSCode with the clangd extension for C++ development (through Remote SSH extension). I have my .clang-format and .clang-tidy files with the rules I want to enforce. If I format the code manually (either Ctrl-Alt-F or right click -> Format document) the code gets formatted without any issues. Same if I manually go for the clang-tidy suggested fixes (Ctrl-.). Instead, if I save, nothing happens.
These are my settings overrides for C++. I tried moving them inside the different levels (User, Remote, Workspace) but there is no difference.
"[cpp]": {
"editor.codeActionsOnSave": {
"source.fixAll": true,
},
"editor.formatOnSave": true,
"editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
}
The same rules for ESLint or rustfmt work with no issues.

I investigated the quick-fix issue a bit, and found that "editor.codeActionsOnSave": { "source.fixAll": true } relies on a new Language Server Protocol (LSP) enhancement added in LSP 3.17 (code actions with type source.fixAll).
Clangd does not currently support this, but it seems to me like it shouldn't be too difficult to add support for it, as clangd already classifies quick-fixes based on whether they're suitable for being applied automatically (e.g. Select All --> "Auto Fix..." makes use of this).
I filed https://github.com/clangd/clangd/issues/1446 with some more details.

Related

VS Code Prettier FormatOnSave stopped working in my new project

I created a new project with create-react-app. I have configured prettier tool. Unfortunately, formatting on save doesn't work on this project. Works fine on other projects. What could have happened?
My VSCode setting is:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSaveMode": "modifications",
}
Since this is a new project, I presume that means it is not in source control. The setting "modifications" will not work in that situation, as described on the site (https://code.visualstudio.com/docs/getstarted/settings):
// Controls if format on save formats the whole file or only modifications. Only applies when `editor.formatOnSave` is enabled.
// - file: Format the whole file.
// - modifications: Format modifications (requires source control).
// - modificationsIfAvailable: Will attempt to format modifications only (requires source control). If source control can't be used, then the whole file will be formatted.
Try using "modificationsIfAvailable" instead:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSaveMode": "modificationsIfAvailable",
}

files.exclude for Visual Studio Code not working as expected for files ending in .plan and .tfstate

Friends,
I have the following code in files.exclude in User settings which I understand is global unless you want to override in Workspace settings
"files.exclude": {
"**/.terraform/": true,
"**/.plan": true,
"**/.tfstate": true
}
The above code works perfectly fine, hiding the unwanted .terraform folder but not working as expected for files ending in .plan and .tfstate or .tfstate.backup files.
I want terraform plan and state files to be ignore by Visual Studio Code, how to achieve that?
Please find below snapshot, I am still seeing .plan and .tfstate or .tfstate.backup files.:
Looks like your glob patterns are off. Assuming you want these exclusions to be recursive, I believe you're looking for this:
"files.exclude": {
"**/.terraform/": true,
"**/*.plan": true,
"**/*.tfstate*": true,
}

VSCode codeActionsOnSave ignore specific file

I'm using organizeImports on save, but there are some instances where the order matters and this causes an issue. I can't find anywhere if there's a way to simply ignore a page, either through comments within the page (ideally) or within the config settings.
Perhaps there's an extension that provides this functionality if not baked in. In any case, really appreciate any help tracking down a solution for this.
Couldn't find how to do it properly but here is a solution that might solve your end needs if you use prettier.
The way I handled organizeImports selectively for files was as follow.
1 - Make sure your default formatter is prettier (as explained below)
https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
2 - In your settings set formatOnSave to true but organizeImports to false such as below.
{
"editor.codeActionsOnSave": {
"source.organizeImports": false
},
"editor.formatOnSave": true
}
3 - Install https://github.com/simonhaenisch/prettier-plugin-organize-imports
This is a prettier plugin that allows organizing import as part of the prettier formatting and has an option to disable organize import for files (i.e. // organize-imports-ignore )
If you're using ESLint (which I would highly recommend), you can use sort-imports or import/order (via eslint-plugin-import) to sort your import statements across the entire project, then ignore the rule in specific files/regions with special comments, like this:
/* eslint-disable import/order */
import * from "abcdefg";
import "cool-module";
// etc...
VSCode has a great ESLint plugin, which in combination with
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
},
will auto format and fix your code whenever the file is saved.

How do I disable Visual Studio Code's C++ access modifier indent?

Version 1.15.0 of vscode seems to aggressively change the indenting of access modifiers in C++ code, despite disabling autoIndent and formatOnType.
When I enter this:
class Foo
{
public:
Foo();
};
It gets reformatted as soon as I hit enter after typing "public:", to this:
class Foo
{
public:
Foo();
};
In my user settings I have "editor.autoIndent" and "editor.formatOnType" set to false. I also have "C_Cpp.formatting" in the C++ extension set to "Disabled".
Is there some way to control this behavior?
Have you tried to set this option to false?
"C_Cpp.clang_format_formatOnSave": false,
Edited
Also take a look at this option:
editor.formatOnSave
I found a way to do this by modifying the language configuration file for C++. It is located in the Microsoft VS Code directory here: resources/app/extension/cpp/language-configuration.json.
I removed the items in the "increaseIndentPattern" and "decreaseIndentPattern" that were related to access modifiers.
This works, but editing the file directly seems like a bad idea.

Set .history folder as ignored by intellisense

I need to ignore .history folder from intellisense.
Now it looks like this when I start typing Focus:
As you can see intellisense will offer me every Identical class found in .history folder and this is very very confusing (and find correct one).
I try find something in vscode settings, and on editor site but no success.
.history folder is ignored from git, display in explorer, and tslint:
"files.exclude": {
"**/.history": true ...
},
"files.watcherExclude": {
"**/.history/**": true ...
},
"tslint.exclude": ["**/.history/**"] ...
How to achieve ignore .history from intellisense?
Next part is based on the answer from Matt
An important assumption:
Visual Studio Code itself does not contain automatic import and you need an extension for it.
Solution:
I am using extension Auto Import (steoates.autoimport) which contains the setting autoimport.filesToScan. I changed default value from "**/*.{ts,tsx}" to "{src,e2e,node_modules}/**/*.{ts,tsx}" and now everything work as I expected.
Those suggestions are coming whatever extension you have installed that is providing auto-import functionality. Please check to see if that extension has its own exclude setting that you also need to configure
In your settings.json, you could add this:
"autoimport.filesToScan": "./index.{ts,tsx} ./App.{ts,tsx} ./src/**.*.{ts,tsx}"
Or, if you prefer:
"autoimport.filesToScan": "./{index,App,src/**}.{ts,tsx}"