ReSharper Keep existing line breaks not working - visual-studio

Under Formatting Style | Line Breaks and Wrapping | Preserve Existing Formatting I have "Keep existing line breaks" option checked. However, when I apply ReSharper Reformat Code function to file it removes my existing line breaks and places all the code in a statement on a single line.
Which setting could be annulling the "Keep existing line breaks" option and forcing the code on a single line?
I do have "Wrap long lines" under Line Wrapping unchecked but I don't think that should impact the "Keep existing line breaks" option.
Message = string.IsNullOrWhiteSpace(someString) ?
Properties.Resources.SomeResource : someBool ?
Message = Properties.Resources.SomeResource + Environment.NewLine + customMessage : customMessage;
After applying Reformat Code ReSharper function this statement with two line breaks becomes a single 240 character long line.
Visual Studio 2015
ReSharper 2017.1.3

I ran into a similar issue with the JetBrains CodeCleanup tool, take a look at the ternary operator formatting rules, for me it was the wrap_ternary_expr_style setting. I believe it's trying to fix the formatting of the ternary operator and since you don't have a max line length it condenses it down.

Related

How can I record a "(" in a macro in vsVim?

I'm trying to record a macro that will type a small chunk of text that contains a parenthesis "(", whenever I try to play the macro it doesn't type the "();" at the end of the line.
My guess is that parenthesis in a macro may have some special meaning but in my case, I just wanna type in "(".
What's breaking my macro? How can I get the parenthesis to be typed?
(note: I'm using the Visual Studio vsVim plugin)
Edit: I can't remember the exact macro anymore, but it went something along the lines of this:
q a 5 j ^ 2 w i .EndInit(); esc q
It was supposed to append .EndInit(); on every 5th line but the output was just .EndInit.
Note that on other machine without the resharper plugin, this problem doesn't occur!
The most likely issue here is that R# is eating the ( keystroke. This means it never makes it to VsVim and hence doesn't get recorded as part of the macro.
The following issue tracks cleaning this up. It would include fixes for R# as well.
https://github.com/jaredpar/VsVim/issues/1393

Is there a way to delete all comments in a file using Notepad++?

Notepad++ obviously recognizes all comments as such. Is there a way to simply delete all?
Edit: Stat-R's bookmark method has helped greatly, not only for removing comments but for conditionally removing lines in general.
For a general file, first of all you need to know the comment operator of the language you are writing the file in. For example, in java script the comment operator is //.
For the following code...
In NP++, you need to
Mark the lines that contains '//'. Make sure the bookmark option is enabled.
Then, choose from NP++ menu Search>Bookmark>Remove Bookmarked lines
EDIT:
Another solution after #Chris Mirno 's suggestion is as follows:
Use regular expression. See the image below. It is self explanatory
To understand it better, refer to these
In the Find & Replace Dialog, put the following regex and adjust the search options as depicted.
/\*.*?\*/
Replace with: (empty)
Select Mode: Regular Expression AND .(dot) matches newline
This should remove all your C style comments spanned across lines.
Star-R and Chris Mirno Answer are also Correct and Good.
But For Line Comment:
//.*?(?=\r?$)
Explanation:
// will be the Starting Position
.*? Will be any character
(?=\r?$) will search to the end of the line (as it is required in line comment)
Note:
But Still check each of the line because for example if your code contains soap format like
//www.w3.org/2001/XMLSchema-instance\x2......");
it will capture this line because the starting is // and it goes to end of the line so watch out for this :)
Warning to all using Stat-R's solution:
This method will remove lines of code if formatted like this:
echo "hello"; //This comment will be detected
Following his method, the entire line will be removed.
Therefore make sure to go through and make these comments, their own line before doing this method.
I have had some luck running a macro for the above. Basically:
search for // (F3)
select to end of line (shift+end)
delete (delete)
Put // into the search dialog by just searching for it once. Then record the three steps in a macro, then play it back until EOF.
The first time I did it I had a problem, but then it worked, not sure what I did differently.
Anton Largiader's answer was the most reliable one, including complex inline comments.
However, it will leave many empty lines, including ones with empty characters (space, tabs...) so I would just add another step to make it almost perfect:
After running the macro, just do:
Edit > Line Operations > Remove Empty Lines
OR
Edit > Line Operations > Remove Empty Lines (Containing Blank Characters)
1st option is good if you wish to remove only really empty lines
2nd options will remove every empty line even containing space etc. so there will be no more actual spacing left between code blocks. 1st option might be the safest with some manual cleanup afterwards.
As someone suggested in another post, the simplest and most reliable is maybe to export the all text in .RTF format using Menu Plugin-->NppExport-->Export to RTF and then:
-Open the newly created file in Word
-Select any part of any comment
-On the top-right side of Word clic Select--> Select all texts with similar formatting
-Remove the selected comments all at once (del or cut if doesn't work)
To remove Powershell comments if someone find it handy:
Removing Comment in a Powershell using Notepad ++
To find just lines beginning with # (and not with # elsewhere in the line).
Notepad++ SEARCH Menu > Find
‘Mark‘ Tab – fill in as below.
Select ‘Mark All’ (clear all marks if used previously).
Regex ^[#}
enter image description here
SEARCH Menu > bookmark > Remove (or do anything on the list with
them)
Clear all marks to reset
You can select no comments just code by doing the following:
Regex ^[^#}
enter image description here
Enter ctrl+shift+K to remove comment

Visual Studio macro to find a string and delete matching lines

In my Visual Studio (2010 C#) solution, I need to delete all lines of code that contain a matching string pattern.
For example, I want to delete all lines that contain ".BackColor = System.Drawing.Color.Yellow;". The Find and Replace feature of Visual Studio isn't good enough, because you cannot tell it to wipe out the matching lines.
So I think I would need a macro for that. Any help is appreciated.
You can use the "Find and Replace" feature of Visual Studio to delete matching lines.
The key is to match the whole line including the end of line character as well. You can do this in wildcard or regular expression mode. In wildcard mode, begin the expression with * and end the expression with *\n. The asterisks will match any number of characters, and the \n will match the end of line character.
In your case, your find query would be "*.BackColor = System.Drawing.Color.Yellow;*\n". The replace field should then be left blank.
To enable wildcard mode, select 'Wildcards' in the 'Use:' field of the 'Find options' section of the 'Find and Replace' dialog.
With Visual Studio 2015, 2017, 2019, 2022 this worked for me. Open Search window, check the "use regular expressions" checkmark. Fill "find what" with
.*myCodeFragmentHere.*\r?\n
fill "replace" with an empty string.
Remove all regexp (brackets, dots) from the code fragment in your search expression.
I tend to create macros in VS by running the macro recorder then editing the resulting code.
So, manually search for the pattern, and press F3. Stop the macro then (or press the line-start key, select to end of line, press delete and then stop the macro).
Edit the macro, the command to delete a line is:
DTE.ActiveDocument.Selection.SelectLine()
DTE.ActiveDocument.Selection.Delete()
You can set the find text with FindText:
DTE.ActiveDocument.Selection.FindText(".BackColor = System.Drawing.Color.Yellow;", vsFindOptions.vsFindOptionsFromStart)
Building upon #HolgerJeromin's answer: instead of guessing the right indentation match (could be tabs could be spaces could be more or less), I prefer matching the beginning of the line using the ^\s* pattern.
For example, to remove all lines having a ProducesResponseType attribute, I use
^\s*\[ProducesResponseType.*\n
(works on Windows too using VS 2019).
For Visual Studio 2015 (in which there are no macros and no wildcards), I did the following:
Open Find and Replace (Ctrl+H)
Set to use Regular Expressions (Alt+E)
Set the Find box to
({line of code string})\r?\n({next line tabbing})
Leave the Replace box empty
Replace
Where-
{line of code string} = the line of code you wish to remove. Note you need to escape characters such as parenthesis and quotes with a backward slash ()
{next line tabbing} = the number of spaces preceding the next line of code (without this your line will be removed but the next line would have double the spaces before it
For example, to remove
DoSomething("hello");
From -
class A
{
void SomeMethod()
{
DoSomething("hello");
DoSomethingElse();
}
}
Replace the following
(DoSomething(\"hello\")\;)\r?\n({ })
I was trying to remove an attribute ([OperationContract] in my case) and none of the other answers worked for me. I finally got it to work by using the following:
\[OperationContract\]\r\n\t\t (Use Regular Expressions)

Visual Studio 2008: Find and Replace with new line character?

Sometimes i would like to search for text containing a new line character and there are other times i would like to replace text with a new line character.
How can i do this with visual studio 2008?
Use a RegEx search:
In the Find Dialog - Expand "Find Options"
Check the box for Use: Regular Expressions
Next to the search box there is now an arrow that is active, it will show you available RegEx options/values.
The value you want will be \n. So "SearchValue\n" should do it.
Be aware that that its not a standard RegEx that you use, it's VS specific.
Replace can also use the RegEx values.
Adding on to Brian Schmitt's answer...
Regular expression searches using \n work as expected. However you have to be a little careful when using \n in regex replaces with Visual Studio 2008. For example, if you search for \n and replace with \n (yes, the exact same thing) all of the line breaks in your file(s) will be converted to Unix-style newlines (LF). This may be a bug in Visual Studio. I find it hard to believe this is the intended functionality.
To get around this, you can use tagged expressions, using curly braces: e.g. search for SearchValue{\n} and replace with ReplaceValue\1. This ensures that the same line-break character(s) that were found when searching will also be used when replacing.
You can try my Multiline Search and Replace Macro.

How do you do block comments in YAML?

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
How do I comment a block of lines in YAML?
YAML supports inline comments, but does not support block comments.
From Wikipedia:
Comments begin with the number sign ( # ), can start anywhere on a line, and continue until the end of the line
A comparison with JSON, also from Wikipedia:
The syntax differences are subtle and seldom arise in practice: JSON allows extended charactersets like UTF-32, YAML requires a space after separators like comma, equals, and colon while JSON does not, and some non-standard implementations of JSON extend the grammar to include Javascript's /* ... */ comments. Handling such edge cases may require light pre-processing of the JSON before parsing as in-line YAML.
# If you want to write
# a block-commented Haiku
# you'll need three pound signs
The specification only describes one way of marking comments:
An explicit comment is marked by a “#” indicator.
That's all. There aren't any block comments.
I am not trying to be smart about it, but if you use Sublime Text for your editor, the steps are:
Select the block
Cmd + / on Mac or Ctrl + / on Linux and Windows
Profit
I'd imagine that other editors have similar functionality too. Which one are you using? I'd be happy to do some digging.
In Vim you can do one of the following:
Comment all lines: :%s/^/#
Comment lines 10 - 15: :10,15s/^/#
Comment line 10 to current line: :10,.s/^/#
Comment line 10 to end: :10,$s/^/#
or using visual block:
Select a multiple-line column after entering visual block via Ctrl+v.
Press r followed by # to comment out the multiple-line block replacing the selection, or Shift+i#Esc to insert comment characters before the selection.
An alternative approach:
If
your YAML structure has well defined fields to be used by your app
AND you may freely add additional fields that won't mess up with your app
then
at any level you may add a new block text field named like "Description" or "Comment" or "Notes" or whatever
Example:
Instead of
# This comment
# is too long
use
Description: >
This comment
is too long
or
Comment: >
This comment is also too long
and newlines survive from parsing!
More advantages:
If the comments become large and complex and have a repeating pattern, you may promote them from plain text blocks to objects
Your app may -in the future- read or update those comments
One way to block commenting in YAML is by using a text editor like Notepad++ to add a # (comment) tag to multiple lines at once.
In Notepad++ you can do that using the "Block Comment" right-click option for selected text.
Emacs has comment-dwim (Do What I Mean) - just select the block and do a:
M-;
It's a toggle - use it to comment AND uncomment blocks.
If you don't have yaml-mode installed you will need to tell Emacs to use the hash character (#).
If you are using Eclipse with the YEdit plugin (an editor for .yaml files), you can comment-out multiple lines by:
selecting lines to be commented, and then
Ctrl + Shift + C
And to uncomment, follow the same steps.
For RubyMine users on Windows:
Open the file in the editor.
Select the block and press:
Ctrl + /,
And you will have the selected block starting with #.
Now if you want to uncomment the commented block, press the same key combination Ctrl + forward slash again.
In the Azure DevOps browser (pipeline YAML editor),
Ctrl + K + C Comment Block
Ctrl + K + U Uncomment Block
There also a 'Toggle Block Comment' option, but this did not work for me.
There are other 'weird' ways too: Right-click to see 'Command Palette' or F1
Then choose a cursor option.
Now it is just a matter of #.
Or even smarter [Ctrl + K] + [Ctrl + C]
In a .gitlab-ci.yml file, the following works:
To comment out a block (multiline): Select the whole block section >
Ctrl K C
To uncomment already commented out block (multiline): Select the
whole block section > Ctrl K U

Resources