Resharper multi-line assignment alignment - resharper

I'm using Visual Studio Community 2017 and Resharper Ultimate 2017.1.
When I type multiple line assignments construct like this
a =
b =
c = 1;
it will auto adjust the code like this:
a =
b =
c = 1;
I search the Resharper and VS options but I didn't find anything useful.

For C++ code, you can set "Indentation and Alignment | Continuous line indent" to "None" to get the formatting you prefer, but it will also affect declarations other than assignment.
In general, you can select a piece of code, hit Alt + Enter and invoke "Format selection | Configure". This action will open a dialog with formatting settings that affect the selection, so you can play with them and check how formatting changes.

Related

Disable resharper line break in a section of code?

Using inspection, I can easily disable and restore resharper reformatting settings. For instance like this:
// ReSharper disable SuggestVarOrType_BuiltInTypes
int i = 1;
// ReSharper restore SuggestVarOrType_BuiltInTypes
So.. is there any way to do the same to line breaking, so that I can have long lines in special places in my code and still have resharper break the others?
Basically, I guess, I ask for the secret word to put in place of "SuggestVarOrType_BuiltInTypes" above :-)
Try using these comments:
// #formatter:wrap_lines False
{the code block for which you would like to disable wrapping long lines}
// #formatter:wrap_lines restore
By the way, you may find out most of the formatter comments on your own using this way:
select needed code block | hit the Alt+Enter shortcut | hit the "Right arrow" key on the "Format selection" item in the menu to open a submenu | hit "Configure" -> it will open the new window which shows you all formatter settings which affect the code block.
Then you are welcome to change all the settings you like and click on Save as comments button -> you will get everything you need as comments around the code block.

IntelliJ (Android Studio) Feature similar to IntelliSense Toggle Completion Mode in VS/SSMS

In Microsoft IDEs there's the option to Toggle the Completion Mode, which means that it will automatically select the top result if you click . (Dot) or ;
Which is very intuitive and saves alot of clicks in the lifetime of the code writing process.
Is something similar possible in Android Studio ?
When I click Ctrl+Alt+Space it achieves exactly the desired result, but only for the currently typed expression, and then reverts to its old ways as soon as I move to the next line/expression.
Settings | Editor | General | Code Completion -> Insert selected variant by typing dot, space, etc.

Android Studio: disable warning "if statement can be simplified"

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'.

How to set max line length in Android Studio code editor?

How can I set a maximum length for Android Studio's code editor, or may be set a word wrap?
In my Android Studio, no matter how long a statement I type on a single line, it never automatically goes to the next line, but rather a horizontal scroll bar appears.
When I autoformat using the Ctrl+Alt+L (reference), nothing happens.
My SO or Google search found a lot of material on this for Eclipse, but not for Android Studio.
If you need not a soft wrap,
but actually, brake one long line onto several lines you need:
Go to Preferences(mac) or Settings(windows) -> Editor -> Code style -> Java/Kotlin -> Wrapping and braces (tab) -> Check "Ensure that right margin is not exceeded"
Now try to reformat your code:
press
OPTION (ALT) + CMD + L on your Mac
(or CTRL + ALT + L on PC)
Edit 13.02.19
As noted in comments this option seems not available in settings for Kotlin.
However, there is a workaround to manually add it.
Here is what it takes to make it work:
Export the settings scheme.
Open saved xml file.
Find <codeStyleSettings language="kotlin"> tag (or create)
Add the WRAP_LONG_LINES setting manually:
<codeStyleSettings language="kotlin">
...
<option name="WRAP_LONG_LINES" value="true" />
</codeStyleSettings>
Save edits and and import back the scheme.
Here you go!
MacOS:
Click on Android Studio ( left top corner )
Click Preferences
Type Code Style in search
Click on Code Style
There is a filed right to the 'Hard wrap at' - you can enter needed about of symbols you need
In File | Settings, select "Editor", then "Code Style". There is an option "Wrap when typing reaches right margin".
Edit: Just tried it, and it doesn't seem to work. Anyway, maybe the option "Use soft wrap" in the "General" group is more what you want.
#Solace, actually #Fahim Faysal's answer is quite close to what you need, but since you want to solve the problem no matter how long a statement I type on a single line, it never automatically goes to the next line, 2 more steps you need to follow:
[Step 9] type the max length you prefer at `"Hard wrap at" ()
[Step 10] change "Wrap on typing" from Default: No or No to Yes
With version 4.1.2 and with Dart language you need to go to "File/Settings/Editor/Code Style/Dart" and modify the "Line length" to the preferred number.
In Android Studio:
File->Setting->
Editor->code Style->java->Wrapping and braces(tab:right side)->keep when reformatting->Line break(make unchecked)
You can change the max column width from:
File > Settings > Editor > Code Style > Right Margin (columns)
Android Studio 3.5:
Mac -
Android Studio > Preferences > Editor > CodeStyle > HardWrap at: ____
The existing answers already answer the question in straight forward way. As a different approach you could set the style guide to kotlin style guide code style. Then if you do alt + ctrl + l as you did, you'll see auto wrap as you expected.
Just open the settings, search for kotlin and look for kotlin under code style.
On the top right hand side of the settings window you'll see Set from..., click on it.
Then on predifined style > kotlin style guide
If you'd like to follow the kotlin formatting style this could reduce more effort. For more information: https://developer.android.com/kotlin/style-guide
My Environment:
Ubuntu 18.04
AndroidStudio 3.3.2
My operate:
File -> Settings -> Editor -> Code Style
Scheme -> select Project
Hard wrap at '120'
Apply
To move the grey vertical line, that tells Android Studio where to wrap your code, go to
Settings- Editor- Code Style- Java or Kotlin- Wrapping and Braces- Hard wrap at
and enter the number of characters, where the line should be.
For dart
settings => code style => Dart => Line length
In settings/code style/kotlin/wrapping and braces replace the "do not wrap" values with "wrap if long"
You can change the Hard wrap at param

How to disable automatic tab indentation change after copy paste?

When I copy a piece of code and paste it somewhere else in the IDE, my custom code indentation levels are changed by Visual Studio automatically. How do I prevent this?
Example:
My current tab settings are:
I want to continue using the Smart Indenting feature and actual tab characters (not spaces instead of them). I only want to prevent this particular behavior.
It looks like you are using Visual Assist X (a guess from the font highlighting), which does this. VS2012 itself doesn't have this feature so far as I know.
To turn it off in Visual Assist X, under VASSISTX->Visual Assist X Options..., select Advanced->Corrections and uncheck "Format after paste (in C/C++/C#/Javascript)".

Resources