Skip to content

Instantly share code, notes, and snippets.

@osipxd
Last active May 8, 2025 16:06
Show Gist options
  • Save osipxd/77af6f9420abf276cb1a0b6ee66a493f to your computer and use it in GitHub Desktop.
Save osipxd/77af6f9420abf276cb1a0b6ee66a493f to your computer and use it in GitHub Desktop.
Collection of useful Structural Replace templates

Collection of useful Structural Replace templates.

How to Use?

  1. Copy the desired template XML
  2. In IntelliJ IDEA open Edit | Find | Replace Structurally...
  3. Press Ctrl + V ( + V)
  4. Click Find

Templates

Gradle

Migrate to type-safe project accessors

Replaces project references made in string notation with type-safe notation.

 dependencies {
-    implementation(project(":project-name:subproject-name"))
+    implementation(projects.projectName.subprojectName)
 }

Template:

<replaceConfiguration name="Migrate to type-safe project accessors" text="project(&quot;:$projectPath$&quot;)" recursive="false" type="Kotlin" pattern_context="default" search_injected="false" reformatAccordingToStyle="false" shortenFQN="false" replacement="projects$transformedPath$">
  <constraint name="__context__" within="" contains="" />
  <constraint name="projectPath" within="" contains="" />
  <variableDefinition name="transformedPath" script="&quot;projectPath.text.replace(':', '.').split('[_-]').collect { word -&gt; word.capitalize() }.join('')&quot;" />
</replaceConfiguration>

KMP: Compact sourceSet dependecies block

Reduces nesting level of dependncies declaration block in a KMP project. Requires Kotlin Gradle Plugin 1.9.20+

 kotlin {
     sourceSets {
-        commonTest {
-            dependencies {
-                implementation(kotlin("test"))
-            }
+        commonTest.dependencies {
+            implementation(kotlin("test"))
         }
     }
 }

Template:

<replaceConfiguration name="KMP: Compact sourceSet dependecies block" text="$sourceSet$ {&#10;    dependencies {&#10;        $dependencies$&#10;    }&#10;}" recursive="false" type="Kotlin" pattern_context="default" search_injected="false" reformatAccordingToStyle="false" shortenFQN="false" replacement="$sourceSet$.dependencies {&#10;    $dependencies$&#10;}">
  <constraint name="__context__" within="" contains="" />
  <constraint name="dependencies" minCount="0" maxCount="2147483647" within="" contains="" />
  <constraint name="sourceSet" within="" contains="" />
</replaceConfiguration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment