Skip to content

Commit

Permalink
Adjust tests for fuzzy searched completions
Browse files Browse the repository at this point in the history
  • Loading branch information
rochala committed Mar 1, 2024
1 parent 6e02dc3 commit f702cab
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 128 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ object Completion:
def completionPrefix(path: List[untpd.Tree], pos: SourcePosition)(using Context): String =
def fallback: Int =
var i = pos.point - 1
while i >= 0 && Chars.isIdentifierPart(pos.source.content()(i)) do i -= 1
while i >= 0 && Character.isUnicodeIdentifierPart(pos.source.content()(i)) do i -= 1
i + 1

path match
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package dotty.tools.pc
package completions

import java.nio.file.Path
import java.nio.file.Paths

import scala.collection.mutable
import scala.meta.internal.metals.Fuzzy
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.mtags.CoursierComplete
import scala.meta.internal.pc.{IdentifierComparator, MemberOrdering}
import scala.meta.pc.*

import dotty.tools.dotc.ast.NavigateAST
import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.ast.untpd
import dotty.tools.dotc.ast.NavigateAST
import dotty.tools.dotc.core.Comments.Comment
import dotty.tools.dotc.core.Constants.Constant
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Denotations.SingleDenotation
import dotty.tools.dotc.core.Flags
import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.NameOps.*
Expand All @@ -27,14 +18,23 @@ import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.interactive.Completion
import dotty.tools.dotc.interactive.Completion.Mode
import dotty.tools.dotc.interactive.Interactive
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.dotc.util.SrcPos
import dotty.tools.pc.AutoImports.AutoImportsGenerator
import dotty.tools.pc.completions.OverrideCompletions.OverrideExtractor
import dotty.tools.pc.buildinfo.BuildInfo
import dotty.tools.pc.completions.OverrideCompletions.OverrideExtractor
import dotty.tools.pc.utils.MtagsEnrichments.*
import dotty.tools.dotc.core.Denotations.SingleDenotation
import dotty.tools.dotc.interactive.Interactive

import java.nio.file.Path
import java.nio.file.Paths
import scala.collection.mutable
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.mtags.CoursierComplete
import scala.meta.internal.pc.CompletionFuzzy
import scala.meta.internal.pc.IdentifierComparator
import scala.meta.internal.pc.MemberOrdering
import scala.meta.pc.*

