Skip to content

Commit

Permalink
First working schema loading
Browse files Browse the repository at this point in the history
  • Loading branch information
hegyibalint committed Jul 15, 2024
1 parent 0fb4f02 commit db49524
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 43 deletions.
7 changes: 4 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 2 additions & 0 deletions lsp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, GradleConnector>()
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) {
Expand All @@ -34,6 +36,19 @@ class DeclarativeLanguageServer : LanguageServer, LanguageClientAware {
override fun initialize(params: InitializeParams?): CompletableFuture<InitializeResult> {
val serverCapabilities = ServerCapabilities()
serverCapabilities.setTextDocumentSync(TextDocumentSyncKind.Full)
serverCapabilities.setHoverProvider(true)

client.showMessage(MessageParams(
Message
))

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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, AnalysisSequenceResult>()

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?) {
Expand All @@ -32,4 +51,12 @@ class DeclarativeTextDocumentService(): TextDocumentService, LanguageClientAware
System.err.println("Saved document: ${params?.textDocument?.uri}")
}

override fun hover(params: HoverParams?): CompletableFuture<Hover> {
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ 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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.gradle.declarative.lsp.utils

object TextDocumentUtils {



}

This file was deleted.

0 comments on commit db49524

Please sign in to comment.