Jump to loop start/end shortcut in Visual Studio? - c#

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)

Related

Erase only one already printed character from the console in c#

So I want to create a script that first prints out values from a (pretty big) 2D array and then change one character by erasing it and printing a new one without clearing out the whole console and redrawing it because that creates flickering which is very annoying when i want to make often changes. So is there any way to replace or erase one character that has been already printed or any other efficient way to do it? I just don't want flickering.
move the cursor and write a space " ". (Or whatever character you want to use as a replacement.)
To delete the last character input in the console you can use the following command:
Console.Write("\b");
However, if you're attempting to remove a character in the middle of the string you'd have to do some clever backspacing to the character you want, and then reprint the rest of the string. You could write a loop to backspace until the character you wish to remove, add the removed characters to a stack, and pop them off to rewrite it.
The second approach would be very inefficient due to console commands being naturally slow, and also really misusing the console backspace command.

If else formatting Visual Studio not indenting correctly

So this is going to be a kind of strange question, I have never run into this formatting issue with any other programs I have written. I think it is because my if statements has multiple || which needed multiple lines to make it appear more readable.
Notice the dotted line under my else if(). It is getting shifted right for some reason under the if instead of the else.
Is there some kind of formatting setting that can fix this? Or is there a different way I am supposed to do this else if statement? Having all those OR checks is pretty much necessary but I did not wan't to put them all on a single line as that would look real ugly.
Thanks!
Tools -> Options -> Text Editor -> C# -> Code Style -> Formatting -> New Lines
Uncheck everything and save.
Then, CTRL + E, D to format the document.
Also, you're using a huge compound if statement there, which is quite frankly a mess. If you need to check multiple items as starting values, put them all into a list.
List<String> ModelNumberPrefixes = new List<String>();
ModelNumberPrefixes.Add("A1C1C");
ModelNumberPrefixes.Add("A1C1D");
//etc
ModelNumberPrefixes.ForEach(s => {
if (ModelNumber.StartsWith(s)) {
//Whatever you need to do in your big if block
}
});
To format a selection: Ctrl+K, Ctrl+F
To format a document: Ctrl+K, Ctrl+D
See the pre-defined keyboard shortcuts . (These two are Edit.FormatSelection and Edit.FormatDocument.)

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.

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