Is it possible to customize the Visual Studio autoformat? - c++

I'm using Visual Studio to develop a C/C++ library. I would like to know if there is a way to customize the autoformat tool (Ctrl+K,F) so that:
It automatically break lines that are bigger than 120 columns
Format a function/method parameter the following way:
void myFunction(int parameterA,
float parameterB,
string paramterC)
Format the brackets the following way:
void myFunction()
{
// Code goes here...
}
To summarize, I want something similar to what Eclipse does with it's autoformat (Ctrl + Shift + F) that can be customized by editing a XML file.

If those options are not good enough for you get UniversalIndentGUI it is a frontend for a whole slew of code formatting engines, you should be able to get the style that you want from it

Tools | Options | Text Editor | C++
That is all I know that lets you edit how the code is formatted and the auto formatter uses these options (as few as they are).

Related

Automatically format Curly Braces on Same Line in C++ VSCode

I have a problem with the way intellisense autocompletes snippets in VSCode, currently, when I write an if statement, it autocompletes to:
if (condition)
{
// some code
}
However, I would like it to autocomplete to:
if (condition) {
// some code
}
If I use my formatter in VSCode, it formats it to the convention above, however I would like to avoid having to press a separate command for this to occur. Optimally the intellisense snippet would just format it like this by default.
Any help with this would be appreciated.
This is a bigger subject than you might think
So typically with C++, the preferred formatter is the CLang Formatter. CLang's tool is so good, ECMAScript's ESLint has been taking notes, and has implemented many of the same rules found in the CLang formatter, specifically the rules that have to do with brackets.
VSCode has a default formatter that can be used for C++. And this really comes down to what your using.
Problem:
What your asking for, essentially, is for your brackets to be inserted using a certain style, rather than to be auto formatted to a specific style.
Solution
The snippets you are using are obviously predefined, and VSCode supports writing your own snippets.
Like this:
// FILE: #(".../.config/code/user/snippets/cpp.json")
{
"Print to console": {
"prefix": "if(",
"body": ["if($1){\n$2\n}", "$2"],
"description": "If Condition"
}
}
The problem with snippets is: You would need to defined every snippet you use, styling each one.
VSCode Snippets Documentation (official)
Snippet files are extensible, meaning they can be turned into VSCode extensions. Because a snippet library is quick & easy to write in comparison to most other extensions, there are a lot of them available in the Visual Studio Marketplace, and are all 100% Free to download.
You could download a pack that contains the snippets you wont, and then just ad the '\n' line character where needed, throughout the document.
Preferred Solution
It may not be what you want. But the best way to do this, is to do what 90% of C/Cpp developers do who write c/cpp in VSCode; use CLang formatter, and configure VSCode to format your code when you save. I am always hitting CTRL + S because I don't like throwing work down the drain because I forgot to save. And my if statements are all auto formatted exactly as you displayed the example in your question.

Get intelliSense info inside VS extension

I'm trying to write an extension to IntelliSense in Visual Studio 2015.
Right now you can write code something like
constexpr auto res = offset(ClassName, fieldName);
And when you put your mouse over the res variable, VS IntelliSense will show you the result of the method, e.g. offset of the field fieldName inside ClassName class.
Is there a way to:
get information about the offset from the IntelliSense or
evaluate custom code on the selected text from VS extension?
Is there a way to:
get information about the offset from the IntelliSense or
evaluate custom code on the selected text from VS extension?
Unfortunately, the C++ Intellisense feature doesn't allow for something like this. If you were working with the Roslyn based C# or VB.NET compilers, the story is a bit different, as you can access the same pipeline the C# or VB.Net Intellisense features use. But C++ Intellisense is an entirely different beast

Can WebStorm automatically format the code when I am writing?

