I'd like for VSCode to indent automatically indent when I create a newline in the following case:
if(statement)
func();
The default functionality does the following when hitting newline:
if(statement)
func();
This is a longstanding issue in VSCode: https://github.com/microsoft/vscode/issues/43244
I'd appreciate any kind of hack/extension that can accomplish this behavior. There are other instances of indentation getting mangled in the github issue link, but I only really care about this simple case.
Figured out how to do this without installing an extension. There may be a better way that can be done in settings.json but I couldn't find it. You can modify a languages configuration directly from the source, which for me was C:\Program Files\Microsoft VS Code\resources\app\extensions\cpp\language-configuration.json. There is a guide for these files settings. I added the following to my c++ language configuration:
"onEnterRules": [
{
"beforeText": "^\\s*(?:if|while)\\(.*\\)\\s*$",
"action": {
"indent": "indent"
}
},
{
"beforeText": "(?=)",
"previousLineText": "^\\s*(?:if|while)\\(.*\\)\\s*$",
"action": {
"indent": "outdent"
}
}
]
This works, but unfortunately the official c++ vscode extension C/C++ for Visual Studio Code breaks it for some reason.
Below was my initial method of doing this, which breaks too many things to be useful.
"indentationRules": {
"increaseIndentPattern": "^\\s*if\\(.*\\)\\s*$",
"decreaseIndentPattern": "(?!)"
}
The field decreaseIndentPattern must be set (here the regex will never capture anything), otherwise it ignores the indentationRules field (I guess they never tested whether just one would be set?) Note that these edits need to be done with administrative privleges, and I found VSCode pretty convenient for making them. Also these changes do not take effect until VSCode is closed.
So as it turns out I've run into the same issues mentioned in this PR: https://github.com/microsoft/vscode/pull/115454. This fix breaks too much other vscode indentation behavior, such as deindenting after the first properly indented line in if statements.
Related
In C++ or C#, it's generally a good practice to enclose each case within curly braces (e.g., see C# switch statement with curly braces for each case/default block within the switch statement?).
But Visual Studio Code defaults to creating a template that leaves them out.
What UI preferences can I change so that they are included by default?
Edit: I am not interested in a debate about whether adding curly braces should always be done or not, but rather knowing how to change VS Code's UI for this context.
You should add a snippet by yourself.
Select Command palette (F1) -> Preferences: Configure User Snippets -> C++ and add the following code.
"switch2": {
"prefix": "switch2",
"body": "switch (${1:expression}) {\n\tcase ${2:/* constant-expression */}: {\n\t\t${3:/* code */}\n\t\tbreak;\n\t}\n\tdefault: {\n\t\tbreak;\n\t}\n}"
}
Ended up slightly modifying #umitu's answer. Posting it here just in case someone else finds it useful as well:
"switch2": {
"prefix": "switch2",
"body": "switch (${1:expression}) {\ncase ${2:/* constant-expression */}: {\n\t${3:/* code */}\n\t} break;\ncase ${4:/* constant-expression */}: {\n\t${5:/* code */}\n\t} break;\ndefault: {\n\t${6:/* code */}\n\t} break;\n}"
}
And now the default snippet looks like the following:
In itself, in Visual Studio Code, there is a shortcut (or emmet, not sure if it's called like this) that creates the basic structure of HTML. Typing exclamation mark and pressing TAB bring all the codes that we need to write when we start to build a web application.
However, I am looking for similar shortcut in Visual Studio Community 2019 that brings starting codes for basic applications in C.
For instance,
include <stdio.h>
include <stdlib.h>
int main(){
return 0;
}
P.S. : I know that not every application runs the same starting template or they are not even have a starting template. But, I as a new learner, just writing this structure above all the time so, it is kinda a starting template.
This page explains how to create your own snippets
Those are the steps on macOS:
Select User Snippets under Code > Pereferences
Select the C language
Create the snippet in the JSON file
A snippet generating the basic structure of a program could look like this:
{
"Skeleton": {
"prefix": "main",
"body": [
"#include <stdio.h>",
"#include <stdlib.h>",
"",
"int main(void)",
"{",
"\t${1:code}",
"\treturn 0;",
"}",
""
],
"description": "Dummy C program skeleton"
}
}
This is what it looks like:
Take enough time to understand the answer before ignorantly under-voting.
There isn't an out-of-the-box shortcut for C snippets.
However, you can create your own snippets or install this extension from the market.
Name: C Snippets
Id: harry-ross-software.c-snippets
Description: Snippets for the C programming language
Version: 1.3.0
Publisher: Harry Ross Software
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=Harry-Ross-Software.c-snippets
click to see image example
you can check all available snippets by pressing ctrl+space together
There is a lot of logging in my C++ project. The logging is done via a log stream and the log lines have the following format:
APP_LOG_XXX() << ... ;
Those log lines blend with the rest of the code and make it harder to be read.
I want to somehow make these log lines appear in dimmed colors, or even better to hide/fold by a hotkey or click. There are a lot of log lines already, so wrapping them up in #pragma region would take much time (or would require writing a separate script). I wonder if there is an easier way.
(There is a very similar question on SO, but it's about Visual Studio, not Visual Studio Code).
You can use extension Highlight.
Set the color to a version close to your theme background color
Add to your settings.json
"highlight.regexes": {
"(APP_LOG_XXX\\(\\) <<[^;]+;)": {
"regexFlags": "mg",
"decorations": [
{ "color": "#f0f0f0" }
]
}
}
Or you can use the opacity decoration property instead. The following configuration will dim the text while preserving its current syntax highlighting:
"highlight.regexes": {
"(APP_LOG_XXX\\(\\) <<[^;]+;)": {
"regexFlags": "mg",
"decorations": [
{ "opacity": "0.4" }
]
}
}
I have some code that compiles fine but I type the closing brace } for the else, it moves all the code from the else { all the way to the left and throws away all indentation.
if (some_condition) {
some_real_code();
} else {
obj.some(stuff);
obj(some,other(stuff));
and when I type the final } I get:
if (some_condition) {
some_real_code();
} else {
obj.
some(stuff);
obj(
some,
other(stuff));
}
The only way I've found to deal with this when it happens is to select a brace in my code, copy it to my clipboard, then do a right-click "paste simple" in clion, which doesn't do any reformatting.
Is there any better way? For example, an a phone, if it autocorrects you and you delete the autocorrected word and retype the same word again, it won't re-autocorrect you because it figures you actually knew what you meant when you do it the second time.
Thank you.
edit: I'm not saying clion is bad or wrong for not understanding my code because in my real code I use language features that it doesn't claim to have support for. I'm just looking for how to work around it's rather aggressive lack of support.
Please, switch off "Reformat block on typing '}'":
Seems that you would be interested in for-IDE-stub implementation in guarded block (Per-ide variable: in CLion it’s CLION_IDE , in AppCode – APPCODE_IDE , in Android Studio – STUDIO_IDE)
I would not turn autoformatting off, because in the majority of cases it is useful. But when this undesired autoformatting happens, I just do the following workaround:
Cancel the autoformatting (Ctrl+Z). The curly bracket is cancelled too.
Instead of typing bare }, I type it commented: //}.
Then just uncomment this line (Ctrl+/ or remove the slashes).
Profit! :)
I am so used to code like this:
if (...)
{
//somecode
}
that I simply have to change the following to the above:
if (...){
//somecode
}
I get a lot of code from other people that I have to go through and sometimes this is what I get. Is it possible to auto-format that in Visual Studio 2008 for C++?
Now some may think I should get used to it. Believe me, I am trying, but it is very distracting and I hope there is a simple solution.
VS2008 has a simple formatter (Edit menu, Advanced, Format Selection), but it doesn't move braces.
Check out astyle and its --style=ansi flag.