-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathPresentationCompilation.scala
More file actions
272 lines (247 loc) · 11.2 KB
/
PresentationCompilation.scala
File metadata and controls
272 lines (247 loc) · 11.2 KB
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
269
270
271
272
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc. dba Akka
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.tools.nsc.interpreter
import scala.collection.mutable
import scala.reflect.internal.util.{Position, RangePosition}
import scala.tools.nsc.ast.parser.Tokens
import scala.tools.nsc.backend.JavaPlatform
import scala.tools.nsc.util.ClassPath
import scala.tools.nsc.{Settings, interactive}
import scala.tools.nsc.reporters.StoreReporter
import scala.tools.nsc.classpath._
import scala.tools.nsc.interpreter.Results.{Error, Result}
trait PresentationCompilation { self: IMain =>
private final val Cursor = IMain.DummyCursorFragment
/** Typecheck a line of REPL input, suitably wrapped with "interpreter wrapper" objects/classes, with the
* presentation compiler. The result of this method gives access to the typechecked tree and to autocompletion
* suggestions.
*
* The caller is responsible for calling [[PresentationCompileResult#cleanup]] to dispose of the compiler instance.
*/
def presentationCompile(cursor: Int, buf: String): Either[Result, PresentationCompilationResult] = {
if (global == null) Left(Error)
else {
val pc = newPresentationCompiler()
def cursorIsInKeyword(): Boolean = {
val scanner = pc.newUnitParser(buf).newScanner()
scanner.init()
while (scanner.token != Tokens.EOF) {
val token = scanner.token
val o = scanner.offset
scanner.nextToken()
if ((o to scanner.lastOffset).contains(cursor)) {
return (!Tokens.isIdentifier(token) && pc.syntaxAnalyzer.token2name.contains(token))
}
}
false
}
// Support completion of "def format = 42; for<TAB>" by replacing the keyword with foo_CURSOR_ before
// typechecking. Only do this when needed to be able ot correctly return the type of `foo.bar<CURSOR>`
// where `bar` is the complete name of a member.
val line1 = if (!cursorIsInKeyword()) buf else buf.patch(cursor, Cursor, 0)
val parserSource = pc.newSourceFile(line1)
val trees = pc.newUnitParser(new pc.CompilationUnit(parserSource)).parseStats() match {
case Nil => List(pc.EmptyTree)
case xs => xs
}
val importer = global.mkImporter(pc)
//println(s"pc: [[$line1]], <<${trees.size}>>")
val request = new Request(line1, trees map (t => importer.importTree(t)), parserSource, generousImports = true, storeResultInVal = false)
val origUnit = request.mkUnit
val unit = new pc.CompilationUnit(origUnit.source)
unit.body = pc.mkImporter(global).importTree(origUnit.body)
val richUnit = new pc.RichCompilationUnit(unit.source)
// disable brace patching in the parser, the snippet template isn't well-indented and the results can be surprising
pc.currentRun.parsing.withIncompleteHandler((pos, msg) => ()) {
pc.unitOfFile(richUnit.source.file) = richUnit
richUnit.body = unit.body
pc.enteringTyper(pc.typeCheck(richUnit))
}
val inputRange = pc.wrappingPos(trees)
// too bad dependent method types don't work for constructors
val result = new PresentationCompileResult(pc, inputRange, cursor, buf) { val unit = richUnit ; override val compiler: pc.type = pc }
Right(result)
}
}
/** Create an instance of the presentation compiler with a classpath comprising the REPL's configured classpath
* and the classes output by previously compiled REPL lines.
*
* You may directly interact with this compiler from any thread, although you must not access it concurrently
* from multiple threads.
*
* You may downcast the `reporter` to `StoreReporter` to access type errors.
*/
def newPresentationCompiler(): interactive.Global = {
def copySettings: Settings = {
val s = new Settings(_ => () /* ignores "bad option -nc" errors, etc */)
s.processArguments(global.settings.recreateArgs, processAll = false)
s.YpresentationAnyThread.value = true
s
}
val storeReporter: StoreReporter = new StoreReporter(copySettings)
val interactiveGlobal = new interactive.Global(copySettings, storeReporter) { self =>
def mergedFlatClasspath = {
val replOutClasspath = ClassPathFactory.newClassPath(replOutput.dir, settings, closeableRegistry)
AggregateClassPath(replOutClasspath :: global.platform.classPath :: Nil)
}
override lazy val platform: ThisPlatform = {
new JavaPlatform {
lazy val global: self.type = self
override lazy val classPath: ClassPath = mergedFlatClasspath
}
}
}
new interactiveGlobal.TyperRun()
interactiveGlobal
}
abstract class PresentationCompileResult(val compiler: interactive.Global, val inputRange: Position, val cursor: Int, val buf: String) extends PresentationCompilationResult {
val unit: compiler.RichCompilationUnit // depmet broken for constructors, can't be ctor arg
override def cleanup(): Unit = {
compiler.askShutdown()
}
import compiler.CompletionResult
def completionsAt(cursor: Int): CompletionResult = compiler.completionsAt(positionOf(cursor))
def positionOf(cursor: Int): Position =
unit.source.position(cursor)
def typedTreeAt(selectionStart: Int, selectionEnd: Int): compiler.Tree =
compiler.typedTreeAt(new RangePosition(unit.source, selectionStart, selectionStart, selectionEnd))
// offsets are 0-based
def treeAt(start: Int, end: Int): compiler.Tree = treeAt(unit.source.position(start).withEnd(end))
def treeAt(pos: Position): compiler.Tree = {
import compiler.{Locator, Template, Block}
new Locator(pos) locateIn unit.body match {
case t@Template(_, _, constructor :: (rest :+ last)) =>
if (rest.isEmpty) last
else Block(rest, last)
case t =>
t
}
}
def typeString(tree: compiler.Tree): String = {
tree.tpe match {
case null | compiler.NoType | compiler.ErrorType => ""
case tp if compiler.nme.isReplWrapperName(tp.typeSymbol.name) => ""
case tp => compiler.exitingTyper(tp.toString)
}
}
def treeString(tree: compiler.Tree): String =
compiler.showCode(tree)
override def print = {
val tree = treeAt(inputRange)
import compiler._
object makeCodePrinterPrintInferredTypes extends Transformer {
private def printableTypeTree(tp: Type): TypeTree = {
val tree = TypeTree(tp)
tree.wasEmpty = false
tree
}
override def transform(tree: Tree): Tree = super.transform(tree) match {
case ValDef(mods, name, tt @ build.SyntacticEmptyTypeTree(), rhs) =>
if (tree.symbol != null && tree.symbol != NoSymbol && nme.isReplWrapperName(tree.symbol.owner.name)) {
treeCopy.ValDef(tree, mods &~ (Flag.PRIVATE | Flag.LOCAL), name.dropLocal, printableTypeTree(tt.tpe), rhs)
} else {
treeCopy.ValDef(tree, mods, name, printableTypeTree(tt.tpe), rhs)
}
case DefDef(mods, name, tparams, vparamss, tt @ build.SyntacticEmptyTypeTree(), rhs) =>
treeCopy.DefDef(tree, mods, name, tparams, vparamss, printableTypeTree(tt.tpe), rhs)
case t => t
}
}
val tree1 = makeCodePrinterPrintInferredTypes.transform(tree)
val tpString = typeString(tree1) match {
case "" => ""
case s => " // : " + s
}
treeString(tree1) + tpString
}
override def typeAt(start: Int, end: Int) =
typeString(typedTreeAt(start, end))
val NoCandidates = (-1, Nil)
type Candidates = (Int, List[CompletionCandidate])
override def completionCandidates(filter: Boolean, tabCount: Int): Candidates = {
import compiler._
import CompletionResult.NoResults
def isMemberDeprecated(m: Member) = m match {
case tm: TypeMember if tm.viaView.isDeprecated || tm.viaView != NoSymbol && tm.viaView.owner.isDeprecated =>
true
case _ =>
m.sym.isDeprecated ||
m.sym.hasGetter && m.sym.getterIn(m.sym.owner).isDeprecated
}
def isMemberUniversal(m: Member) =
definitions.isUniversalMember(m.sym) ||
(m match {
case t: TypeMember =>
t.implicitlyAdded && t.viaView.info.params.head.info.bounds.isEmptyBounds
case _ =>
false
})
def memberArity(m: Member): CompletionCandidate.Arity =
if (m.sym.paramss.isEmpty) CompletionCandidate.Nullary
else if (m.sym.paramss.size == 1 && m.sym.paramss.head.isEmpty) CompletionCandidate.Nilary
else CompletionCandidate.Other
def defStringCandidates(matching: List[Member], isNew: Boolean): List[CompletionCandidate] = {
val seen = new mutable.HashSet[Symbol]()
val ccs = for {
member <- matching
if seen.add(member.sym)
sym <- if (member.sym.isClass && isNew) member.sym.info.decl(nme.CONSTRUCTOR).alternatives else member.sym.alternatives
sugared = sym.sugaredSymbolOrSelf
} yield {
CompletionCandidate(
name = member.symNameDropLocal.decoded,
arity = memberArity(member),
isDeprecated = isMemberDeprecated(member),
isUniversal = isMemberUniversal(member),
declString = () => {
if (sym.isPackageObjectOrClass) ""
else {
val tp = member.prefix memberType sym
val desc = Seq(if (isMemberDeprecated(member)) "(deprecated)" else "", if (isMemberUniversal(member)) "(universal)" else "")
val methodOtherDesc = if (!desc.exists(_ != "")) "" else " " + desc.filter(_ != "").mkString(" ")
sugared.defStringSeenAs(tp) + methodOtherDesc
}
},
alias = member.aliasInfo.fold[Option[String]](None)(s => Some(s.sym.nameString))
)
}
ccs
}
val found = this.completionsAt(cursor) match {
case NoResults => NoCandidates
case r =>
def shouldHide(m: Member): Boolean =
filter && tabCount == 0 && (isMemberDeprecated(m) || isMemberUniversal(m))
val matching = r.matchingResults(nameMatcher = if (filter) {entered => candidate => candidate.startsWith(entered)} else _ => _ => true).filterNot(shouldHide)
val pos1 = positionOf(cursor)
import compiler._
val locator = new Locator(pos1)
val tree = locator locateIn unit.body
var isNew = false
new TreeStackTraverser {
override def traverse(t: Tree): Unit = {
if (t eq tree) {
isNew = path.dropWhile { case _: Select | _: Annotated => true; case _ => false}.headOption match {
case Some(_: New) => true
case _ => false
}
} else super.traverse(t)
}
}.traverse(unit.body)
val candidates = defStringCandidates(matching, isNew)
val pos = cursor - r.positionDelta
(pos, candidates.sortBy(_.name))
}
found
}
}
}