Files
Githug-Android/app/src/main/java/solutions/tretter/githugandroid/CompletionCandidates.kt
2026-06-26 19:00:51 -05:00

51 lines
1.7 KiB
Kotlin

package solutions.tretter.githugandroid
internal fun fileCompletionCandidates(repo: RepoState): List<String> {
val prefix = repo.currentDirPrefix()
return repo.files
.filterNot { it.deleted }
.map { it.name }
.filter { it.startsWith(prefix) }
.map { it.removePrefix(prefix) }
.filter { it.isNotBlank() }
}
internal fun directoryCompletionCandidates(repo: RepoState): List<String> {
val prefix = repo.currentDirPrefix()
return repo.files
.filterNot { it.deleted }
.map { it.name }
.filter { it.startsWith(prefix) }
.map { it.removePrefix(prefix) }
.flatMap { file ->
val parts = file.split('/').dropLast(1)
parts.indices.map { index -> parts.take(index + 1).joinToString("/") + "/" }
}
.distinct()
}
internal fun contextualCompletionCandidates(
candidates: List<String>,
commandBeforeToken: String,
token: String,
directoriesOnly: Boolean,
): List<String> {
val matches = candidates.sorted().filter { it.startsWith(token) }
if (directoriesOnly) return matches
val commandTokens = GitSandboxEngine.tokenizeCommand(commandBeforeToken.trim())
if (commandTokens == listOf("git", "bisect", "run")) {
val scriptMatches = matches.filter { candidate ->
val normalized = candidate.removePrefix("./")
normalized.endsWith(".sh") && '/' !in normalized && !normalized.startsWith(".")
}
if (scriptMatches.isNotEmpty()) return scriptMatches
}
return matches
}
private fun RepoState.currentDirPrefix(): String {
return if (currentDir == ".") "" else currentDir.trimEnd('/') + "/"
}