Is there quick way to find missing #endregion buried in code, the compiler error always point to end of C# code with no indication where it is missing.
Press Ctrl + M , O // Collapse all functions and regions;
Press Ctrl + F and type #region // Scroll down from Begin of the class;
It will clearly show like below Screenshot with no + sign but other region will have + sign;
You can notice closed-region will be collapsed in grey color and '#region' will be not visible but open-region will be shown as in the Screenshot;
Note : If you don't know where should be the #endregion, then either delete the
'#region' or write #endregion just below the start #region;
Then error will boom.
Ctrl+Shift+F for a #region|#endregion with regex flag on. Then look through search results for correct structure. If you do not have nested regions, they should alternate. If you have, I expect you to have correct indents, so first #region without #endregion with same indent in previous line is what you are looking for.
Note that you can use Ctrl+Shift+F for searching in a single file too.
Related
I often find myself writing something daft like:
String.Format("{1}: {0}", reason, message);
or something similar but with far more string placeholders.
Is there an automated refactoring in ReSharper to change the order of the placeholders and the arguments? I have a tendency to mess up the mapping if I try to change the order by hand.
Obviously, the example above is trivial. What I am doing in reality is often writing something like:
String.Format("{0}.{2} = {1}.{3} AND {0}.{4} = {1}.{5} AND {6}.{7} = {1}.{8}",
table1Alias, table2Alias, col1A, col2A, col1B, col2B, table3Alias, col3C, col2C);
and thinking to myself that it would be great if I could move table3Alias up to the front next to the other aliases.
(ReSharper 7.1.3)
For C# 6+ (ReSharper 10+ / 2016+):
Place your cursor at string.Format
Press Alt + Enter
Select Use string interpolation
Press Alt + Enter again
Select Convert to string.Format
No, there is no such functionality.
Just place your cursor in table3Alias, then press Ctrl + Alt + Shift + Left/Right arrow. This changes the parameter order in a function call.
There's also an option for removing one when you press Ctrl + Shift + R
There's also a shortcut to add a format item. You may be able to do what you want with combining these. The exact functionality you ask is not implemented.
In Avalonedit, how do I get the line number of the very top line that is visible to the user?
I believe I'm supposed to make some use out of VisualLines but it isn't helping.
You can use GetDocumentLineByVisualTop:
int firstLine = textView.GetDocumentLineByVisualTop(textView.ScrollOffset.Y).LineNumber;
textView.VisualLines[0].FirstDocumentLine.LineNumber would work as well, but you have to be careful there - the visual lines collection can be invalid if a redraw was requested but not performed yet (see VisualLinesValid/EnsureVisualLines()).
This question already has answers here:
Closed 12 years ago.
Similar Post:
Hidden Features of Visual Studio (2005-2008)?
What are some shortcut keys you know of that make programming faster, easier, or all around more enjoyable?
One of my favorite items is CTRL + . to add an imports/using statement to the top of the code file.
Of course there is Intelli-Sense, it opens automatically or when you hit Ctrl + Space. Select the variable / class / function or whatever it shows you and hit Enter or Tab to insert it.
Code snippets, also provided by the Intelli-Sense list insert code so you do not need to type everything again. Etc. type "prop" and double hit Tab.
Ctrl + K, Ctrl + D to format your total file so it looks cleaner.
#region / #endregion to group your code and allow you to hide the whole region
I like CTRL + K + D, which formats the code in a file.
And although it's not a Visual Studio feature, I highly recommend Resharper.
Couple of my favorites:
CTRL + TAB -- moves you between open code pages
SHIFT + CTRL + F -- allows you to do a search across the entire solution (as sometimes find all references does not work when your solutions get huge)
SHIFT + DELETE -- allows you to remove an entire line from your code without highlighting it.
Ive just moved from VB.Net to C#. I dont understand why people are using it/prefer it as it is soo much more time consuming!
In VB.NET, you simply type your code and VB.NET formats is as you go, For example:
removes unneccessary whitespace,
automatically puts in brackets,
tabs blocks of code,
automatically creates the NEXT, END IF, statements for blocks.
and the opposite/nuiances in C#
if you change the name of an event handler it creates a new one, doesnt rename the existing one
you must have the () at the end of a method
and im sure theres more.
Why is C# backwards like this? Surely there must be a way to improve productivity somehow. Any ideas or free tools out there?
This has nothing to do with the language, and everything to do with the editor.
Regardless, the editor for C# in visual studio does support automatic formatting in several ways.
If you delete and reinsert the closing brace }, it will reformat/reindent automatically.
There are several menu items and corresponding keyboard shortcuts that will reformat code for you:
Ctrl+k+d - this will reformat the whole document.
Ctrl+k+f - this will reformat the selection.
There are also extensive refactoring capabilities - the rename refactoring will rename a member everywhere it is mentioned, even if it is in other projects.
Not automatic BUT.....
Use
Ctrl+K+Ctrl+D
to format document keystroke
Use
Ctrl+K+Ctrl+F
to format selection keystroke
From Visual Studio Format entire file?
I'm using C# in Visual Studio 2008 and it behaves exactly as you describe. Pretty much every time I type a semi-colon or curly brace, it corrects all formatting within the context.
For example...
if (myValue!= null) {
someValue = myValue;
If I type the closing curly brace, it turns into this:
if (myValue != null)
{
someValue = myValue;
}
All dependent on the style settings in Tools > Options
Also there exits some so called code snippets. If you simply type if and press tab tab this will automatically result into
if (true)
{
}
setting the cursor directly onto the true.
Even better is the switch snippet. If you enter switch and press the tab twice you'll get
switch (switch_on)
{
default:
}
where your cursor stands on switch_on.
If you now enter something meaningful like a variable name that holds an enum value (e.g. var color = Color.Red;) and press Enter it will automatically fill in all possible cases.
There are more code snippets available and some are very handy like foreach, try, prop, propg.
I'm rendering text using FormattedText, but there does appear to be any way to perform per-char hit testing on the rendered output. It's read-only, so I basically only need selection, no editing.
I'd use RichTextBox or similar, but I need to output text based on control codes embed in the text itself, so they don't always nest, which makes building the right Inline elements very complex. I'm also a bit worried about performance with that solution; I have a large number of lines, and new lines are appended often.
I've looked at GlyphRun, it appears I could get hit-testing from it or a related class, but I'd be reimplementing a lot of functionality, and it seems like there should be a simpler way...
Does anyone know of a good way to implement this?
You can get the geometry of each character from a FormattedText object and use the bounds of each character to do your hit testing.
var geometry = (GeometryGroup)((GeometryGroup)text.BuildGeometry(new Point(0, 0))).Children[0];
foreach (var c in geometry.Children)
{
if (c.Bounds.Contains(point))
return index;
index++;
}
In OnRender you can render these geometry objects instead of the formatted text.
The best way is to design a good data structure for storing your text and which also considers hit-testing. One example could be to split the text into blocks (words, lines or paragraphs depending on what you need). Then each such block should have a bounding-box which should be recomputed in any formatting operations. Also consider caret positions in your design.
Once you have such facility it becomes very easy to do hit-testing, just use the bounding boxes. It will also help in subsequent operations like highlighting a particular portion of text.
Completely agree with Sesh - the easiest way you're going to get away with not re-implementing a whole load of FormattedText functionality is going to be by splitting up the individual items you want to hit-test into their own controls/inlines.
Consider using a TextBlock and adding each word as it's own Inline ( or ), then either bind to the inline's IsMouseDirectlyOver property, our add delegates to the MouseEnter & MouseLeave events.
If you want to do pixel-level hit testing of the actual glyphs (i.e. is the mouse exactly in the dot of this 'i'), then you'll need to use GlyphRuns and do manual hit testing on the glyphs (read: hard work).
I'm very late to the party--if the party is not over, and you don't need the actual character geometry, I found something like this useful:
for (int i = 0; i < FormattedText.Text.Length; i++)
{
characterHighlightGeometry = FormattedText.BuildHighlightGeometry(new Point(), i, 1);
CharacterHighlightGeometries.Children.Add(characterHighlightGeometry);
}
BuildGeometry() only includes the actual path geometry of a character. BuildHighlightGeometry() generates the outer bounds of all characters--including
spaces, so an index to a space can be located by:
foreach (var c in CharacterHighlightGeometries.Children)
{
if (c.Bounds.Contains(centerpoint))
{
q = c;
cpos = index;
break;
}
index++;
}
Hope this helps.