Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bennyhuo/kotlin-trim-indent
This is a Kotlin compiler plugin for a compile-time indent trim of raw String.
https://github.com/bennyhuo/kotlin-trim-indent
compile-plugin compiler kotlin
Last synced: about 2 hours ago
JSON representation
This is a Kotlin compiler plugin for a compile-time indent trim of raw String.
- Host: GitHub
- URL: https://github.com/bennyhuo/kotlin-trim-indent
- Owner: bennyhuo
- License: mit
- Created: 2022-06-02T07:59:08.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-06-19T22:44:17.000Z (5 months ago)
- Last Synced: 2024-06-21T02:47:38.910Z (5 months ago)
- Topics: compile-plugin, compiler, kotlin
- Language: Kotlin
- Homepage:
- Size: 95.7 KB
- Stars: 40
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Trim Indent
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.bennyhuo.kotlin/trimindent-gradle-plugin/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.bennyhuo.kotlin/trimindent-gradle-plugin)
This is a Kotlin compiler plugin for a compile-time indent trim of raw String. It is pretty useful for String templates with multiline String variables.
## Examples
We have two Strings as below:
```kotlin
val s = """
hello
world
!!!
""".trimIndent()val s2 = """
$s
hello2
world2
!!!
""".trimIndent()
```After compilation, we will get:
```kotlin
val s = "\n hello\n world\n !!!\n".trimIndent()
val s2 = "\n " + s + "\n hello2\n world2 !!!\n".trimIndent()
```No spaces are dropped in the compile-time.
In the runtime, the `trimIndent` function is called. For `s`, the common indent will be removed as expected:
```
hello
world
!!!
```But for `s2`, things are going to be weird.
```
hello
world
!!!
hello2
world2
!!!
```This is because the computation of common indent size is based on the runtime String value which also contains the new lines of `s`.
With this plugin installed, the runtime value of String template variables are ignored, and only the white spaces of the String literal will be removed so that `s2` will be like:
```
hello
world
!!!
hello2
world2
!!!
```With version 1.7.10.3, you can also do this magic:
```
val s1 = """
if(a > 1) {
return true
}
""".trimIndent()
val s2 = """
def test(a) {
$s1
}
""".trimIndent()val s3 = """
class Test {
$s2
}
""".trimIndent()
```The result value of s3 will be:
```
class Test {
def test(a) {
if(a > 1) {
return true
}
}
}
```It is really useful when you use Kotlin String template as a template to render some other Strings.
## Try it
```
plugins {
...
id("com.bennyhuo.kotlin.trimindent") version ""
}
```## Change Log
See [releases](ttps://github.com/bennyhuo/Kotlin-Trim-Indent/releases).