Skip to content

Commit

Permalink
Update file content periodically
Browse files Browse the repository at this point in the history
This helps to experiment with an external editor.
Otherwise, you would have to select a different file
and select the previous one again to update the views.
  • Loading branch information
h0tk3y committed Jul 11, 2024
1 parent 25b9883 commit 7480adb
Showing 1 changed file with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.times
import kotlinx.coroutines.delay
import org.gradle.client.build.action.GetResolvedDomAction
import org.gradle.client.build.model.ResolvedDomPrerequisites
import org.gradle.client.core.gradle.dcl.*
Expand All @@ -46,6 +47,7 @@ import org.gradle.internal.declarativedsl.evaluator.runner.stepResultOrPartialRe
import org.gradle.tooling.BuildAction
import org.jetbrains.skiko.Cursor
import java.io.File
import kotlin.time.Duration.Companion.milliseconds

private const val NOT_DECLARED = "Not declared"
private const val NOTHING_DECLARED = "Nothing declared"
Expand All @@ -63,24 +65,59 @@ class GetDeclarativeDocuments : GetModelAction.GetCompositeModelAction<ResolvedD
@Suppress("LongMethod")
override fun ColumnScope.ModelContent(model: ResolvedDomPrerequisites) {
val selectedBuildFile = remember { mutableStateOf<File>(model.declarativeBuildFiles.first()) }
val fileUpdatesCount = remember { mutableStateOf<Long>(0) }

val buildFileContent =
remember(selectedBuildFile.value, fileUpdatesCount.value) { selectedBuildFile.value.readText() }
val settingsFileContent = remember(selectedBuildFile.value, fileUpdatesCount, model.settingsFile) {
model.settingsFile.takeIf { it.exists() }?.readText() ?: ""
fun readBuildFile() = selectedBuildFile.value.takeIf { it.canRead() }?.readText().orEmpty()
fun readSettingsFile() = model.settingsFile.takeIf { it.canRead() }?.readText().orEmpty()

val buildFileContent = remember(selectedBuildFile.value) {
mutableStateOf(readBuildFile())
}
val settingsFileContent = remember(selectedBuildFile.value, model.settingsFile) {
mutableStateOf(readSettingsFile())
}

val highlightedSourceRangeByFileId = remember {
mutableStateOf(mapOf<String, IntRange>())
}

fun updateFileContents() {
var changed = false
val newBuildFileContent = readBuildFile()
if (newBuildFileContent != buildFileContent.value) {
changed = true
buildFileContent.value = newBuildFileContent
}
val newSettingsFileContent = readSettingsFile()
if (newSettingsFileContent != settingsFileContent.value) {
changed = true
settingsFileContent.value = newSettingsFileContent
}
if (changed) {
highlightedSourceRangeByFileId.value = emptyMap()
}
}

LaunchedEffect(Unit) {
while (true) {
delay(300.milliseconds)
updateFileContents()
}
}


val analyzer = analyzer(model)
val projectResult = remember(selectedBuildFile.value, buildFileContent) {
analyzer.evaluate(selectedBuildFile.value.name, buildFileContent)
val projectResult by remember {
derivedStateOf {
analyzer.evaluate(selectedBuildFile.value.name, buildFileContent.value)
}
}
val settingsResult = remember(model.settingsFile, settingsFileContent) {
analyzer.evaluate(model.settingsFile.name, settingsFileContent)
val settingsResult by remember {
derivedStateOf {
analyzer.evaluate(model.settingsFile.name, settingsFileContent.value)
}
}
val domWithConventions = AnalysisDocumentUtils.documentWithConventions(settingsResult, projectResult)

val highlightedSourceRangeByFileId = mutableStateOf(mapOf<String, IntRange>())

val hasAnyConventionContent =
domWithConventions?.overlayNodeOriginContainer?.collectToMap(domWithConventions.document)
Expand Down Expand Up @@ -132,7 +169,7 @@ class GetDeclarativeDocuments : GetModelAction.GetCompositeModelAction<ResolvedD
mutationArgumentsContainer
)
// Trigger recomposition:
fileUpdatesCount.value += 1
updateFileContents()
}
)
) {
Expand Down

0 comments on commit 7480adb

Please sign in to comment.