diff --git a/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeFeatures.kt b/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeFeatures.kt new file mode 100644 index 0000000..9a2140f --- /dev/null +++ b/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeFeatures.kt @@ -0,0 +1,24 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.gradle.declarative.lsp + +/** + * Holds feature flags coming from the clients + */ +data class DeclarativeFeatures( + val mutations: Boolean = false +) \ No newline at end of file diff --git a/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeLanguageServer.kt b/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeLanguageServer.kt index de4acc4..3bc6dc9 100644 --- a/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeLanguageServer.kt +++ b/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeLanguageServer.kt @@ -16,6 +16,7 @@ package org.gradle.declarative.lsp +import com.google.gson.JsonObject import org.eclipse.lsp4j.CompletionOptions import org.eclipse.lsp4j.InitializeParams import org.eclipse.lsp4j.InitializeResult @@ -65,6 +66,16 @@ class DeclarativeLanguageServer : LanguageServer, LanguageClientAware { "Initialization parameters must not be null" } + val declarativeFeatures = params.initializationOptions?.let { + if (it is JsonObject) it else null + }?.let { + it.get("declarativeFeatures")?.asJsonObject + }?.let { + DeclarativeFeatures( + mutations = it.get("mutations")?.asBoolean ?: false + ) + } ?: DeclarativeFeatures() + val serverCapabilities = ServerCapabilities() // Here we set the capabilities we support serverCapabilities.setTextDocumentSync(TextDocumentSyncKind.Full) @@ -105,8 +116,19 @@ class DeclarativeLanguageServer : LanguageServer, LanguageClientAware { ) // Initialize the LSP services - textDocumentService.initialize(client, documentStore, mutationRegistry, declarativeResources) - workspaceService.initialize(client, documentStore, mutationRegistry, declarativeResources) + textDocumentService.initialize( + client, + documentStore, + mutationRegistry, + declarativeFeatures, + declarativeResources + ) + workspaceService.initialize( + client, + documentStore, + mutationRegistry, + declarativeResources + ) } initialized = true @@ -144,5 +166,4 @@ class DeclarativeLanguageServer : LanguageServer, LanguageClientAware { private val LOGGER = LoggerFactory.getLogger(DeclarativeLanguageServer::class.java) private const val MODEL_FETCH_PROGRESS_TOKEN = "modelFetchProgress" } - -} +} \ No newline at end of file diff --git a/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeTextDocumentService.kt b/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeTextDocumentService.kt index fafdf2d..6f100a8 100644 --- a/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeTextDocumentService.kt +++ b/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeTextDocumentService.kt @@ -16,6 +16,8 @@ package org.gradle.declarative.lsp +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.ClientInfo import org.eclipse.lsp4j.CodeAction import org.eclipse.lsp4j.CodeActionParams import org.eclipse.lsp4j.Command @@ -74,6 +76,7 @@ class DeclarativeTextDocumentService : TextDocumentService { private lateinit var client: LanguageClient private lateinit var documentStore: VersionedDocumentStore private lateinit var mutationRegistry: MutationRegistry + private lateinit var declarativeFeatures: DeclarativeFeatures private lateinit var declarativeResources: DeclarativeResourcesModel private lateinit var analysisSchema: AnalysisSchema private lateinit var schemaAnalysisEvaluator: SimpleAnalysisEvaluator @@ -82,9 +85,11 @@ class DeclarativeTextDocumentService : TextDocumentService { client: LanguageClient, documentStore: VersionedDocumentStore, mutationRegistry: MutationRegistry, + declarativeFeatures: DeclarativeFeatures, declarativeResources: DeclarativeResourcesModel ) { this.client = client + this.declarativeFeatures = declarativeFeatures this.documentStore = documentStore this.mutationRegistry = mutationRegistry this.declarativeResources = declarativeResources @@ -227,6 +232,12 @@ class DeclarativeTextDocumentService : TextDocumentService { override fun codeAction(params: CodeActionParams?): CompletableFuture>> { LOGGER.trace("Code action requested: {}", params) + + // If the clients do not support mutations, we don't need to provide any code actions + if (!declarativeFeatures.mutations) { + return CompletableFuture.completedFuture(mutableListOf()) + } + val mutations: List> = params?.let { val uri = URI(it.textDocument.uri) mutationRegistry.getApplicableMutations(uri, params.range).map { mutation -> diff --git a/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeWorkspaceService.kt b/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeWorkspaceService.kt index cbc3112..f992e42 100644 --- a/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeWorkspaceService.kt +++ b/lsp/src/main/kotlin/org/gradle/declarative/lsp/DeclarativeWorkspaceService.kt @@ -19,6 +19,8 @@ package org.gradle.declarative.lsp import com.google.gson.JsonArray import com.google.gson.JsonObject import org.eclipse.lsp4j.ApplyWorkspaceEditParams +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.ClientInfo import org.eclipse.lsp4j.DidChangeConfigurationParams import org.eclipse.lsp4j.DidChangeWatchedFilesParams import org.eclipse.lsp4j.ExecuteCommandParams