What is the best way to prevent ReSharper from wrapping certain lines of code. For example, an if statement with one condition and a simple body should not be wrapped. ReSharper currently forces this line...
if (item == null) { return null; }
... to break like this:
if (item == null)
{
return null;
}
I don't want to blindly disable this feature necessarily... just make it a bit more intelligent.
Update for newer versions of ReSharper
(my version is 2021.1)
Since a picture is worth than a thousand words, you probably want to configure it like so. In this way it takes care of constructs such as using too:
Check the ones that start with Keep existing arrangement [...] if you want to instruct ReSharper to leave, say, single-line ifs alone if they are already written that way.
Took me longer to figure out than I wanted it to, hopefully it will help someone else.
You can change this in the R# Settings:
Open the menu Resharper -> Options
Go to Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping
Change to option Break line in single embedded statement to Put on a single line or Do not change
Edit:
As mentioned by #Alexander it would be more suitable to use the option Break Line in a block with a single statement which I think is available since R# 8
In latest Resharper it is under General >> "Wrap log lines"
Related
I have seen some wired behaviour from the ReSharper and its suggestions. One that particularly made me buff was the one below. Now leave alone that a string can be null, and the suggestions is simply wrong. Can I change some settings to make it stop telling me to remove if-statements that are null checks?
Android studio marks warnings in Java code, some of them I consider useless and want to disable them.
I know I can configure Inspections that are enabled, but for some of these I can't find where it can be disabled. Then code is marked to have issues, and I want to have clean code so that I see real problems.
Example of warning:
'if' statement can be replaced with 'return ...'
And I don't want to put annotations to my code, rather I'd like to switch this off in IDE.
Thanks
When you click on the lightbulb and then on the suggested action's arrow, you get submenu with options. First one should be "Edit inspection profile setting", which should navigate you to the exact place in Settings, where you can edit given inspection.
This is a more general answer for future viewers. These are various ways to suppress the warning.
Statement
Add the following comment above the line with the if statement.
//noinspection SimplifiableIfStatement
if (...)
Method
Add the following line at the beginning of the method.
#SuppressWarnings("SimplifiableIfStatement")
private boolean myIfMethod() {
if (...) return false;
return (...);
}
Class
Add the following line at the beginning of the class.
#SuppressWarnings("SimplifiableIfStatement")
public class MyClass { ... }
Current project
Position your cursor on the if statement and press Alt + Enter. Then choose Simplify > Disable inspection.
All projects
Position your cursor on the if statement and press Alt + Enter. Then choose
Reapplying the inspection
If you disabled the inspection by mistake, you can turn it on again.
Go to File > Settings > Editor > Inspections > J2ME issues
Check the line for "if statement may be replaced with && or || expression"
Notes
The inspection is there for a purpose. It would generally be better to follow the warnings advice and just simplify the if statement. (However, sometimes I find the "simplification" harder to read. Thus my answer here.)
You don't really need answers like this. You can autogenerate the suppression code for any warning by clicking on the code with the warning and pressing Alt + Enter and then expanding the options the lightbulb. You will be given options to suppress the warning for the statement, method, class, etc.
Settings -> Editor -> Inspections -> Java -> Control flow issues -> Redundant 'if' statement
You can type "if statement" in the search box to highlight all inspections involving if.
In Android Studio 3.4.1 at least, if you right click on the Analysis marker at top right of the code pane you can change the level of highlighting there. I've set my level to 'Syntax only'.
I love using the Auto Formatting tool in Visual Studio CTRL K + CTRL D However, there are times when VS makes the formatting just a tad off from what I want if, for instance, I'm making a detailed kind of pseudo-code that relies on specific comments and indentation. Is there a way I can still use the CTRL K + CTRL D command and set visual studio to ignore a certain block of code or set of line numbers? Also, if there's an answer in VS 2013 but not in 2012, please post because I may be upgrading soon. Thanks in advance.
I got hit by this problem today with my pseudo-code comments too and thought I'd share the way I solved it.
While there is no way to prevent Visual Studio from auto-formatting parts of code, there is a way to prevent it from auto-formatting pseudo-code comments.
Instead of using something this like this
// if condition
// do this
// else
// do something else
use the tripple-slashes /// instead
/// if condition
/// do this
/// else
/// do something else
As a bonus you get automatic insertion of /// in new lines and auto-indenting that keeps previous row's indentation level.
This seems not to depend on editor indentation options.
It also seems not to mess up XML documentation
(Yes, I know this is an old question. No, I do not want to take Cameron's right for accepted answer.)
I use https://github.com/nsf/gocode in conjunction with https://github.com/Shougo/neocomplete.vim for my Go autocompletion.
It works really well, except for one little thing. When I type something like
fmt.pri
I get autocomplete option like so:
fmt.Println(a ...interface{}) (n int, err error)
Since I'm new to Go, this is super helpful, because now I know what arguments the method takes, what are the types, and also what does it return.
Unfortunately, as soon as I write past bracket, the information is gone.
fmt.Println(
So on more complex methods that I'm using for first time, I'm stuck, and have to jump to source definition, or run godoc.
It would be much easier to have that information available somewhere, preferably on the bottom of Vim where the command/status line is.
Does anyone know how such a thing could be achieved?
Preview window breaks my Vim so it's not an option.
I use autocomplpop (well, my fork of it) and it has a feature where it does a small split window with the completion text in it that sticks around. It does this when you set...
let g:acp_completeoptPreview = 1
I'm sure neocomplcache has a similar feature. Glancing through its documentation it discusses a preview window in several places. Search for preview in its docs and see what you can find.
This is ultimately controlled by 'completeopt' containing 'preview' (:h completeopt). The auto-completing packages often set these values as part of their functionality, which is why with autocomplpop you need to use its option to control it instead of just doing 'completeopt+=preview'.
A product like Resharper has many settings, and I can't seem to find the right settings to get the exact behavior I desire from it.
The behavior I am trying to change is this:
Suppose the endresult I want is
but the line currently is
As you expect I start typing:
and after a tab I get this
but I wanted it to become
Does anyone know how I can change the Resharper settings in such a manner that my desired behavior is realized?
Thank you.
EDIT: Note that tab does perform the desired behavior in the default VS Intellisense.
In this particular case, here's what you should do:
Go to ReSharper > Options > Environment > IntelliSense > IntelliSense Behavior, and set Automatically insert parentheses after completion to Opening only.
Now, with the caret right before SomeMethod(), type in Math.Ab, and complete Abs with Enter, as correctly suggested by Rob H. You'll end up with Math.Abs(SomeMethod();
Finally, press Ctrl+Shift+Enter to invoke the Complete Statement, which in this case will insert the required parenthesis at the end of the statement, and put the caret on the next line.
Keep in mind though that this might not be the optimal solution provided that you probably have plenty of other usages of code completion that could conflict with this kind of settings change.
If that kind of similar code modification is required in multiple places, you should possibly create a search and replace pattern instead.
Hitting Enter instead of Tab will insert without overwriting, although you then end up with
Math.Abs()SomeMethod();