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

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.

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.

Customize Eclipse CDT to use Ratliff style for auto formatting

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.

Switch autocompletion for enum in Visual Assist or Vanilla Visual Studio

Is there anyway to have a switch expand with all the values of an enum using Visual Assist or Vanilla Visual Studio?. The included snipsets just insert a basic switch with just a default entry. I'm using last visual studio 2013 update.
VS 2013 can build a switch statement for an enum using a default snippet. (Note: use a default VS snippet, not a VA Snippet.)
As you type "swit", accept the suggested code snippet from VS. Replace "switch_on" with your enum and press Enter. Your statement will expand. (You need two Enters if one selected your enum from a listbox.)
After the switch has initially been generated, if you add new enum values, then you can use the Add Missing Case Statements command in Visual Assist to populate the switch statement with the new values (shift+alt+q, m).
In Visual Studio 2015, the auto-completion of switch cases is defeated if the discriminator is declared with const as in this example:
auto const enu = static_cast<MediaType>(discriminant);
Remove const, and voila--it works again!
(Then you can put the const back in, if you wish.)

Function definitions missing from intellisense in Visual Studio C++ 2005-2013

The following problem plagues one of my projects for a long time:
Some function definitions (from .cpp files) are excluded/hidden from intellisense!
It is not possible to "Goto Definition" for those functions, nor are the listed in the Navigation Bar.
The functions do appear in the autocompletion list, though. The problem is for .cpp files only, the .h files are parsed fine. 'Goto Declaration' works, too.
This is the same since 2005, with every new version, I was hoping for a fix, but it does not seem to be regognized as a bug by anyone else.
UPDATE:
I have tracked this down to the following: All functions containing a certain macro are not recognized by intellisense. The original macro was
#define forlist(x,list) for( auto x= list.begin(); x.valid(); ++x)
but you can also use the simplified test case
#define fortest(x) for( auto x= 1; x< 2; ++x)
void myclass::TestFN()
{
fortest( g )
{
g;
}
}
Next step would be to find a workaround (or try to go through micrsoft bug reporting).
Please don't rant too much about this macro. This is existing code of a list implementation which I am not able to change. I could just NOT use the macro, but I still think this is a VS bug.
One funny thing is, that the following (really ***ic macro) works fine:
#define fortest(x) for( auto x= 1; x< 2; ++x) {
void myclass::TestFN()
{
fortest( g )
g;
}
}
Could it be that intellisense treats case 1 as an illegal local function definition?
(see http://connect.microsoft.com/VisualStudio/feedback/details/781121/c-intellisense-mistakes-loop-expression-for-function-definition)
The following work fine, too
#define fortest(x) for( auto x= 1; x< 2; ++x)
void myclass::TestFN()
{
fortest( g )
g;
}
As usual, interest in my question ebbed up after a couple of hours, so I had to figure it out by myself...
We just have to use the concept of cpp.hint files.
Basically you have to put the troublesome macros into a file named cpp.hint and put that file in your solution directory (which did not work for me)
OR in a parent-directory where your code files reside in. (worked for me)
In that file we just put the troublesome macros WITHOUT right-hand-side, so e.g.:
#define forlist(x,list)
NOTE: Your must reset IntelliSense cache for use new data from changed cpp.hint file. You should:
delete ipch folder (usually placed in Solution folder).
delete all *.sdf files in Solution folder.
delete all *.VC.db files in Solution folder or in ipch folder.
For more advanced macros (like having 'start' and 'end' macros for code blocks), there are some other tricks.
The original link is:
http://msdn.microsoft.com/en-us/library/dd997977.aspx
The reason for the trouble is that Intellisense performance would (potentially) decrease dramatically if it had to parse all macros in a project, so it only parses those given explicitly in 'cpp.hint'.

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.