Eclipse / CDT auto-indent on new line only incorrect for structs? - eclipse-cdt

Using new Eclipse and CDT versions built into STM32CubeIDE. I have the built in formatter options adjusted and use clang-format as my main beautifier. Everywhere I can see in the Window >> Preferences menus I have indent turned to 2 spaces-only.
Next line indent works correctly with everything but structs which the indentation seems to be doubled.
It doesn't seem to be indenting 2 units twice, because pressing tab moves me 4 spaces while inside a struct block. Clang-Format with CTRL + SHIFT + F does fix the incorrect formatting, but I'd rather a proper solution.
Either it's a bug, or somewhere this is yet another setting for "4 space indent but only while inside a struct block"?
See examples:
typedef struct
{
//New lines created inside the block start here, indented at 4 and not 2
//I get here if I press tab from the start column
//This is where it should intent to, manually pressed spaced twice
} some_new_t;
void foo()
{
//Correct
}
if (something)
{
//Correct
}
while(1)
{
//Correct
}
#ifdef TEST
//Doesn't indent, that's fine
#endif
EDIT: Applies to unions as well

Figured it out. This was not a 2 spaces vs 4 spaces issue, rather double indent when you only wanted one.
For some reason CDT has a lot more references to C++ and still Java than C and this was labeled under something misleading.
Under Window, Preferences, C/C++, Code Style, Formatter >> Edit >> Indentation >> Indent there are two options that can be checked.
'public', 'protected', 'private' within class body
and
Declarations relative to 'public' 'private'
You can check ONLY ONE of these for C structures and unions to indent at your chosen width. For whatever non-C reason, each one counts as including one indent width.

Related

IntelliSense bug c; changing to continue;

I am using VScode with intelliSense for programming in c++. When I type a line ending with c; for example
int a=b+c;
Intellisense still offers me option continue;, so when I hit Enter to go to the next line, it changes c; to continue;, what I obviously don't like and don't want.
In general I would expect it wouldn't offer any options after semicolon. Can I somehow change this setting?

CLion inconsistent auto indentation for line comment

