Skip to content

Commit 1695d18

Browse files
committed
Exclude Java module class in completion in imports
When completing imports, we don't want to show duplicates for instance when the user does: import java.io.FileDesc<tab> When completing imports, we filter out the Java module classes, to keep only the classes. This avoids showing twice `java.io.FileDescriptor`.
1 parent 0579311 commit 1695d18

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

compiler/src/dotty/tools/dotc/interactive/Interactive.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ object Interactive {
205205
* 3. are not a primary constructor,
206206
* 4. have an existing source symbol,
207207
* 5. are the module class in case of packages,
208-
* 6. are mutable accessors, to exclude setters for `var`,
209-
* 7. have same term/type kind as name prefix given so far
208+
* 6. are not Java module classes when completing imports (to avoid duplicate results).
209+
* 7. are mutable accessors, to exclude setters for `var`,
210+
* 8. have same term/type kind as name prefix given so far
210211
*
211212
* The reason for (2) is that we do not want to present compiler-synthesized identifiers
212213
* as completion results. However, if a user explicitly writes all '$' characters in an
@@ -223,6 +224,7 @@ object Interactive {
223224
!sym.isPrimaryConstructor &&
224225
sym.sourceSymbol.exists &&
225226
(!sym.is(Package) || !sym.moduleClass.exists) &&
227+
(!inImport || !sym.is(allOf(JavaDefined, Module), butNot = Package)) &&
226228
!sym.is(allOf(Mutable, Accessor)) &&
227229
(!termOnly || sym.isTerm) &&
228230
(!typeOnly || sym.isType)

language-server/test/dotty/tools/languageserver/CompletionTest.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,26 @@ class CompletionTest {
116116
("myClass", Class, "Object{...}"),
117117
("myTrait", Class, "Object{...}")))
118118
}
119+
120+
@Test def importJavaClass: Unit = {
121+
code"""import java.io.FileDesc${m1}""".withSource
122+
.completion(m1, Set(("FileDescriptor", Class, "Object{...}")))
123+
}
124+
125+
@Test def importJavaStaticMethod: Unit = {
126+
code"""import java.lang.System.lineSep${m1}""".withSource
127+
.completion(m1, Set(("lineSeparator", Method, "(): String")))
128+
}
129+
130+
@Test def importJavaStaticField: Unit = {
131+
code"""import java.lang.System.ou${m1}""".withSource
132+
.completion(m1, Set(("out", Field, "java.io.PrintStream")))
133+
}
134+
135+
@Test def completeJavaModuleClass: Unit = {
136+
code"""object O {
137+
val out = java.io.FileDesc${m1}
138+
}""".withSource
139+
.completion(m1, Set(("FileDescriptor", Module, "java.io.FileDescriptor")))
140+
}
119141
}

0 commit comments

Comments
 (0)