Search in sources :

Example 16 with JSQualifiedNamedElement

use of com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement 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 17 with JSQualifiedNamedElement

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

the class FlexMoveClassProcessor method detectConflicts.

private MultiMap<PsiElement, String> detectConflicts(UsageInfo[] usages) {
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    final Collection<PsiElement> filesToMove = Arrays.asList(myElementsToMove);
    JSVisibilityUtil.Options options = new JSVisibilityUtil.Options();
    for (PsiElement file : filesToMove) {
        options.overridePackage(file, myTargetPackage);
    }
    for (UsageInfo usage : usages) {
        final PsiElement element = usage.getElement();
        if (!(element instanceof JSReferenceExpression)) {
            continue;
        }
        if (CommonRefactoringUtil.isAncestor(element, filesToMove)) {
            continue;
        }
        JSReferenceExpression refExpr = (JSReferenceExpression) element;
        final PsiElement resolved = refExpr.resolve();
        if (!(resolved instanceof JSQualifiedNamedElement)) {
            continue;
        }
        PsiElement containingClass = null;
        if (resolved instanceof JSFunction && ((JSFunction) resolved).isConstructor() && myElements.contains(containingClass = resolved.getParent()) || myElements.contains(resolved)) {
            JSRefactoringConflictsUtil.checkAccessibility((JSAttributeListOwner) resolved, (JSClass) containingClass, null, refExpr, conflicts, true, options);
        }
    }
    for (PsiElement fileToMove : filesToMove) {
        JSRefactoringConflictsUtil.checkOutgoingReferencesAccessibility(fileToMove, filesToMove, null, true, conflicts, Conditions.alwaysTrue(), options);
    }
    //JSRefactoringConflictsUtil.analyzeModuleConflicts(myProject, myElements, usages, myTargetDirectory, conflicts);
    return conflicts;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSFunction(com.intellij.lang.javascript.psi.JSFunction) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) JSVisibilityUtil(com.intellij.lang.javascript.refactoring.JSVisibilityUtil) PsiElement(com.intellij.psi.PsiElement)

Example 18 with JSQualifiedNamedElement

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

the class FlexMoveFileRefactoringHandler method doMove.

@Override
public void doMove(Project project, PsiElement[] elements, @Nullable PsiElement targetContainer, @Nullable MoveCallback callback) {
    if (elements.length == 0) {
        return;
    }
    Collection<JSQualifiedNamedElement> adjusted = new ArrayList<>(elements.length);
    for (PsiElement element : elements) {
        final JSQualifiedNamedElement e = adjustForMove(element);
        if (!JSRefactoringUtil.checkReadOnlyStatus(e, null, RefactoringBundle.message("move.title")))
            return;
        adjusted.add(e);
    }
    new FlexMoveClassDialog(project, adjusted, targetContainer, callback).show();
}
Also used : ArrayList(java.util.ArrayList) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) PsiElement(com.intellij.psi.PsiElement)

Example 19 with JSQualifiedNamedElement

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

