Indention in C++ Builder - c++

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.

Related

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.

AutoHotKey if statement not working

What is wrong with this?
if A_OSVersion = WIN_7 or A_OSVersion = WIN_XP
{
MsgBox, Win7 or XP
} else {
if WinActive("ahk_exe Explorer") {
!f::!h
}
}
The goal is to replace Alt+F with Alt+H in Explorer when I'm running Windows 8, 8.1, or 10, and I am testing on a Windows 7 machine, so the code shouldn't run at all, but the hotkey does and the MsgBox doesn't. Do I need to reorder things for it to work? The syntax looks right to me, but I've been having a hard time with syntax for AHKscript.
Kind comments are appreciated!
This
!f::!h
isn't a command, it's a key remap and thus, implicitly contains a return. You cannot use it within executable context. For details, see http://ahkscript.org/docs/misc/Remap.htm#Remarks somewhere below.
You wouldn't wanna have a return somewhere in between the lines which should be executed, would you.
(this was mostly a copy of my answer here)
For context-sensitive hotkeys, use the preprocessor directives:
#if (A_OSVersion = "WIN_7" or A_OSVersion = "WIN_XP") and not winActive("ahk_exe Explorer")
!f::!h
#if
#if winActive("ahk_exe Explorer") and !(A_OSVersion = "WIN_7" or A_OSVersion = "WIN_XP")
!f::!h
#if
To do it within normal script flow you will need to used the Hotkey Command but then it will not be a remap but a hotkey that sends another set of keys...

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.

Visual Studio Auto Format Curly Brace on Next Line

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.

Is there any way to make Visual Studio stop indenting namespaces?

Visual Studio keeps trying to indent the code inside namespaces.
For example:
namespace Foo
{
void Bar();
void Bar()
{
}
}
Now, if I un-indent it manually then it stays that way. But unfortunately if I add something right before void Bar(); - such as a comment - VS will keep trying to indent it.
This is so annoying that basically because of this only reason I almost never use namespaces in C++. I can't understand why it tries to indent them (what's the point in indenting 1 or even 5 tabs the whole file?), or how to make it stop.
Is there a way to stop this behavior? A config option, an add-in, a registry setting, hell even a hack that modifies devenv.exe directly.
As KindDragon points out, Visual Studio 2013 Update 2 has an option to stop indenting.
You can uncheck TOOLS -> Options -> Text Editor -> C/C++ -> Formatting -> Indentation -> Indent namespace contents.
Just don't insert anything before the first line of code. You could try the following approach to insert a null line of code (it seems to work in VS2005):
namespace foo
{; // !<---
void Test();
}
This seems to suppress the indentation, but compilers may issue warnings and code reviewers/maintainers may be surprised! (And quite rightly, in the usual case!)
Probably not what you wanted to hear, but a lot of people work around this by using macros:
#define BEGIN_NAMESPACE(x) namespace x {
#define END_NAMESPACE }
Sounds dumb, but you'd be surprised how many system headers use this. (glibc's stl implentation, for instance, has _GLIBCXX_BEGIN_NAMESPACE() for this.)
I actually prefer this way, because I always tend to cringe when I see un-indented lines following a {. That's just me though.
Here is a macro that could help you. It will remove indentation if it detects that you are currently creating a namespace. It is not perfect but seems to work so far.
Public Sub aftekeypress(ByVal key As String, ByVal sel As TextSelection, ByVal completion As Boolean) _
Handles TextDocumentKeyPressEvents.AfterKeyPress
If (Not completion And key = vbCr) Then
'Only perform this if we are using smart indent
If DTE.Properties("TextEditor", "C/C++").Item("IndentStyle").Value = 2 Then
Dim textDocument As TextDocument = DTE.ActiveDocument.Object("TextDocument")
Dim startPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
Dim matchPoint As EditPoint = sel.ActivePoint.CreateEditPoint()
Dim findOptions As Integer = vsFindOptions.vsFindOptionsMatchCase + vsFindOptions.vsFindOptionsMatchWholeWord + vsFindOptions.vsFindOptionsBackwards
If startPoint.FindPattern("namespace", findOptions, matchPoint) Then
Dim lines = matchPoint.GetLines(matchPoint.Line, sel.ActivePoint.Line)
' Make sure we are still in the namespace {} but nothing has been typed
If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace[\s\w]+)?[\s\{]+$") Then
sel.Unindent()
End If
End If
End If
End If
End Sub
Since it is running all the time, you need to make sure you are installing the macro inside in your EnvironmentEvents project item inside MyMacros. You can only access this module in the Macro Explorer (Tools->Macros->Macro Explorer).
One note, it does not currently support "packed" namespaces such as
namespace A { namespace B {
...
}
}
EDIT
To support "packed" namespaces such as the example above and/or support comments after the namespace, such as namespace A { /* Example */, you can try to use the following line instead:
If System.Text.RegularExpressions.Regex.IsMatch(lines, "^[\s]*(namespace.+)?[\s\{]+$") Then
I haven't had the chance to test it a lot yet, but it seems to be working.
You could also forward declare your types (or whatever) inside the namespace then implement outside like this:
namespace test {
class MyClass;
}
class test::MyClass {
//...
};
Visual Studio 2017+
You can get to this "Indent namespace contents" setting under Tools->Options then Text Editor->C/C++->Formatting->Indention. It's deep in the menus but extremely helpful once found.
I understand the problem when there are nested namespaces. I used to pack all the namespaces in a single line to avoid the multiple indentation. It will leave one level, but that's not as bad as many levels. It's been so long since I have used VS that I hardly remember those days.
namespace outer { namespace middle { namespace inner {
void Test();
.....
}}}