Skip to content

Fix #4535: Handle Null and Nothing in completions #5744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class Definitions {
lazy val Any_getClass: TermSymbol = enterMethod(AnyClass, nme.getClass_, MethodType(Nil, ClassClass.typeRef.appliedTo(TypeBounds.empty)), Final)
lazy val Any_isInstanceOf: TermSymbol = enterT1ParameterlessMethod(AnyClass, nme.isInstanceOf_, _ => BooleanType, Final)
lazy val Any_asInstanceOf: TermSymbol = enterT1ParameterlessMethod(AnyClass, nme.asInstanceOf_, _.paramRefs(0), Final)
lazy val Any_typeTest: TermSymbol = enterT1ParameterlessMethod(AnyClass, nme.isInstanceOfPM, _ => BooleanType, Final | Synthetic)
lazy val Any_typeTest: TermSymbol = enterT1ParameterlessMethod(AnyClass, nme.isInstanceOfPM, _ => BooleanType, Final | Synthetic | Artifact)
// generated by pattern matcher, eliminated by erasure

def AnyMethods: List[TermSymbol] = List(Any_==, Any_!=, Any_equals, Any_hashCode,
Expand Down
23 changes: 14 additions & 9 deletions compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dotty.tools.dotc.interactive

import java.nio.charset.Charset

import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.config.Printers.interactiv
import dotty.tools.dotc.core.Contexts.{Context, NoContext}
Expand All @@ -10,13 +12,13 @@ import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Names.{Name, TermName}
import dotty.tools.dotc.core.NameKinds.SimpleNameKind
import dotty.tools.dotc.core.NameOps.NameDecorator
import dotty.tools.dotc.core.Symbols.{defn, NoSymbol, Symbol}
import dotty.tools.dotc.core.Symbols.{NoSymbol, Symbol, defn}
import dotty.tools.dotc.core.Scopes
import dotty.tools.dotc.core.StdNames.{nme, tpnme}
import dotty.tools.dotc.core.TypeError
import dotty.tools.dotc.core.Types.{NameFilter, NamedType, Type, NoType}
import dotty.tools.dotc.core.Types.{NameFilter, NamedType, NoType, Type}
import dotty.tools.dotc.printing.Texts._
import dotty.tools.dotc.util.{NoSourcePosition, SourcePosition}
import dotty.tools.dotc.util.{NameTransformer, NoSourcePosition, SourcePosition}

import scala.collection.mutable

Expand Down Expand Up @@ -150,7 +152,8 @@ object Completion {
nameToSymbols.map { case (name, symbols) =>
val typesFirst = symbols.sortWith((s1, s2) => s1.isType && !s2.isType)
val desc = description(typesFirst)
Completion(name.toString, desc, typesFirst)
val label = NameTransformer.decodeIllegalChars(name.toString)
Completion(label, desc, typesFirst)
}
}

Expand Down Expand Up @@ -207,11 +210,13 @@ object Completion {
* considered.
*/
def addMemberCompletions(qual: Tree)(implicit ctx: Context): Unit = {
addAccessibleMembers(qual.tpe)
if (!mode.is(Mode.Import)) {
// Implicit conversions do not kick in when importing
implicitConversionTargets(qual)(ctx.fresh.setExploreTyperState())
.foreach(addAccessibleMembers)
if (!qual.tpe.widenDealias.isBottomType) {
addAccessibleMembers(qual.tpe)
if (!mode.is(Mode.Import) && !qual.tpe.isRef(defn.NullClass)) {
// Implicit conversions do not kick in when importing
implicitConversionTargets(qual)(ctx.fresh.setExploreTyperState())
.foreach(addAccessibleMembers)
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions compiler/src/dotty/tools/dotc/util/NameTransformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ object NameTransformer {
else name
}

/** Decode expanded characters starting with `$u`, followed by the character's unicode expansion. */
def decodeIllegalChars(name: String): String = {
if (name.contains("$u")) {
val sb = new mutable.StringBuilder()
var i = 0
while (i < name.length - 5) {
if (name(i) == '$' && name(i + 1) == 'u') {
sb.append(Integer.valueOf(name.substring(i + 2, i + 6), 16).toChar)
i += 6
} else {
sb.append(name(i))
i += 1
}
}
while (i < name.length) {
sb.append(name(i))
i += 1
}
sb.result()
}
else name
}

/** Replace operator symbols by corresponding expansion strings.
*
* @param name the string to encode
Expand Down
21 changes: 21 additions & 0 deletions compiler/test/dotty/tools/repl/TabcompleteTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,25 @@ class TabcompleteTests extends ReplTest {
assertEquals(comp.find(_.startsWith("<")), None)
assert(!comp.contains("package"))
}

@Test def `null` = fromInitialState { implicit s =>
val comp = tabComplete("null.")
assertEquals(
List("!=", "##", "==", "asInstanceOf", "clone", "eq", "equals", "finalize", "getClass", "hashCode",
"isInstanceOf", "ne", "notify", "notifyAll", "synchronized", "toString", "wait"),
comp.distinct.sorted)
}

@Test def anyRef = fromInitialState { implicit s =>
val comp = tabComplete("(null: AnyRef).")
assertEquals(
List("!=", "##", "+", "->", "==", "asInstanceOf", "clone", "ensuring", "eq", "equals", "finalize", "formatted",
"getClass", "hashCode", "isInstanceOf", "ne", "notify", "notifyAll", "synchronized", "toString", "wait", "→"),
comp.distinct.sorted)
}

@Test def `???` = fromInitialState { implicit s =>
val comp = tabComplete("???.")
assertEquals(Nil, comp)
}
}