Skip to content

Commit 0564d02

Browse files
authored
Merge pull request #81 from Strum355/detailed-overload-completion
Parameter lists in completion labels
2 parents e106e02 + 2f60180 commit 0564d02

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

package-lock.json

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/kotlin/org/javacs/kt/completion/Completions.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ fun completions(file: CompiledFile, cursor: Int): CompletionList {
4848
}
4949

5050
private val callPattern = Regex("(.*)\\((\\$\\d+)?\\)")
51+
private val methodSignature = Regex("""(?:fun|constructor) (?:<(?:[a-zA-Z\?\!\: ]+)(?:, [A-Z])*> )?([a-zA-Z]+\(.*\))""")
5152

5253
private fun completionItem(d: DeclarationDescriptor, surroundingElement: KtElement, file: CompiledFile): CompletionItem {
5354
val result = d.accept(RenderCompletionItem(), null)
5455

56+
result.label = methodSignature.find(result.detail)?.groupValues?.get(1) ?: result.label
57+
5558
if (isGetter(d) || isSetter(d)) {
5659
val name = extractVarName(d)
5760

src/test/kotlin/org/javacs/kt/CompletionsTest.kt

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ class InstanceMemberTest : SingleFileTestFixture("completions", "InstanceMember.
1010
val completions = languageServer.textDocumentService.completion(completionParams(file, 3, 15)).get().right!!
1111
val labels = completions.items.map { it.label }
1212

13-
assertThat(labels, hasItem("instanceFoo"))
14-
assertThat(labels, hasItem("extensionFoo"))
15-
assertThat(labels, hasItem("fooVar"))
16-
assertThat(labels, not(hasItem("privateInstanceFoo")))
17-
assertThat(labels, not(hasItem("getFooVar")))
18-
assertThat(labels, not(hasItem("setFooVar")))
13+
assertThat(labels, hasItem(startsWith("instanceFoo")))
14+
assertThat(labels, hasItem(startsWith("extensionFoo")))
15+
assertThat(labels, hasItem(startsWith("fooVar")))
16+
assertThat(labels, not(hasItem(startsWith("privateInstanceFoo"))))
17+
assertThat(labels, not(hasItem(startsWith("getFooVar"))))
18+
assertThat(labels, not(hasItem(startsWith("setFooVar"))))
1919

20-
assertThat("Reports instanceFoo only once", completions.items.filter { it.label == "instanceFoo" }, hasSize(1))
21-
assertThat("Reports extensionFoo only once", completions.items.filter { it.label == "extensionFoo" }, hasSize(1))
20+
assertThat("Reports instanceFoo only once", completions.items.filter { it.label.startsWith("instanceFoo") }, hasSize(1))
21+
assertThat("Reports extensionFoo only once", completions.items.filter { it.label.startsWith("extensionFoo") }, hasSize(1))
2222
}
2323

2424
@Test fun `find count extension function`() {
@@ -32,28 +32,28 @@ class InstanceMemberTest : SingleFileTestFixture("completions", "InstanceMember.
3232
val completions = languageServer.textDocumentService.completion(completionParams(file, 13, 16)).get().right!!
3333
val labels = completions.items.map { it.label }
3434

35-
assertThat(labels, hasItem("instanceFoo"))
35+
assertThat(labels, hasItem(startsWith("instanceFoo")))
3636
// assertThat(labels, not(hasItem("extensionFoo"))) Kotlin will probably implement this later
37-
assertThat(labels, hasItem("fooVar"))
38-
assertThat(labels, not(hasItem("privateInstanceFoo")))
39-
assertThat(labels, not(hasItem("getFooVar")))
40-
assertThat(labels, not(hasItem("setFooVar")))
37+
assertThat(labels, hasItem(startsWith("fooVar")))
38+
assertThat(labels, not(hasItem(startsWith("privateInstanceFoo"))))
39+
assertThat(labels, not(hasItem(startsWith("getFooVar"))))
40+
assertThat(labels, not(hasItem(startsWith("setFooVar"))))
4141

42-
assertThat(completions.items.filter { it.label == "instanceFoo" }.firstOrNull(), hasProperty("insertText", equalTo("instanceFoo")))
42+
assertThat(completions.items.filter { it.label.startsWith("instanceFoo") }.firstOrNull(), hasProperty("insertText", equalTo("instanceFoo")))
4343
}
4444

4545
@Test fun `complete unqualified function reference`() {
4646
val completions = languageServer.textDocumentService.completion(completionParams(file, 17, 8)).get().right!!
4747
val labels = completions.items.map { it.label }
4848

49-
assertThat(labels, hasItem("findFunctionReference"))
49+
assertThat(labels, hasItem(startsWith("findFunctionReference")))
5050
}
5151

5252
@Test fun `complete a function name within a call`() {
5353
val completions = languageServer.textDocumentService.completion(completionParams(file, 22, 27)).get().right!!
5454
val labels = completions.items.map { it.label }
5555

56-
assertThat(labels, hasItem("findFunctionReference"))
56+
assertThat(labels, hasItem(startsWith("findFunctionReference")))
5757
assertThat(labels, not(hasItem("instanceFoo")))
5858
}
5959
}
@@ -73,19 +73,20 @@ class FunctionScopeTest : SingleFileTestFixture("completions", "FunctionScope.kt
7373
val completions = languageServer.textDocumentService.completion(completionParams(file, 4, 10)).get().right!!
7474
val labels = completions.items.map { it.label }
7575

76+
println("ok lol" + labels.toString())
7677
assertThat(labels, hasItem("anArgument"))
7778
assertThat(labels, hasItem("aLocal"))
7879
assertThat(labels, hasItem("aClassVal"))
79-
assertThat(labels, hasItem("aClassFun"))
80+
assertThat(labels, hasItem(startsWith("aClassFun")))
8081
assertThat(labels, hasItem("aCompanionVal"))
81-
assertThat(labels, hasItem("aCompanionFun"))
82+
assertThat(labels, hasItem(startsWith("aCompanionFun")))
8283

8384
assertThat("Reports anArgument only once", completions.items.filter { it.label == "anArgument" }, hasSize(1))
8485
assertThat("Reports aLocal only once", completions.items.filter { it.label == "aLocal" }, hasSize(1))
8586
assertThat("Reports aClassVal only once", completions.items.filter { it.label == "aClassVal" }, hasSize(1))
86-
assertThat("Reports aClassFun only once", completions.items.filter { it.label == "aClassFun" }, hasSize(1))
87+
assertThat("Reports aClassFun only once", completions.items.filter { it.label.startsWith("aClassFun") }, hasSize(1))
8788
assertThat("Reports aCompanionVal only once", completions.items.filter { it.label == "aCompanionVal" }, hasSize(1))
88-
assertThat("Reports aCompanionFun only once", completions.items.filter { it.label == "aCompanionFun" }, hasSize(1))
89+
assertThat("Reports aCompanionFun only once", completions.items.filter { it.label.startsWith("aCompanionFun") }, hasSize(1))
8990
}
9091
}
9192

@@ -109,7 +110,7 @@ class FillEmptyBodyTest : SingleFileTestFixture("completions", "FillEmptyBody.kt
109110
val completions = languageServer.textDocumentService.completion(completionParams(file, 3, 20)).get().right!!
110111
val labels = completions.items.map { it.label }
111112

112-
assertThat(labels, hasItem("bar"))
113+
assertThat(labels, hasItem(startsWith("bar")))
113114
}
114115
}
115116

