Search in sources :

Example 1 with JSPackageStatement

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

the class JSImplicitlyInternalDeclarationInspection method process.

private static void process(final JSNamedElement node, final ProblemsHolder holder) {
    if (!DialectDetector.isActionScript(node))
        return;
    JSFunction fun = PsiTreeUtil.getParentOfType(node, JSFunction.class);
    if (fun != null)
        return;
    ASTNode nameIdentifier = node.findNameIdentifier();
    if (nameIdentifier == null)
        return;
    JSClass clazz = JSResolveUtil.getClassOfContext(node);
    if (clazz == null) {
        PsiElement parent = JSResolveUtil.findParent(node);
        if (!(parent instanceof JSPackageStatement))
            return;
    }
    JSAttributeList attributeList = ((JSAttributeListOwner) node).getAttributeList();
    JSAttributeList.AccessType accessType = attributeList != null ? attributeList.getAccessType() : null;
    if (accessType == JSAttributeList.AccessType.PACKAGE_LOCAL && attributeList.findAccessTypeElement() == null && attributeList.getNamespaceElement() == null && !JSResolveUtil.isConstructorFunction(node)) {
        holder.registerProblem(nameIdentifier.getPsi(), FlexBundle.message("js.implicitly.internal.declaration.problem"), new LocalQuickFix() {

            @NotNull
            @Override
            public String getFamilyName() {
                return FlexBundle.message("js.implicitly.internal.declaration.problem.add.internal.fix");
            }

            @Override
            public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
                PsiElement anchor = descriptor.getPsiElement();
                JSChangeVisibilityUtil.setVisibility((JSAttributeListOwner) anchor.getParent(), JSAttributeList.AccessType.PACKAGE_LOCAL);
            }
        });
    }
}
Also used : JSAttributeList(com.intellij.lang.javascript.psi.ecmal4.JSAttributeList) JSPackageStatement(com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) ASTNode(com.intellij.lang.ASTNode) JSAttributeListOwner(com.intellij.lang.javascript.psi.ecmal4.JSAttributeListOwner) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement)

Example 2 with JSPackageStatement

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

the class CreateJSSubclassIntention method invoke.

@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final JSClass jsClass = PsiTreeUtil.getParentOfType(element, JSClass.class);
    if (jsClass == null)
        return;
    final PsiElement parent = jsClass.getParent();
    if (!(parent instanceof JSPackageStatement))
        return;
    final JSPackageStatement jsPackageStatement = (JSPackageStatement) parent;
    final String defaultTemplateName = ActionScriptCreateClassOrInterfaceFix.ACTION_SCRIPT_CLASS_WITH_SUPERS_TEMPLATE_NAME;
    final String className;
    final String packageName;
    final String templateName;
    final PsiDirectory targetDirectory;
    final Collection<String> interfaces;
    final Map<String, Object> templateAttributes;
    final JSClass superClass;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        className = suggestSubclassName(jsClass.getName());
        packageName = "foo";
        templateName = defaultTemplateName;
        targetDirectory = WriteAction.compute(() -> ActionScriptCreateClassOrInterfaceFix.findOrCreateDirectory(packageName, jsPackageStatement));
        interfaces = jsClass.isInterface() ? Collections.singletonList(jsClass.getQualifiedName()) : Collections.emptyList();
        templateAttributes = Collections.emptyMap();
        superClass = jsClass.isInterface() ? null : jsClass;
    } else {
        CreateClassParameters p = ActionScriptCreateClassOrInterfaceFix.createAndShow(defaultTemplateName, jsClass, suggestSubclassName(jsClass.getName()), true, jsPackageStatement.getQualifiedName(), jsClass, JSBundle.message("new.actionscript.class.dialog.title"), () -> ActionScriptCreateClassOrInterfaceFix.getApplicableTemplates(ActionScriptCreateClassOrInterfaceFix.ACTIONSCRIPT_TEMPLATES_EXTENSIONS, project));
        if (p == null)
            return;
        className = p.getClassName();
        packageName = p.getPackageName();
        templateName = p.getTemplateName();
        targetDirectory = p.getTargetDirectory();
        superClass = ActionScriptCreateClassOrInterfaceFix.calcClass(p.getSuperclassFqn(), element);
        interfaces = p.getInterfacesFqns();
        templateAttributes = new HashMap<>(p.getTemplateAttributes());
    }
    JSClass createdClass = ActionScriptCreateClassOrInterfaceFix.createClass(templateName, className, packageName, superClass, interfaces, targetDirectory, getTitle(jsClass), true, templateAttributes, aClass -> {
        if (aClass != null && !aClass.isInterface() && (jsClass.isInterface() || !interfaces.isEmpty())) {
            new MyImplementMethodsHandler(aClass).execute();
        }
    });
    if (createdClass != null) {
        createdClass.navigate(true);
    }
}
Also used : CreateClassParameters(com.intellij.lang.javascript.validation.fixes.CreateClassParameters) JSPackageStatement(com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement) PsiDirectory(com.intellij.psi.PsiDirectory) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement)

