scoop:scoop.readline:0.1.0
src/completion/filename.scoop
Selected production source · src/completion/filename.scoop
1
package scoop.readline3
import scoop.platform.fs.*4
import scoop.platform.env.homeDirectory5
import scoop.platform.system.*6
import scoop.collections.sortedBy7
import scoop.text.*9
/** Completes native paths, preserving platform separators, quoting and ~/ prefixes. */10
public class FilenameCompleter public constructor(): Completer {11
public override fun complete(line: String, cursor: Long, context: Context): Completions =12
completePath(line, cursor)14
/** Completes only the token ending at cursor; filesystem errors other than absence propagate. */15
public fun completePath(line: String, cursor: Long): Completions {16
if (cursor < 0L || cursor > line.charLength)17
throw IndexOutOfBoundsException()18
val token = filenameToken(line, cursor)19
var slash = token.text.lastIndexOf('/')20
if (operatingSystem == OperatingSystem.Windows) {21
val backslash = token.text.lastIndexOf('\\')22
if (backslash > slash)23
slash = backslash24
}25
val prefix =26
if (slash < 0L)27
""28
else29
token.text.slice(0L, slash + 1L)30
val partial = token.text.slice(slash + 1L, token.text.charLength)31
var directory =32
if (prefix.byteLength == 0L)33
Path.fromString(".")34
else35
Path.fromString(prefix)36
if (prefix.startsWith("~/")) {37
val home = homeDirectory()38
if (home != None)39
directory = home!!.resolve(Path.fromString(prefix.slice(2L, prefix.charLength)))40
}41
val candidates = ArrayList<Candidate>()42
try {43
directory.entries().use {44
entries -> for (entry in entries) {45
val name = entry.path.fileName!!.decodeUtf8()46
if (!name.startsWith(partial))47
continue48
val separator =49
if (operatingSystem == OperatingSystem.Windows)50
"\\"51
else52
"/"53
val suffix =54
if (entry.path.metadata().kind == FileKind.Directory)55
separator56
else57
""58
val replacement = escapeFilename(prefix + name + suffix, token.quote)59
candidates.add(Candidate(name + suffix, replacement))60
}61
}62
} catch (error: PlatformException) {63
if (64
error.kind !=65
PlatformErrorKind.NotFound &&66
error.kind !=67
PlatformErrorKind.NotDirectory68
)69
throw error70
}71
return Completions(token.start, candidates.sortedBy { it.display }.toArray())72
}73
}75
private struct FilenameToken(val start: Long, val text: String, val quote: Char?)77
private fun filenameToken(line: String, cursor: Long): FilenameToken {78
val windows = operatingSystem == OperatingSystem.Windows79
var start = 0L80
var quote: Char? = None81
var escape = false82
var index = 0L83
val result = StringBuilder()84
while (index < cursor) {85
val character = line.getCharAt(index)86
if (escape) {87
result.append(character);88
escape = false89
}90
else91
if (!windows && character == '\\' && quote != Some('\''))92
escape = true93
else94
if (quote != None) {95
if (character == quote!!) {96
if (97
windows &&98
index +99
1L <100
cursor &&101
line.getCharAt(index + 1L) ==102
character103
) {104
result.append(character)105
index += 1L106
} else107
quote = None108
} else109
result.append(character)110
} else111
if (character == '\'' || character == '"') {112
quote = Some(character)113
start = index + 1L114
result.clear()115
} else116
if (117
character.toString().isBlank() || ";|&()<>=\u0060".contains(character)118
) {119
start = index + 1L120
result.clear()121
} else122
result.append(character)123
index += 1L124
}125
if (escape)126
result.append('\\')127
return FilenameToken(start, result.toString(), quote)128
}130
private fun escapeFilename(text: String, quote: Char?): String {131
if (operatingSystem == OperatingSystem.Windows) {132
when (quote) {133
case Some(val character) -> {134
val mark = character.toString()135
return text.replace(mark, mark + mark)136
}137
case None -> {138
for (character in text) {139
if (character.toString().isBlank() || "\"';|&()<>=\u0060".contains(character)) {140
return "\"" + text + "\""141
}142
}143
return text144
}145
}146
}147
val result = StringBuilder()148
for (character in text) {149
if (quote == Some('\'')) {150
if (character == '\'')151
result.append("'\\''")152
else153
result.append(character)154
} else {155
val escape =156
if (quote == Some('"'))157
"\\\"$\u0060".contains(character)158
else159
character.toString().isBlank() || "\\\"';|&()<>=\u0060".contains(character)160
if (escape)161
result.append('\\')162
result.append(character)163
}164
}165
return result.toString()166
}