Search in sources :

Example 1 with JSPsiElementBase

use of com.intellij.lang.javascript.psi.JSPsiElementBase in project intellij-plugins by JetBrains.

the class AngularJSReferenceExpressionResolver method resolve.

@NotNull
@Override
public ResolveResult[] resolve(@NotNull JSReferenceExpressionImpl expression, boolean incompleteCode) {
    if (myReferencedName == null)
        return ResolveResult.EMPTY_ARRAY;
    if (myRef.getParent() instanceof JSDefinitionExpression) {
        final PsiElement sibling = PsiTreeUtil.prevVisibleLeaf(myRef);
        if (sibling != null && sibling.getNode().getElementType() == JSTokenTypes.LET_KEYWORD) {
            return new JSResolveResult[] { new JSResolveResult(myRef) };
        }
        final AngularJSRepeatExpression repeat = PsiTreeUtil.getParentOfType(myRef, AngularJSRepeatExpression.class);
        if (repeat != null) {
            for (JSDefinitionExpression def : repeat.getDefinitions()) {
                if (PsiTreeUtil.isAncestor(def, myRef, true))
                    return new JSResolveResult[] { new JSResolveResult(myRef) };
            }
        }
        final AngularJSAsExpression as = PsiTreeUtil.getParentOfType(myRef, AngularJSAsExpression.class);
        if (as != null) {
            if (PsiTreeUtil.isAncestor(as.getDefinition(), myRef, true))
                return new JSResolveResult[] { new JSResolveResult(myRef) };
        }
        JSClass clazz = myRef.getQualifier() == null ? AngularJS2IndexingHandler.findDirectiveClass(myRef) : null;
        if (clazz != null && DialectDetector.isTypeScript(clazz)) {
            final TypeScriptResolveProcessor localProcessor = new TypeScriptResolveProcessor(myReferencedName, myContainingFile, myRef, incompleteCode);
            localProcessor.setToProcessHierarchy(true);
            JSReferenceExpressionImpl.doProcessLocalDeclarations(clazz, myQualifier, localProcessor, false, false, null);
            return localProcessor.getResultsAsResolveResults();
        }
    }
    if (AngularJSAsExpression.isAsControllerRef(myRef, myRef.getParent())) {
        final PsiElement resolve = AngularIndexUtil.resolve(myParent.getProject(), AngularControllerIndex.KEY, myReferencedName);
        if (resolve != null) {
            return new JSResolveResult[] { new JSResolveResult(resolve) };
        }
    } else if (AngularJSFilterExpression.isFilterNameRef(myRef, myParent)) {
        final PsiElement resolve = AngularIndexUtil.resolve(myParent.getProject(), AngularFilterIndex.KEY, myReferencedName);
        if (resolve != null) {
            return new JSResolveResult[] { new JSResolveResult(resolve) };
        }
    } else if (myQualifier == null) {
        final Collection<JSPsiElementBase> localVariables = getItemsByName(myReferencedName, myRef);
        if (!localVariables.isEmpty()) {
            return ContainerUtil.map2Array(localVariables, JSResolveResult.class, item -> new JSResolveResult(item));
        }
    }
    return super.resolve(expression, incompleteCode);
}
Also used : JSPsiElementBase(com.intellij.lang.javascript.psi.JSPsiElementBase) AngularJSRepeatExpression(org.angularjs.lang.psi.AngularJSRepeatExpression) JSDefinitionExpression(com.intellij.lang.javascript.psi.JSDefinitionExpression) JSResolveResult(com.intellij.lang.javascript.psi.resolve.JSResolveResult) TypeScriptResolveProcessor(com.intellij.lang.javascript.ecmascript6.TypeScriptResolveProcessor) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) AngularJSAsExpression(org.angularjs.lang.psi.AngularJSAsExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with JSPsiElementBase

use of com.intellij.lang.javascript.psi.JSPsiElementBase in project intellij-plugins by JetBrains.

the class AngularJSProcessor method process.

public static void process(final PsiElement element, final Consumer<JSPsiElementBase> consumer) {
    final PsiElement original = CompletionUtil.getOriginalOrSelf(element);
    PsiFile hostFile = FileContextUtil.getContextFile(original != element ? original : element.getContainingFile().getOriginalFile());
    if (!(hostFile instanceof XmlFile)) {
        hostFile = original.getContainingFile();
    }
    if (!(hostFile instanceof XmlFile))
        return;
    final XmlFile file = (XmlFile) hostFile;
    final Collection<JSPsiElementBase> cache = CachedValuesManager.getCachedValue(file, () -> {
        final Collection<JSPsiElementBase> result = new ArrayList<>();
        processDocument(file.getDocument(), result);
        return CachedValueProvider.Result.create(result, PsiModificationTracker.MODIFICATION_COUNT);
    });
    for (JSPsiElementBase namedElement : cache) {
        if (scopeMatches(original, namedElement)) {
            consumer.consume(namedElement);
        }
    }
}
Also used : JSPsiElementBase(com.intellij.lang.javascript.psi.JSPsiElementBase) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 3 with JSPsiElementBase

use of com.intellij.lang.javascript.psi.JSPsiElementBase in project intellij-plugins by JetBrains.

the class ActionScriptGenerateToStringHandler method createFix.

protected BaseCreateMethodsFix createFix(final JSClass jsClass) {
    return new BaseCreateMethodsFix<JSVariable>(jsClass) {

        @Override
        public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
            evalAnchor(editor, file);
            final boolean[] needOverride = new boolean[1];
            JSInheritanceUtil.processOverrides(jsClass, new JSOverrideHandler() {

                public boolean process(@NotNull List<JSPsiElementBase> elements, final PsiElement scope, final String className) {
                    needOverride[0] = !"Object".equals(className);
                    return false;
                }
            }, "toString", null, myJsClass, true);
            @NonNls String functionText = "public " + (needOverride[0] ? "override " : "") + "function toString():String {\nreturn " + (needOverride[0] ? "super.toString()" : "\"" + jsClass.getName());
            final String semicolon = JSCodeStyleSettings.getSemicolon(file);
            boolean first = true;
            final Set<JSVariable> jsVariables = getElementsToProcess();
            if (!jsVariables.isEmpty()) {
                functionText += needOverride[0] ? " + \"{" : "{";
                for (JSVariable var : jsVariables) {
                    if (!first) {
                        functionText += " + \",";
                    }
                    first = false;
                    functionText += var.getName() + "=\" + String(" + var.getName() + ")";
                }
                functionText += "+\"}\"";
            } else {
                if (!needOverride[0])
                    functionText += "\"";
            }
            functionText += semicolon + "\n}";
            doAddOneMethod(project, functionText, anchor);
        }
    };
}
Also used : NonNls(org.jetbrains.annotations.NonNls) JSOverrideHandler(com.intellij.lang.javascript.psi.resolve.JSOverrideHandler) JSVariable(com.intellij.lang.javascript.psi.JSVariable) NotNull(org.jetbrains.annotations.NotNull) BaseCreateMethodsFix(com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix) Project(com.intellij.openapi.project.Project) JSPsiElementBase(com.intellij.lang.javascript.psi.JSPsiElementBase) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Aggregations

JSPsiElementBase (com.intellij.lang.javascript.psi.JSPsiElementBase)3 PsiElement (com.intellij.psi.PsiElement)3 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 TypeScriptResolveProcessor (com.intellij.lang.javascript.ecmascript6.TypeScriptResolveProcessor)1 JSDefinitionExpression (com.intellij.lang.javascript.psi.JSDefinitionExpression)1 JSVariable (com.intellij.lang.javascript.psi.JSVariable)1 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)1 JSOverrideHandler (com.intellij.lang.javascript.psi.resolve.JSOverrideHandler)1 JSResolveResult (com.intellij.lang.javascript.psi.resolve.JSResolveResult)1 BaseCreateMethodsFix (com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 ArrayList (java.util.ArrayList)1 AngularJSAsExpression (org.angularjs.lang.psi.AngularJSAsExpression)1 AngularJSRepeatExpression (org.angularjs.lang.psi.AngularJSRepeatExpression)1 NonNls (org.jetbrains.annotations.NonNls)1