@@ -127,7 +128,7 @@ class MiddleOfFunctionTest : SingleFileTestFixture("completions", "MiddleOfFunct
127128
val completions = languageServer.textDocumentService.completion(completionParams(file, 3, 11)).get().right!!
128129
val labels = completions.items.map { it.label }
129130

130-
assertThat(labels, hasItem("subSequence"))
131+
assertThat(labels, hasItem(startsWith("subSequence")))
131132
}
132133
}
133134

@@ -145,28 +146,28 @@ class CompleteStaticsTest : SingleFileTestFixture("completions", "Statics.kt") {
145146
val completions = languageServer.textDocumentService.completion(completionParams(file, 4, 16)).get().right!!
146147
val labels = completions.items.map { it.label }
147148

148-
assertThat(labels, hasItem("isNull"))
149+
assertThat(labels, hasItem(startsWith("isNull")))
149150
}
150151

151152
@Test fun `java static method reference`() {
152153
val completions = languageServer.textDocumentService.completion(completionParams(file, 7, 17)).get().right!!
153154
val labels = completions.items.map { it.label }
154155

155-
assertThat(labels, hasItem("isNull"))
156+
assertThat(labels, hasItem(startsWith("isNull")))
156157
}
157158

158159
@Test fun `object method`() {
159160
val completions = languageServer.textDocumentService.completion(completionParams(file, 5, 17)).get().right!!
160161
val labels = completions.items.map { it.label }
161162

162-
assertThat(labels, hasItem("objectFun"))
163+
assertThat(labels, hasItem(startsWith("objectFun")))
163164
}
164165

