VS2013 auto-indents closing bracket on new line, why? - c#

I cannot seem to find a way to disable the closing bracket to be auto-indented with all the other args. Example (note the spaces in front of closing bracket)
func(
arg1,
arg2
)
I want to have it aligned with the function call.
func(
arg1,
arg2
)
But I cannot find the setting in VS formatting options. I am pretty sure my previous VS instances did not have this auto-setting. Now, when I finish typing arg2 and press ENTER, it auto aligns me to the wrong spot.
EDIT: All autoformatting is disabled and tried pretty much all in Formatting options. Maybe the issue is with extensions? I am using Resharper.

So I finally found the answer, the reason why VS formatting options did not take effect was because the formatting options from ReSharper took precedence.
The setting is in Resharper -> Editor -> Editor Behavior
"Auto-format on closing brace"
"Auto-format on semicolon"
"Auto-insert closing brace"

Related

Jump to loop start/end shortcut in Visual Studio?

Is there any keyboard shortcut I can use to go up to start of, for example, foreach loop which I'm editing at the moment?
And maybe there is also a shortcut to go to the end of the loop?
Maybe there is something like that when I'm using Resharper, if not in vanilla Visual Studio?
I'm not writing about debugging! Just writing the code.
Sorry, but I can't find anything about this, but it seems like some basic functionality IDE should have...
You can use Ctrl+]: it
Moves the cursor to the matching brace in the document. If the cursor is on an opening >brace, this will move to the corresponding closing brace and vice versa
You can certainly jump between the braces. If you have your cursor either side of one of the braces (at the start or end of your loop) use CTRL + ] to jump to the other one. This will work on any set of braces, not just loops.
Do you mean you want to move the cursor to the starting/ending bracket?
I don't know of one that does this, although you can collapse the loop to see both what happens before and after at the same time.
although, to be fair, if your loop has so many lines that you can't see both brackets at the same time, it might be a good idea to make the code inside the brackets shorter (for example, with an extra method:
foreach (string entry in entryList)
{
AnalyseValue(entry);
}
You can create bookmarks on specific lines of code (Ctrl+K, Ctrl+K) and navigate to your next book or previous bookmark (Next Bookmark => Ctrl+K, Ctrl+N) (Previous Bookmark =>
Ctrl+P)

How to automatically move opening bracket on a new line and add closing curly bracket in Visual Studio 2015?

Meet long time reader, first time poster :P
At the moment I have to manually type opening curly bracket, then closing curly bracket and only after that IDE will format curly brackets as I want, it will put curly brackets on a new lines:
http://i.imgur.com/19FWufk.gif
What I want is when I type opening curly bracket and then press enter, I want IDE to automatically put opening curly bracket on a new line, then add empty line, then add closing curly bracket and focus on empty line in the end.
I don't want it to do all of that when I put opening curly bracket and then type some text afterwards(when I initialize class, for example).
I don't want any other automatic completion of other characters(quotes, parentheses, etc.)
My settings atm:
http://i.imgur.com/JwH1mSx.png
http://i.imgur.com/Ts3puFV.png
http://i.imgur.com/oolzFor.png
http://i.imgur.com/Nozl1aC.png
Looks like I can't achieve what I want using default VS settings.
When I check "Automatic brace completion" it starts to automatically complete quotes and parentheses, which is really annoying because I have to switch to arrows or mouse all the time to move cursor to a proper position, so I can continue to enter code.
Is there some extension for Visual Studio to achieve what I want? I tried productivity power tools so far and couldn't achieve it either:
https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.ProductivityPowerTools
Thanks in advance!
You can try ReSharper(30 days free trial) or Code maid extension which is really good at reorganizing code (http://www.codemaid.net/)
This may also help
How to use Visual Studio automatic brace completion to surround an existing block?
Looks like I've found a solution. I've downloaded and installed ReSharper and now it does what I wanted it to do: it puts an opening curly bracket on a new line, then add empty line, then add closing curly bracket and focusing cursor on empty line.
And it doesn't automatically complete quotes and round brackets:
To achieve that I had to turn off automatic brace completion in VS options:
And then I had to configure options of ReSharper:
And now finally it works as I wanted it to! Took me several hours to achieve it, though.

Format document in Visual Studio to insert braces?

Is there a way to "Format document" in Visual Studio to insert braces around single-statement blocks for C# code? For example this:
if (x)
y();
... would become something like:
if (x) { y(); }
The auto formatting seems to deal with indentation but not this brace insertion. Is there a way to do it?
Actually, there seems to be something built-in to Visual Studio to do this.
If you go to Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines and make sure that you have Place open braces on new lines for control block checked.
Then, go to your document and use the key combination CTRLKD, this should reformat your document and add the curly braces.
In case you have resharper you can configure it to force braces depending on your criteria.
Than in the existing code press ctrlaltshiftf, it will format whole file. Or select just part of the code, in this case resharper will format just selection
P.S. ctrlaltf opens clean up dialog. You can configure cleanup options.
I gave a vote to Gimly's answer as it is pretty much correct. These things change over time of course. I would have added a comment but I wanted to paste in some images. Location of settings in VS2019 is at:
Uncheck the appropriate check boxes and select OK.
The shortcut key did not work, which is a shame because, I love them! This documentation suggests that you use
CTRL-K, CTRL-E or, you use the broom icon:
.
Neither option worked for me perhaps because, that is not the intention, given the list of options. However, if you mark and cut all your code and then, paste the code back into the file, the new standard is adopted.

Resharper closing parenthesis indentation on function with multiple arguments

I have some lines of code in c# that Resharper indents like this:
Console.WriteLine("Hello");
this.MySuperFunction(
argument1,
argument2,
argument3
);
Console.WriteLine("World");
Due to my personal coding style, I would like the above to appear with the closing parenthesis (or brace) without any indentation, like so:
Console.WriteLine("Hello");
this.MySuperFunction(
argument1,
argument2,
argument3
);
Console.WriteLine("World");
I tried playing with the various options on Resharper, but couldn't find any. Is there a way I can make this work?
It seems this is not currently possible with ReSharper. There is however an open issue that appears to be slated for release with version 9. If you're interested in raising awareness to the issue and hope to ensure it gets included in the release I recommend you create an account and up vote the issue.
Update to 2016.2 version. An option Intent method calls' parenthesis appeared in this version:

Emacs typeover skeleton-pair-insert-maybe

In Eclipse, editing Java code, if I type an open-paren, I get a pair of parens. If I then "type through" the second paren, it does not insert an additional paren. How do I get that in emacs?
The Eclipse editor is smart enough to know, when I type the close-paren, that I am just finishing what I started. The cursor advances past the close paren. If I then type a semicolon, same thing: it just overwrites past the semicolon, and I don't get two of them.
In emacs, in java-mode, or csharp-mode if I bind open-paren to skeleton-pair-insert-maybe, I get an open-close paren pair, which is good. But then if I "type through" the close paren, I get two close-parens.
Is there a way to teach emacs to not insert the close paren after an immediately preceding skeleton-pair-insert-maybe? And if that is possible, what about some similar intelligence to avoid doubling the semicolon?
I'm asking about parens, but the same applies to double-quotes, curly braces, square brackets, etc. Anything inserted with skeleton-pair-insert-maybe.
This post shows how to do what you want. As a bonus it also shows how to set it up so that if you immediately backspace after the opening char, it will also delete the closing char after the cursor.
Update:
Since I posted this answer, I've discovered Autopair which is a pretty much perfect system for this use case. I've been using it a lot and loving it.
To summarize what I did, I looked at this post, and took what I wanted out of it. What I ended up with was simpler, because I didn't have the additional requirements he had.
I used these two new definitions:
(defvar cheeso-skeleton-pair-alist
'((?\) . ?\()
(?\] . ?\[)
(?" . ?")))
(defun cheeso-skeleton-pair-end (arg)
"Skip the char if it is an ending, otherwise insert it."
(interactive "*p")
(let ((char last-command-char))
(if (and (assq char cheeso-skeleton-pair-alist)
(eq char (following-char)))
(forward-char)
(self-insert-command (prefix-numeric-value arg)))))
And then in my java-mode-hook, I bound the close-paren and close-bracket this way:
(local-set-key (kbd ")") 'cheeso-skeleton-pair-end)
(local-set-key (kbd "]") 'cheeso-skeleton-pair-end)
I use paredit-mode, which does this and a lot more.
ParEdit sounds like it would handle the parenthesis part of your need, with the caveat that it was designed for Common Lisp and Scheme. Steve Yegge mentions JDEE for emacs Java development, but I can't speak for that from personal experience, and I couldn't find any documentation on it talking about structured editing.

Categories