Customize Eclipse CDT to use Ratliff style for auto formatting - eclipse-cdt

How to customize Eclipse CDT to use Ratliff style for auto formatting?
Ratliff Style of formating code indents the terminating brace to the same level as the contents of the block:
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
doSomething(i);
}
else {
doSomethingElse(i);
}
}
Eclipse CDT allows a number of common styles to be selected for auto formatting. Ratliff is not in the list but you can modify them to meet your specific requirements:
except there is no option in the list of choices to have the end brace align with the block contents:

No one has implemented this option in CDT's formatter.
CDT is open source, you're welcome to contribute a patch that implements it.
However, given how little CDT's formatter is maintained in general these days, a better use of your time might be to contribute support for the Ratcliff style to clang-format (which also doesn't currently support it, as far as I can tell), and use clang-format in CDT via a plugin like CppStyle.

Related

How to get the next enum value from an enum in protobuf?

I have a protobuf message with non-consecutive enum values something like this:
message Information {
enum Versions {
version1 = 0;
version2 = 1;
version3 = 10;
version4 = 20;
version5 = 30;
}
}
I want to have a C++ function GetNextVersion() which takes in one enum version and gives the next version as output. For eg: GetNextVersion(Information::version4) should give Information::version5 as output. Is there any inbuilt and easy method to do this?
You can use protobuf's reflection to achieve the goal:
Information::Versions GetNextVersion(Information::Versions ver) {
const auto *desc = Information::Versions_descriptor();
auto cur_idx = desc->FindValueByNumber(ver)->index();
if (cur_idx >= desc->value_count() - 1) {
throw runtime_error("no next enum");
}
auto next_idx = cur_idx + 1;
return Information::Versions(desc->value(next_idx)->number());
}
int main() {
try {
auto ver = Information::version1;
while (true) {
cout << ver << endl;
ver = GetNextVersion(ver);
}
} catch (const runtime_error &e) {
cout << e.what() << endl;
}
return 0;
}
Is there any inbuilt and easy method to do this?
I see no easy method to get that.
But I can suggest metaprogramming approaches (at least on Linux) with C++ code generation.
You could, assuming you have access to the source code of protobuf-c :
write some GNU gawk script to parse that C++ code and generate the C++ code of GetNextVersion
perhaps write some GNU sed (or a Python one) script doing the same.
write some GCC plugin and use it to parse that C++ code and generate the C++ code of GetNextVersion
write some GNU emacs code doing the same.
wait a few months and (in spring 2021) use Bismon. I am developing it, so contact me by email
extend and adapt the Clang static analyzer for your needs.
extend and adapt the SWIG tool for your needs.
extend and adapt the RPGGEN tool for your needs.
use GNU bison or ANTLR to parse C++ code, or design your domain specific language with some documented EBNF syntax and write some code generator with them.
You could also keep the description of enum Versions in some database (sqlite, PostGreSQL, etc...) or some JSON file or some CSV file (or an XML one, using XSLT or libexpat) and emit it (for protobuf) and the source code of GetNextVersion using some Python script, or GNU m4, or GPP.
You could write a GNU guile script or some rules for CLIPS generating some C++ code with your protobuf description.
In a few months (spring 2021), the RefPerSys system might be helpful. Before that, you could contribute and extend it and reuse it for your needs.
A pragmatic approach could be to add a comment in your protobuf declaration to remind you of editing another file when you need to change the protobuf message and protocol.
No, there isn't.
You define your own data type, so you also must define the operators for it.
So, your GetNextVersion()method contains that knowledge how to increment the version number. If you had decided to use an integer, then the compiler knows already how to increment that, but you wanted something special and that is the price you have to pay for it.

How to stop visual studio 2017 c++ from autocompleting ) while in the middle of FOR statement?

So every time I write something like
for (auto i = 0; i < my_vector.size();
the moment I add the semicolon after a function call in a for statement, it turns into
for (auto i = 0; i < my_vector.size());
and then I have to manually delete the automatic bracket close because I havent finished the for statement, super annoying.
I don't remember earlier VS versions doing this.
Anyway I tried going in options -> text editor -> c/c++ -> formatting -> general, I tried switching off "Automatically format statement when I type a ;" and also "Automatically format braces when they are automatically completed". Both had some minor effect but the problem I described was still happening.
I like autocomplete most of the time, I find it useful so I don't want to turn it off completely but how can I make it stop doing that one thing?
Update the Visual Studio. That particular (VS 2017) bug is now fixed starting with the VS 2017 15.4 Preview 2 update.

Visual Studio 2013 C++ auto indenting doesn't work well

For some reason, after I type the following block in Visual Studio 2013:
for (int i = 0; i < m; i++)
if (some_condition)
{
}
VS 2013 automatically indents the code to this:
for (int i = 0; i < m; i++)
if (some_condition)
{
}
If I put the if between curly brackets, it formats the code correctly. This behavior is very frustrating. I don't remember having this issue in VS 2012.
If I disable auto formatting, it doesn't even put the cursor at the same indent level as the line above. Even notepad++ works better than VS.
Is there a way to fix this? Maybe to import the indenting rules from VS2012 which worked just fine?
Bug is fixed with VS 2013 Update 1. Try install it Tools -> Extensions and Updates -> Updates -> Product Updates -> Update 1
Can you change settings as below and check
In Tool->Options->Text editor->C/C++->Format-> Indentation (un-check this option)
and reformate code block once or try new code.

Auto formatting - Eclipse C++

Hi all I'm trying to stop the auto formatting in eclipse from indenting my opening and close brace after a function that has a throw() statement on the end.
e.g
void function(std::string param) throw()
{
}
after auto format will look like this:
void function(std::string param) throw()
{
}
But it will carry on adding another tab to the front of the brackets every time I autoformat my code. Anyone know how I can turn this feature off, or is it a bug
Eclipse info:
Version: Juno Release
Build id: 20120614-1722
using CDT
Note:
I am using my own code style profile.
The name of that style is Whitesmiths - Try to change it to Allman.
In C/C++ perspective mode, Goto Window > Preferences > C/C++ > Code Style
Choose BSD/Allman or whatever you want.
You might want to consider Astyle, this is a eclipse plguin, an interface to a wondeful code beautyfication command line tool. you can set the indentation, formatting, etc.
Very handy tool.

Indention in C++ Builder

In C++ Builder, how can I make sure that even correctly nested code like:
void func () {
...
..
)
C++ Builder correctly nested only doing so:
void func ()
{
...
...
}
This is very stressful, because I always have to correct by hand. So how can I make which indents code as well in the first instance?
The code formatter in C++Builder 2010 should do this automatically for you. (It is invoked with CTRL-D) You’ll have to set the preferences to how you like your code to be formatted, but this is a real time saver new with this most recent release.
Select the block, then press CTRL+SHIFT+I. This is in Borland C++ 6.
I am using this free tools:
http://www.cnpack.org/index.php?lang=en
using tabs to shift marked blocks to left or right.
I don't have a copy of this to run, but the documentation mentions "Indentation, Spaces, and Line breaks" as some of the options under the formatter tab of the "Tools > Options" dialog.
What you're looking for is probably under the "Line Breaks" section.