Disable rearranging methods while auto-formatting - android-studio

I'm using Android Studio 2.2 and I like auto-formatting my code using ctrl-alt-L. The problem, however, is that it also reorders all of my methods. (For some reason, it wants to put all of my static methods at the top of the class.) Is there a way to disable the rearranging of methods while auto-formatting?
Note: I'm guessing that I could go into Settings -> Editor -> Code Style -> Java and under the "Arrangement" tab delete all of the "Matching rules", but there has to be a simpler way than that, no?

From IntelliJ support at Jetbrain, you can configure default behavior for "Reformat Code" action using ctrl + alt + shift + L shortcut, where you can enable/disable Rearrange code option: https://www.jetbrains.com/help/idea/2016.2/reformat-file-dialog.html.
Also, after a reformat action (i.e. ctrl + alt + L) there will be a pop-up that will say something like, "No lines changed: code is already properly formatted. Show reformat dialog: Ctrl+Alt+Shift+L". The word "Show" will be linked to the "Reformat File Dialog" which is another way to get to the dialog to enable/disable the Rearrange code option.
Arrangement behavior can be customized in File -> Settings -> Editor -> Code Style -> Java -> Arrangement.

Related

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 create my own 'Reformat Code' style in Android Studio?

In Eclipse,(Window -> Preferences -> Java -> Code Style -> Formatter) there was a facility to create my own format style and apply it in the code. Is there a way to do that in Android Studio(for Java code) ?
To change formatter options go to File | Settings | Code Style | Java
To reformat your code manually, press Ctrl + Alt + L (or using the menu Code | Reformat code...). To reformat the whole source code of your module, just select the module folder first.
More information about reformating source code can be found on the IntelliJ Web Help
In Android Studio 3.3.2 (running in Mac OS) "Android Studio" -> "Preferences" -> "Editor" -> "Code Style" -> "Java".
I would be useful to find a way to export/import only code format settings in order to share them between the team.
Open global settings/preferences via ⌘, or Android Studio > Prefences.
In the left hand side bar, go to Editor > Code Style.
open the scheme: drop down menu and select Default IDE.
Click the cog menu to the right of the drop down menu and select duplicate....
Then rename your new profile, and change the general settings as well as the settings for each language in the sub menu of Code Style as you see fit.
Then with you new profile selected, reformat your code with ⌘ + ⌥ + L
These style are global and can be used in other projects.
Same method applies for Code Cleanup too.

How to format code in Android Studio (IntelliJ IDEA)

I want to format code in a freshly created Android application.
I messed up the code in this manner:
private static final
int AUTO_HIDE_DELAY_MILLIS
= 3000;
I want to format it with Ctrl + Alt + L, but it is not repairing the code. I use Windows 8.1.
Just right-click on the project or directory, and select Reformat Code
If you want to:
Order/FormatCode: Ctrl + Alt + L
Implement Methods: Ctrl + I
Override Methods: Ctrl + O
There isn't any need for them to be capital letters. This is just for the example.
First you will need to configure how your code is formatted. This can be done by going to
[Main Menu] -> File -> Settings
then go to
(Project Settings) -> Code Style -> Java
In this area you can define all the formatting rules that you would like to apply to your code.
Now you should check (or change) the keyboard shortcut for reformatting code by going to
[Main Menu] -> File -> Settings
then go to
(IDE Settings) -> Keymap
Just type Reformat Code into the search box at the top right to find it.
Once this is done, highlight the code you want to reformat and use the keyboard shortcut to reformat your code based on the rules you defined.
In Android Studio (on the Mac), I select the code chunk, and then select the Code menu item and click Auto-Indent Lines.
See screenshot:
Shortcut key (on Mac) is Ctrl + Alt + I.
At least on my Android Studio version, "keep when formatting - line breaks" was enabled by default. So you need to go to
Preferences → Editor → Code Style → Java and disable that checkbox (see the below screenshot).
Alt + Ctrl + Shift + L is the shortcut key for arranging the code in Ubuntu (Android Studio).

Android studio auto fix

I don't know what the exact name is for s feature like this, but I will just describe it.
I am using android studio and I added some java classes from a different project. Now it says
"missing package statement: yada yada". But there is not option in the error popup to just automatically add the missing package statement. I know it seems lazy to not type it, but I like to do things quickly.
When there is an error like this, is there a way for android studio to automatically added the needed lines of code like eclipse would?
This is a common frustration. While not a perfect solution, I found https://stackoverflow.com/a/16616085/967131 to work for me.
For Windows/Linux, you can go to File -> Settings -> Editor -> General
-> Auto Import -> Java and make the following changes:
change "Insert imports on paste" dropdown value to "All"
check "Add unambiguous imports on the fly" option
On a Mac, do the same thing in Android Studio -> Preferences
That way, as you type, or when you copy and paste, many imports will be added automatically. Those that aren't are ambiguous; put the cursor on the class, press alt+enter and select which version of the class you meant.
For Windows/Linux, you can go to File -> Settings -> Editor -> General -> Auto Import -> Java and make the following changes:
change "Insert imports on paste" dropdown value to "All"
check "Add unambiguous imports on the fly" option
On a Mac, do the same thing in Android Studio -> Preferences

Does Resharper 4.1 support both Camel Humps and normal selection modes?

I've found the setting for Camel Humps in resharper:
Resharper -> Options -> Editor -> Use CamelHumps
The problem is that I would still like to be able to use the normal selection mode (i.e. the default behaviour for CTRL+Arrow and CTRL+SHIFT+Arrow) as well as the CamelHumps mode.
For example consider this variable:
private int MyVeryLongCamelCaseName;
Now if I want to copy the entire variable then I want the VS default behaviour for CTRL+SHIFT+Left-Arrow which is to select the entire variable if the cursor is on the M.
However if I want to change the name to say MyExtremelyLongCamelCaseName then I would like the CamelHumps behaviour provided by Resharper.
Is there any way to have both behaviours with different shortcuts?
disable CamelHumps in Resharper -> Options -> Editor -> Use CamelHumps.
go to Tools -> Options -> Keyboard and
bind the keys you want to 'Resharper.HumpNext' and 'ResharperHuntPrev' (I use alt+left / alt+right)
and bind keys to 'Resharper.HumpNextExtent' and 'Resharper.HumpPrevExtent' for selection shortkeys. (I had to remove keybindings for shift+alt+left/right from 'Edit.Char[Left/Right]ExtentColumn' first)
I think ctrl+W shotkey will help.
I don't think there is an option for this but as a work around you can use the Extend Selection shortcut which is CTRL+ALT+Right. Not ideal but faster than hitting CTRL+SHIFT+Right multiple times for a symbol that has a lot of camel humps.

Resources