【Android Studio】Gradle Version Catalog「Live Template」を使って インライン・テーブル に瞬時に書き換える

だるいですよね、インラインテーブル記述。


name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }
animal = { type.name = "pug" }

👉 TOML: 日本語 v0.5.0 hatena-bookmark

Android Studio でも

Graadle Version Catalog で、最初は、こんな記述でも


[plugins]
android-material = "com.google.android.material:material:1.8.0-alpha02"

やがて、


[plugins]
android-material = { module = "com.google.android.material:material", version = "1.8.0-alpha02" }

となり


[versions]
android-material = "1.8.0-alpha02"

[plugins]
android-material = { module = "com.google.android.material:material", version.ref = "android-material" }

というふうに変わっていきます。

大量にやってるのも地味にだるい。

Live Templete を使って書き換えます。


Preferences

  ↓

Live Templates

から作っていきます。

ここマクロたちは、思ったより使いづらいものが多い。

live template macro

👉 intellij-community/platform/lang-impl/src/com/intellij/codeInsight/template/macro at master · JetBrains/intellij-community hatena-bookmark

割と使える Groovy のマクロ。

groovyScript(, [arg, ...])

Executes the Groovy script passed as a string.

The first argument is a string with either the text of the script or the path to the file that contains the script. The function passes other optional arguments to the script as values for _1, _2, _3, ..., _n variables. Also, you can access the current editor from inside the script using the _editor variable.

The following example shows a groovyScript() function that splits the selected text into words and displays them as a numbered list:


groovyScript("def result = ''; _1.split().eachWithIndex { item, index -> result = result + index.next() + '. ' + item + System.lineSeparator() }; return result;", SELECTION);

👉 Edit Template Variables dialog | IntelliJ IDEA hatena-bookmark

以下の感じで設定して4つのテンプレートを記述しました。

どれも似たような記述なので、一つだけ貼っておきます。

使い回せます。

使い方は、該当文字列をコピーしてから、Abbreviation を入力で、テンプレートを吐き出しながらキーなどを編集してください。

結果。

toml live template

キーの編集に連携されて関連記述が編集されるのがいいところでしょうか。

けどまあ、Android Studio 本体の機能更新が待たれるところですが。

 

🙆 追記

複数行一括のほうがいいので以下の方法を実際は使っています。