Example 3 with JSPackageStatement

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

the class ActionScriptImportHandler method _resolveTypeName.

// TODO _str should be JSReferenceExpression for caching!
@Nullable
private JSImportedElementResolveResult _resolveTypeName(@Nullable final String _name, @NotNull PsiElement context) {
    String name = _name;
    if (name == null)
        return null;
    JSResolveUtil.GenericSignature genericSignature = JSResolveUtil.extractGenericSignature(name);
    if (genericSignature != null) {
        name = genericSignature.elementType;
    }
    final Ref<JSImportedElementResolveResult> resultRef = new Ref<>();
    final String name1 = name;
    JSResolveUtil.walkOverStructure(context, context1 -> {
        JSImportedElementResolveResult resolved = null;
        if (context1 instanceof XmlBackedJSClassImpl) {
            XmlTag rootTag = ((XmlBackedJSClassImpl) context1).getParent();
            if (rootTag != null && name1.equals(rootTag.getLocalName())) {
                final XmlElementDescriptor descriptor = rootTag.getDescriptor();
                PsiElement element = descriptor != null ? descriptor.getDeclaration() : null;
                if (element instanceof XmlFile) {
                    element = XmlBackedJSClassFactory.getXmlBackedClass((XmlFile) element);
                }
                final String s = element instanceof JSClass ? ((JSClass) element).getQualifiedName() : rootTag.getLocalName();
                resolved = new JSImportedElementResolveResult(s);
            } else {
                resolved = resolveTypeNameUsingImports(name1, context1);
            }
        } else if (context1 instanceof JSQualifiedNamedElement) {
            if (context1 instanceof JSClass && name1.equals(context1.getName())) {
                resolved = new JSImportedElementResolveResult(((JSQualifiedNamedElement) context1).getQualifiedName());
            } else {
                resolved = resolveTypeNameUsingImports(name1, context1);
                if (resolved == null && context1.getParent() instanceof JSFile) {
                    final String qName = ((JSQualifiedNamedElement) context1).getQualifiedName();
                    final String packageName = qName == null ? "" : context1 instanceof JSPackageStatement ? qName + "." : qName.substring(0, qName.lastIndexOf('.') + 1);
                    if (packageName.length() != 0) {
                        final PsiElement byQName = JSClassResolver.findClassFromNamespace(packageName + name1, context1);
                        if (byQName instanceof JSQualifiedNamedElement) {
                            resolved = new JSImportedElementResolveResult(((JSQualifiedNamedElement) byQName).getQualifiedName());
                        }
                    }
                }
            }
        } else {
            resolved = resolveTypeNameUsingImports(name1, context1);
            PsiElement contextOfContext;
            if (resolved == null && context1 instanceof JSFile && (contextOfContext = context1.getContext()) != null) {
                XmlBackedJSClassImpl clazz = contextOfContext instanceof XmlElement ? (XmlBackedJSClassImpl) XmlBackedJSClassImpl.getContainingComponent((XmlElement) contextOfContext) : null;
                if (clazz != null) {
                    SinkResolveProcessor r = new SinkResolveProcessor(name1, new ResolveResultSink(null, name1));
                    r.setForceImportsForPlace(true);
                    boolean b = clazz.doImportFromScripts(r, clazz);
                    if (!b) {
                        PsiElement resultFromProcessor = r.getResult();
                        JSQualifiedNamedElement clazzFromComponent = resultFromProcessor instanceof JSQualifiedNamedElement ? (JSQualifiedNamedElement) resultFromProcessor : null;
                        if (clazzFromComponent != null) {
                            resolved = new JSImportedElementResolveResult(clazzFromComponent.getQualifiedName(), clazz, null);
                        }
                    }
                }
            }
        }
        if (resolved != null) {
            resultRef.set(resolved);
            return false;
        }
        if (context1 instanceof JSPackageStatement)
            return false;
        return true;
    });
    JSImportedElementResolveResult result = resultRef.get();
    if (genericSignature != null && result != null) {
        // TODO: more than one type parameter
        StringBuilder genericSignatureBuffer = new StringBuilder();
        genericSignatureBuffer.append(".<");
        genericSignatureBuffer.append(resolveTypeName(genericSignature.genericType, context).getQualifiedName());
        genericSignatureBuffer.append(">");
        result = result.appendSignature(genericSignatureBuffer.toString());
    }
    return result;
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) XmlFile(com.intellij.psi.xml.XmlFile) JSPackageStatement(com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) Ref(com.intellij.openapi.util.Ref) XmlElement(com.intellij.psi.xml.XmlElement) JSFile(com.intellij.lang.javascript.psi.JSFile) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with JSPackageStatement

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

the class ActionScriptReferenceExpressionResolver method resolve.

@NotNull
@Override
public ResolveResult[] resolve(@NotNull JSReferenceExpressionImpl expression, boolean incompleteCode) {
    if (myReferencedName == null)
        return ResolveResult.EMPTY_ARRAY;
    PsiElement currentParent = JSResolveUtil.getTopReferenceParent(myParent);
    if (JSResolveUtil.isSelfReference(currentParent, myRef)) {
        if (!(currentParent instanceof JSPackageStatement) || myParent == currentParent) {
            return new ResolveResult[] { new JSResolveResult(currentParent) };
        }
    }
    if (isConditionalVariableReference(currentParent, myRef)) {
        if (ModuleUtilCore.findModuleForPsiElement(myRef) == null) {
            // do not red highlight conditional compiler definitions in 3rd party library/SDK source code
            return new ResolveResult[] { new JSResolveResult(myRef) };
        } else {
            return resolveConditionalCompilationVariable(myRef);
        }
    }
    if (myRef.isAttributeReference()) {
        return dummyResult(myRef);
    }
    if (JSCommonTypeNames.ANY_TYPE.equals(myReferencedName)) {
        if (currentParent instanceof JSImportStatement && myQualifier instanceof JSReferenceExpression)
            return ((JSReferenceExpression) myQualifier).multiResolve(false);
        if (myParent instanceof JSE4XNamespaceReference)
            return dummyResult(myRef);
    }
    // nonqualified items in implements list in mxml
    if (myParent instanceof JSReferenceListMember && myParent.getParent().getNode().getElementType() == JSStubElementTypes.IMPLEMENTS_LIST && myRef.getQualifier() == null && myContainingFile instanceof JSFile && XmlBackedJSClassImpl.isImplementsAttribute((JSFile) myContainingFile)) {
        PsiElement byQName = ActionScriptClassResolver.findClassByQNameStatic(myRef.getText(), myRef);
        // for some reason Flex compiler allows to implement non-public interface in default package, so let's not check access type here
        if (byQName != null)
            return new ResolveResult[] { new JSResolveResult(byQName) };
        return ResolveResult.EMPTY_ARRAY;
    }
    SinkResolveProcessor<ResolveResultSink> localProcessor;
    if (myLocalResolve) {
        final PsiElement topParent = JSResolveUtil.getTopReferenceParent(myParent);
        localProcessor = new SinkResolveProcessor<ResolveResultSink>(myReferencedName, myRef, new ResolveResultSink(myRef, myReferencedName)) {

            @Override
            public boolean needPackages() {
                if (myParent instanceof JSReferenceExpression && topParent instanceof JSImportStatement) {
                    return true;
                }
                return super.needPackages();
            }
        };
        localProcessor.setToProcessHierarchy(true);
        JSReferenceExpressionImpl.doProcessLocalDeclarations(myRef, myQualifier, localProcessor, true, false, null);
        PsiElement jsElement = localProcessor.getResult();
        if (myQualifier instanceof JSThisExpression && localProcessor.processingEncounteredAnyTypeAccess() && // this is from ecma script closure, proceed it in JavaScript way
        jsElement != null) {
            localProcessor.getResults().clear();
            jsElement = null;
        }
        if (myQualifier == null) {
            final JSReferenceExpression namespaceReference = JSReferenceExpressionImpl.getNamespaceReference(myRef);
            ResolveResult[] resolveResultsAsConditionalCompilationVariable = null;
            if (namespaceReference != null && (namespaceReference == myRef || namespaceReference.resolve() == namespaceReference)) {
                if (jsElement == null && ModuleUtilCore.findModuleForPsiElement(myRef) == null) {
                    // do not red highlight conditional compiler definitions in 3rd party library/SDK source code
                    return new ResolveResult[] { new JSResolveResult(myRef) };
                }
                resolveResultsAsConditionalCompilationVariable = resolveConditionalCompilationVariable(myRef);
            }
            if (resolveResultsAsConditionalCompilationVariable != null && resolveResultsAsConditionalCompilationVariable.length > 0 && (jsElement == null || resolveResultsAsConditionalCompilationVariable[0].isValidResult())) {
                return resolveResultsAsConditionalCompilationVariable;
            }
        }
        if (jsElement != null || localProcessor.isEncounteredDynamicClasses() && myQualifier == null || !localProcessor.processingEncounteredAnyTypeAccess() && !localProcessor.isEncounteredDynamicClasses()) {
            return localProcessor.getResultsAsResolveResults();
        }
    } else {
        final QualifiedItemProcessor<ResolveResultSink> processor = new QualifiedItemProcessor<>(new ResolveResultSink(myRef, myReferencedName), myContainingFile);
        processor.setTypeContext(JSResolveUtil.isExprInTypeContext(myRef));
        JSTypeEvaluator.evaluateTypes(myQualifier, myContainingFile, processor);
        if (processor.resolved == QualifiedItemProcessor.TypeResolveState.PrefixUnknown) {
            return dummyResult(myRef);
        }
        if (processor.resolved == QualifiedItemProcessor.TypeResolveState.Resolved || processor.resolved == QualifiedItemProcessor.TypeResolveState.Undefined || processor.getResult() != null) {
            return processor.getResultsAsResolveResults();
        } else {
            localProcessor = processor;
        }
    }
    ResolveResult[] results = resolveFromIndices(localProcessor);
    if (results.length == 0 && localProcessor.isEncounteredXmlLiteral()) {
        return dummyResult(myRef);
    }
    return results;
}
Also used : JSPackageStatement(com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement) JSE4XNamespaceReference(com.intellij.lang.javascript.psi.e4x.JSE4XNamespaceReference) JSReferenceListMember(com.intellij.lang.javascript.psi.ecmal4.JSReferenceListMember) JSImportStatement(com.intellij.lang.javascript.psi.ecmal4.JSImportStatement) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with JSPackageStatement

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

the class FlashRunConfiguration method getRefactoringElementListener.

@Override
public RefactoringElementListener getRefactoringElementListener(final PsiElement element) {
    if (!myRunnerParameters.isOverrideMainClass()) {
        return null;
    }
    final Module module = ModuleManager.getInstance(getProject()).findModuleByName(myRunnerParameters.getModuleName());
    if (!(element instanceof PsiDirectoryContainer) && !(element instanceof JSPackage) && !(element instanceof JSPackageStatement) && (module == null || !module.equals(ModuleUtilCore.findModuleForPsiElement(element)))) {
        return null;
    }
    final String currentPackage = StringUtil.getPackageName(myRunnerParameters.getOverriddenMainClass());
    if ((element instanceof PsiDirectoryContainer || element instanceof JSPackage || element instanceof JSPackageStatement) && Comparing.strEqual(FlexRefactoringListenerProvider.getPackageName(element), currentPackage)) {
        return new FlexRunConfigRefactoringListener.PackageRefactoringListener(this);
    }
    if (element instanceof PsiDirectory && containsClass(module, ((PsiDirectory) element), myRunnerParameters.getOverriddenMainClass())) {
        return new FlexRunConfigRefactoringListener.PsiDirectoryRefactoringListener(this);
    }
    final JSClass jsClass = FlexRefactoringListenerProvider.getJSClass(element);
    if (jsClass != null && Comparing.strEqual(jsClass.getQualifiedName(), myRunnerParameters.getOverriddenMainClass())) {
        return new FlexRunConfigRefactoringListener.JSClassRefactoringListener(this);
    }
    return null;
}
Also used : JSPackageStatement(com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement) PsiDirectory(com.intellij.psi.PsiDirectory) PsiDirectoryContainer(com.intellij.psi.PsiDirectoryContainer) JSPackage(com.intellij.lang.javascript.psi.ecmal4.JSPackage) Module(com.intellij.openapi.module.Module) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass)

Aggregations

JSPackageStatement (com.intellij.lang.javascript.psi.ecmal4.JSPackageStatement)7 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)6 PsiElement (com.intellij.psi.PsiElement)5 PsiDirectory (com.intellij.psi.PsiDirectory)3 JSFile (com.intellij.lang.javascript.psi.JSFile)2 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)2 JSPackage (com.intellij.lang.javascript.psi.ecmal4.JSPackage)2 Module (com.intellij.openapi.module.Module)2 PsiDirectoryContainer (com.intellij.psi.PsiDirectoryContainer)2 NotNull (org.jetbrains.annotations.NotNull)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)1 ASTNode (com.intellij.lang.ASTNode)1 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)1 JSFunction (com.intellij.lang.javascript.psi.JSFunction)1 JSE4XNamespaceReference (com.intellij.lang.javascript.psi.e4x.JSE4XNamespaceReference)1 JSAttributeListOwner (com.intellij.lang.javascript.psi.ecmal4.JSAttributeListOwner)1 JSImportStatement (com.intellij.lang.javascript.psi.ecmal4.JSImportStatement)1 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)1 JSReferenceListMember (com.intellij.lang.javascript.psi.ecmal4.JSReferenceListMember)1