Skip to content

Commit

Permalink
Remove pre Scala 3.0 final hacks, future proof some Scala 3 stuff (#1288
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alexarchambault authored Sep 7, 2022
1 parent f7e4c22 commit 4a9e698
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 539 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dotty.ammonite.compiler

import dotty.tools.dotc.core.NameOps._
import dotty.tools.dotc.core.Names.{Name, termName}
import dotty.tools.dotc.core.StdNames.nme
import dotty.tools.dotc.interactive.Completion
import dotty.tools.dotc.util.Chars.{isOperatorPart, isScalaLetter}

trait AmmCompletionExtras {

private val bslash = '\\'
private val isDot = (x: Char) => x == '.'
private val brackets = List('[',']','(',')','{','}')

private def label(name: Name): String = {

def maybeQuote(name: Name, recurse: Boolean): String =
if (recurse && name.isTermName)
name.asTermName.qualToString(maybeQuote(_, true), maybeQuote(_, false))
// initially adapted from
// https://github.com/scala/scala/blob/decbd53f1bde4600c8ff860f30a79f028a8e431d/
// src/reflect/scala/reflect/internal/Printers.scala#L573-L584
else if (name == nme.CONSTRUCTOR) "this"
else {
val decName = name.decode.toString
val hasSpecialChar = decName.exists { ch =>
brackets.contains(ch) || ch.isWhitespace || isDot(ch)
}
def isOperatorLike = (name.isOperatorName || decName.exists(isOperatorPart)) &&
decName.exists(isScalaLetter) &&
!decName.contains(bslash)
lazy val term = name.toTermName

val needsBackTicks = hasSpecialChar ||
isOperatorLike ||
nme.keywords(term) && term != nme.USCOREkw

if (needsBackTicks) s"`$decName`"
else decName
}

maybeQuote(name, true)
}


def maybeBackticked(input: String, hasBackTick: Boolean): Completion =
Completion(label(termName(input)), "", Nil)
def backtick(completion: Completion): Completion =
if (completion.label.startsWith("`")) completion
else completion.copy(label = label(termName(completion.label)))

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class DottyParser(source: SourceFile)(using Context) extends Parser(source) {
stats +++= localDef(in.offset)
else
empty = true
statSepOrEnd(stats, empty, altEnd = Tokens.CASE)
statSepOrEnd(stats, noPrevStat = empty, altEnd = Tokens.CASE)
do ()
stats.toList
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dotty.ammonite.compiler

import dotty.tools.dotc.interactive.Completion

trait AmmCompletionExtras {

def maybeBackticked(input: String, hasBackTick: Boolean): Completion =
Completion.backtickCompletions(Completion(input, "", Nil), hasBackTick)
def backtick(completion: Completion): Completion =
Completion.backtickCompletions(completion, completion.label.startsWith("`"))

}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class Compiler(
val out = vf.output
out.write(src)
out.close()
new SourceFile(vf, scala.io.Codec.UTF8)
new SourceFile(vf, new String(src, "UTF-8").toCharArray)
}

implicit val ctx: Context = run.runContext.withSource(sourceFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Names.{Name, termName}
import dotty.tools.dotc.core.Symbols.{Symbol, defn}
import dotty.tools.dotc.core.TypeError
import dotty.tools.dotc.interactive.Interactive
import dotty.tools.dotc.interactive.{Completion, Interactive}
import dotty.tools.dotc.util.SourcePosition

object AmmCompletion {
object AmmCompletion extends AmmCompletionExtras {

def completions(
pos: SourcePosition,
Expand All @@ -37,6 +37,8 @@ object AmmCompletion {
val prefix = Completion.completionPrefix(path, pos)
val completer = new DeepCompleter(mode, prefix, pos)

val hasBackTick = prefix.headOption.contains('`')

var extra = List.empty[Completion]

val completions = path match {
Expand All @@ -46,8 +48,8 @@ object AmmCompletion {
val complete = dependencyCompleteOpt.get
val (pos, completions) = complete(prefix)
val input0 = prefix.take(pos)
extra ++= completions.distinct.toList
.map(s => Completion(Completion.label(termName(input0 + s)), "", Nil))
extra = extra ::: completions.distinct.toList
.map(s => maybeBackticked(input0 + s, hasBackTick))
Map.empty
case Import(expr, _) :: _ =>
completer.directMemberCompletions(expr)
Expand All @@ -60,7 +62,7 @@ object AmmCompletion {
}
}

val describedCompletions = extra ++ Completion.describeCompletions(completions)
val describedCompletions = extra ++ Completion.describeCompletions(completions).map(backtick)
val offset = Completion.completionOffset(path)

(pos.span.start - prefix.length, describedCompletions)
Expand Down
Loading

0 comments on commit 4a9e698

Please sign in to comment.