|
| 1 | +package technology.svelte.completion; |
| 2 | + |
| 3 | +import com.intellij.codeInsight.completion.CompletionParameters; |
| 4 | +import com.intellij.codeInsight.completion.CompletionProvider; |
| 5 | +import com.intellij.codeInsight.completion.CompletionResultSet; |
| 6 | +import com.intellij.codeInsight.lookup.LookupElementBuilder; |
| 7 | +import com.intellij.psi.PsiElement; |
| 8 | +import com.intellij.psi.PsiWhiteSpace; |
| 9 | +import com.intellij.psi.xml.XmlAttribute; |
| 10 | +import com.intellij.psi.xml.XmlElement; |
| 11 | +import com.intellij.util.ProcessingContext; |
| 12 | +import technology.svelte.lexer.SvelteTokenTypes; |
| 13 | +import technology.svelte.psi.impl.MacroNodeImpl; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +/** |
| 19 | + * Provides keywords to be auto-completed |
| 20 | + */ |
| 21 | +public class KeywordCompletionProvider extends CompletionProvider<CompletionParameters> { |
| 22 | + // constant lists |
| 23 | + private static final String[] KEYWORDS = { |
| 24 | + // CoreMacros.php |
| 25 | + "if", "ifset", "for", "foreach", "while", "first", "last", "sep", "class", "attr", "captute", |
| 26 | + "var", "default", "dump", "debugbreak", "l", "r", |
| 27 | + |
| 28 | + // FormMacros.php |
| 29 | + "form", "input", "label", "formContainer", |
| 30 | + |
| 31 | + // UIMacros.php |
| 32 | + "include", "extends", "block", "define", "snippet", "widget", "control", "href", "link", "plink", "contentType", "status", |
| 33 | + }; |
| 34 | + |
| 35 | + private static final List<String> PAIR_MACROS = Arrays.asList( |
| 36 | + "if", "ifset", "for", "foreach", "while", "first", "last", "sep" |
| 37 | + ); |
| 38 | + |
| 39 | + private static final String[] FILTERS = { |
| 40 | + // static |
| 41 | + "normalize", "toascii", "webalize", "truncate", "lower", "upper", "firstupper", "capitalize", "trim", "padleft", "padright", |
| 42 | + "replacere", "url", "striptags", "nl2br", "substr", "repeat", "implode", "number", |
| 43 | + |
| 44 | + // methods in Helpers.php |
| 45 | + "espaceHtml", "escapeHtmlComment", "escapeXML", "escapeCss", "escapeJs", "strip", "indent", "date", "bytes", |
| 46 | + "length", "replace", "dataStream", "null", |
| 47 | + }; |
| 48 | + |
| 49 | + |
| 50 | + // CompletionResultSet wants list of LookupElements |
| 51 | + private List<LookupElementBuilder> KEYWORD_LOOKUPS = new ArrayList(); |
| 52 | + private List<LookupElementBuilder> ATTR_LOOKUPS = new ArrayList(); |
| 53 | + private List<LookupElementBuilder> FILTER_LOOKUPS = new ArrayList(); |
| 54 | + private HashMap<String, LookupElementBuilder> CLOSING_LOOKUPS = new HashMap<String, LookupElementBuilder>(); |
| 55 | + |
| 56 | + public KeywordCompletionProvider() { |
| 57 | + super(); |
| 58 | + |
| 59 | + for(String keyword: KEYWORDS) { |
| 60 | + KEYWORD_LOOKUPS.add(LookupElementBuilder.create(keyword)); |
| 61 | + ATTR_LOOKUPS.add(LookupElementBuilder.create("n:" + keyword)); |
| 62 | + } |
| 63 | + for(String filter: FILTERS) FILTER_LOOKUPS.add(LookupElementBuilder.create(filter)); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void addCompletions(@NotNull CompletionParameters params, |
| 68 | + ProcessingContext ctx, |
| 69 | + @NotNull CompletionResultSet results) { |
| 70 | + |
| 71 | + PsiElement curr = params.getPosition().getOriginalElement(); |
| 72 | + |
| 73 | + // n: attributes |
| 74 | + if(curr.getParent() instanceof XmlAttribute) { |
| 75 | + for(LookupElementBuilder x: ATTR_LOOKUPS) results.addElement(x); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + // Keywords |
| 80 | + if(curr.getNode().getElementType() == SvelteTokenTypes.MACRO_NAME) { |
| 81 | + for(LookupElementBuilder x: KEYWORD_LOOKUPS) results.addElement(x); |
| 82 | + |
| 83 | + HashSet<String> openedMacros = new HashSet<String>(); // macros which are opened before (should complete closing) |
| 84 | + HashSet<String> closedMacros = new HashSet<String>(); // which are closed (should not complete closing) |
| 85 | + |
| 86 | + // Go up and find open keywords (and offer them for closing) |
| 87 | + PsiElement cursor = curr; |
| 88 | + while(cursor != null && (cursor.getPrevSibling() != null || cursor.getParent() != null)) { |
| 89 | + // macro found {xx} found |
| 90 | + if(cursor instanceof MacroNodeImpl) { |
| 91 | + MacroNodeImpl node = (MacroNodeImpl) cursor; |
| 92 | + String name = node.getMacroName(); |
| 93 | + |
| 94 | + if(name.charAt(0) == '/') { // closing macro |
| 95 | + closedMacros.add(name.substring(1)); |
| 96 | + } else if(PAIR_MACROS.contains(name)) { |
| 97 | + if(closedMacros.contains(name)) closedMacros.remove(name); // is closed later |
| 98 | + else openedMacros.add(name); // not closed, let us close it |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + PsiElement tmp; |
| 104 | + if((tmp = cursor.getPrevSibling()) != null) cursor = tmp; // Go prev if possible... |
| 105 | + else cursor = cursor.getParent(); // ... or up |
| 106 | + } |
| 107 | + |
| 108 | + for(String name: openedMacros) { |
| 109 | + if(name.equals("if")) { |
| 110 | + results.addElement(reuseClosingTag("else")); |
| 111 | + results.addElement(reuseClosingTag("elseif")); |
| 112 | + } |
| 113 | + results.addElement(reuseClosingTag("/" + name)); |
| 114 | + } |
| 115 | + |
| 116 | + results.stopHere(); |
| 117 | + } |
| 118 | + |
| 119 | + // Modifiers (if after pipe) |
| 120 | + PsiElement prev = curr.getPrevSibling(); |
| 121 | + if(prev != null && prev instanceof PsiWhiteSpace) prev = prev.getPrevSibling(); |
| 122 | + if(prev != null && prev.getNode().getElementType() == SvelteTokenTypes.MODIFIER) { |
| 123 | + for(LookupElementBuilder x: FILTER_LOOKUPS) results.addElement(x); |
| 124 | + results.stopHere(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Get closing tag |
| 129 | + private LookupElementBuilder reuseClosingTag(String name) { |
| 130 | + if(!CLOSING_LOOKUPS.containsKey(name)) { |
| 131 | + CLOSING_LOOKUPS.put(name, LookupElementBuilder.create(name)); |
| 132 | + } |
| 133 | + |
| 134 | + return CLOSING_LOOKUPS.get(name); |
| 135 | + } |
| 136 | +} |
0 commit comments