JetBrains / kotlin-compiler-server

Server for executing kotlin code
Apache License 2.0
248 stars 73 forks source link

Support completion hints for type-safe builders inferences in *.kts files #708

Open CeruleanW opened 11 months ago

CeruleanW commented 11 months ago

Description

The auto-completion feature in the Kotlin Compiler Server does not support type-safe builders when used in a standalone context outside of a function, which is the scenario for *.kts files. This is particularly noticeable with DSLs.

Expected Behavior

Auto-completion should work consistently for type-safe builders, e.g. HTML builders, both within functions and in standalone usage. This would significantly improve the development experience, especially when working with DSLs.

Current Behavior

While auto-completion works correctly within a function scope, it fails to provide proper suggestions or hints for standalone type-safe builders without a main function.

Steps to Reproduce

Use a type-safe HTML builder within a function and observe the functioning auto-completion. Use the same builder in a standalone context. Notice the lack of auto-completion hints.

Example


package html

// Inside a function - auto-completion works
fun main() {
    html {
        head {
            title { +"HTML encoding with Kotlin" }
        }
    }
}

// Standalone - auto-completion does not work
html {
    head {
        title { +"HTML encoding with Kotlin" }
    }
}

class HTML() : TagWithText("html") {
    fun head(init: Head.() -> Unit) = initTag(Head(), init)
    val sampleValue = 222
}

// [Rest of the provided code for Element, TextElement, Tag, etc.]

In the above example, the auto-completion feature assists with the html builder when used inside the main function but does not provide assistance for the standalone html builder.

Snipaste_2023-11-29_09-57-19 Snipaste_2023-11-29_09-58-14

CeruleanW commented 11 months ago

Sample code: Playground.txt