Skip to content

Commit

Permalink
prevent keyword completions when they dont make any sense.
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Dec 31, 2023
1 parent 631ddea commit ecf5d9e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.util.TextRange;
import com.intellij.patterns.PsiElementPattern;
import com.intellij.patterns.StandardPatterns;
import com.intellij.plugins.haxe.HaxeLanguage;
import com.intellij.plugins.haxe.lang.lexer.HaxeTokenTypeSets;
Expand Down Expand Up @@ -64,7 +63,8 @@ public HaxeKeywordCompletionContributor() {
// foo.b<caret> - bad
// i<caret> - good
extend(CompletionType.BASIC,
psiElement().inFile(StandardPatterns.instanceOf(HaxeFile.class)).andNot(idInExpression.and(inComplexExpression)),
psiElement().inFile(StandardPatterns.instanceOf(HaxeFile.class))
.andNot(idInExpression.and(inComplexExpression)),
new CompletionProvider<>() {
@Override
protected void addCompletions(@NotNull CompletionParameters parameters,
Expand All @@ -82,91 +82,93 @@ private static void suggestKeywords(PsiElement position, @NotNull CompletionResu

List<LookupElement> lookupElements = new ArrayList<>();

if (dotFromIterator.accepts(completionElementAsComment)) {
addKeywords(lookupElements, Set.of(keywordOnly(OTRIPLE_DOT)));
return;
}

if (packageExpected.accepts(completionElementAsComment)) {
addKeywords(lookupElements, PACKAGE_KEYWORD);
return;
}
// avoid showing keyword suggestions when not relevant
if (allowLookupPattern.accepts(completionElementAsComment)) {

if (toplevelScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, TOP_LEVEL_KEYWORDS);
}
if (dotFromIterator.accepts(completionElementAsComment)) {
addKeywords(lookupElements, Set.of(keywordOnly(OTRIPLE_DOT)));
return;
}

if (moduleScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, MODULE_STRUCTURES_KEYWORDS);
addKeywords(lookupElements, VISIBILITY_KEYWORDS);
}
if (packageExpected.accepts(completionElementAsComment)) {
addKeywords(lookupElements, PACKAGE_KEYWORD);
return;
}

if (classDeclarationScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, CLASS_DEFINITION_KEYWORDS);
}
if (interfaceDeclarationScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, INTERFACE_DEFINITION_KEYWORDS);
}
if (toplevelScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, TOP_LEVEL_KEYWORDS);
}

if (abstractTypeDeclarationScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, ABSTRACT_DEFINITION_KEYWORDS);
}
if (moduleScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, MODULE_STRUCTURES_KEYWORDS);
addKeywords(lookupElements, VISIBILITY_KEYWORDS);
}

if (interfaceBodyScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, INTERFACE_BODY_KEYWORDS);
}
if (classDeclarationScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, CLASS_DEFINITION_KEYWORDS);
}
if (interfaceDeclarationScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, INTERFACE_DEFINITION_KEYWORDS);
}

if (classBodyScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, CLASS_BODY_KEYWORDS);
addKeywords(lookupElements, VISIBILITY_KEYWORDS);
addKeywords(lookupElements, ACCESSIBILITY_KEYWORDS);
}
if (abstractTypeDeclarationScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, ABSTRACT_DEFINITION_KEYWORDS);
}

if (functionBodyScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, METHOD_BODY_KEYWORDS);
addKeywords(lookupElements, VALUE_KEYWORDS);
}
if (initScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, VALUE_KEYWORDS);
}
if (interfaceBodyScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, INTERFACE_BODY_KEYWORDS);
}

if (classBodyScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, CLASS_BODY_KEYWORDS);
addKeywords(lookupElements, VISIBILITY_KEYWORDS);
addKeywords(lookupElements, ACCESSIBILITY_KEYWORDS);
}

if (insideSwitchCase.accepts(completionElementAsComment)) {
addKeywords(lookupElements, SWITCH_BODY_KEYWORDS);
}
if (functionBodyScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, METHOD_BODY_KEYWORDS);
addKeywords(lookupElements, VALUE_KEYWORDS);
}
if (initScope.accepts(completionElementAsComment)) {
addKeywords(lookupElements, VALUE_KEYWORDS);
}

if (isAfterIfStatement.accepts(completionElementAsComment)) {
addKeywords(lookupElements, Set.of(keywordWithSpace(KELSE)));
}

if (isInsideLoopBlock.accepts(completionElementAsComment)) {
addKeywords(lookupElements, LOOP_BODY_KEYWORDS);
}
if (insideSwitchCase.accepts(completionElementAsComment)) {
addKeywords(lookupElements, SWITCH_BODY_KEYWORDS);
}

if (isInsideForIterator.accepts(completionElementAsComment)) {
addKeywords(lookupElements, LOOP_ITERATOR_KEYWORDS);
}
if (isAfterIfStatement.accepts(completionElementAsComment)) {
addKeywords(lookupElements, Set.of(keywordWithSpace(KELSE)));
}

HaxePropertyAccessor propertyAccessor = PsiTreeUtil.getParentOfType(position, HaxePropertyAccessor.class);
if (isPropertyGetterValue.accepts(propertyAccessor)) {
result.stopHere();
lookupElements.clear();
addKeywords(lookupElements, PROPERTY_KEYWORDS, 1.1f);
addKeywords(lookupElements, Set.of(keywordOnly(PROPERTY_GET)), 1.2f);
}
if (isPropertySetterValue.accepts(propertyAccessor)) {
result.stopHere();
lookupElements.clear();
addKeywords(lookupElements, PROPERTY_KEYWORDS, 1.1f);
addKeywords(lookupElements, Set.of(keywordOnly(PROPERTY_SET)), 1.2f);
}
if (isInsideLoopBlock.accepts(completionElementAsComment)) {
addKeywords(lookupElements, LOOP_BODY_KEYWORDS);
}

if (isInsideForIterator.accepts(completionElementAsComment)) {
addKeywords(lookupElements, LOOP_ITERATOR_KEYWORDS);
}

HaxePropertyAccessor propertyAccessor = PsiTreeUtil.getParentOfType(position, HaxePropertyAccessor.class);
if (isPropertyGetterValue.accepts(propertyAccessor)) {
result.stopHere();
lookupElements.clear();
addKeywords(lookupElements, PROPERTY_KEYWORDS, 1.1f);
addKeywords(lookupElements, Set.of(keywordOnly(PROPERTY_GET)), 1.2f);
}
if (isPropertySetterValue.accepts(propertyAccessor)) {
result.stopHere();
lookupElements.clear();
addKeywords(lookupElements, PROPERTY_KEYWORDS, 1.1f);
addKeywords(lookupElements, Set.of(keywordOnly(PROPERTY_SET)), 1.2f);
}

addKeywords(lookupElements, PP_KEYWORDS, -0.2f);
addKeywords(lookupElements, MISC_KEYWORDS, -0.1f);

addKeywords(lookupElements, PP_KEYWORDS, -0.2f);
addKeywords(lookupElements, MISC_KEYWORDS, -0.1f);

}
result.addAllElements(lookupElements);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.intellij.patterns.PsiElementPattern;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;

import static com.intellij.patterns.PlatformPatterns.psiElement;
import static com.intellij.patterns.StandardPatterns.string;
Expand Down Expand Up @@ -83,4 +84,18 @@ public class HaxeKeywordCompletionPatterns {
.afterSiblingSkipping(psiElement().whitespaceCommentEmptyOrError(), psiElement(HaxeIfStatement.class));
public static final PsiElementPattern.Capture<PsiElement> dotFromIterator =psiElement().afterSiblingSkipping(psiElement().whitespaceCommentEmptyOrError(), psiElement(
HaxeIterable.class));

// must use afterLeafSkipping as afterLeaf skips whitespaceCommentEmptyOrError by default
public static final PsiElementPattern.Capture<PsiElement> allowLookupPattern = psiElement()
.afterLeafSkipping(psiElement(PsiErrorElement.class), psiElement()
.andOr(
psiElement().whitespaceCommentEmptyOrError(),
psiElement().withElementType(PLBRACK),
psiElement().withElementType(PLCURLY),
psiElement().withElementType(PLPAREN),
psiElement().withElementType(PRBRACK),
psiElement().withElementType(PRCURLY),
psiElement().withElementType(PRPAREN),
psiElement().withElementType(OSEMI)
));
}

0 comments on commit ecf5d9e

Please sign in to comment.