-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathScalafixCompletions.scala
188 lines (166 loc) · 5.48 KB
/
ScalafixCompletions.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package scalafix.internal.sbt
import scalafix.interfaces.ScalafixRule
import java.io.File
import java.nio.file._
import sbt.complete._
import sbt.complete.DefaultParsers._
class ScalafixCompletions(
workingDirectory: () => Path,
loadedRules: () => Seq[ScalafixRule],
terminalWidth: Option[Int]
) {
private type P = Parser[String]
private type ArgP = Parser[ShellArgs.Arg]
private type KVArgP =
Parser[ArgP] // nested parser allows to match the key only
private val sep: P = token("=" | " ").examples("=")
private val string: P = StringBasic
private def valueAfterKey(
key: String,
keyAliases: String*
)(
valueParser: ArgP
): KVArgP = {
val keyParser = Parser.oneOf((key +: keyAliases).map(literal)).examples(key)
val sepValueParser = (sep ~> valueParser).!!!("missing or invalid value")
keyParser.^^^(sepValueParser)
}
private def uri(protocol: String): Parser[ShellArgs.Rule] =
token(protocol + ":") ~> NotQuoted.map(x => ShellArgs.Rule(s"$protocol:$x"))
private val filepathParser: Parser[Path] = {
def toAbsolutePath(path: Path, cwd: Path): Path = {
if (path.isAbsolute) path
else cwd.resolve(path)
}.normalize()
// Extend FileExamples to tab complete when the prefix is an absolute path or `..`
class AbsolutePathExamples(cwd: Path, prefix: String = "")
extends FileExamples(cwd.toFile, prefix) {
override def withAddedPrefix(addedPrefix: String): FileExamples = {
val nextPrefix =
if (addedPrefix.startsWith(".")) addedPrefix
else prefix + addedPrefix
val (b, p) = AbsolutePathCompleter.mkBase(nextPrefix, cwd)
new AbsolutePathExamples(b, p)
}
}
object AbsolutePathCompleter {
def mkBase(prefix: String, fallback: Path): (Path, String) = {
val path = toAbsolutePath(Paths.get(prefix), fallback)
if (prefix.endsWith(File.separator)) path -> ""
else {
if (path.getFileName != null)
path.getParent -> path.getFileName.toString
else fallback -> ""
}
}
}
string
.examples(new AbsolutePathExamples(workingDirectory()))
.map { f =>
toAbsolutePath(Paths.get(f), workingDirectory())
}
.filter(f => Files.exists(f), x => x)
}
private val namedRule: P = {
token(
string,
TokenCompletions.fixed((seen, _) => {
val candidates = loadedRules().iterator
.filterNot(_.isExperimental)
.filter(_.name.startsWith(seen))
val rules = candidates
.map { candidate =>
val output = s"${candidate.name}\n ${candidate.description}"
new Token(
display = terminalWidth.map(output.take).getOrElse(output).trim,
append = candidate.name.stripPrefix(seen)
)
}
.toSet[Completion]
Completions.strict(rules)
})
).filter(!_.startsWith("-"), x => x)
}
private val namedRule2: Parser[ShellArgs.Rule] =
namedRule.map(s => ShellArgs.Rule(s))
private lazy val gitDiffParser: P = {
val jgitCompletion = new JGitCompletion(workingDirectory())
token(
NotQuoted,
TokenCompletions.fixed((seen, _) => {
val last20Commits =
jgitCompletion.last20Commits
.filter { case (_, sha1) => sha1.startsWith(seen) }
.zipWithIndex
.map {
case ((log, sha1), i) =>
val j = i + 1
val idx = if (j < 10) " " + j.toString else j.toString
new Token(
display = s"|$idx| $log",
append = sha1.stripPrefix(seen)
)
}
.toSet
val branchesAndTags =
jgitCompletion.branchesAndTags
.filter(info => info.startsWith(seen))
.map { info =>
new Token(display = info, append = info.stripPrefix(seen))
}
.toSet
Completions.strict(last20Commits ++ branchesAndTags)
})
)
}
def hide(p: P): P = p.examples()
def parser: Parser[ShellArgs] = {
val pathParser: Parser[Path] = token(filepathParser)
val fileRule: Parser[ShellArgs.Rule] =
(token("file:") ~> pathParser)
.map(path => ShellArgs.Rule(path.toUri.toString))
val keyOnlyArg: ArgP = {
val flags = Seq(
"--auto-suppress-linter-errors",
"--check",
"--diff",
"--help",
"--stdout",
"--syntactic",
"--verbose",
"--version"
).map(f => literal(f).map(ShellArgs.Extra(_))) ++ Seq(
"--no-cache".^^^(ShellArgs.NoCache)
)
Parser.oneOf(flags) |
hide(string) // catch-all for all args not known to sbt-scalafix
.map(ShellArgs.Extra(_))
}
val rule: ArgP =
fileRule |
uri("class") |
uri("replace") |
uri("github") |
uri("dependency") |
namedRule2
val keyValueArg: KVArgP = {
val diffBase: KVArgP = valueAfterKey("--diff-base") {
gitDiffParser.map(v => ShellArgs.Extra("--diff-base", Some(v)))
}
val files: KVArgP = valueAfterKey("--files", "-f") {
pathParser.map(v => ShellArgs.File(v.toString))
}
val rules: KVArgP = valueAfterKey("--rules", "-r")(rule)
diffBase |
files |
rules
}
val shellArg: ArgP =
rule |
keyValueArg.flatMap(identity) |
keyOnlyArg.&(not(keyValueArg, ""))
(token(Space) ~> shellArg).*.map { args =>
ShellArgs(args)
}
}
}