Hi, I'm trying to use the Structural Replace tool ...
# intellij
a
Hi, I'm trying to use the Structural Replace tool to create an inspection to replace an object to a class with a companion object containing the original object fields, but I don't understand what is the problem here ?
b
You can't match a full declaration with a template variable, you need to either put the
val/var/fun
keyword, then substitute the identifier with a template variable and put the count filter on it like this:
Copy code
<replaceConfiguration name="Unnamed" text="object $CLASS$ {&#10;    var $FIELD$&#10;    &#10;    fun $METHODS$()&#10;}" recursive="false" type="Kotlin" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="false" replacement="class $CLASS$ {&#10;    companion object { &#10;        var $FIELD$&#10;&#10;        fun $METHODS$()&#10;    }&#10;}">
  <constraint name="__context__" within="" contains="" />
  <constraint name="CLASS" within="" contains="" />
  <constraint name="FIELD" minCount="0" maxCount="2147483647" within="" contains="" />
  <constraint name="METHODS" minCount="0" maxCount="2147483647" within="" contains="" />
</replaceConfiguration>
Unfortunately, this still doesn't get you the desired behavior because when searching for
object
declarations, companion objects are matched as well. This is planned to change in 2021.1 (see KTIJ-20631).
a
okay I see, thanks !