the class FlexMoveInnerClassProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    try {
        ActionScriptCreateClassOrInterfaceFix.createClass(myClassName, myPackageName, myTargetDirectory, false);
    } catch (Exception e) {
        Messages.showErrorDialog(myProject, e.getMessage(), getCommandName());
        return;
    }
    final PsiFile sourceFile = myElement.getContainingFile();
    Collection<String> importsInTargetFile = new HashSet<>();
    Collection<String> namespacesInTargetFile = new HashSet<>();
    List<FormatFixer> formatters = new ArrayList<>();
    //JSRefactoringUtil.addRemovalFormatters(mySourceClass, myMembersToMove, Condition.TRUE, Condition.TRUE, postponedFormatters);
    JSClass targetClass = myElement instanceof JSClass ? (JSClass) myElement : null;
    JSRefactoringUtil.fixOutgoingReferences(myElement, importsInTargetFile, namespacesInTargetFile, Collections.singletonList(((JSAttributeListOwner) myElement)), targetClass, false, false);
    myElement.setName(myClassName);
    Collection<UsageInfo> usagesToProcess = new ArrayList<>(Arrays.asList(usages));
    for (Iterator<UsageInfo> i = usagesToProcess.iterator(); i.hasNext(); ) {
        UsageInfo usage = i.next();
        PsiElement element;
        if (usage instanceof NonCodeUsageInfo || (element = usage.getElement()) == null || !PsiTreeUtil.isAncestor(myElement, element, false)) {
            continue;
        }
        if (element instanceof JSReferenceExpression) {
            ((JSReferenceExpression) element).bindToElement(myElement);
        } else if (element instanceof PsiNamedElement) {
            ((PsiNamedElement) element).setName(myClassName);
        } else {
            continue;
        }
        i.remove();
    }
    final PsiElement clazz = ActionScriptClassResolver.findClassByQNameStatic(StringUtil.getQualifiedName(myPackageName, myClassName), GlobalSearchScope.projectScope(myProject));
    PsiElement toInsert = myElement instanceof JSVariable ? JSRefactoringUtil.getVarStatementCopy((JSVariable) myElement) : myElement.copy();
    final PsiElement inserted = clazz.replace(toInsert);
    PsiFile insertedContainingFile = inserted.getContainingFile();
    JSQualifiedNamedElement newClass = inserted instanceof JSVarStatement ? ((JSVarStatement) inserted).getVariables()[0] : (JSQualifiedNamedElement) inserted;
    SmartPsiElementPointer<JSQualifiedNamedElement> newClassPointer = SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(newClass);
    JSRefactoringUtil.handleDocCommentAndFormat(inserted, formatters);
    JSRefactoringUtil.deleteWithNoPostponedFormatting(myElement);
    if (myPackageName.length() > 0) {
        for (UsageInfo usage : usagesToProcess) {
            if (usage instanceof NonCodeUsageInfo || usage.getFile() != sourceFile)
                continue;
            final PsiElement element = usage.getElement();
            if (element == null)
                continue;
            ImportUtils.doImport(element, StringUtil.getQualifiedName(myPackageName, myClassName), true);
        }
    }
    JSRefactoringUtil.postProcess(sourceFile, newClass, Collections.singletonList(sourceFile), importsInTargetFile, namespacesInTargetFile, formatters, true, false);
    boolean makePublic = false;
    newClass = newClassPointer.getElement();
    List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usage : usagesToProcess) {
        if (usage instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usage);
        } else {
            JSReferenceExpression refExpr = (JSReferenceExpression) usage.getElement();
            if (refExpr == null) {
                continue;
            }
            makePublic |= JSPsiImplUtils.getQNameForMove(refExpr, newClass) != null;
            refExpr.bindToElement(newClass);
        }
    }
    JSChangeVisibilityUtil.setVisibility((JSAttributeListOwner) newClass, makePublic ? JSAttributeList.AccessType.PUBLIC : JSAttributeList.AccessType.PACKAGE_LOCAL);
    myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
    if (myMoveCallback != null) {
        myMoveCallback.refactoringCompleted();
    }
    OpenFileDescriptor descriptor = new OpenFileDescriptor(myProject, insertedContainingFile.getVirtualFile(), newClass.getTextOffset());
    FileEditorManager.getInstance(myProject).openTextEditor(descriptor, true);
}
Also used : NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) JSVariable(com.intellij.lang.javascript.psi.JSVariable) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) JSVarStatement(com.intellij.lang.javascript.psi.JSVarStatement) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) FormatFixer(com.intellij.lang.javascript.refactoring.FormatFixer) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo)

Example 20 with JSQualifiedNamedElement

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

the class FlexCssElementDescriptorProvider method getPropertyDescriptorsDynamically.

@NotNull
private static Collection<? extends CssPropertyDescriptor> getPropertyDescriptorsDynamically(@NotNull List<CssSimpleSelector> selectors, @NotNull Module module) {
    FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
    GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(false);
    Set<JSClass> visited = ContainerUtil.newLinkedHashSet();
    Set<CssPropertyDescriptor> result = ContainerUtil.newLinkedHashSet();
    Project project = module.getProject();
    for (CssSimpleSelector selector : selectors) {
        final JSClass jsClass = getClassFromMxmlDescriptor(selector, module);
        if (jsClass != null) {
            fillPropertyDescriptorsDynamically(jsClass, visited, result);
            continue;
        }
        final String shortClassName = selector.getElementName();
        Collection<JSQualifiedNamedElement> candidates = JSResolveUtil.findElementsByName(shortClassName, project, scope);
        for (JSQualifiedNamedElement candidate : candidates) {
            if (candidate instanceof JSClass) {
                fillPropertyDescriptorsDynamically((JSClass) candidate, visited, result);
            }
        }
    }
    for (Iterator<CssPropertyDescriptor> iterator = result.iterator(); iterator.hasNext(); ) {
        CssPropertyDescriptor propertyDescriptor = iterator.next();
        List<Set<FlexStyleIndexInfo>> values = fileBasedIndex.getValues(FlexStyleIndex.INDEX_ID, propertyDescriptor.getPropertyName(), scope);
        if (values.size() == 0) {
            iterator.remove();
        }
    }
    return result;
}
Also used : JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)27 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)13 PsiElement (com.intellij.psi.PsiElement)12 NotNull (org.jetbrains.annotations.NotNull)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 PsiFile (com.intellij.psi.PsiFile)8 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)8 JSFile (com.intellij.lang.javascript.psi.JSFile)4 Project (com.intellij.openapi.project.Project)4 XmlFile (com.intellij.psi.xml.XmlFile)4 Nullable (org.jetbrains.annotations.Nullable)4 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)3 Module (com.intellij.openapi.module.Module)3 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)3 PsiDirectory (com.intellij.psi.PsiDirectory)3 ArrayList (java.util.ArrayList)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)2 JSFunction (com.intellij.lang.javascript.psi.JSFunction)2 Attachment (com.intellij.openapi.diagnostic.Attachment)2