Help Provide Feedback: Vote Up Kotlin Refactoring Issues relevant to you


#1

The issues here are taken from the query project: Kotlin State: -Fixed,-Duplicate,-Rejected,-{As Designed},-Obsolete type: Feature,{Feature Group} refactoring.

For any Feature Groups child issues were added.

I extracted a summary from the issue and placed it here for convenience. If you find the summary relevant to your needs, please follow the issue link and vote the issue up. If there is an issue that is missing please open it on YouTrack and add a comment with a link to it here.

Kotlin YouTrack Refactoring Issues

 

KT-15679 - Rename refactoring for header/impl interface/class/function/typealias

Rename refactoring for header/impl should work at header or impl side for:

  • interface

  • class

  • function

  • type alias

 

 

KT-15638 - Extract Interface/Superclass: suggest to search and update usages of refactored class (like for Java)

Dummy Kotlin:

class SelfRef {
  fun refer(p: SelfRef) {}
} 

Extract Interface/Superclass, select refer() , get the result.
Now implemented interface/parent class refers to implementing/child class.
In Java after the same refactorings IDEA suggests to search usages and change them to interface/parent class, when possible.
NB: this occurs NOT only for self-references, but for any.
The same could be implemented in Kotlin.

 

 

KT-14272 - Support Move function refactoring when instance is not captured

class Test {
  fun can_be_global(): Int = 3
}

 

 

KT-13623 - Enhancement for Quick fix: while in a apply-style block, offer “Create member function” without “this.” and similarly allow “Extract Method” refactoring

class SomeClass () {
}

fun main(args: Array<String>) {
  SomeClass().apply {
      this.newMethod1("abc") //Quick fix  "Create member function" is offered
      newMethod2("abc") //Quick fix  "Create member function" is currently not offered. The user
                       // is forced to put "this." in to achieve the desired 
                       // effect of creating a new SomeClass method

      println(123)//when selecting this expression, Extract Method (to "applied" class SomeClass) 
                  // is not currently offered (currently only extract function is offered)
  }
}

 

 

KT-13436 - Replace ‘when’ with return: handle case when all branches jump out (return Nothing)

Example:

fun test(a: Int): String {
  when (a) {
    0 -> return "No"
    1 -> return "Yes"
    else -> TODO("hmmm?")
  }
}

Here expression in else branch returns Nothing , so it’s safe to refactor this code to:

fun test(a: Int): String {
  return when (a) {
    0 -> "No"
    1 -> "Yes"
    else -> TODO("hmmm?")
  }
}

which will allow to convert this function to expression body.

 

 

KT-13309 - Create field from local variable intention/refactoring

This should be allowed to do via “Introduce property refactoring” and/or an intention

class A {
  fun foo(p: String) {
    val <caret>x = p.length  //I want to turn this local variable to a field/property
  }
}

The refactoring should generate something like this:

class A {
  val x: Int //I think `var` declaration should not be forced here

  fun foo(p: String) {
    x = p.length
  }
}

 

 

KT-12740 - Refactor / Rename element with @JvmName: element name is suggested as current value, while the change is applied to annotation

Dummy Kotlin:
KotlinFile.kt

class Access {
    @get:JvmName("getObject")
    @set:JvmName("setObject")
    var subject: Int = 0
} 
@JvmName("filterValidString")
fun List<String>.filterValid(): List<String> { return this } 

Java usage:

public class JavaUser {
    public void use(Access p1, List<String> p2) {
        int v = p1.getObject();
        p1.setObject(v);
        KotlinFileKt.filterValidString(p2);
    }
} 

Call Refactor / Rename (Shift+F6) on getObject() , setObject() , filterValidString() .
The dialog suggests subject and filterValid as current names. That’s the issue. It should suggest annotation values.
On applying Rename the annotation value is changed, not the element name (expected).
NB: on calling Rename for the value of @file:JvmName() annotation it suggests annotation value, so it’s Ok.

 

 

KT-12727 - Rename only one directory presenting package: FQN in text file is lost

Set up a module with the following structure:
…/module-content-root/source-root-1/pack1/SubjectJava.kt with SubjectJava class.
…/module-content-root/source-root-1/pack1/SubjectKotlin.kt with SubjectKotlin class.
…/module-content-root/source-root-1/pack2/Ref.txt
…/module-content-root/source-root-2/pack1/Weight.kt
Ref.txt has the content with classes FQNs:

pack1.SubjectJava
pack1.SubjectKotlin 

In the Project tool window select pack1 in source root 1. Refactor / Rename (Shift+F6).
You’ll be asked, what to rename, the whole package or only the directory. Select only one directory.
In the dialog ask to search for text occurrences. Go on.
Result: in the text file Java FQN is preserved, Kotlin FQN is lost:

pack1ren.SubjectJava
SubjectKotlin 

NB: no problem in case of renaming the whole package.

 

 

KT-12713 - Either/ Result to handle errors without exceptions

 

 

KT-12348 - Rename class refactoring: propose to rename usages in comments

 

 

KT-12162 - Refactor / Rename Java bean property from Kotlin: field name could be kept in sync with accessors

Provide a Java bean with a property:

public class Bean {
    private String prop = "value";
    public String getProp() { return prop; }
    public void setProp(String prop) { this.prop = prop; }
} 

Provide a usage in Kotlin code:

fun context(bean: Bean) {
    bean.prop = "a"
    println(bean.prop)
} 

Try to Refactor / Rename (Shift+F6) java property from the reference in .kt file.
All goes Ok, but in Java code only accessors are changed. The field name is still prop .
Would be better to rename it accordingly?

 

 

KT-11883 - IntelliJ - Extract Variable refactoring - Specify type explicitly by default

I haven’t found a way to configure that I need “specify type explicitly” checkbox to be set by default on Extract Variable refactoring.

 

 

KT-11050 - “Extract constant” refactoring

It is useful to have the refactoring which creates “val” property in companion object.

Relates to: KT-4938

Subtask of: KT-4728

 

 

KT-10346 - Support || in when conditions in “Introduce subject” intention

Comma-separated conditions for when without argument are prohibited.
So, instead of

when {
  x is A, x is B -> println("A or B")
}

we have

when {
  x is A || x is B -> println("A or B")
}

However, this is not supported in “Introduce subject” intention action for when . Ideally it should be something like “treat || arguments as separate or-ed conditions when determining applicability and introducing when subject”, so the example above should be refactored into:

when (x) {
  is A, is B -> println("A or B")
}

 

 

KT-9941 - Unwrap does nothing when called from a companion object

class Test {
  companion object {
    <cursor> val v = "";
  }
}
  1. Invoke “Unwrap”.
    Expected:
class Test {
  val v = "";
}

Actual: no changes.
Rather inconvenient especially after converting Java to Kotlin when all statics are converted to members of the companion object.

 

 

KT-9851 - Create “Extract/Introduce lateinit var” Refactoring

I’m looking for the Kotlin equivalent refactoring to https://www.jetbrains.com/idea/help/extract-field.html, for example:

//Before
class MyClass() : SomeLibrary {
    override fun someLibraryInitMethod() {
        var theSomething = newSomething()
    }

    private fun useTheSomethingLater() {
        theSomething.doSomething() // is in error since theSomething isn't in scope
    }
}

becomes (after putting cursor on “theSomething” and selecting “Extract …”)

//After
class MyClass() {
    private lateinit var something : Something

    override fun someLibraryInitMethod() {
        theSomething = newSomething()
    }

    private fun useTheSomethingLater() {
        theSomething.doSomething() //now not in error
    }
}

 

 

KT-9822 - Inline should provide label inside lambda

Inlining the val toDigest

fun String.toSha1():String {
    val toDigest = this
    val digest = with(MessageDigest.getInstance("SHA-1")) {
        update(toDigest.toByteArray("UTF-8"))
        digest()
    }
    return BigInteger(1, digest).toString(16)
}

