package solutions.tretter.githugandroid internal fun fileCompletionCandidates(repo: RepoState): List { 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 { 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, commandBeforeToken: String, token: String, directoriesOnly: Boolean, ): List { 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('/') + "/" }