Skip to content

Commit 6d69f84

Browse files
vlad20012yopox
authored andcommitted
COMP: don't show popup for let binding starting with a lowercase char
1 parent db9105d commit 6d69f84

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Use of this source code is governed by the MIT license that can be
3+
* found in the LICENSE file.
4+
*/
5+
6+
package org.rust.lang.core.completion
7+
8+
import com.intellij.codeInsight.completion.CompletionConfidence
9+
import com.intellij.psi.PsiElement
10+
import com.intellij.psi.PsiFile
11+
import com.intellij.util.ThreeState
12+
import org.rust.lang.core.psi.RsElementTypes.IDENTIFIER
13+
import org.rust.lang.core.psi.RsLetDecl
14+
import org.rust.lang.core.psi.RsPatBinding
15+
import org.rust.lang.core.psi.ext.elementType
16+
import org.rust.lang.core.psi.ext.topLevelPattern
17+
18+
class RsCompletionConfidence : CompletionConfidence() {
19+
override fun shouldSkipAutopopup(contextElement: PsiElement, psiFile: PsiFile, offset: Int): ThreeState {
20+
// Don't show completion popup when typing a `let binding` identifier starting with a lowercase letter.
21+
// If the identifier is uppercase, the user probably wants to type a destructuring pattern
22+
// (`let Foo { ... }`), so we show the completion popup in this case
23+
if (contextElement.elementType == IDENTIFIER) {
24+
val identText = contextElement.node.chars
25+
if (identText.firstOrNull()?.isLowerCase() == true && !"mu".startsWith(identText)) {
26+
val parent = contextElement.parent
27+
if (parent is RsPatBinding && parent.topLevelPattern.parent is RsLetDecl) {
28+
return ThreeState.YES
29+
}
30+
}
31+
}
32+
return ThreeState.UNSURE
33+
}
34+
}

src/main/resources/META-INF/rust-core.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@
241241

242242
<codeCompletionConfigurable instance="org.rust.ide.settings.RsCodeCompletionConfigurable"/>
243243

244+
<completion.confidence language="Rust"
245+
implementationClass="org.rust.lang.core.completion.RsCompletionConfidence"/>
246+
244247
<completion.contributor language="Rust"
245248
implementationClass="org.rust.lang.core.completion.RsKeywordCompletionContributor"
246249
id="RsKeywordCompletionContributor"

src/test/kotlin/org/rust/lang/core/completion/RsCompletionAutoPopupTest.kt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,76 @@ class RsCompletionAutoPopupTest : RsCompletionTestBase() {
6666
}
6767
""", "|")
6868

69+
fun `test popup is not shown when typing lowercase let binding`() = checkPopupIsNotShownAfterTyping("""
70+
struct a1 { f: i32 }
71+
const a2: i32 = 1;
72+
fn main() {
73+
let /*caret*/
74+
}
75+
""", "a")
76+
77+
fun `test popup is not shown when typing nested lowercase let binding`() = checkPopupIsNotShownAfterTyping("""
78+
struct a1 { f: i32 }
79+
const a2: i32 = 1;
80+
fn main() {
81+
let (a, /*caret*/)
82+
}
83+
""", "a")
84+
85+
fun `test popup is shown when typing uppercase let binding`() = checkPopupIsShownAfterTyping("""
86+
struct A1 { f: i32 }
87+
const A2: i32 = 1;
88+
fn main() {
89+
let /*caret*/
90+
}
91+
""", "A")
92+
93+
fun `test popup is shown when typing if let binding`() = checkPopupIsShownAfterTyping("""
94+
struct a1 { f: i32 }
95+
const a2: i32 = 1;
96+
fn main() {
97+
if let /*caret*/
98+
}
99+
""", "a")
100+
101+
fun `test popup is shown when typing while let binding`() = checkPopupIsShownAfterTyping("""
102+
struct a1 { f: i32 }
103+
const a2: i32 = 1;
104+
fn main() {
105+
while let /*caret*/
106+
}
107+
""", "a")
108+
109+
fun `test popup is shown when typing binding in match arm`() = checkPopupIsShownAfterTyping("""
110+
struct a1 { f: i32 }
111+
const a2: i32 = 1;
112+
fn main() {
113+
match 1 {
114+
/*caret*/
115+
}
116+
}
117+
""", "a")
118+
119+
fun `test popup is shown when typing let mut 1`() = checkPopupIsShownAfterTyping("""
120+
fn main() {
121+
let /*caret*/
122+
}
123+
""", "m")
124+
125+
fun `test popup is shown when typing let mut 2`() = checkPopupIsShownAfterTyping("""
126+
fn main() {
127+
let m/*caret*/
128+
}
129+
""", "u")
130+
131+
fun `test popup is not shown when typing lowercase let mut binding`() = checkPopupIsNotShownAfterTyping("""
132+
struct a1 { f: i32 }
133+
const a2: i32 = 1;
134+
fn main() {
135+
let mut /*caret*/
136+
}
137+
""", "a")
138+
69139
override fun setUp() {
70140
super.setUp()
71141
tester = CompletionAutoPopupTester(myFixture)

0 commit comments

Comments
 (0)