Skip to content
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

Add explicit flag enabling mutation codeActions #10

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
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