Skip to content

Commit

Permalink
Do not use the real document overlays for now
Browse files Browse the repository at this point in the history
The text mutation utilities cannot handle the mutations
touching the underlay document content.

For now, use an empty "underlay" document, so that the
overlay result of that stub and the real file is equivalent
to the real file, which the mutation utilities can handle
just fine.
  • Loading branch information
h0tk3y committed Nov 13, 2024
1 parent ba07ca2 commit 9487515
Showing 1 changed file with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ import org.gradle.internal.declarativedsl.analysis.SchemaTypeRefContext
import org.gradle.internal.declarativedsl.dom.DeclarativeDocument
import org.gradle.internal.declarativedsl.dom.DocumentResolution
import org.gradle.internal.declarativedsl.dom.mutation.MutationParameterKind
import org.gradle.internal.declarativedsl.dom.operations.overlay.DocumentOverlay
import org.gradle.internal.declarativedsl.dom.operations.overlay.DocumentOverlayResult
import org.gradle.internal.declarativedsl.dom.resolution.DocumentResolutionContainer
import org.gradle.internal.declarativedsl.evaluator.main.AnalysisDocumentUtils
import org.gradle.internal.declarativedsl.evaluator.main.AnalysisDocumentUtils.resolvedDocument
import org.gradle.internal.declarativedsl.evaluator.main.AnalysisSequenceResult
import org.gradle.internal.declarativedsl.evaluator.main.SimpleAnalysisEvaluator
import org.gradle.internal.declarativedsl.evaluator.runner.stepResultOrPartialResult
import org.slf4j.LoggerFactory
import java.net.URI
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -297,20 +300,23 @@ class DeclarativeTextDocumentService : TextDocumentService {
}

private fun parse(uri: URI, text: String): DocumentOverlayResult {
fun AnalysisSequenceResult.lastStepDocument() =
stepResults.values.last().stepResultOrPartialResult.resolvedDocument()

val fileName = uri.path.substringAfterLast('/')
val fileSchema = schemaAnalysisEvaluator.evaluate(fileName, text)
val settingsSchema = schemaAnalysisEvaluator.evaluate(
declarativeResources.settingsFile.name,
declarativeResources.settingsFile.takeIf { it.canRead() }?.readText().orEmpty()
)

val document = AnalysisDocumentUtils.documentWithModelDefaults(settingsSchema, fileSchema)
when (document != null) {
true -> LOGGER.trace("Parsed declarative model for document: {}", uri)
false -> LOGGER.error("Failed to parse declarative model for document: {}", uri)
}

return document!!
val document = schemaAnalysisEvaluator.evaluate(fileName, text).lastStepDocument()

// Workaround: for now, the mutation utilities cannot handle mutations that touch the underlay document content.
// To avoid that, use an empty document as an underlay instead of the real document produced from the
// settings file.
// TODO: carry both the real overlay and the document produced from just the current file, run the mutations
// against the latter for now.
// TODO: once the mutation utilities start handling mutations across the overlay, pass them the right overlay.
val emptyUnderlay = schemaAnalysisEvaluator.evaluate("empty-underlay/build.gradle.dcl", "").lastStepDocument()

LOGGER.trace("Parsed declarative model for document: {}", uri)

return DocumentOverlay.overlayResolvedDocuments(emptyUnderlay, document)
}

private fun <T> withDom(uri: URI, work: (DocumentOverlayResult, String) -> T): T? {
Expand Down

0 comments on commit 9487515

Please sign in to comment.