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

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.

Related

How can I configure Emacs to highlight C++ that violate a detailed code style?

I've been researching existing C++ code style tools and have yet to find any packages which will allow me to highlight sections of a file which break a detailed code style configuration. While there seem to be several options for basic code style settings (what should/shouldn't be indented, line length > some threshold, etc), other issues do not seem to be addressed. For context, I'm hoping to be able to recognize when I do the following:
{ on same line as function definition (should be next line)
{ on next line after if statement (should be same line)
no space between ) and {
no space between comparisons (should be a == b instead of a== b,a==b, etc)
consecutive new lines
type *var_name or type * var_name instead of type* var_name
and so on...
This style is heavily enforced on my team, and I am having difficulty minimizing inconsistencies. I'm looking for either an existing emacs tool which allow me to customize these settings extensively, or suggestions on how to create an emacs package myself identifies these errors.
As Noufal suggests, Flymake is one option.
Another is Flycheck. I switched from Flymake to Flycheck a few years ago and haven't looked back. Flycheck supports a large number of languages and tools, and seems to require less hand-holding than Flymake.
From its GitHub README:
Features
Supports over 30 programming and markup languages with more than 60 different syntax checking tools
Fully automatic, fail-safe, on-the-fly syntax checking in background
Nice error indication and highlighting
Optional error list popup
Many customization options
A comprehensive manual
A simple interface to define new syntax checkers
A “doesn't get in your way” guarantee
Many 3rd party extensions
For C and C++ code, Flycheck supports Clang and Cppcheck out of the box, and there is a plugin for Google's C++ style guide as well.
And of course you can add your own checkers if you wish.
If you can configure your tool to emit output in the format that can be understood by flymake, it should be able to do it.
Many tools such as gcc itself and others do this so that flymake works.

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
}

Vim go to std library method/function definition

I love some of the plugins affiliated with vim and I have installed most of them as git submodules (clang_complete, cvim, fugitive, NerdTree, pathogen, snipMate, supertab, taglist) However there are two basic featured that I cannot get working. i.e.
1) Example -> if I compile the following example
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
I can not figure out how to jump for example to the function open i.e. “myfile.open” from the std. library. If I go and paste the code to visual studio and click go to definition it is taking me directly to that place. I have read that this could be done with ctags, however I am asking myself if the autocompletion is already working i.e. when I type “myfile.” I see a whole bunch of methods that clang is giving me , so I guess that it should be some other better way to jump to the definition from vim something similar to (ctrl + ]) which should work out of the box.
2) In visual studio/.net whenever I type the dot . I see methods and when I scroll down the method it is supplying short description what is this method doing. Is there a way to enable something similar in vim. As far as I can tell this should be a property of the std library however I can not see any such thing in std.
You are confusing Vim with an IDE, I think.
You can't expect any IDE feature to be available, at least without some effort, in a (very powerful) text editor.
Vim's equivalent of "Go to declaration", <C-]> or :tag foo<cr> relies on the availabilty of one or more tags generated by exuberant-ctags or some compatible program against your code base and any standard libs. Because Vim doesn't come with such a tags file for (AFAIK) any language, you'll have to generate one by yourself and tell Vim where to find it. There's even a program, hdrtag, specifically written for that, I've never used it, though.
I think I've seen some autocompletion plugins show the signature of the method in the popup menu, some others (PHP, Python) open a small "preview" window. That's as far as you can get. There's also <C-w>} if you have an up to date tags file.
Again: Vim is not an IDE.
Make sure that ctags can find the source code that is in the std lib when you generate the index and you should be fine.
By default those index tools only adds code in the current dir (and its subfolders),
so you need to manually tell him where that code is installed on your system.

Eclipse copy code with syntax highlighting

