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 1a2128b commit d1b5257
Showing 1 changed file with 24 additions and 24 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 @@ -104,11 +107,9 @@ class DeclarativeTextDocumentService : TextDocumentService {
val uri = URI(it.textDocument.uri)
val text = it.textDocument.text
val dom = parse(uri, text)
if (dom != null) {
run {
documentStore.storeInitial(uri, text, dom)
processDocument(uri)
} else {
documentStore.remove(uri)
}
}
}
Expand All @@ -121,12 +122,8 @@ class DeclarativeTextDocumentService : TextDocumentService {
val version = it.textDocument.version
val text = change.text
val dom = parse(uri, change.text)
if (dom != null) {
documentStore.storeVersioned(uri, version, text, dom)
processDocument(uri)
} else {
documentStore.remove(uri)
}
documentStore.storeVersioned(uri, version, text, dom)
processDocument(uri)
}
}
}
Expand Down Expand Up @@ -304,21 +301,24 @@ class DeclarativeTextDocumentService : TextDocumentService {
)
}

private fun parse(uri: URI, text: String): DocumentOverlayResult? {
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 d1b5257

Please sign in to comment.