Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move a function from one Kotlin class to another using IntelliJ?

I'm using IntelliJ IDEA to refactor some Kotlin code. I have two classes in the same file and I want to move a function from one class to another using Refactor -> Move (F6), but that doesn't work, and I get tooltip message that says: "Cannot perform refactoring. Move declaration is only supported for top-level declarations and nested classes". Am I doing something wrong? Or that refactoring is simply not supported?

[edit1] I tried to do the same operation with Java classes and everything works perfectly; so why this is not allowed for Kotlin?

[edit2] I thought that the problem is only when to two classes are in the same file, but it turns out that is not possible to move a function between classes in separate files!

like image 506
Ali FELLAHI Avatar asked Apr 01 '19 13:04

Ali FELLAHI


People also ask

How do I move a method from one class to another in IntelliJ?

Open your class in the editor, place a caret at the static method you want to move and press F6 . In the Move Members dialog specify the following options: In the Members to be moved to another class (static only) field, select the checkboxes next to the methods that you want to move to another class.

How do I extract a method in IntelliJ?

To extract method:Press Ctrl+Alt+M or from the main menu, select Refactor | Extract | Method. In the dialog that opens, configure a method options, such as visibility, parameters, and so on. You can also change a name of the method if you need.

How do I run Kotlin main function in IntelliJ?

When your project is set up correctly, make sure that your main function is called "main" and has a parameter of type Array<String> . Next to it a Kotlin "K" will appear, which you can click on to run your main function.


1 Answers

It's a well-known Kotlin-only problem.

  • in IDEA (both free and paid editions);
  • in Android Studio.

Official ticket

like image 81
Andrei Konstantinov Avatar answered Oct 08 '22 14:10

Andrei Konstantinov