Search in sources :

Example 1 with TrailingPatternConsumer

use of ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer in project oxy-template-support-plugin by mutant-industries.

the class JavaContext method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    Module module;
    PsiElement element = parameters.getOriginalFile().getViewProvider().findElementAt(parameters.getOffset() - 1, OxyTemplateInnerJs.INSTANCE);
    if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT && (element = element.getPrevSibling()) == null) {
        return;
    }
    if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
        element = element.getPrevSibling();
    }
    while (element != null && element.getNode().getElementType() != JSTokenTypes.DOT) {
        element = element.getLastChild();
        if (element instanceof PsiErrorElement) {
            element = element.getPrevSibling();
        }
    }
    if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT || (element = element.getPrevSibling()) == null) {
        return;
    }
    if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
        element = element.getPrevSibling();
    }
    if (!(element instanceof JSExpression) || (module = ModuleUtilCore.findModuleForPsiElement(parameters.getOriginalFile())) == null) {
        return;
    }
    JSSimpleTypeProcessor typeProcessor = new JSSimpleTypeProcessor();
    JSTypeEvaluator.evaluateTypes((JSExpression) element, parameters.getOriginalFile().getViewProvider().getPsi(OxyTemplateInnerJs.INSTANCE), typeProcessor);
    JSType type = typeProcessor.getType();
    List<String> possibleJavaTypes = new LinkedList<>();
    if (type != null) {
        if (type instanceof JSCompositeTypeImpl) {
            possibleJavaTypes.addAll(((JSCompositeTypeImpl) type).getTypes().stream().filter(jsType -> jsType instanceof JSTypeImpl).map(JSType::getTypeText).collect(Collectors.toList()));
        } else if (type instanceof JSTypeImpl) {
            possibleJavaTypes.add(type.getTypeText());
        }
    }
    JavaPsiFacade facade = JavaPsiFacade.getInstance(parameters.getOriginalFile().getProject());
    GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
    List<PsiClass> possibleInstancesOf = new LinkedList<>();
    boolean suggestionsFound = false;
    PsiClass aClass;
    for (String possibleType : possibleJavaTypes) {
        if ((aClass = facade.findClass(possibleType, scope)) != null) {
            possibleInstancesOf.add(aClass);
            possibleInstancesOf.addAll(ExtenderProvider.getExtenders(aClass));
        }
    }
    for (PsiClass psiClass : possibleInstancesOf) {
        List<String> alreadySuggested = new LinkedList<>();
        for (PsiMethod method : psiClass.getAllMethods()) {
            if (method.getContainingClass() == null || Object.class.getName().equals(method.getContainingClass().getQualifiedName()) || method.getReturnType() == null || shippedBaseExtenderMethods.contains(method.getName()) || !method.getModifierList().hasModifierProperty(PsiModifier.PUBLIC) || InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_INTERFACE_FQN) && !InheritanceUtil.isInheritor(method.getContainingClass(), EXTENDER_BASE_CLASS_FQN)) {
                continue;
            }
            String insertText = method.getName();
            String presentableText = method.getName();
            if (insertText.matches("((^is)|(^get)|(^set))[A-Z].*")) {
                insertText = presentableText = StringUtils.decapitalize(insertText.replaceFirst("(^is)|(^get)|(^set)", ""));
            } else {
                presentableText = method.getPresentation() == null ? presentableText + "()" : method.getPresentation().getPresentableText();
                insertText += "()";
            }
            if (alreadySuggested.contains(insertText)) {
                continue;
            }
            result.consume(LookupElementBuilder.create(method, insertText).withIcon(method.getIcon(0)).withTypeText(method.getReturnType().getPresentableText(), true).withTailText(" (" + psiClass.getContainingFile().getName() + ")", true).withPresentableText(presentableText).withInsertHandler(new TrailingPatternConsumer(INSERT_CONSUME)).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE));
            alreadySuggested.add(insertText);
            suggestionsFound = true;
        }
    }
    if (suggestionsFound) {
        result.stopHere();
    }
}
Also used : Arrays(java.util.Arrays) PsiErrorElement(com.intellij.psi.PsiErrorElement) JSTypeEvaluator(com.intellij.lang.javascript.psi.resolve.JSTypeEvaluator) StringUtils(com.sun.xml.internal.ws.util.StringUtils) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) CompletionParameters(com.intellij.codeInsight.completion.CompletionParameters) JSCompositeTypeImpl(com.intellij.lang.javascript.psi.types.JSCompositeTypeImpl) Extender(ool.web.model.Extender) TokenType(com.intellij.psi.TokenType) PsiModifier(com.intellij.psi.PsiModifier) CompletionResultSet(com.intellij.codeInsight.completion.CompletionResultSet) CompletionContributor(com.intellij.codeInsight.completion.CompletionContributor) PsiClass(com.intellij.psi.PsiClass) JSTokenTypes(com.intellij.lang.javascript.JSTokenTypes) PsiElement(com.intellij.psi.PsiElement) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) JSExpression(com.intellij.lang.javascript.psi.JSExpression) JSType(com.intellij.lang.javascript.psi.JSType) LinkedList(java.util.LinkedList) Module(com.intellij.openapi.module.Module) OxyTemplateInnerJs(ool.intellij.plugin.lang.OxyTemplateInnerJs) JSSimpleTypeProcessor(com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) PsiMethod(com.intellij.psi.PsiMethod) ExtenderProvider(ool.intellij.plugin.psi.reference.innerjs.ExtenderProvider) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Collectors(java.util.stream.Collectors) AutoCompletionPolicy(com.intellij.codeInsight.lookup.AutoCompletionPolicy) List(java.util.List) TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) Pattern(java.util.regex.Pattern) BaseExtender(ool.web.model.BaseExtender) NotNull(org.jetbrains.annotations.NotNull) JSTypeImpl(com.intellij.lang.javascript.psi.types.JSTypeImpl) JavaPsiFacade(com.intellij.psi.JavaPsiFacade) JSType(com.intellij.lang.javascript.psi.JSType) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) JSExpression(com.intellij.lang.javascript.psi.JSExpression) LinkedList(java.util.LinkedList) PsiErrorElement(com.intellij.psi.PsiErrorElement) JSTypeImpl(com.intellij.lang.javascript.psi.types.JSTypeImpl) JSCompositeTypeImpl(com.intellij.lang.javascript.psi.types.JSCompositeTypeImpl) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) JSSimpleTypeProcessor(com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 2 with TrailingPatternConsumer

use of ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer in project oxy-template-support-plugin by mutant-industries.

the class JsGlobalVariable method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiElement psiElement = parameters.getPosition();
    if (psiElement.getNode().getElementType() != JSTokenTypes.IDENTIFIER || !InnerJsReferenceExpressionResolver.isGlobalVariableSuspect(psiElement)) {
        return;
    }
    for (GlobalVariableDefinition variable : GlobalVariableIndex.getGlobals(parameters.getOriginalFile().getProject()).values()) {
        assert variable.getName() != null;
        String typeText = (variable.getType() == null || variable.getName().equals(GlobalVariableTypeProvider.CONTROLLERS_GLOBAL_VARIABLE_NAME)) ? "" : variable.getType().getTypeText().replaceFirst("^.+\\.", "");
        result.consume(LookupElementBuilder.create(variable.getName()).withInsertHandler(new TrailingPatternConsumer(INSERT_CONSUME)).withTailText(" (" + variable.getContainingFile().getName() + ")", true).withTypeText(typeText, true).withIcon(OxyTemplateFileType.INSTANCE.getIcon()).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE));
    }
}
Also used : TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) GlobalVariableDefinition(ool.intellij.plugin.psi.reference.innerjs.globals.GlobalVariableDefinition) PsiElement(com.intellij.psi.PsiElement)