produces

fun String.toSha1():String {
    val digest = with(MessageDigest.getInstance("SHA-1")) {
        update(this.toByteArray("UTF-8"))
        digest()
    }
    return BigInteger(1, digest).toString(16)
}

witch is false.
It should produce

fun String.toSha1():String {
    val digest = with(MessageDigest.getInstance("SHA-1")) {
        update(this@toSha1.toByteArray("UTF-8"))
        digest()
    }
    return BigInteger(1, digest).toString(16)
}

 

 

KT-9748 - Rename function may change semantics of the code when overload with default parameters exist

class C {
    fun foo() {
        bar()
    }

    fun bar(p: Int = 0) {
    }
}

Rename ‘foo’ to ‘bar’ or ‘bar’ to ‘foo’. Semantics of the function ‘foo’ body is changed so that it calls itself.

 

 

KT-9383 - Refactor / Rename: inconsistent update of annotation references with use-site targets

Provide usages of annotation with use-site targets:

annotation class AnnTarget
class Dummy(@field:AnnTarget val pf: Int,
            @get:AnnTarget val pg: Int,
            @set:AnnTarget var ps: Int,
            @param:AnnTarget var pp: Int) 

Close the project, reopen the project and file.
The source looks correct. All AnnTarget entries are dark-blue, Quick Doc (Ctrl+Q) works on them. Ok.
Refactor / Rename AnnTarget to something, AnnTarget2 . Wait for analysis to complete.
One stable effect is that all entries are still dark-blue, while @field:AnnTarget2 is black. Why is that?
But there are other effects, that occurred only once for me (out of 5-6 attempts):

  • One time entries in @get: and @set: have not been renamed.
  • One time Quick Doc said “No documentation found” for @field:AnnTarget2 .
  • One time Ctrl+B said “Cannot find declaration” for @field:AnnTarget2 .

 

 

KT-9212 - Inline rename for method parameters

 

 

KT-9054 - Copy / pasting a Kotlin file should bring up the Copy Class dialog

In Java, if you have a class you want to duplicate, you can just select the file in the Project toolbar and press CTRL-C, CTRL-V it. It brings up the “Copy Class” dialog which is so easy to use.
In Kotlin, it asks me to specify a new directory, and if I try to keep the directory the same, it treats that as an error. So I can’t just copy / paste it quickly anymore.

 

 

KT-8980 - Renaming constructor parameter is very slow

For example, create a class with constructor parameter (not even val!) named “element”. Renaming it takes quite some time.

 

 

KT-8954 - Refactor / Copy could have “Update package directive” option

Invoke Refactor / Move for a .kt file, check the dialog: there is “Update package directive” option.
Invoke Refactor / Copy for the same file, check the dialog: there is no such option.
Actually it may be useful on copy to different directory.

 

 

KT-8563 - Refactor / Rename inserts line breaks without reason

Create sample Kotlin class like:

public class OneLineKotlin {
    var subject = 1
    var aux = 1
    public fun simple0() { subject }
    public fun simple1() { subject = 2 }
    public fun simple2() { aux = subject }
} 

Refactor / Rename subject to, say subject2 . Result:

public class OneLineKotlin {
    var subject2 = 1
    var aux = 1
    public fun simple0() {
        subject2
    }
    public fun simple1() { subject2 = 2 }
    public fun simple2() { aux = subject2
    }
} 

Why the line breaks are added?
It’s not the fully formatted code, otherwise every function body would get line breaks, while this result is different.
Probably no “formatting” or line breaks inserting should be done.

 

 

KT-8545 - Rename: in-place mode could be supported for Kotlin

Settings / … / Editor / General / Refactorings / Enable in-place mode = Yes.
Call Rename (Shift+F6) on a Java class, field, method: it’s in-place.
Call Rename on a Kotlin class, property, function: the dialog is still shown.

 

 

KT-8514 - Refactor / Rename: “Rename variables” option could be supported for object declaration

Declare an object, like:

public object ObjectA {
    fun objectContent() { println("Inside an object.") }
}
fun main(args: Array<String>) {
    val objectA = ObjectA
    objectA.objectContent()
} 

Call Refactor / Rename on the object name: there is no “Rename variables” option in the dialog.
In general, a variable can exist, like in the example.
(We can make it more complex, e.g. surround the assignment with a condition, so the assignment can have some sense.)
So, the option could be supported.

 

 

KT-8397 Safe Delete leaves syntactically incorrect code

import kotlin.data as import
class A // Safe delete A
import class B

ACTUAL:

import kotlin.data as import

import class B // Error: Expecting qualified name

EXPECTED:

import kotlin.data as import

@import class B // OK

 

 

KT-8349 - Rename: changing Kotlin file item to Kotlin class item loses selection in the project tree

Create package P.
Open Project tool window to see the P item in the tree.
In P create a file FileName.kt with the only class ClassName inside.
Select FileName.kt in the tree.
Refactor / Rename the file to ClassName (so you want to get single-class file).
Refactoring is successful, but the file node is transformed to a class node, and loses selection. That’s the issue.
No problem if file item is changed to file item.
Failed to model the same case in Java support.

 

 

KT-8306 - Poor conflict resolution in Rename refactoring

import D
import D.A
class D { // Rename D to A
    open class A
}
class E : A()

ACTUAL:

import A
import A.A
class A {
    open class A
}
class E : A() // Error: This type is final, so it cannot be inherited from

EXPECTED:

import A
import A.A
class A {
    open class A
}
class E : A.A() // OK

 

 

KT-8180 - Implement Copy Class action for Kotlin classes

 

 

KT-7520 - Exception when try rename label from usage

fun foo<R>(f: () -> R) = f()

public fun test() {
    foo {
        return@<caret0>foo false
    }

    foo (fun (): Boolean {
        return@<caret1>foo<caret2> false
    })
}

 

 

KT-7516 - Rename refactoring doesn’t rename related labels

fun <caret>foo<R>(f: () -> R) = f()

public fun test() {
    foo {
        return@foo false
    }
}

Result:

fun bar<R>(f: () -> R) = f()

public fun test() {
    bar {
        return@foo false // red code here
    }
}

 

 

KT-7107 - Rename refactoring for labels

 

 

KT-6731 - Rename class/method refactoring should also rename relevant run configurations

 

 

KT-6322 - ‘Inline Super Class’ fails with exception when invoked on java class

Invoke ‘Inline’ refactoring on ‘B’ reference in the following code:

public class K: B() {
    override fun run() {
    }
}

where B.java is

public abstract class B implements Runnable {
}

IDE will show ‘Inline Super Class’ dialog but will fail with exception when you press ‘Refactor’ (note that if ‘B’ is Kotlin class refactoring will be disabled):

 

 

KT-6215 - Converter: provide an option to place multiple java files converted to Kotlin in one Kotlin file

 

 

KT-5864 - Refactoring: convert get/set methods to property

 

 

KT-5602 - move class/method refactoring should be able to move file

 

 

KT-4473 - Getter rename does not work right from JSP bean

I have JSP bean that uses Kotlin data class.

data class foo(val bar:String) 
<jsp:useBean id="a" type="foo" /> ${a.bar} 

I rename bar into zzz form JSP code.
I see warning in JSP after rename around ${a.zzz}
In the Kotlin class I see getZzz instead of zzz for the property

 

 

KT-4379 Support renaming import alias

for example:
import java.lang.String as jString
then pressing Shift-F6 while cursor is over jString or other use of jString in code, does not allow renaming

 

 

KT-2638 - Inline function/non-trivial property refactoring

The absence of this feature is extremely annoying. It devalues the whole local function feature in the language.

fun main(args: Array<String>) {
    fun local() { // Inline refactoring is not available here
        println("Test")
    }

    local()
}

 

 

KT-1351 References are not updated after java private package class rename

References are not updated after java private package class rename if kotlin file has same package but placed in another folder.

 

Feature Groups