Scoop API

scoop:scoop.readline:0.1.0

src/completion/filename.scoop

Selected production source · src/completion/filename.scoop

1package scoop.readline
3import scoop.platform.fs.*
4import scoop.platform.env.homeDirectory
5import scoop.platform.system.*
6import scoop.collections.sortedBy
7import scoop.text.*
9/** Completes native paths, preserving platform separators, quoting and ~/ prefixes. */
10public 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 = backslash
24 }
25 val prefix =
26 if (slash < 0L)
27 ""
28 else
29 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 else
35 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 continue
48 val separator =
49 if (operatingSystem == OperatingSystem.Windows)
50 "\\"
51 else
52 "/"
53 val suffix =
54 if (entry.path.metadata().kind == FileKind.Directory)
55 separator
56 else
57 ""
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.NotDirectory
68 )
69 throw error
70 }
71 return Completions(token.start, candidates.sortedBy { it.display }.toArray())
72 }
75private struct FilenameToken(val start: Long, val text: String, val quote: Char?)
77private fun filenameToken(line: String, cursor: Long): FilenameToken {
78 val windows = operatingSystem == OperatingSystem.Windows
79 var start = 0L
80 var quote: Char? = None
81 var escape = false
82 var index = 0L
83 val result = StringBuilder()
84 while (index < cursor) {
85 val character = line.getCharAt(index)
86 if (escape) {
87 result.append(character);
88 escape = false
89 }
90 else
91 if (!windows && character == '\\' && quote != Some('\''))
92 escape = true
93 else
94 if (quote != None) {
95 if (character == quote!!) {
96 if (
97 windows &&
98 index +
99 1L <
100 cursor &&
101 line.getCharAt(index + 1L) ==
102 character
103 ) {
104 result.append(character)
105 index += 1L
106 } else
107 quote = None
108 } else
109 result.append(character)
110 } else
111 if (character == '\'' || character == '"') {
112 quote = Some(character)
113 start = index + 1L
114 result.clear()
115 } else
116 if (
117 character.toString().isBlank() || ";|&()<>=\u0060".contains(character)
118 ) {
119 start = index + 1L
120 result.clear()
121 } else
122 result.append(character)
123 index += 1L
124 }
125 if (escape)
126 result.append('\\')
127 return FilenameToken(start, result.toString(), quote)
130private 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 text
144 }
145 }
146 }
147 val result = StringBuilder()
148 for (character in text) {
149 if (quote == Some('\'')) {
150 if (character == '\'')
151 result.append("'\\''")
152 else
153 result.append(character)
154 } else {
155 val escape =
156 if (quote == Some('"'))
157 "\\\"$\u0060".contains(character)
158 else
159 character.toString().isBlank() || "\\\"';|&()<>=\u0060".contains(character)
160 if (escape)
161 result.append('\\')
162 result.append(character)
163 }
164 }
165 return result.toString()