Code Monkey home page Code Monkey logo

kotlin-trim-indent's Introduction

Trim Indent

License: MIT Maven Central

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:

val s = """
    hello
    world
    !!!
""".trimIndent()

val s2 = """
    $s
    hello2
    world2
    !!!
""".trimIndent()

After compilation, we will get:

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 "<latest-version>"
}

Change Log

See releases.

kotlin-trim-indent's People

Contributors

bennyhuo avatar

Stargazers

 avatar Maxim avatar GAURAV avatar Tim Kersey avatar Steven Shaw avatar Luiz T. avatar styluo avatar Adam avatar Sebastian Schuberth avatar 茶 avatar  avatar lt avatar  avatar Hasiy avatar Andrew Johnson avatar huixing avatar Jia Yanwei avatar Jeffro Hu avatar panpf avatar 行一 avatar 贇 avatar masx200 avatar cketti avatar RicardoJiang avatar Roman Mitasov avatar rami3l avatar Siubeng avatar  avatar RE avatar Hulk Su avatar  avatar 陈 轲 avatar alleny avatar zsub avatar Mistletoe avatar liuleshuai avatar Ultra-Dejavu avatar Night avatar  avatar  avatar

Watchers

 avatar  avatar

kotlin-trim-indent's Issues

可不可以在插入多行时, 考虑缩进

比如这段代码

val s1 = """
    if(a > 1) {
        return true
    }
""".trimIndent()
val s2 = """
    def test(a) {
        $s1
    }
""".trimIndent()
println(s2)

会打印

def test(a) {
    if(a > 1) {
    return true
}
}

能不能打印成这样?

def test(a) {
    if(a > 1) {
        return true
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.