Skip to content

Commit

Permalink
Add explicit flag enabling mutation codeActions
Browse files Browse the repository at this point in the history
  • Loading branch information
hegyibalint committed Nov 15, 2024
1 parent a9b700b commit ea8b979
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -144,5 +166,4 @@ class DeclarativeLanguageServer : LanguageServer, LanguageClientAware {
private val LOGGER = LoggerFactory.getLogger(DeclarativeLanguageServer::class.java)
private const val MODEL_FETCH_PROGRESS_TOKEN = "modelFetchProgress"
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -227,6 +232,12 @@ class DeclarativeTextDocumentService : TextDocumentService {

override fun codeAction(params: CodeActionParams?): CompletableFuture<MutableList<Either<Command, CodeAction>>> {
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<Either<Command, CodeAction>> = params?.let {
val uri = URI(it.textDocument.uri)
mutationRegistry.getApplicableMutations(uri, params.range).map { mutation ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea8b979

Please sign in to comment.