/
SemanticdbTreePrinter.scala
268 lines (249 loc) Β· 8.95 KB
/
SemanticdbTreePrinter.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package scala.meta.internal.decorations
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.UserConfiguration
import scala.meta.internal.metap.PrinterSymtab
import scala.meta.internal.semanticdb.Print
import scala.meta.internal.semanticdb.SelectTree
import scala.meta.internal.{semanticdb => s}
import scala.meta.metap.Format
class SemanticdbTreePrinter(
isHover: Boolean,
printSymbol: String => String,
createSymtab: => PrinterSymtab,
rightArrow: String,
) {
lazy val symtab = createSymtab
val nonPrintable: Set[String] = Set("apply", "unapply", "unapplySeq")
def printType(t: s.Type): String =
t match {
case s.Type.Empty => ""
case s.RepeatedType(tpe) =>
s"${printType(tpe)}*"
case s.SingleType(prefix, symbol) =>
s"${printPrefix(prefix)}${printSymbol(symbol)}"
case s.TypeRef(prefix, symbol, typeArguments) =>
val tuple = isTuple(symbol)
val isFunction = symbol.startsWith("scala/Function")
val sym =
if (tuple || isFunction) ""
// don't print unnamed types
else if (symbol.startsWith("local")) "_"
else printSymbol(symbol)
val typeArgs = printTypeArgs(typeArguments, tuple, isFunction)
s"${printPrefix(prefix)}${sym}${typeArgs}"
case s.WithType(types) =>
val simpleTypes = types.dropWhile {
case s.TypeRef(_, sym, _) =>
sym == "scala/AnyRef#" || sym == "java/lang/Object#"
case _ => false
}
simpleTypes.map(printType).mkString(" with ")
case s.ConstantType(constant) =>
printConstant(constant)
case s.ByNameType(tpe) =>
s"=> ${printType(tpe)}"
case s.ThisType(symbol) =>
s"this.${printSymbol(symbol)}"
case s.IntersectionType(types) =>
types.map(printType).mkString(" & ")
case s.UnionType(types) =>
types.map(printType).mkString(" | ")
case s.SuperType(prefix, symbol) =>
s"super.${printPrefix(prefix)}${printSymbol(symbol)}"
case s.AnnotatedType(annots, tp) =>
val mapped = annots
.map(x => s"@${printType(x.tpe)}")
.reduceLeft((x, y) => s"$x $y")
s"$mapped ${printType(tp)}"
// this should not need to be printed but just in case we revert to semanticdb printer
case s.UniversalType(_, tpe) =>
if (isHover)
Print.tpe(Format.Detailed, t, symtab)
/* We don't want to print the full [x] => F[x]
because that cannot show up in the actual code
*/
else {
tpe match {
case t: s.TypeRef =>
printSymbol(t.symbol)
case tpe =>
printType(tpe)
}
}
case s.ExistentialType(tpe, _) =>
if (isHover)
Print.tpe(Format.Detailed, t, symtab)
else
s"${printType(tpe)}"
case s.StructuralType(tpe, scope) =>
if (isHover) {
Print.tpe(Format.Detailed, t, symtab)
} else {
s"${printType(tpe)} {${printScope(scope)}}"
}
case s.MatchType(scrutinee, cases) =>
s"${printType(scrutinee)} match { ${cases.size} cases }"
}
def printScope(scope: Option[s.Scope]): String = {
if (scope.exists(_.hardlinks.nonEmpty)) "..." else ""
}
def printPrefix(t: s.Type): String = {
printType(t) match {
case "" => ""
case s => s"$s."
}
}
def printTypeArgs(
typeArgs: Seq[s.Type],
isTuple: Boolean = false,
isFunction: Boolean = false,
): String =
typeArgs match {
case Nil => ""
case _ if isTuple =>
typeArgs.map(printType).mkString("(", ", ", ")")
case _ if isFunction =>
val argTypes :+ returnType = typeArgs.map(printType)
argTypes.mkString("(", ", ", ")") + s" $rightArrow $returnType"
case _ =>
typeArgs.map(printType).mkString("[", ", ", "]")
}
def printArgs(args: Seq[s.Tree]): String =
args match {
case Nil => ""
case _ => args.flatMap(printTree(_)).mkString("(", ", ", ")")
}
def printConstant(c: s.Constant): String =
c match {
case s.FloatConstant(value) => value.toString
case s.LongConstant(value) => value.toString
case s.DoubleConstant(value) => value.toString
case s.NullConstant() => "null"
case s.IntConstant(value) => value.toString
case s.CharConstant(value) => value.toString
case s.ByteConstant(value) => value.toString
case s.UnitConstant() => "()"
case s.ShortConstant(value) => value.toString
case s.Constant.Empty => ""
case s.BooleanConstant(value) => value.toString
case s.StringConstant(value) => value
}
def printTree(
t: s.Tree,
isExplicitTuple: => Boolean = false,
): Option[String] =
t match {
case s.Tree.Empty => None
case s.OriginalTree(_) => None
case s.TypeApplyTree(function, typeArguments)
// only print type parameters for tuple if it's not a tuple literal
if isNotTupleTree(function) || isExplicitTuple =>
Some(
printTree(function)
.filterNot(nonPrintable)
.getOrElse("") + printTypeArgs(typeArguments)
)
case s.ApplyTree(function, arguments) =>
Some(printTree(function).getOrElse("") + printArgs(arguments))
case s.LiteralTree(constant) =>
Some(printConstant(constant))
case s.SelectTree(_, id) =>
id.flatMap(printTree(_))
case s.FunctionTree(parameters, body) =>
printTree(body).map(printed => printArgs(parameters) + "=>" + printed)
case s.IdTree(symbol) =>
Some(printSymbol(symbol))
case s.MacroExpansionTree(beforeExpansion, _) =>
printTree(beforeExpansion)
case _ => None
}
def printSyntheticInfo(
textDocument: s.TextDocument,
synthetic: s.Synthetic,
userConfig: UserConfiguration,
isInlineProvider: Boolean = false,
): Seq[(String, s.Range)] = {
def isExplicitTuple(range: s.Range) =
range.inString(textDocument.text).exists(_.startsWith("Tuple"))
def gatherSynthetics(tree: s.Tree, treeRange: Option[s.Range] = None) = {
for {
range <- treeRange.orElse(synthetic.range).toList
syntheticString <- printTree(tree, isExplicitTuple(range)).toList
} yield (syntheticString, range)
}
/* We don't want type trees for anything than toplevel,
* since that might actually be contained in for comprehension.
* `ignoreTypesTrees` is only set to false for toplevel synthetics.
*/
def tryTree(
tree: s.Tree,
ignoreTypesTrees: Boolean = true,
): Seq[(String, s.Range)] =
tree match {
/**
* implicit val str = ""
* def hello()(implicit a : String)
* hello()<<(str)>>
*/
case tree @ s.ApplyTree(org: s.OriginalTree, _)
if userConfig.showImplicitArguments =>
gatherSynthetics(tree, org.range)
/**
* def hello[T](T object) = object
* hello<<[String]>>("")
*/
case tree @ s.TypeApplyTree(_: s.OriginalTree | _: s.SelectTree, _)
if !ignoreTypesTrees && userConfig.showInferredType =>
gatherSynthetics(tree)
/**
* implicit def implicitFun(object: T): R = ???
* def fun(r: R) = ???
* fun(<<implicitFun(>>new T<<)>>)
*/
case s.ApplyTree(id: s.IdTree, _)
if userConfig.showImplicitConversionsAndClasses =>
def synthetics(syntheticString: String, range: s.Range) = {
if (isHover && isInlineProvider)
List(
(
syntheticString,
range
.withEndCharacter(range.startCharacter)
.withEndLine(range.startLine),
)
)
else
List(
(
syntheticString + "(",
range
.withEndCharacter(range.startCharacter)
.withEndLine(range.startLine),
),
(")", range),
)
}
for {
syntheticString <- printTree(id).toList
range <- synthetic.range.toList
synth <- synthetics(syntheticString, range)
} yield synth
// needed in case of synthetics inside of more complex synthetic trees such as for comprehensions
case appl: s.ApplyTree =>
(appl.arguments :+ appl.function).flatMap(tryTree(_))
case tree: s.TypeApplyTree =>
tryTree(tree.function)
case sel: s.SelectTree =>
tryTree(sel.qualifier)
case s.FunctionTree(_, body) =>
tryTree(body)
case _ => Nil
}
tryTree(synthetic.tree, ignoreTypesTrees = false)
}
private def isTuple(symbol: String) = symbol.startsWith("scala/Tuple")
private def isNotTupleTree(function: s.Tree) = function match {
case SelectTree(_, Some(id)) => !isTuple(id.symbol)
case _ => true
}
}