private static Collection suggestKeywords()

in src/org/intellij/jflex/editor/JFlexCompletionContributor.java [74:106]


  private static Collection<String> suggestKeywords(PsiElement position) {
    if (position instanceof LeafPsiElement && ((LeafPsiElement)position).getElementType() == FLEX_STRING) return Collections.emptySet();
    if (PsiTreeUtil.getParentOfType(position, JFlexUserCodeSection.class) != null) return Collections.emptySet();
    boolean inDeclare = PsiTreeUtil.getParentOfType(position, JFlexDeclarationsSection.class) != null;
    PsiFile flexFile = position.getContainingFile();
    Language language = flexFile.getLanguage();
    String flexFileText = flexFile.getText();
    int positionOffset = position.getTextRange().getEndOffset();

    JFlexExpression expr = PsiTreeUtil.getParentOfType(position, JFlexExpression.class);
    boolean inMacro = expr != null && expr.getText().substring(0, positionOffset - expr.getTextRange().getStartOffset()).indexOf('\n') == -1;

    String fragment = (inDeclare ? "%%\n" : "%%\n%%\n") + flexFileText.substring(flexFileText.lastIndexOf("%%", positionOffset-1) + 2, positionOffset);
    boolean empty = StringUtil.isEmptyOrSpaces(fragment);
    String text = empty ? CompletionInitializationContext.DUMMY_IDENTIFIER : fragment;
    int completionOffset = empty ? 0 : fragment.length();
    PsiFile file = PsiFileFactory.getInstance(flexFile.getProject()).createFileFromText("a.flex", language, text, true, false);
    GeneratedParserUtilBase.CompletionState state = new GeneratedParserUtilBase.CompletionState(completionOffset) {
      @Override
      public @Nullable String convertItem(Object o) {
        if (o == null) return null;
        if (o instanceof IElementType[]) return super.convertItem(o);
        if (o == FLEX_ID || o == FLEX_CHAR || o == FLEX_STRING ||
            o == FLEX_NUMBER || o == FLEX_RAW || o == FLEX_VERSION) return null;
        String text = o.toString();
        return text.length() == 1 || inMacro && text.startsWith("%") || !inMacro && text.startsWith("[") ?
               null : text;
      }
    };
    file.putUserData(GeneratedParserUtilBase.COMPLETION_STATE_KEY, state);
    TreeUtil.ensureParsed(file.getNode());
    return state.items;
  }