diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 564ffcb..7dce2e9 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -10,9 +10,10 @@ gradle-tooling = "+" declarative-dsl = "+" [libraries] -kotlinx-coroutines = { group = "org.jetbrains.kotlinx", name="kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } -lsp4j = { group = "org.eclipse.lsp4j", name="org.eclipse.lsp4j", version.ref = "lsp4j" } -logback-classic = { group = "ch.qos.logback", name="logback-classic", version.ref = "logback-classic" } +kotlinx-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } +kotlinx-coroutines-core-jvm = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core-jvm", version.ref = "kotlinx-coroutines" } +lsp4j = { group = "org.eclipse.lsp4j", name = "org.eclipse.lsp4j", version.ref = "lsp4j" } +logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logback-classic" } gradle-tooling-api = { module = "org.gradle:gradle-tooling-api", version.ref = "gradle-tooling" } gradle-declarative-dsl-core = { module = "org.gradle:gradle-declarative-dsl-core", version.ref = "declarative-dsl" } gradle-declarative-dsl-evaluator = { module = "org.gradle:gradle-declarative-dsl-evaluator", version.ref = "declarative-dsl" } diff --git a/lsp/build.gradle.kts b/lsp/build.gradle.kts index c5f5196..53497b8 100644 --- a/lsp/build.gradle.kts +++ b/lsp/build.gradle.kts @@ -6,6 +6,8 @@ plugins { dependencies { implementation(project(":model")) + implementation(libs.kotlinx.coroutines.core) + implementation(libs.kotlinx.coroutines.core.jvm) implementation(libs.lsp4j) implementation(libs.gradle.tooling.api) implementation(libs.gradle.declarative.dsl.core) diff --git a/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeLanguageServer.kt b/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeLanguageServer.kt index e02f1b1..a220d69 100644 --- a/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeLanguageServer.kt +++ b/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeLanguageServer.kt @@ -1,23 +1,25 @@ package org.gradle.declarative.lsp.server +import kotlinx.coroutines.* import org.eclipse.lsp4j.* import org.eclipse.lsp4j.services.* +import org.gradle.declarative.dsl.tooling.models.DeclarativeSchemaModel +import org.gradle.declarative.lsp.tooling.ConnectionHandler import org.gradle.tooling.GradleConnector +import java.io.File +import java.net.URI import java.util.concurrent.CompletableFuture import java.util.logging.Logger import kotlin.system.exitProcess class DeclarativeLanguageServer : LanguageServer, LanguageClientAware { - val LOG = Logger.getLogger(DeclarativeLanguageServer::class.java.name) + private lateinit var client: LanguageClient - lateinit var client: LanguageClient - val connectorMap = mutableMapOf() + private val textDocumentService = DeclarativeTextDocumentService() + private val workspaceService = DeclarativeWorkspaceService() - val textDocumentService = DeclarativeTextDocumentService() - val workspaceService = DeclarativeWorkspaceService() - - var initialized = false - var tracingLevel = TraceValue.Off; + private var initialized = false + private var tracingLevel = TraceValue.Off; private fun checkInitialized() { if (!initialized) { @@ -34,6 +36,15 @@ class DeclarativeLanguageServer : LanguageServer, LanguageClientAware { override fun initialize(params: InitializeParams?): CompletableFuture { val serverCapabilities = ServerCapabilities() serverCapabilities.setTextDocumentSync(TextDocumentSyncKind.Full) + serverCapabilities.setHoverProvider(true) + + val workspaceFolder = params!!.workspaceFolders[0] + val workspaceFolderFile = File(URI.create(workspaceFolder.uri)) + System.err.println("Fetching declarative model for workspace folder: $workspaceFolderFile") + ConnectionHandler(workspaceFolderFile).let { + val declarativeModel = it.getDeclarativeModel() + textDocumentService.setDeclarativeModel(declarativeModel) + } initialized = true System.err.println("Gradle Declartive Language Server: initialized") diff --git a/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeTextDocumentService.kt b/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeTextDocumentService.kt index 767145a..c7314d2 100644 --- a/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeTextDocumentService.kt +++ b/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeTextDocumentService.kt @@ -1,23 +1,42 @@ package org.gradle.declarative.lsp.server -import org.eclipse.lsp4j.DidChangeTextDocumentParams -import org.eclipse.lsp4j.DidCloseTextDocumentParams -import org.eclipse.lsp4j.DidOpenTextDocumentParams -import org.eclipse.lsp4j.DidSaveTextDocumentParams +import org.eclipse.lsp4j.* import org.eclipse.lsp4j.services.LanguageClient import org.eclipse.lsp4j.services.LanguageClientAware import org.eclipse.lsp4j.services.TextDocumentService +import org.gradle.declarative.dsl.tooling.models.DeclarativeSchemaModel +import org.gradle.internal.declarativedsl.evaluator.main.AnalysisSequenceResult +import org.gradle.internal.declarativedsl.evaluator.main.SimpleAnalysisEvaluator +import java.io.File +import java.net.URI +import java.util.concurrent.CompletableFuture class DeclarativeTextDocumentService(): TextDocumentService, LanguageClientAware { + lateinit var schemaEvaluator: SimpleAnalysisEvaluator lateinit var client: LanguageClient + val schemaStore = mutableMapOf() + override fun connect(client: LanguageClient?) { this.client = client!! } + fun setDeclarativeModel(declarativeModel: DeclarativeSchemaModel) { + schemaEvaluator = SimpleAnalysisEvaluator.withSchema(declarativeModel.settingsSequence, declarativeModel.projectSequence) + } + override fun didOpen(params: DidOpenTextDocumentParams?) { - System.err.println("Opened document: ${params?.textDocument?.uri}") + params?.textDocument?.uri?.let { uri -> + System.err.println("Opened document: $uri") + System.err.println("Parsing declarative model for document: $uri") + + val file = File(URI.create(uri)) + val schema = schemaEvaluator.evaluate(file.name, file.readText()) + + schemaStore[uri] = schema + System.err.println("Parsed declarative model for document: $uri") + } } override fun didChange(params: DidChangeTextDocumentParams?) { @@ -32,4 +51,12 @@ class DeclarativeTextDocumentService(): TextDocumentService, LanguageClientAware System.err.println("Saved document: ${params?.textDocument?.uri}") } + override fun hover(params: HoverParams?): CompletableFuture { + params?.textDocument?.uri?.let { uri -> + val position = params.position + System.err.println("Hovering over document: $uri at line ${position.line} column ${position.character}") + } + + return CompletableFuture.completedFuture(null) + } } \ No newline at end of file diff --git a/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeWorkspaceService.kt b/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeWorkspaceService.kt index 286a6db..8d4c9fd 100644 --- a/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeWorkspaceService.kt +++ b/lsp/src/main/kotlin/org/gradle/declarative/lsp/server/DeclarativeWorkspaceService.kt @@ -5,10 +5,11 @@ import org.eclipse.lsp4j.DidChangeWatchedFilesParams import org.eclipse.lsp4j.services.LanguageClient import org.eclipse.lsp4j.services.LanguageClientAware import org.eclipse.lsp4j.services.WorkspaceService +import org.gradle.declarative.dsl.tooling.models.DeclarativeSchemaModel class DeclarativeWorkspaceService(): WorkspaceService, LanguageClientAware { - lateinit var client: LanguageClient + private lateinit var client: LanguageClient override fun connect(client: LanguageClient?) { this.client = client!! diff --git a/lsp/src/main/kotlin/org/gradle/declarative/lsp/tooling/ConnectionHandler.kt b/lsp/src/main/kotlin/org/gradle/declarative/lsp/tooling/ConnectionHandler.kt index fe9ad79..2b49772 100644 --- a/lsp/src/main/kotlin/org/gradle/declarative/lsp/tooling/ConnectionHandler.kt +++ b/lsp/src/main/kotlin/org/gradle/declarative/lsp/tooling/ConnectionHandler.kt @@ -1,26 +1,21 @@ package org.gradle.declarative.lsp.tooling -import org.gradle.declarative.lsp.action.GetResolvedDomAction -import org.gradle.declarative.lsp.model.ResolvedDomPrerequisites import org.gradle.declarative.dsl.tooling.models.DeclarativeSchemaModel import org.gradle.tooling.GradleConnector import org.gradle.tooling.ProjectConnection import java.io.File class ConnectionHandler(val projectRoot: File) { - fun getDeclarativeModel(): ResolvedDomPrerequisites { - val action = GetResolvedDomAction() - + fun getDeclarativeModel(): DeclarativeSchemaModel { var connection: ProjectConnection? = null try { connection = GradleConnector .newConnector() .forProjectDirectory(projectRoot) .connect() - val model = connection - .action(action) - .run() - return model + return connection + .model(DeclarativeSchemaModel::class.java) + .get() } finally { connection?.close() } diff --git a/lsp/src/main/kotlin/org/gradle/declarative/lsp/utils/ParamsUtils.kt b/lsp/src/main/kotlin/org/gradle/declarative/lsp/utils/ParamsUtils.kt new file mode 100644 index 0000000..1b5e188 --- /dev/null +++ b/lsp/src/main/kotlin/org/gradle/declarative/lsp/utils/ParamsUtils.kt @@ -0,0 +1,7 @@ +package org.gradle.declarative.lsp.utils + +object TextDocumentUtils { + + + +} \ No newline at end of file diff --git a/lsp/src/test/kotlin/org/gradle/declarative/lsp/tooling/ConnectionHandlerTest.kt b/lsp/src/test/kotlin/org/gradle/declarative/lsp/tooling/ConnectionHandlerTest.kt deleted file mode 100644 index 6f80911..0000000 --- a/lsp/src/test/kotlin/org/gradle/declarative/lsp/tooling/ConnectionHandlerTest.kt +++ /dev/null @@ -1,19 +0,0 @@ -package org.gradle.declarative.lsp.tooling - -import org.junit.jupiter.api.Assertions.* -import org.junit.jupiter.api.Test -import java.io.File - -class ConnectionHandlerTest { - - @Test - fun testConnect() { - // Get the test-projects/testbed-java-application from the resource directory -// val projectRoot = File("") -// val connectionHandler = ConnectionHandler(projectRoot) -// val declarativeModel = connectionHandler.getDeclarativeModel() -// assertNotNull(declarativeModel) -// declarativeModel.projectSchema - } - -} \ No newline at end of file