|
| 1 | +package org.ca65; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.completion.*; |
| 4 | +import com.intellij.codeInsight.lookup.LookupElementBuilder; |
| 5 | +import com.intellij.psi.PsiNamedElement; |
| 6 | +import com.intellij.psi.util.PsiTreeUtil; |
| 7 | +import com.intellij.util.ProcessingContext; |
| 8 | +import org.ca65.psi.AsmTypes; |
| 9 | +import org.jetbrains.annotations.NotNull; |
| 10 | + |
| 11 | +import static com.intellij.patterns.PlatformPatterns.psiElement; |
| 12 | + |
| 13 | +public class AsmIdentifierCompletionContributor extends CompletionContributor { |
| 14 | + public AsmIdentifierCompletionContributor() { |
| 15 | + // Offer to auto-complete identifiers after a mnemonic |
| 16 | + extend(CompletionType.BASIC, psiElement().afterLeaf(psiElement(AsmTypes.MNEMONIC)), new LabelCompletionProvider()); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class LabelCompletionProvider extends CompletionProvider<CompletionParameters> { |
| 21 | + public void addCompletions(@NotNull CompletionParameters parameters, |
| 22 | + @NotNull ProcessingContext context, |
| 23 | + @NotNull CompletionResultSet resultSet) { |
| 24 | + PsiNamedElement[] namedElements = PsiTreeUtil.getChildrenOfType(parameters.getOriginalFile(), PsiNamedElement.class); |
| 25 | + if (namedElements != null) { |
| 26 | + for (PsiNamedElement namedElement : namedElements) { |
| 27 | + String elementName = namedElement.getName(); |
| 28 | + if (elementName != null) { |
| 29 | + if (!namedElement.getText().startsWith("@")) { |
| 30 | + resultSet.addElement(LookupElementBuilder.createWithIcon(namedElement)); |
| 31 | + } else { |
| 32 | + // Present the label name, but complete with '@' prefix. |
| 33 | + resultSet.addElement(LookupElementBuilder.create( |
| 34 | + "@" + namedElement.getName()) |
| 35 | + .withPsiElement(namedElement) |
| 36 | + .withPresentableText(namedElement.getName()) |
| 37 | + .withIcon(namedElement.getIcon(0)) |
| 38 | + ); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments