Skip to content

Commit

Permalink
fix(completion): improve completion contribution for DevInT language #…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
phodal committed Mar 13, 2024
1 parent 8fc17ec commit ae27be4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
Expand Up @@ -2,12 +2,14 @@
package cc.unitmesh.devti.language

import cc.unitmesh.devti.language.psi.DevInFile
import cc.unitmesh.devti.language.psi.DevInTypes
import com.intellij.codeInsight.AutoPopupController
import com.intellij.codeInsight.editorActions.TypedHandlerDelegate
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiFile
import com.intellij.psi.util.elementType

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

return when (charTyped) {
'`' -> {
// val offset = editor.caretModel.primaryCaret.offset
// if (offset == 0) {
// return Result.CONTINUE
// }
//
// val element = file.findElementAt(offset - 1)
// if (element?.elementType == DevInTypes.CODE_CONTENT || element?.elementType == DevInTypes.CODE_BLOCK_END) {
// return Result.CONTINUE
// }
val offset = editor.caretModel.primaryCaret.offset
if (offset == 0) {
return Result.CONTINUE
}

val element = file.findElementAt(offset - 1)
if (element?.elementType == DevInTypes.CODE_CONTENT || element?.elementType == DevInTypes.CODE_BLOCK_END) {
return Result.CONTINUE
}

PsiDocumentManager.getInstance(project).commitDocument(editor.document)
AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null)
Expand Down
Expand Up @@ -6,14 +6,12 @@ import com.intellij.codeInsight.completion.CompletionContributor
import com.intellij.codeInsight.completion.CompletionInitializationContext
import com.intellij.codeInsight.completion.CompletionType
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.patterns.PsiElementPattern
import com.intellij.psi.PsiElement

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

init {
extend(CompletionType.BASIC, declarationPattern(), CodeLanguageProvider())
extend(CompletionType.BASIC, psiElement(DevInTypes.LANGUAGE_ID), CodeLanguageProvider())
extend(CompletionType.BASIC, psiElement(DevInTypes.VARIABLE_ID), CustomVariableProvider())
}

Expand All @@ -22,8 +20,4 @@ class DevInCompletionContributor : CompletionContributor() {
context.dummyIdentifier = INPUT_DUMMY_IDENTIFIER
}
}

fun declarationPattern(): PsiElementPattern.Capture<PsiElement> =
psiElement()
.and(psiElement(DevInTypes.LANGUAGE_ID))
}

0 comments on commit ae27be4

Please sign in to comment.