Example 3 with TrailingPatternConsumer

use of ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer in project oxy-template-support-plugin by mutant-industries.

the class UnclosedMacroTag method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    MacroName elementAt;
    if (parameters.getPosition().getNode().getElementType() != OxyTemplateTypes.T_MACRO_NAME || (elementAt = PsiTreeUtil.getParentOfType(parameters.getPosition(), MacroName.class)) == null || elementAt.getPrevSibling().getPrevSibling().getNode().getElementType() != OxyTemplateTypes.T_XML_CLOSE_TAG_START) {
        return;
    }
    String macroTagToBeClosedName = OxyTemplateHelper.getPreviousUnclosedMacroTagName(elementAt.getPrevSibling());
    if (macroTagToBeClosedName != null) {
        result.withPrefixMatcher(new CamelHumpMatcher(elementAt.getText().replace(CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED, ""))).consume(LookupElementBuilder.create(macroTagToBeClosedName + ">").withPresentableText("m:" + macroTagToBeClosedName).withInsertHandler(new TrailingPatternConsumer(INSERT_CONSUME)).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE));
    }
}
Also used : TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) MacroName(ool.intellij.plugin.psi.MacroName) CamelHumpMatcher(com.intellij.codeInsight.completion.impl.CamelHumpMatcher)

Example 4 with TrailingPatternConsumer

use of ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer in project oxy-template-support-plugin by mutant-industries.

the class JsMacroName method addMacroNameCompletionVariant.