class Completions(
text: String,
Expand Down Expand Up @@ -110,10 +110,11 @@ class Completions(
val (all, result) =
if exclusive then (advanced, SymbolSearch.Result.COMPLETE)
else
val keywords = KeywordsCompletions.contribute(adjustedPath, completionPos, comments)
val keywords = KeywordsCompletions.contribute(path, completionPos, comments)
val allAdvanced = advanced ++ keywords
val fuzzyMatcher: Name => Boolean = name =>
Fuzzy.matchesSubCharacters(completionPos.query, name.toString)
if completionMode.is(Mode.Member) then CompletionFuzzy.matchesSubCharacters(completionPos.query, name.toString)
else CompletionFuzzy.matches(completionPos.query, name.toString)

path match
// should not show completions for toplevel
Expand Down Expand Up @@ -387,7 +388,7 @@ class Completions(

// class Fo@@
case (td: TypeDef) :: _
if Fuzzy.matches(
if CompletionFuzzy.matches(
td.symbol.name.decoded.replace(Cursor.value, "").nn,
filename
) =>
Expand Down Expand Up @@ -883,14 +884,13 @@ class Completions(
val byLocalSymbol = compareLocalSymbols(s1, s2)
if byLocalSymbol != 0 then byLocalSymbol
else
val byRelevance = compareByRelevance(o1, o2)
if byRelevance != 0 then byRelevance
val f1 = fuzzyScore(sym1)
val f2 = fuzzyScore(sym2)
val byFuzzy = Integer.compare(f1, f2)
if byFuzzy != 0 then byFuzzy
else
val byFuzzy = Integer.compare(
fuzzyScore(sym1),
fuzzyScore(sym2)
)
if byFuzzy != 0 then byFuzzy
val byRelevance = compareByRelevance(o1, o2)
if byRelevance != 0 then byRelevance
else
val byIdentifier = IdentifierComparator.compare(
s1.name.show,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.meta.internal.pc.Keyword

import dotty.tools.dotc.ast.NavigateAST
import dotty.tools.dotc.ast.Positioned
import dotty.tools.dotc.ast.untpd.*
import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.ast.untpd
import dotty.tools.dotc.ast.untpd.UntypedTreeTraverser
import dotty.tools.dotc.core.Comments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ abstract class BaseCompletionSuite extends BasePCSuite:

if (assertSingleItem && items.length != 1) then
fail(
s"expected single completion item, obtained ${items.length} items.\n${items}"
s"expected single completion item, obtained ${items.length} items.\n${items.map(_.getLabel.nn + "\n")}"
)

if (items.size <= itemIndex) then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ class CompletionDocSuite extends BaseCompletionSuite:
|Found documentation for scala/collection/Iterator.
|Iterator scala.collection
|""".stripMargin,

includeDocs = true
includeDocs = true,
topLines = Some(1)
)

@Test def `scala5` =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
|def main = 100.inc@@
|""".stripMargin,
"""|incr: Int (extension)
|asInstanceOf[X0]: X0
|isInstanceOf[X0]: Boolean
|""".stripMargin
)

@Test def `simple-old-syntax` =
check(
"""|package example
"""package example
|
|object Test:
| implicit class TestOps(a: Int):
| def testOps(b: Int): String = ???
|
|def main = 100.test@@
|""".stripMargin,
"""|testOps(b: Int): String (implicit)
|""".stripMargin
"""testOps(b: Int): String (implicit)
|""".stripMargin,
topLines = Some(1)
)

@Test def `simple2` =
Expand Down Expand Up @@ -93,7 +96,10 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
|
|def main = "foo".iden@@
|""".stripMargin,
"""|identity: String (implicit)
"""|identity: String (implicit)
|indent(x$0: Int): String
|stripIndent(): String
|isDefinedAt(idx: Int): Boolean
|""".stripMargin // identity2 won't be available
)

Expand Down Expand Up @@ -152,7 +158,8 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
| def incr: Int = num + 1
|
|def main = 100.incr
|""".stripMargin
|""".stripMargin,
assertSingleItem = false
)

@Test def `simple-edit-old` =
Expand All @@ -174,7 +181,8 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
| def incr: Int = num + 1
|
|def main = 100.incr
|""".stripMargin
|""".stripMargin,
assertSingleItem = false
)

@Test def `simple-edit-suffix` =
Expand Down Expand Up @@ -262,6 +270,8 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
| def main = 100.inc@@
|""".stripMargin,
"""|incr: Int (extension)
|asInstanceOf[X0]: X0
|isInstanceOf[X0]: Boolean
|""".stripMargin
)

Expand All @@ -276,6 +286,8 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
| def main = 100.inc@@
|""".stripMargin,
"""|incr: Int (implicit)
|asInstanceOf[X0]: X0
|isInstanceOf[X0]: Boolean
|""".stripMargin
)

Expand All @@ -290,6 +302,8 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
| def main = 100.inc@@
|""".stripMargin,
"""|incr: Int (extension)
|asInstanceOf[X0]: X0
|isInstanceOf[X0]: Boolean
|""".stripMargin
)

Expand All @@ -304,6 +318,8 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
| def main = 100.inc@@
|""".stripMargin,
"""|incr: Int (implicit)
|asInstanceOf[X0]: X0
|isInstanceOf[X0]: Boolean
|""".stripMargin
)

Expand Down Expand Up @@ -391,7 +407,8 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
|testVal: Int (implicit)
|testVar: Int (implicit)
|testOps(b: Int): String (implicit)
|""".stripMargin
|""".stripMargin,
topLines = Some(4)
)

@Test def `implicit-val-edit` =
Expand All @@ -413,5 +430,6 @@ class CompletionExtensionSuite extends BaseCompletionSuite:
| val testVal: Int = 42
|
|def main = 100.testVal
|""".stripMargin
|""".stripMargin,
assertSingleItem = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -698,28 +698,26 @@ class CompletionKeywordSuite extends BaseCompletionSuite:

@Test def `derives-with-extends` =
check(
"""
|package foo
|
|trait Bar {}
|trait Baz {}
|
|class Foo(x: Int) extends Bar with Baz der@@
""".stripMargin,
"""|package foo
|
|trait Bar {}
|trait Baz {}
|
|class Foo(x: Int) extends Bar with Baz der@@
|""".stripMargin,
"""|derives
|""".stripMargin
)

@Test def `derives-with-constructor-extends` =
check(
"""
|package foo
|
|trait Bar {}
|class Baz(b: Int) {}
|
|class Foo(x: Int) extends Bar with Baz(1) @@
""".stripMargin,
"""|package foo
|
|trait Bar {}
|class Baz(b: Int) {}
|
|class Foo(x: Int) extends Bar with Baz(1) der@@
|""".stripMargin,
"""|derives
|""".stripMargin
)
Expand All @@ -737,63 +735,3 @@ class CompletionKeywordSuite extends BaseCompletionSuite:
""".stripMargin,
""
)

@Test def `only-keywords` =
check(
"""
|package foo
|
|object Main {
| class Baz(x: Int) @@
|}
""".stripMargin,
""
)

@Test def `only-keywords-1` =
check(
"""
|package foo
|
|object Main {
| class Baz(x: Int)
| p@@
|}
""".stripMargin,
""
)

@Test def `only-keywords-2` =
check(
"""
|package foo
|
|class Baz(x: Int) @@
""".stripMargin,
""
)

@Test def `def-after-extension` =
check(
"""
|object Main {
| extension (x: Int) @@
|}
""".stripMargin,
"""|derives
|private
|""".stripMargin
)

@Test def `def-after-extension-newline` =
check(
"""
|object Main {
| extension (x: Int)
| @@
|}
""".stripMargin,
"""|derives
|private
|""".stripMargin
)
Original file line number Diff line number Diff line change
Expand Up @@ -925,12 +925,15 @@ class CompletionOverrideSuite extends BaseCompletionSuite:
| def@@
|}
|""".stripMargin,

"""|def hello1: Int
|override val hello2: Int
|override def equals(x$0: Any): Boolean
|override def hashCode(): Int
|override def toString(): String
|override val hello2: Int
|""".stripMargin,
includeDetail = false,
topLines = Some(3)
topLines = Some(5)
)

@Test def `path-dependent` =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class CompletionSnippetNegSuite extends BaseCompletionSuite:

@Test def `member` =
checkSnippet(
"""
|object Main {
| List.appl@@
|}
|""".stripMargin,
"apply"
"""|object Main {
| List.appl@@
|}
|""".stripMargin,
"""|apply
|unapplySeq""".stripMargin
)

@Test def `scope` =
Expand Down
Loading

0 comments on commit f702cab

Please sign in to comment.