Skip to content

Commit 8928a04

Browse files
kasiaMarekWojciechMazur
authored andcommitted
improvement: sorting workspace members with same name by frequency
[Cherry-picked cb079b9]
1 parent 783d05c commit 8928a04

File tree

5 files changed

+73
-15
lines changed

5 files changed

+73
-15
lines changed

presentation-compiler/src/main/dotty/tools/pc/ScalaPresentationCompiler.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ case class ScalaPresentationCompiler(
4848
sh: Option[ScheduledExecutorService] = None,
4949
config: PresentationCompilerConfig = PresentationCompilerConfigImpl(),
5050
folderPath: Option[Path] = None,
51-
reportsLevel: ReportLevel = ReportLevel.Info
51+
reportsLevel: ReportLevel = ReportLevel.Info,
52+
completionItemPriority: CompletionItemPriority = (_: String) => 0,
5253
) extends PresentationCompiler:
5354

5455
def this() = this("", None, Nil, Nil)
@@ -63,6 +64,11 @@ case class ScalaPresentationCompiler(
6364
.map(StdReportContext(_, _ => buildTargetName, reportsLevel))
6465
.getOrElse(EmptyReportContext)
6566

67+
override def withCompletionItemPriority(
68+
priority: CompletionItemPriority
69+
): PresentationCompiler =
70+
copy(completionItemPriority = priority)
71+
6672
override def withBuildTargetName(buildTargetName: String) =
6773
copy(buildTargetName = Some(buildTargetName))
6874

@@ -142,7 +148,8 @@ case class ScalaPresentationCompiler(
142148
params,
143149
config,
144150
buildTargetIdentifier,
145-
folderPath
151+
folderPath,
152+
completionItemPriority
146153
).completions()
147154

148155
}

presentation-compiler/src/main/dotty/tools/pc/completions/CompletionProvider.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ import org.eclipse.lsp4j.InsertTextFormat
3232
import org.eclipse.lsp4j.InsertTextMode
3333
import org.eclipse.lsp4j.Range as LspRange
3434
import org.eclipse.lsp4j.TextEdit
35+
import scala.meta.pc.CompletionItemPriority
3536

3637
class CompletionProvider(
3738
search: SymbolSearch,
3839
driver: InteractiveDriver,
3940
params: OffsetParams,
4041
config: PresentationCompilerConfig,
4142
buildTargetIdentifier: String,
42-
folderPath: Option[Path]
43+
folderPath: Option[Path],
44+
referenceCounter: CompletionItemPriority
4345
)(using reports: ReportContext):
4446
def completions(): CompletionList =
4547
val uri = params.uri().nn
@@ -86,7 +88,8 @@ class CompletionProvider(
8688
folderPath,
8789
autoImportsGen,
8890
unit.comments,
89-
driver.settings
91+
driver.settings,
92+
referenceCounter
9093
).completions()
9194

