Skip to content

Commit ae27be4

Browse files
committed
fix(completion): improve completion contribution for DevInT language #101
The completion contribution for the DevInT language has been improved by using more specific element patterns and removing unnecessary imports and code. The completion is now triggered only when the caret is not at the beginning of the document and is not inside a code block, ensuring that the completion popup is shown only when it is relevant and useful to the user.
1 parent 8fc17ec commit ae27be4

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

exts/devin-lang/src/main/kotlin/cc/unitmesh/devti/language/DevInTypedHandler.kt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
package cc.unitmesh.devti.language
33

44
import cc.unitmesh.devti.language.psi.DevInFile
5+
import cc.unitmesh.devti.language.psi.DevInTypes
56
import com.intellij.codeInsight.AutoPopupController
67
import com.intellij.codeInsight.editorActions.TypedHandlerDelegate
78
import com.intellij.openapi.editor.Editor
89
import com.intellij.openapi.project.Project
910
import com.intellij.psi.PsiDocumentManager
1011
import com.intellij.psi.PsiFile
12+
import com.intellij.psi.util.elementType
1113

1214
class DevInTypedHandler : TypedHandlerDelegate() {
1315
override fun checkAutoPopup(charTyped: Char, project: Project, editor: Editor, file: PsiFile): Result {
@@ -17,15 +19,15 @@ class DevInTypedHandler : TypedHandlerDelegate() {
1719

1820
return when (charTyped) {
1921
'`' -> {
20-
// val offset = editor.caretModel.primaryCaret.offset
21-
// if (offset == 0) {
22-
// return Result.CONTINUE
23-
// }
24-
//
25-
// val element = file.findElementAt(offset - 1)
26-
// if (element?.elementType == DevInTypes.CODE_CONTENT || element?.elementType == DevInTypes.CODE_BLOCK_END) {
27-
// return Result.CONTINUE
28-
// }
22+
val offset = editor.caretModel.primaryCaret.offset
23+
if (offset == 0) {
24+
return Result.CONTINUE
25+
}
26+
27+
val element = file.findElementAt(offset - 1)
28+
if (element?.elementType == DevInTypes.CODE_CONTENT || element?.elementType == DevInTypes.CODE_BLOCK_END) {
29+
return Result.CONTINUE
30+
}
2931

3032
PsiDocumentManager.getInstance(project).commitDocument(editor.document)
3133
AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null)

exts/devin-lang/src/main/kotlin/cc/unitmesh/devti/language/completion/DevInCompletionContributor.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import com.intellij.codeInsight.completion.CompletionContributor
66
import com.intellij.codeInsight.completion.CompletionInitializationContext
77
import com.intellij.codeInsight.completion.CompletionType
88
import com.intellij.patterns.PlatformPatterns.psiElement
9-
import com.intellij.patterns.PsiElementPattern
10-
import com.intellij.psi.PsiElement
119

1210
class DevInCompletionContributor : CompletionContributor() {
1311
private val INPUT_DUMMY_IDENTIFIER = "DevInDummy"
1412

1513
init {
16-
extend(CompletionType.BASIC, declarationPattern(), CodeLanguageProvider())
14+
extend(CompletionType.BASIC, psiElement(DevInTypes.LANGUAGE_ID), CodeLanguageProvider())
1715
extend(CompletionType.BASIC, psiElement(DevInTypes.VARIABLE_ID), CustomVariableProvider())
1816
}
1917

@@ -22,8 +20,4 @@ class DevInCompletionContributor : CompletionContributor() {
2220
context.dummyIdentifier = INPUT_DUMMY_IDENTIFIER
2321
}
2422
}
25-
26-
fun declarationPattern(): PsiElementPattern.Capture<PsiElement> =
27-
psiElement()
28-
.and(psiElement(DevInTypes.LANGUAGE_ID))
2923
}

0 commit comments

Comments
 (0)