There are some weird auto indentation going on in CLion that I don't understand why. Here is an example
int i1(5); // some comments, then I hit Enter
// auto indentation goes here. I hit Enter again
// It goes here
float f = 5; // some comments, then I hit Enter
// then auto indentation goes here. I hit Enter again
// It then goes here.
float f2 = 6;
// If I don't comment on the previous line, there is no weird indentation
It seems like whenever I use = to initialize an variable, and then add comment at the end of the line, it will give me extra indentation on the next line. But in all other instance, it will not give me extra indentation.
I recently installed CLion and it was using default code style. It seems like the the extra indentation is controlled by the continuation indent setting, but I only want this continuation indent setting be application to codes, not comments. Where can I change me setting to disable the continuation indentation for comment when using = for assignment?
This bug has been fixed in CLion 2018.2 EAP (https://www.jetbrains.com/clion/nextversion/ ).
I personally uninstalled CLion 2018.1 because of this bug, since the EAP was stable enough.

Opening code written in emacs on Xcode appears badly indented

This is my first post on stack overflow, so please forgive me for any mistakes.
I learned c++ with Xcode and recently started working with a group that uses Emacs. This group has a huge code in c++ and so I did a CMake interface to generate a project in Xcode. What happened is that the code appears badly indented in Xcode. For instance, these lines in emacs:
if ( argc > 4 ) {
std::string argument( argv[arg_index++] );
// NOTE: file_name should NOT be "aboveCrack" or "belowCrack"
if ( argument == "aboveCrack" ) {
surf_to_draw = CrackMn3DGraphDX2::EAboveSurface;
}
else if ( argument == "belowCrack" ) {
surf_to_draw = CrackMn3DGraphDX2::EBelowSurface;
}
else {
// argument 4 is comp. crack surface output name
got_file_name = true;
postCompSurface_file_name = argument;
}
}
if ( !got_file_name && argc > 5 ) {
got_file_name = true;
postCompSurface_file_name = argv[arg_index++];
if ( argc > 6 ) {
// get comp. crack surface output style
postCompSurface_style = argv[arg_index++];
}
}
Look like this in Xcode:
if ( argc > 4 ) {
std::string argument( argv[arg_index++] );
// NOTE: file_name should NOT be "aboveCrack" or "belowCrack"
if ( argument == "aboveCrack" ) {
surf_to_draw = CrackMn3DGraphDX2::EAboveSurface;
}
else if ( argument == "belowCrack" ) {
surf_to_draw = CrackMn3DGraphDX2::EBelowSurface;
}
else {
// argument 4 is comp. crack surface output name
got_file_name = true;
postCompSurface_file_name = argument;
}
}
if ( !got_file_name && argc > 5 ) {
got_file_name = true;
postCompSurface_file_name = argv[arg_index++];
if ( argc > 6 ) {
// get comp. crack surface output style
postCompSurface_style = argv[arg_index++];
}
}
Which is impossible to program with.
I searched and apparently it has something to do with the tabs in Emacs. Based on this, one fix I could find was to open each file in Emacs and do C-x h (mark all) followed by M-x untabify. This transforms the tabs in spaces and everything looks good in Xcode.
The problems with this idea are that it requires to change the files one by one and it won't stop this from happening again in the future.
Therefore, my question is: is there a way to open the Emacs indented files in Xcode preserving the indentation?
Many thanks!
Nathan Shauer
The first setting that you need to put in your .emacs is: (setq-default indent-tabs-mode nil). This will make sure emacs uses spaces instead of tabs for indentation.
Also, I created a tiny function:
(defun rag/untabify-buffer ()
;; get rid of all the tabs in a buffer
(interactive)
(untabify (point-min) (point-max))
nil)
Add this to before-save-hook and this will make sure all the files will be untabified when you make a change and save a file. Once you've untabified all files, you can remove the hook
No. While it is possible to use emacs to make these changes or even a number of other tools which can automate such changes, it won't really fix your problem as you will likely have to do it every time you check out the code from version control. Depending on the version control system used, it is also possible that doing such formatting changes will result in the code appearing to be modified, which will result in larger checkins and make other useful tools less useful because more will appear to have been changed than was actually changed. This will likely frustrate other project members.
There are two basic approaches, but one depends on the version control solution being used by the project. The first solution is to get the project to agree on a coding standard which specifies either that normal spaces must be used for indentation or that tabs are to be used. The problems you are seeing are primarily due to a mix. Emacs is able to handle this sort of mixed formatting quite well, but other editors, like Xcode are not so smart.
The other approach, which can work quite well because it doesn't rely on everyone following the standard is to configure the version control system to translate tabs as part of the checkin process into spaces. How this is done depends on the version control system being used.
Essentially, this is a problem which needs to be addressed at the project or version control level. Anything you do will only need to be repeated every time you do a fresh pull from version control for any files which have been modified. Fix it at the repository level and the issue will go away.

Kate (text editor) indentation, c++

I use the kate text editor for writing c++ code. I really like the editor except for its indentation behavior which drives me mad. I have the following problem: If I want to write code like
if( true )
{
//code
}
the indentation messes everything up initially: instead of inserting a tab and jumping to the position marked "//code" when hitting enter, kate just inserts a single blank space. So to describe it more in detail: You start from
if( true )
{//your cursor is here
}
and on pressing enter, kate produces something like
if( true )
{
[ ]//your cursor is here
}
where '[ ]' stands for a single blank space. But instead, I want kate to insert a tabulator to give the result indicated at the start. Or, to repeat it more verbosely, I want that kate gives me
if( true )
{
<tabulator>//your cursor is here
}
on hitting enter. I have played around with all settings and can not make it work. It drives me crazy. I selected "default identation mode normal", "Ident using tabulators" (8 characters). Does anybody know how to customize this behavior? I looked up the katerc file but couldn't find any options that would help me...
edit: I should add that it would be ok if kate would just give me
if( true )
{
//your cursor is here
}
on pressing enter. But this additional blank space is absolutely annoying.
Ok, I tried for half an hour, I don't know why I found out how to do it right AFTER posting the question :). So in case anybody has the same issue, here is the "solution": I missed that kate seems to have a global setting for the indentation mode as well as a local one for every file. In my case - for some reason - my file had special indentation options set. You can alter them via the menu bar by chosing "Tools -> Indentation". This local option overrides the global one! Or the global one is just the default for the local options, I don't know exactly...
You can create a config file .kateconfig and add the variables kate: replace-tabs off; tab-indents: true;
More on this in the manual.

Displaying Up/Down Arrow C++ on Windows

So unfortunately from what I have read, there isn't a hex code for an up and down arrow. There is only a "friendly code."(See bottom of linked page) Does anyone know how to use this friendly code? Ideally, I just want a printf() or std::cout statement that prints the up or down arrow, in C/C++.
Thanks! http://www.yellowpipe.com/yis/tools/ASCII-HTML-Characters/
Answers that assume a Unicode environment will not work with the Windows console at all, because it does not use Unicode. It uses a code page to determine which characters can be displayed and what the character codes are; on my US-based Windows 7 system that is Code page 437. You can see that the arrows are at the top of the list, but unfortunately they're in the control character range. This means they are stripped out of normal output entirely. You need special console output functions to display them.
Edit: It appears you don't need special console output, only a few characters like '\x0a' are stripped. The following string should print all 4 arrows: "\x18\x19\x1a\x1b".
Disregard this answer if you're using the Windows API and not a console program.
You need to use a std::wstring:
#include <iostream>
#include <string>
int main()
{
std::wstring s(L"←→↑↓");
std::wcout << s << "\n";
}
Note that there is a difference between L"←→↑↓" and "←→↑↓". The latter is invalid, as neither ← nor → and certainly not ↑ and ↓ are valid ASCII characters.
On my machine, this gets printed as <-->??, probably because the terminal doesn't support the characters.
Simply copy and paste the text of the arrow keys from the code below and add it into your code.
#include<iostream>
using namespace std;
int main(){
// for up, down arrows.
cout<<" ↑ ↓ ";
return 0;
}