9295
val items = completions.zipWithIndex.map { case (item, idx) =>

presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class Completions(
5050
workspace: Option[Path],
5151
autoImports: AutoImportsGenerator,
5252
comments: List[Comment],
53-
options: List[String]
53+
options: List[String],
54+
completionItemPriority: CompletionItemPriority
5455
)(using ReportContext):
5556

5657
given context: Context = ctx
@@ -911,6 +912,20 @@ class Completions(
911912
else 0
912913
end compareLocalSymbols
913914

915+
private def workspaceMemberPriority(symbol: Symbol): Int =
916+
completionItemPriority
917+
.workspaceMemberPriority(
918+
SemanticdbSymbols.symbolName(symbol),
919+
)
920+
921+
def compareFrequency(o1: CompletionValue, o2: CompletionValue): Int =
922+
(o1, o2) match
923+
case (w1: CompletionValue.Workspace, w2: CompletionValue.Workspace) =>
924+
workspaceMemberPriority(w1.symbol)
925+
.compareTo(workspaceMemberPriority(w2.symbol))
926+
case _ => 0
927+
end compareFrequency
928+
914929
def compareByRelevance(o1: CompletionValue, o2: CompletionValue): Int =
915930
Integer.compare(
916931
computeRelevancePenalty(o1, application),
@@ -1020,17 +1035,20 @@ class Completions(
10201035
)
10211036
if byIdentifier != 0 then byIdentifier
10221037
else
1023-
val byOwner =
1024-
s1.owner.fullName.toString
1025-
.compareTo(s2.owner.fullName.toString)
1026-
if byOwner != 0 then byOwner
1038+
val byFrequency = compareFrequency(o1, o2)
1039+
if byFrequency != 0 then byFrequency
10271040
else
1028-
val byParamCount = Integer.compare(
1029-
s1.paramSymss.flatten.size,
1030-
s2.paramSymss.flatten.size
1031-
)
1032-
if byParamCount != 0 then byParamCount
1033-
else s1.detailString.compareTo(s2.detailString)
1041+
val byOwner =
1042+
s1.owner.fullName.toString
1043+
.compareTo(s2.owner.fullName.toString)
1044+
if byOwner != 0 then byOwner
1045+
else
1046+
val byParamCount = Integer.compare(
1047+
s1.paramSymss.flatten.size,
1048+
s2.paramSymss.flatten.size
1049+
)
1050+
if byParamCount != 0 then byParamCount
1051+
else s1.detailString.compareTo(s2.detailString)
10341052
end if
10351053
end if
10361054
end if

presentation-compiler/test/dotty/tools/pc/base/BasePCSuite.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import dotty.tools.pc.utils._
2222
import org.eclipse.lsp4j.MarkupContent
2323
import org.eclipse.lsp4j.jsonrpc.messages.Either as JEither
2424
import org.junit.runner.RunWith
25+
import scala.meta.pc.CompletionItemPriority
2526

2627
object TestResources:
2728
val scalaLibrary = BuildInfo.ideTestsDependencyClasspath.map(_.toPath).toSeq
@@ -30,6 +31,7 @@ object TestResources:
3031

3132
@RunWith(classOf[ReusableClassRunner])
3233
abstract class BasePCSuite extends PcAssertions:
34+
val completionItemPriority: CompletionItemPriority = (_: String) => 0
3335
private val isDebug = ManagementFactory.getRuntimeMXBean.getInputArguments.toString.contains("-agentlib:jdwp")
3436

3537
val tmp = Files.createTempDirectory("stable-pc-tests")
@@ -53,6 +55,7 @@ abstract class BasePCSuite extends PcAssertions:
5355
.withExecutorService(executorService)
5456
.withScheduledExecutorService(executorService)
5557
.withSearch(search)
58+
.withCompletionItemPriority(completionItemPriority)
5659
.newInstance("", myclasspath.asJava, scalacOpts.asJava)
5760

5861
protected def config: PresentationCompilerConfig =
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dotty.tools.pc.tests.completion
2+
3+
import dotty.tools.pc.base.BaseCompletionSuite
4+
import scala.meta.pc.CompletionItemPriority
5+
import org.junit.Test
6+
7+
class CompletionContextSuite extends BaseCompletionSuite:
8+
override val completionItemPriority: CompletionItemPriority = {
9+
case "scala/concurrent/Future." => -1
10+
case _ => 0
11+
}
12+
// scala.concurrent.Future should be ranked higher than java.util.concurrent.Future
13+
val futureCompletionResult: List[String] =
14+
List("Future - scala.concurrent", "Future - java.util.concurrent")
15+
16+
@Test
17+
def `context` =
18+
check(
19+
"""package fut
20+
|object A {
21+
| Futur@@
22+
|}""".stripMargin,
23+
"""Future - scala.concurrent
24+
|Future - java.util.concurrent
25+
|""".stripMargin,
26+
filter = futureCompletionResult.contains
27+
)

0 commit comments

Comments
 (0)