@Override
public boolean addMacroNameCompletionVariant(@NotNull CompletionResultSet result, @NotNull PsiElement position, @NotNull PsiElement lookupElement, @NotNull String fqn, @NotNull String macroName, @NotNull String macroNamespace) {
    result.withPrefixMatcher(new CamelHumpMatcher(namespaceFqn + position.getText().replace(CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED, ""))).consume(LookupElementDecorator.withInsertHandler(LookupElementBuilder.create(lookupElement, fqn + "();").withIcon(OxyTemplateFileType.INSTANCE.getIcon()).withPresentableText(macroName + "([Object] params) (" + macroNamespace + ")").withTailText(" " + lookupElement.getContainingFile().getVirtualFile().getPath().replaceFirst("^.+((src)|(WEB_INF))/", ""), true).withInsertHandler(new TrailingPatternConsumer(INSERT_CONSUME) {

        @Override
        public void handleInsert(InsertionContext context, LookupElement item) {
            CaretModel caretModel = context.getEditor().getCaretModel();
            caretModel.moveToOffset(caretModel.getOffset() - 2);
            super.handleInsert(context, item);
        }
    }).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE), new IncludeAutoInsert()));
    return fqn.startsWith(namespaceFqn.substring(0, Math.max(namespaceFqn.length(), namespaceFqn.indexOf("."))));
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) IncludeAutoInsert(ool.intellij.plugin.editor.completion.handler.IncludeAutoInsert) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) CamelHumpMatcher(com.intellij.codeInsight.completion.impl.CamelHumpMatcher)

Example 5 with TrailingPatternConsumer

use of ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer in project oxy-template-support-plugin by mutant-industries.

the class DwrMethod method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiElement element = parameters.getPosition();
    if (element.getNode().getElementType() != JSTokenTypes.IDENTIFIER || (element = element.getPrevSibling()) == null) {
        return;
    }
    if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
        element = element.getPrevSibling();
    }
    if (element == null || element.getNode().getElementType() != JSTokenTypes.DOT || (element = element.getPrevSibling()) == null) {
        return;
    }
    if (element.getNode().getElementType() == TokenType.WHITE_SPACE) {
        element = element.getPrevSibling();
    }
    if (!(element instanceof JSReferenceExpression) || element.getPrevSibling() != null) {
        return;
    }
    PsiReference reference = element.getReference();
    if (reference == null || !((element = reference.resolve()) instanceof PsiClass) || !DwrReferenceResolver.isDwrClass((PsiClass) element)) {
        return;
    }
    PsiClass dwrClass = (PsiClass) element;
    for (PsiMethod method : dwrClass.getMethods()) {
        if (method.getReturnType() == null || !DwrReferenceResolver.isDwrMethod(method)) {
            continue;
        }
        String presentableText = method.getName() + "()";
        if (method.getPresentation() != null && method.getPresentation().getPresentableText() != null) {
            presentableText = method.getPresentation().getPresentableText().replaceAll("(, )?HttpServlet(Request|Response)", "");
        }
        result.consume(LookupElementBuilder.create(method, method.getName() + "();").withIcon(method.getIcon(0)).withTypeText(method.getReturnType().getPresentableText(), true).withPresentableText(presentableText).withInsertHandler(new TrailingPatternConsumer(INSERT_CONSUME) {

            @Override
            public void handleInsert(InsertionContext context, LookupElement item) {
                CaretModel caretModel = context.getEditor().getCaretModel();
                caretModel.moveToOffset(caretModel.getOffset() - 2);
                super.handleInsert(context, item);
            }
        }).withAutoCompletionPolicy(AutoCompletionPolicy.GIVE_CHANCE_TO_OVERWRITE));
    }
    result.stopHere();
}
Also used : JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) CaretModel(com.intellij.openapi.editor.CaretModel) PsiMethod(com.intellij.psi.PsiMethod) TrailingPatternConsumer(ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer) PsiClass(com.intellij.psi.PsiClass) PsiReference(com.intellij.psi.PsiReference) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

TrailingPatternConsumer (ool.intellij.plugin.editor.completion.handler.TrailingPatternConsumer)5 PsiElement (com.intellij.psi.PsiElement)3 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)2 CamelHumpMatcher (com.intellij.codeInsight.completion.impl.CamelHumpMatcher)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 CaretModel (com.intellij.openapi.editor.CaretModel)2 PsiClass (com.intellij.psi.PsiClass)2 PsiMethod (com.intellij.psi.PsiMethod)2 CompletionContributor (com.intellij.codeInsight.completion.CompletionContributor)1 CompletionParameters (com.intellij.codeInsight.completion.CompletionParameters)1 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)1 AutoCompletionPolicy (com.intellij.codeInsight.lookup.AutoCompletionPolicy)1 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 JSTokenTypes (com.intellij.lang.javascript.JSTokenTypes)1 JSExpression (com.intellij.lang.javascript.psi.JSExpression)1 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)1 JSType (com.intellij.lang.javascript.psi.JSType)1 JSSimpleTypeProcessor (com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor)1 JSTypeEvaluator (com.intellij.lang.javascript.psi.resolve.JSTypeEvaluator)1 JSCompositeTypeImpl (com.intellij.lang.javascript.psi.types.JSCompositeTypeImpl)1