I'm writing a document of programming guidelines for my developers team. I use MS Word. We work with Eclipse CDT (C++). I need to copy-paste C++ code with syntax highlighting from Eclipse to Word. I've tried Notepad++ and it can export text with syntax highlighting, but it's highlighting is limited to basic syntax (it doesn't know about defined class, enum etc...).
Eclipse syntax highlighting is very powerful and I wish to export directly from Eclipse to Word using it's syntax highlighting.
Is there any Eclipse plugin that achieve this purpose? Or some trick to do it (without taking a screen snapshot)?
When you copy the code to word document, you can choose the option "keep source formatting" and the highlighting will be the same as the one in eclipse.
EDIT:
As mentioned in the comments below, this won't work on folded code, so you can right click on the line numbers and select Folding > Expand All
EDIT 2:
Also mentioned in the comments, as of eclipse oxygen, the code is automatically copied with syntax highlighting
When I copy from Eclipse straight to PowerPoint some of the formatting is messed up. Bold and color seems to "keep going". But if I copy from Eclipse to Word -- and then from Word to PowerPoint (with the keep source formatting CNTRL-K) -- the formatting is correct. I recently upgraded Eclipse to Mars and Office to 2013. I still have to go through Word first.
I was trying it too and for some reason not every compile unit would keep the formating.
After a while I've seen that you also won't get the formating kept if there are parts of the code hidden, so for the ones that cannot make it work with the answers above, just make sure that there are no "+" signs on the left of your code (mine were where the imports at the beggining).
Copying of formatting is supported since version 3.2
Note that everything is copied: highlighting of spelling errors, marked variables/types/etc and underscoring of warnings/errors.
To avoid that, turn off spell checking, "Mark Occurrences" and "Report problems as you type" respectively.

Is there anything like GhostDoc for C++

When I'm developing in C#, I heavily use GhostDoc to speed up the process of commenting my code. I'm currently working on a C++ project and I haven't found an equivalent tool. I know about Doxygen, but from what I know it is used to create documentation outside the code, not comments in the code. Are there any good equivalent tools? I would prefer one that runs in VS, but I could handle one that works in any IDE.
(Before someone brings it up, I don't rely solely on GhostDoc to create comments. I just use it to create the starting point for my comments.)
I've written an add-in, Atomineer Pro Documentation, which is very similar to GhostDoc (it generates/updates documentation comments to save a lot of time and effort when documenting), but it parses the code directly for itself and thus is able to handle C, C++, C++/CLI, C#, Java and Visual Basic code, and doesn't require the surrounding code to be in a compiling state before it will work. It will also automatically add/update documentation for more tricky things such as exceptions thrown within the body of a method.
It runs under Visual Studio 11, 2010, 2008 and 2005, and supports Documentation-Xml, Doxygen, JavaDoc and Qt commenting formats, as well as the format/style of comment blocks and the auto-doc rules used being highly configurable. It has a number of other handy features such as aiding conversions of legacy doc-comments to the above formats, and word wrapping in doc-comments and normal block comments.
The above is just a summary of some key features - This comparison of features with other products serves as a more complete list of the many other features available.
Visual Assist helps by providing custom scripts executed while typing (or on other).
For example, you can have a script for comments like this :
/************************************************************************/
/* My comment : $end$ */
/************************************************************************/
That would be suggested (via a combo-box exactly like intellisense) when you start typing "/**" for example.
When you select this suggestion (via Enter/Space/Click - customizable), it will insert the script where your cursor is and just replace markers that are between '$' characters by special values (like the current file name for example).
Here the $end$ marker will make the cursor be at this position when the script is executed. This way, you continue typing smoothly. For example with the previous script set, typing exactly :
/** this is a test comment to show you one of the many features Visual Assit!
will simply give :
/************************************************************************/
/* My comment : this is a test comment to show you one of the many features Visual Assit! */
/************************************************************************/
It's really easy to customize and the behavior of the suggestion (read : intellisense++) system is customizable.
Visual Assist might do the job, though I'm not absolutely sure.