165166
@Test fun `companion object method`() {
166167
val completions = languageServer.textDocumentService.completion(completionParams(file, 6, 16)).get().right!!
167168
val labels = completions.items.map { it.label }
168169

169-
assertThat(labels, hasItem("companionFun"))
170+
assertThat(labels, hasItem(startsWith("companionFun")))
170171
}
171172
}
172173

@@ -175,9 +176,9 @@ class VisibilityTest : SingleFileTestFixture("completions", "Visibility.kt") {
175176
val completions = languageServer.textDocumentService.completion(completionParams(file, 3, 10)).get().right!!
176177
val labels = completions.items.map { it.label }
177178

178-
assertThat(labels, hasItems("privateThisFun", "protectedThisFun", "publicThisFun", "privateThisCompanionFun", "protectedThisCompanionFun", "publicThisCompanionFun", "privateTopLevelFun"))
179-
assertThat(labels, hasItems("protectedSuperFun", "publicSuperFun", "protectedSuperCompanionFun", "publicSuperCompanionFun"))
180-
assertThat(labels, not(hasItems("privateSuperFun", "privateSuperCompanionFun", "publicExtensionFun")))
179+
assertThat(labels, hasItems(startsWith("privateThisFun"), startsWith("protectedThisFun"), startsWith("publicThisFun"), startsWith("privateThisCompanionFun"), startsWith("protectedThisCompanionFun"), startsWith("publicThisCompanionFun"), startsWith("privateTopLevelFun")))
180+
assertThat(labels, hasItems(startsWith("protectedSuperFun"), startsWith("publicSuperFun"), startsWith("protectedSuperCompanionFun"), startsWith("publicSuperCompanionFun")))
181+
assertThat(labels, not(hasItems(startsWith("privateSuperFun"), startsWith("privateSuperCompanionFun"), startsWith("publicExtensionFun"))))
181182
}
182183
}
183184

@@ -210,7 +211,7 @@ class DoubleDotTest : SingleFileTestFixture("completions", "DoubleDot.kt") {
210211
val labels = completions.items.map { it.label }
211212

212213
assertThat(labels, not(hasItem("chars")))
213-
assertThat(labels, hasItem("anyMatch"))
214+
assertThat(labels, hasItem(startsWith("anyMatch")))
214215
}
215216
}
216217

@@ -219,7 +220,7 @@ class QuestionDotTest : SingleFileTestFixture("completions", "QuestionDot.kt") {
219220
val completions = languageServer.textDocumentService.completion(completionParams(file, 2, 8)).get().right!!
220221
val labels = completions.items.map { it.label }
221222

222-
assertThat(labels, hasItem("chars"))
223+
assertThat(labels, hasItem(startsWith("chars")))
223224
}
224225
}
225226

@@ -244,7 +245,7 @@ class EditCallTest : SingleFileTestFixture("completions", "EditCall.kt") {
244245
val completions = languageServer.textDocumentService.completion(completionParams(file, 2, 11)).get().right!!
245246
val labels = completions.items.map { it.label }
246247

247-
assertThat(labels, hasItem("println"))
248-
assertThat(completions.items.filter { it.label == "println" }.firstOrNull(), hasProperty("insertText", equalTo("println")))
248+
assertThat(labels, hasItem(startsWith("println")))
249+
assertThat(completions.items.filter { it.label.startsWith("println") }.firstOrNull(), hasProperty("insertText", equalTo("println")))
249250
}
250251
}

0 commit comments

Comments
 (0)