In Visual Studio when I type some special characters like ;} it will automatically format related code segment, but in WS, nothing happens. I tried to find that option but ended without fruits.
// e.g. In VS, if we type something like:
var abc=123
// and then type ; it will be formatted as:
var abc = 123;
I am trying to understand why many people love WebStorm as a JS IDE. :)
PC: Ctrl + Alt + L
MAC: Command + Alt + L
Here you can find a nice tutorial by John Lindquist: http://www.youtube.com/watch?v=xIxDar8CbJA
This option is provided in WebStorm 8 (coming soon) - see IDEA-120066
Haven't used VS recently so I'm not sure what ;} does, but WebStorm can definitely expand templates. Above a function declaration, try typing /** and then Enter, for example - WebStorm will automatically generate a JSDoc heading.
Look also in the IDE Settings under Live Templates -> JavaScript. Typing us<Enter> will insert a "use strict;" etc.
If you want to reformat code, there's an option under Code -> Reformat Code to reformat the selected text (or the entire file). Though probably it's best to get used to writing code abiding by JavaScript style guidelines :) Comes in handy when you don't have an IDE at hand, e.g. editing on GitHub, in a terminal, on StackOverlfow (cough) etc.
Also, you should enable JSHint, which would highlight abc=123 as needing style improvements.
select Code > Reformat Code from the main menu, code formatting popup will appear.

How to write C++ code to fit within LaTeX listings margins

I'm using the listings package in LaTeX to include source code, but some of my source code runs off to the right and so some lines are obviously too long. Does anyone know if there is a way to mark the margin when I'm writing the code, perhaps depending on the \setlength parameters I set in my LaTeX environment so that I know to stop there? I'm using Microsoft Visual C++ 2010 and TeXworks for this particular problem, but I often just use Emacs to write both C++ code and LaTeX.
Here's a snippet from LaTeX I'm using:
\lstdefinestyle{C++}
{float=h!, frame=single, language={[Visual]C++}, numbers=left, numberstyle=\tiny, tabsize=2}
\lstinputlisting[style=C++, label=lst:RANDU, caption=RANDU C++ Implementation]{RANDU.cpp}
Here's a snippet of what the output of this looks like, I'd like to know where to end lines in my IDE so that it doesn't exceed the right border:
Thanks in advance!
You could use packages like fill-column-indicator or column-enforce-mode. Both available also with MELPA.
The downside is that you will need to manually update the number of columns you want when you update your \setlength, unless you write some code to do it for you.
Use: breaklines=true in the options. Example:
\lstdefinestyle{C++}
{
float=h!, frame=single, language={[Visual]C++}, numbers=left, numberstyle=\tiny, tabsize=2, breaklines=true
}

Visual Studio indentation of function arguments

Visual Studio 2010 indents the following C++ code as:
if (Foo(arg1,
arg2))
{
}
Is there a way to change Visual Studio formatting rules to indent the code as below:
if (Foo(arg1,
arg2))
{
}
No, there's no way "out of the box" to force Visual Studio to indent code that way. It's always going to indent wrapped function parameters with only a single tab.
It turns out that such a style is in accordance with Microsoft's general coding guidelines, and probably why they've written it that way. I don't much care for it either, though, also preferring your style.
But it turns out that you only have to manually indent the first wrapped parameter. Subsequently, when you press Enter, Visual Studio will automatically start the next line underneath your first carefully lined up parameter.
Also remember that (if you've already written the method definitions), you can select multiple lines at a time and use the Tab key to line them all up. You don't have to do one at a time.
In general, there are unfortunately extremely limited code formatting options available for C/C++ code in Visual Studio. The C# programmers get a lot more goodies. You might be able to invest in an add-in or extension like Visual Assist X that gets you more features in the IDE.
I think this is a duplicate of Automatic indentation of arguments list on multiple lines in Visual Studio. In which I provided an answer for Visual Studio 2017.
Under menu Tools → Options → Text editor → C/C++ → Formatting → Indentation → "Within parentheses, align new lines when I type them".
Choose the option "Align contents to opening parentheses.
Try using a tool called Artistic Style (short for astyle). It can customize almost all your required code format. Regards to your indentation format, check its document on max‑instatement‑indent.
Furthermore, this tool can be easily integrated into Visual Studio .NET (check this for a quick setup).