Search in sources :

Example 21 with JSReferenceExpression

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

the class FlexResolveHelper method bindReferenceToElement.

@Override
public JSReferenceExpression bindReferenceToElement(JSReferenceExpression ref, String qName, String newName, boolean justMakeQualified, PsiNamedElement element) {
    PsiFile file;
    if (qName != null && (element instanceof XmlBackedJSClass || (element instanceof XmlFile && JavaScriptSupportLoader.isFlexMxmFile((PsiFile) element)) || (file = element.getContainingFile()) == null || file.getLanguage().isKindOf(JavaScriptSupportLoader.ECMA_SCRIPT_L4))) {
        boolean qualify;
        boolean doImport;
        if (justMakeQualified || ref.getParent() instanceof JSImportStatement || element instanceof PsiDirectoryContainer || (ref.getParent() instanceof JSReferenceListMember && ref.getContainingFile().getContext() instanceof XmlAttributeValue)) {
            qualify = true;
            doImport = false;
        } else {
            doImport = JSImportHandlingUtil.evaluateImportStatus(newName, ref) == JSImportHandlingUtil.ImportStatus.ABSENT && JSImportHandlingUtil.evaluateImportStatus(ref.getReferencedName(), ref) == JSImportHandlingUtil.ImportStatus.ABSENT;
            JSQualifiedNamedElement qualifiedElement = null;
            if (element instanceof JSQualifiedNamedElement) {
                qualifiedElement = (JSQualifiedNamedElement) element;
            } else if (element instanceof JSFile) {
                qualifiedElement = JSPsiImplUtils.findQualifiedElement((JSFile) element);
            } else if (element instanceof XmlFile) {
                qualifiedElement = XmlBackedJSClassFactory.getXmlBackedClass(((XmlFile) element));
            }
            assert qualifiedElement != null : qualifiedElement.getClass();
            // at this moment package declaration is out of date so element has it's original qName
            qualify = JSResolveUtil.shortReferenceIsAmbiguousOrUnequal(newName, ref, qualifiedElement.getQualifiedName(), null);
        }
        if (qualify) {
            ASTNode newChild = JSChangeUtil.createExpressionFromText(ref.getProject(), qName);
            ref.getParent().getNode().replaceChild(ref.getNode(), newChild);
            ref = (JSReferenceExpression) newChild.getPsi();
        }
        if (doImport && qName.indexOf('.') != -1 && !StringUtil.getPackageName(qName).equals(JSResolveUtil.getPackageNameFromPlace(ref))) {
            final SmartPsiElementPointer<JSReferenceExpression> refPointer = SmartPointerManager.getInstance(ref.getProject()).createSmartPsiElementPointer(ref);
            ImportUtils.doImport(ref, qName, false);
            ref = refPointer.getElement();
        }
    }
    return ref;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) ASTNode(com.intellij.lang.ASTNode) JSFile(com.intellij.lang.javascript.psi.JSFile)

Example 22 with JSReferenceExpression

use of com.intellij.lang.javascript.psi.JSReferenceExpression 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 23 with JSReferenceExpression

use of com.intellij.lang.javascript.psi.JSReferenceExpression 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 24 with JSReferenceExpression

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

the class FlexMoveInnerClassProcessor method findUsages.

@NotNull
@Override
protected UsageInfo[] findUsages() {
    final Collection<UsageInfo> result = Collections.synchronizedCollection(new ArrayList<UsageInfo>());
    ReferencesSearch.search(myElement, new LocalSearchScope(myElement.getContainingFile())).forEach(reference -> {
        final PsiElement element = reference.getElement();
        if (!(element instanceof JSReferenceExpression)) {
            return true;
        }
        if (JSResolveUtil.isSelfReference(element)) {
            return true;
        }
        result.add(new UsageInfo(element));
        return true;
    });
    if (myElement instanceof JSClass) {
        final JSFunction constructor = ((JSClass) myElement).getConstructor();
        if (constructor != null) {
            result.add(new UsageInfo(constructor));
            JSRefactoringUtil.addConstructorUsages((JSClass) myElement, result);
        }
    }
    TextOccurrencesUtil.findNonCodeUsages(myElement, myElement.getName(), mySearchInComments, mySearchTextOccurences, StringUtil.getQualifiedName(myPackageName, myClassName), result);
    return UsageViewUtil.removeDuplicatedUsages(result.toArray(new UsageInfo[result.size()]));
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSFunction(com.intellij.lang.javascript.psi.JSFunction) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)24 PsiElement (com.intellij.psi.PsiElement)10 JSFunction (com.intellij.lang.javascript.psi.JSFunction)7 JSFile (com.intellij.lang.javascript.psi.JSFile)6 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)5 NotNull (org.jetbrains.annotations.NotNull)4 JSCallExpression (com.intellij.lang.javascript.psi.JSCallExpression)3 JSExpression (com.intellij.lang.javascript.psi.JSExpression)3 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)3 TextRange (com.intellij.openapi.util.TextRange)3 JSExpressionStatement (com.intellij.lang.javascript.psi.JSExpressionStatement)2 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)2 JSImportStatement (com.intellij.lang.javascript.psi.ecmal4.JSImportStatement)2 FormatFixer (com.intellij.lang.javascript.refactoring.FormatFixer)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 ResolveResult (com.intellij.psi.ResolveResult)2 XmlFile (com.intellij.psi.xml.XmlFile)2 NonCodeUsageInfo (com.intellij.refactoring.util.NonCodeUsageInfo)2 Expression (com.intellij.codeInsight.template.Expression)1