Skip to content

Kotlin DSL support #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6e26af4
Add task for fetching Gradle's runtime dependencies
fwcd Sep 3, 2019
58718e3
Add Gradle runtime libraries for Kotlin DSL projects
fwcd Sep 3, 2019
2c4e02a
List all Gradle dependency JARs in classpath finder
fwcd Sep 3, 2019
68ccda0
Load build script template in Compiler
fwcd Sep 3, 2019
b48aca4
Create separate task for finding Kotlin DSL libs
fwcd Sep 3, 2019
260db73
Use new Gradle tasks in classpath resolver
fwcd Sep 3, 2019
1f1c3e2
Catch class loader exceptions in Compiler
fwcd Sep 3, 2019
f9cb99e
Print entire path in kotlinLSPKotlinDSLDeps
fwcd Sep 3, 2019
2fef42d
Run all Gradle tasks in a single execution
fwcd Sep 3, 2019
8f48d8c
List Kotlin DSL accessors and split into two classpath finders
fwcd Sep 3, 2019
b4a1467
Add Gradle's base-services to the Kotlin DSL classpath
fwcd Sep 4, 2019
66c850d
Separate classpath from build script classpath
fwcd Sep 4, 2019
a2078c9
Add compilation kinds
fwcd Sep 4, 2019
b8ed07d
Query build script classpath and auto-update on change
fwcd Sep 4, 2019
ba80405
Delegate build script class path in WithStdlibResolver
fwcd Sep 4, 2019
2100c1c
Add Todo note to CompilerClassPath.refresh
fwcd Sep 4, 2019
566fd6f
Just update regular class path when build script changes
fwcd Sep 4, 2019
31997f6
Use .kts for in-memory script source files
fwcd Sep 4, 2019
d25bcd7
Update to Kotlin 1.3.50 and use new scripting API
fwcd Sep 4, 2019
bd86167
WIP: Use ScriptDefinition.FromLegacy
fwcd Sep 5, 2019
da8f404
WIP: Use compilation kinds throughout Compiler
fwcd Sep 5, 2019
f57c522
Use custom ScriptingHostConfiguration for DSL script templates
fwcd Sep 5, 2019
0d8f245
Define script templates using ScriptingConfigurationKeys
fwcd Sep 5, 2019
e3aae57
Add support for Kotlin DSL settings/init scripts
fwcd Sep 5, 2019
c6c4d95
Improve script definition logging
fwcd Sep 5, 2019
09815a5
WIP: Experiment with script definition construction
fwcd Sep 5, 2019
a5edd49
Provide more helpful error messages when the Gradle build fails
fwcd Sep 5, 2019
be6f43d
Implicitly import Kotlin DSL
fwcd Sep 5, 2019
5fb5825
Log script template at debug level
fwcd Sep 5, 2019
1226788
Fix tests
fwcd Sep 5, 2019
3dd14ba
Add Gradle DSL unit test
fwcd Sep 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ projectVersion=0.3.0
teamCityUrl=https://teamcity.jetbrains.com
teamCityUsername=guest
teamCityPassword=guest
kotlinVersion=1.3.40
kotlinBuildType=Kotlin_1340_CompilerAllPlugins
kotlinBuild=1.3.41-release-150
kotlinPluginBuild=1.3.41-release-IJ2019.2-1
kotlinVersion=1.3.50
kotlinBuildType=Kotlin_1350_CompilerAllPlugins
kotlinBuild=1.3.50-release-127
kotlinPluginBuild=
6 changes: 5 additions & 1 deletion scripts/update_kt_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def is_plugin_artifact(art_name):
def to_plugin_build(art_name):
return art_name.lstrip("kotlin-plugin-").rstrip(".zip")

class Unnamed:
def name(self):
return ""

def main():
props_file = "gradle.properties"

Expand All @@ -39,7 +43,7 @@ def main():
build = prompt_by("build", builds, TeamCityNode.number).follow()

artifacts = [art for art in build.follow("artifacts").findall("file") if is_plugin_artifact(art.name())]
artifact = prompt_by("plugin build", artifacts, lambda art: to_plugin_build(art.name()))
artifact = prompt_by("plugin build", artifacts, lambda art: to_plugin_build(art.name()), Unnamed())

