resharper code formatting introduces needless linebreak on starting parens - c#

When I have simple wrap enabled on Wrap invocation arguments, I often end up with a formatted line like:
string.Format(
"example string tralala {0} {1}",
foo, bar);
no combination of settings I find prevents this line break other than enabling chop if long. unfortunately that can be a bit of a line hog (it forcibly makes each argument take a line if a function call takes up more than your max word-wrap space).
Is there any solution to this that I am overlooking?

In Resharper 9
Go to Resharper(from Visual Studio Menu)->Options->Code Editing->C#->Formatting Style->Line Breaks and Wrapping
and Uncheck Wrap Long Line from Line Wrapping option

There are a few settings you can use to format your code just the way you like it in re-sharper. in Visual Studio Go: Tools>Options>Re-sharper and click the Options button there again.
Then under the Code Editing section, under c#>Formatting Style. You can use one of the following settings:
Prefer wrap before ( in declaration\invocation
Prefer warp after ( in declaration\invocation
Or set the wrap invocation argument to either (this is most likely the one need setting)
chop always.
chop if too long.
simple wrap.
The un-tick the wrap long lines all together right on top op the *line wrappingsection, willstop-wrapping`, but you'll need to go through these setting to get it just right - or to get it to format the way you code.

Related

How to make Console.Clear clear all except one specific line? (c#)

How to make Console.Clear clear all except one specific line? (c#)
So, as a title says i want to clear the whole console except one specific line using c#. Is it possible?
Well, yes, it's possible. But it's not especially easy. You'll have to become familiar with the Console API, including how to call those functions with .NET. Then, what you can do:
Locate the line you want to save. If you know where it is on the screen, then it's easy to locate in the console screen buffer.
Call FillConsoleOutputCharacter to output spaces from the start of the screen buffer up to the first character of the line that you want to save.
Call FillConsoleOutputCharacter again to output spaces from the end of the line you want to save up to the end of the buffer.
I wrote a series of articles about accessing the console from C# a few years back. The articles are no longer online, but the code is available from http://mischel.com/pubs/consoledotnet.zip. You might find it useful.
yes it is possible. just use this code...
string line = yourlinenottobedeleted;
console.clear();
console.WriteLine(line);
And the deed is done
(if the line that is not to be deleted is not a string, then put int instead of string)

Possible to remove/replace code in entire solution

I have multiple lines of code in my solution that I'd like to remove.
They are, in this particular scenario:
System.Diagnostics.Trace.WriteLine([STRING]);
I could've done a 'Replace All' on System.Diagnostics.Trace.WriteLine but, obviously, I want to remove the entire thing (including the string parameter passed into the method) - I want the whole line of code to be deleted wherever a call to this method is made.
Is there a way in which I can do this (any way to provide a wildcard included in the replace criteria maybe?). Also, could I do this with comments/todo's too?
I appreciate this may seem bad practice to have to get to this point but would, for now, appreciate answers that are only on topic as opposed to answers that try and propose ways of preventing this in the future (which I've already acknowledged).
You can use Regular Expression in Visual Studio Find and Replace dialog, use:
Find: ^[ \t]*(System.Diagnostics.Trace.WriteLine\(.*\)\;).*\n
Replace:
That's will delete the entire line.
You'll need to use the Regular Expression option in the Replace dialog, and something like this should do:
System.Diagnostics.Trace.WriteLine\("\w*"\);
Cheers
EDIT: Obviously, if you are passing variables to WriteLine, just remove the quotes:
System.Diagnostics.Trace.WriteLine\(\w*\);
If you mean to remove all instances of System.Diagnostics.Trace.WriteLine then you could do the following:
Go to Replace in Files dialog box
Enter System.Diagnostics.Trace.WriteLine.* in the find what box
Leave the Replace with box empty
Choose Entire Solution for Look in
Expand Find Options and tick Use and ensure Regular Expressions is shown.

How to find the suspicious statement, like "Name = Name;" in C# by Regex?

My C# code has a lot of statement like "this.Name=...". In order to make my code neat, I let the text editor to replace all "this." to nothing. The code still worked. But later I fund it caused me a lot of new troubles for I wrote some statements like:
this.Name = Name; // the second Name is a parameter.
After the replacement, it became:
Name = Name;
Now, I met too much code. How to find the suspicious code like "Name = Name;" by Regex in VS 2010?
Thanks,
Ying
Why would you want to use Regex when you can simply compile the solution and look for the CS1717 warning:
Assignment made to same variable; did
you mean to assign something else?
Also note that in C# it is a good convention to have your parameters start with lowercase letter.
I would agree that Darin's approach is more robust and should be done first. However you might
have commented out sections of code which will be missed with this approach.
To try and find those you can use "Find in Files". In the Find box tick "Use regular expresssions" and enter {:i}:Wh*=:Wh*\1
:i C Style identifier ("tagged" expression by enclosing in braces)
:Wh* Zero or more white space chars
\1 back reference to tagged identifier found
This approach might bring back some false positives so you could try :Wh+{:i}:Wh*=:Wh*\1:Wh+ if there are too many but at the risk of missing some matches (e.g. where the closing comment mark is immediately after the assignment statement)
You could restore your last commit from your CVS, if you haven't changed too much since.
The problem with doing what you ask is that there might be other cases where "this" shouldn't have been replaced and you haven't seen the problem yet.

Inserting a character inside the body of a paragraph

If someone enters a very long title/sentence, the text will stretch across the web page.
Is there a way to break the text so it continues on to the next line?
Using overflow hidden will hide the text.
I think I should be using the wbr tag.
Should I use the insert(); method for this?
i.e.
string myText = "111111111111111111111111111111111111111111111111111111111111111111111111";
myText = myText.Insert(80, "<wbr/>");
Not sure how cross browser the wbr tag is also!
Strictly speaking, you should use the zero width space (​) for this rather than <wbr>. However, Internet Explorer 6 and earlier are known not to support this (they show an ugly box). So <wbr> is probably the safest choice. Except... Internet Explorer 8 in standards mode is known not to support <wbr>, so you've got yourself a wonderful conundrum here.
You can read more at quirksmode.org.
Do note HBoss' comment in that it's hard to predict where to break, unless you're using a fixed width font like Courier. You should probably heed his advice and break more often than just every 80 characters. (And don't get me started on combining characters.)
As far as ASP.NET is concerned, you can indeed use the Insert method for this, but beware when you need to insert more than one: you'' need to do some book keeping (and a StringBuilder would also be advised).
You could use a regex to find words surrounded by whitespace/special chars, and surround it with a div/span that has different overflow properties.
If you do use <wbr>, be sure to surround the word with <nobr>.
I'm not sure you can solve this by splitting the content with breaks since you aren't guaranteed that the break will fit uniformly across browsers. You have variations of font-size, widths, etc.
Normally when I see content that extends too var it simply overlaps over the rest of the page or the designer sets the overflow so that the content can be scrolled. There could potentially be some CSS tricks you could use, but I'm not aware of any.
As an alternative approach, instead of simply inserting a line break every x number of characters, you might just insert a space after certain characters, for example, punctuation. This will make sure that the content wraps at some point or another.

How do I use a regular expression to add linefeeds?

I have a really long string. I would like to add a linefeed every 80 characters. Is there a regular expression replacement pattern I can use to insert "\r\n" every 80 characters? I am using C# if that matters.
I would like to avoid using a loop.
I don't need to worry about being in the middle of a word. I just want to insert a linefeed exactly every 80 characters.
I don't know the exact C# names, but it should be something like
str.Replace("(.{80})", "$1\r\n");
The idea is to grab 80 characters and save it in a group, then put it back in (I think "$1" is the right syntax) along with the "\r\n".
(Edit: The original regex had a + in it, which you definitely don't want. That would completely eliminate everything except the last line and any leftover pieces--a decidedly suboptimal result.)
Note that this way, you will most likely split inside words, so it might look pretty ugly.
You should be looking more into word wrapping if this is indeed supposed to be readable text. A little googling turned up a couple of functions; or if this is a text box, you can just turn on the WordWrap property.
Also, check out the .Net page at regular-expressions.info. It's by far the best reference site for regexes that I know of. (Jan Goyvaerts is on SO, but nobody told me to say that.)

Categories

Resources