Skip to content

Commit

Permalink
fix: auto import for really long names
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Jun 25, 2024
1 parent 2eb1054 commit d37b794
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 20 deletions.
24 changes: 20 additions & 4 deletions mtags-shared/src/main/scala/scala/meta/internal/metals/Fuzzy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,17 @@ class Fuzzy {
symbol: String,
hasher: StringBloomFilter
): Unit = {
var allUpper = List.empty[String]
val upper = new StringBuilder()
def flushUpper() =
if (upper.nonEmpty) {
allUpper = upper.result() :: allUpper
upper.clear()
}
if (symbol.endsWith("$sp.class")) return
hasher.reset()
var i = 0
var delimiter = i
val upper = new StringBuilder()
var symbolicDelimiter = i
val N = lastIndex(symbol)
while (i < N) {
Expand All @@ -336,6 +342,7 @@ class Fuzzy {
hasher.reset()
delimiter = i + 1
symbolicDelimiter = delimiter
flushUpper()
case _ =>
if (ch.isUpper) {
delimiter = i
Expand Down Expand Up @@ -365,8 +372,9 @@ class Fuzzy {
idx += 1
}
}
flushUpper()
TrigramSubstrings.foreach(
upper.toString,
allUpper,
trigram => hasher.putCharSequence(trigram)
)
}
Expand Down Expand Up @@ -411,8 +419,14 @@ class Fuzzy {
else if (query.length() < PrefixSearchLimit && !isShortQueryRetry)
List(PrefixCharSequence(query))
else {
val result = mutable.Set.empty[CharSequence]
var allUpper = List.empty[String]
val upper = new StringBuilder
def flushUpper() =
if (upper.nonEmpty) {
allUpper = upper.result() :: allUpper
upper.clear()
}
val result = mutable.Set.empty[CharSequence]
var i = 0
var border = 0
while (i < query.length) {
Expand All @@ -421,6 +435,7 @@ class Fuzzy {
case '.' | '/' | '#' | '$' =>
result.add(new ZeroCopySubSequence(query, border, i))
border = i + 1
flushUpper()
case _ =>
if (ch.isUpper) {
if (border != i) {
Expand All @@ -438,8 +453,9 @@ class Fuzzy {
case _ =>
result.add(new ZeroCopySubSequence(query, border, query.length))
}
flushUpper()
if (includeTrigrams) {
TrigramSubstrings.foreach(upper.toString, trigram => result += trigram)
TrigramSubstrings.foreach(allUpper, trigram => result += trigram)
}
result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,62 @@ object TrigramSubstrings {
* }}}
*/
val DefaultMaxTrigrams = 250
type StartFromZero = Boolean

/**
* Iterate over all possible substrings of length 3 for the given string.
*/
def foreach(
inString: String,
inputs: List[String],
f: String => Unit,
maxResults: Int = DefaultMaxTrigrams
): Unit = {
val string = inString.reverse
val N = string.length
val arr = new Array[Char](3)
val N = inputs.length
var max = maxResults
def isNotDone = max > 0
var i = 0
while (i < N && isNotDone) {
var j = i + 1
var j = i
while (j < N && isNotDone) {
var k = j + 1
var k = j
while (k < N && isNotDone) {
arr(2) = string.charAt(i)
arr(1) = string.charAt(j)
arr(0) = string.charAt(k)
max = fromLists(
inputs(k),
(inputs(j), j != k),
(inputs(i), i != j),
f,
max
)
k += 1
}
j += 1
}
i += 1
}
}

private def fromLists(
input1: String,
input2: (String, StartFromZero),
input3: (String, StartFromZero),
f: String => Unit,
maxResults: Int
): Int = {
val N1 = input1.length
val N2 = input2._1.length
val N3 = input3._1.length
val arr = new Array[Char](3)
var max = maxResults
def isNotDone = max > 0
var i = 0
while (i < N1 && isNotDone) {
var j = if (input2._2) 0 else i + 1
while (j < N2 && isNotDone) {
var k = if (input3._2) 0 else j + 1
while (k < N3 && isNotDone) {
arr(0) = input1.charAt(i)
arr(1) = input2._1.charAt(j)
arr(2) = input3._1.charAt(k)
f(new String(arr))
max -= 1
k += 1
Expand All @@ -43,14 +76,15 @@ object TrigramSubstrings {
}
i += 1
}
max
}

/**
* Returns all possible substrings of length 3 for the given string.
*/
def seq(string: String): ArrayBuffer[String] = {
def seq(inputs: List[String]): ArrayBuffer[String] = {
val buf = ArrayBuffer.empty[String]
foreach(string, trigram => buf += trigram)
foreach(inputs.reverse, trigram => buf += trigram)
buf
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,40 @@ class ImportMissingSymbolCrossLspSuite
| }
|}
|""".stripMargin,
scalaVersion = "3.3.3",
scalaVersion = V.scala3,
)

check(
"i6475-2",
"""|package com.randomMetalsTest.app.latestCommentByItem.domain.projection.latestCommentByItemView
|package subscriber {
| class DeleteLatestCommentByItemViewOnLatestCommentByItemDeleted
|}
|package a {
| object O {
| val g: <<DeleteLatestCommentByItemViewOnLatestCommentByItemDeleted>> = ???
| }
|}
|""".stripMargin,
s"""|${ImportMissingSymbol.title(
"DeleteLatestCommentByItemViewOnLatestCommentByItemDeleted",
"com.randomMetalsTest.app.latestCommentByItem.domain.projection.latestCommentByItemView.subscriber",
)}
|${CreateNewSymbol.title("DeleteLatestCommentByItemViewOnLatestCommentByItemDeleted")}
|""".stripMargin,
"""|package com.randomMetalsTest.app.latestCommentByItem.domain.projection.latestCommentByItemView
|
|import com.randomMetalsTest.app.latestCommentByItem.domain.projection.latestCommentByItemView.subscriber.DeleteLatestCommentByItemViewOnLatestCommentByItemDeleted
|package subscriber {
| class DeleteLatestCommentByItemViewOnLatestCommentByItemDeleted
|}
|package a {
| object O {
| val g: DeleteLatestCommentByItemViewOnLatestCommentByItemDeleted = ???
| }
|}
|""".stripMargin,
scalaVersion = V.scala3,
)

}
16 changes: 12 additions & 4 deletions tests/unit/src/test/scala/tests/TrigramSubstringsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class TrigramSubstringsSuite extends BaseSuite {
loc: Location
): Unit = {
test(original) {
val obtained = TrigramSubstrings.seq(original).mkString("\n")
val obtained =
TrigramSubstrings.seq(original.split('.').toList).mkString("\n")
assertNoDiff(obtained, expected)
}
}
Expand All @@ -23,13 +24,20 @@ class TrigramSubstringsSuite extends BaseSuite {
assertNoDiff(obtained, expected)
}
}

check(
"abcd",
"a.bcde",
"""
|bcd
|acd
|abd
|bce
|bde
|cde
|abc
|abd
|abe
|acd
|ace
|ade
|""".stripMargin,
)

Expand Down

0 comments on commit d37b794

Please sign in to comment.