changes = {
"kotlinVersion": version.name(),
Expand Down
8 changes: 4 additions & 4 deletions scripts/utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def require_not_none(description, x):
if x == None:
sys.exit(description + " not present")

def prompt_by(what, nodes, describer):
def prompt_by(what, nodes, describer, default=None):
node_dict = {describer(node): node for node in nodes}
sorted_described = sorted(node_dict.keys(), key=alphanum_sort_key)

Expand All @@ -30,12 +30,12 @@ def prompt_by(what, nodes, describer):
print()

last_entry = sorted_described[-1] if len(sorted_described) > 0 else None
choice = input("Enter a " + what + " to choose [default: " + last_entry + "]: ").strip()
choice = input(f"Enter a {what} to choose [default: {last_entry}]: ").strip()
print()

if len(choice) == 0:
if len(choice) == 0 and last_entry:
return node_dict[last_entry]
elif choice not in node_dict.keys():
sys.exit("Invalid " + what + "!")
return default
else:
return node_dict[choice]
10 changes: 8 additions & 2 deletions server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
implementation 'org.jetbrains.kotlin:kotlin-compiler-embeddable'
implementation 'org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable'
implementation 'org.jetbrains.kotlin:kotlin-scripting-compiler-impl-embeddable'
implementation 'org.jetbrains.kotlin:kotlin-scripting-jvm-host-embeddable'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains:fernflower:1.0'
implementation 'com.pinterest.ktlint:ktlint-core:0.34.2'
Expand All @@ -62,9 +63,9 @@ dependencies {
testImplementation 'org.hamcrest:hamcrest-all:1.3'
testImplementation 'junit:junit:4.11'
testImplementation 'org.openjdk.jmh:jmh-core:1.20'
testImplementation 'org.jetbrains.kotlin:kotlin-scripting-jvm-host-embeddable'

// See https://github.com/JetBrains/kotlin/blob/65b0a5f90328f4b9addd3a10c6f24f3037482276/libraries/examples/scripting/jvm-embeddable-host/build.gradle.kts#L8
compileOnly 'org.jetbrains.kotlin:kotlin-scripting-jvm-host'
testCompileOnly 'org.jetbrains.kotlin:kotlin-scripting-jvm-host'

annotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.20'
Expand Down Expand Up @@ -96,6 +97,11 @@ task copyPropertiesToTestWorkspace(type: Copy) {
into file('src/test/resources/additionalWorkspace')
}

task copyPropertiesToDSLTestWorkspace(type: Copy) {
from "$rootDir/gradle.properties"
into file('src/test/resources/kotlinDSLWorkspace')
}

task fixFilePermissions(type: Exec) {
// When running on macOS or Linux the start script
// needs executable permissions to run.
Expand Down Expand Up @@ -132,7 +138,7 @@ run {
}

test {
dependsOn copyPropertiesToTestWorkspace
dependsOn copyPropertiesToTestWorkspace, copyPropertiesToDSLTestWorkspace

testLogging {
events 'failed'
Expand Down
21 changes: 12 additions & 9 deletions server/src/main/kotlin/org/javacs/kt/CompiledFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.types.KotlinType
import java.nio.file.Paths

class CompiledFile(
val content: String,
val parse: KtFile,
val compile: BindingContext,
val container: ComponentProvider,
val sourcePath: Collection<KtFile>,
val classPath: CompilerClassPath) {

val content: String,
val parse: KtFile,
val compile: BindingContext,
val container: ComponentProvider,
val sourcePath: Collection<KtFile>,
val classPath: CompilerClassPath,
val isScript: Boolean = false,
val kind: CompilationKind = CompilationKind.DEFAULT
) {
/**
* Find the type of the expression at `cursor`
*/
Expand All @@ -38,7 +41,7 @@ class CompiledFile(
bindingContextOf(expression, scopeWithImports).getType(expression)

fun bindingContextOf(expression: KtExpression, scopeWithImports: LexicalScope): BindingContext =
classPath.compiler.compileExpression(expression, scopeWithImports, sourcePath).first
classPath.compiler.compileExpression(expression, scopeWithImports, sourcePath, kind).first

private fun expandForType(cursor: Int, surroundingExpr: KtExpression): KtExpression {
val dotParent = surroundingExpr.parent as? KtDotQualifiedExpression
Expand Down Expand Up @@ -120,7 +123,7 @@ class CompiledFile(
}

val padOffset = " ".repeat(offset)
val recompile = classPath.compiler.createFile(padOffset + surroundingContent)
val recompile = classPath.compiler.createFile(padOffset + surroundingContent, Paths.get("dummy.virtual" + if (isScript) ".kts" else ".kt"), kind)
return recompile.findElementAt(cursor)?.findParent<KtElement>()
}

Expand Down
Loading