Search in sources :

Example 31 with JSClass

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

the class FlexUnitTestCreator method createTest.

public void createTest(final Project project, final Editor editor, final PsiFile file) {
    final JSClass jsClass = FlexUnitTestFinder.findContextClass(file);
    if (jsClass == null)
        return;
    final String testClassName;
    final String packageName;
    final JSClass superClass;
    final PsiDirectory targetDirectory;
    final boolean generateSetUp;
    final boolean generateTearDown;
    final JSMemberInfo[] selectedMemberInfos;
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        testClassName = jsClass.getName() + "Test";
        packageName = StringUtil.getPackageName(jsClass.getQualifiedName());
        superClass = null;
        targetDirectory = jsClass.getContainingFile().getContainingDirectory();
        generateSetUp = true;
        generateTearDown = true;
        selectedMemberInfos = new JSMemberInfo[0];
    } else {
        final CreateFlexUnitTestDialog dialog = new CreateFlexUnitTestDialog(ModuleUtilCore.findModuleForPsiElement(jsClass), jsClass);
        dialog.show();
        if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE) {
            return;
        }
        testClassName = dialog.getTestClassName();
        packageName = dialog.getPackageName();
        superClass = dialog.getSuperClass();
        targetDirectory = dialog.getTargetDirectory();
        generateSetUp = dialog.isGenerateSetUp();
        generateTearDown = dialog.isGenerateTearDown();
        selectedMemberInfos = dialog.getSelectedMemberInfos();
    }
    final Consumer<JSClass> postProcessRunnable = createdClass -> {
        final String methodsText = getMethodsText(createdClass, generateSetUp, generateTearDown, selectedMemberInfos);
        if (!methodsText.isEmpty()) {
            final PsiElement methods = JSChangeUtil.createJSTreeFromText(project, "{" + methodsText + "}", JavaScriptSupportLoader.ECMA_SCRIPT_L4).getPsi();
            if (methods != null) {
                for (final PsiElement psiElement : PsiTreeUtil.getChildrenOfTypeAsList(methods, JSFunction.class)) {
                    createdClass.add(psiElement);
                }
            }
        }
        CodeStyleManager.getInstance(project).reformat(createdClass);
        createdClass.navigate(true);
    };
    CommandProcessor.getInstance().executeCommand(project, () -> ActionScriptCreateClassOrInterfaceFix.createClass(ActionScriptCreateClassOrInterfaceFix.ACTION_SCRIPT_CLASS_WITH_SUPERS_TEMPLATE_NAME, testClassName, packageName, superClass, Collections.emptyList(), targetDirectory, CodeInsightBundle.message("intention.create.test"), true, Collections.emptyMap(), postProcessRunnable), CodeInsightBundle.message("intention.create.test"), null);
}
Also used : JavaScriptSupportLoader(com.intellij.lang.javascript.JavaScriptSupportLoader) JSChangeUtil(com.intellij.lang.javascript.psi.impl.JSChangeUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) JSMemberInfo(com.intellij.lang.javascript.refactoring.util.JSMemberInfo) THashSet(gnu.trove.THashSet) ActionScriptCreateClassOrInterfaceFix(com.intellij.lang.javascript.validation.fixes.ActionScriptCreateClassOrInterfaceFix) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) PsiElement(com.intellij.psi.PsiElement) FunctionKind(com.intellij.lang.javascript.psi.JSFunction.FunctionKind) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) TestCreator(com.intellij.testIntegration.TestCreator) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) StringUtil(com.intellij.openapi.util.text.StringUtil) Set(java.util.Set) Editor(com.intellij.openapi.editor.Editor) CommandProcessor(com.intellij.openapi.command.CommandProcessor) JSFunction(com.intellij.lang.javascript.psi.JSFunction) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) ApplicationManager(com.intellij.openapi.application.ApplicationManager) PsiDirectory(com.intellij.psi.PsiDirectory) JSInheritanceUtil(com.intellij.lang.javascript.psi.resolve.JSInheritanceUtil) Collections(java.util.Collections) Consumer(com.intellij.util.Consumer) JSFunction(com.intellij.lang.javascript.psi.JSFunction) PsiDirectory(com.intellij.psi.PsiDirectory) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) JSMemberInfo(com.intellij.lang.javascript.refactoring.util.JSMemberInfo) PsiElement(com.intellij.psi.PsiElement)

Example 32 with JSClass

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

the class FlexUnitTestFinder method findTestsForClass.

@NotNull
public Collection<PsiElement> findTestsForClass(@NotNull final PsiElement element) {
    final VirtualFile file = element.getContainingFile().getVirtualFile();
    final JSClass jsClass = findSourceElement(element);
    final String className = jsClass == null ? null : jsClass.getName();
    final Pair<Module, FlexUnitSupport> moduleAndSupport = FlexUnitSupport.getModuleAndSupport(element);
    final Module module = moduleAndSupport == null ? null : moduleAndSupport.first;
    final FlexUnitSupport flexUnitSupport = moduleAndSupport == null ? null : moduleAndSupport.second;
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex();
    if (className == null || module == null || flexUnitSupport == null || (file != null && fileIndex.isInTestSourceContent(file))) {
        return Collections.emptyList();
    }
    final Collection<String> allNames = StubIndex.getInstance().getAllKeys(JSNameIndex.KEY, element.getProject());
    final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependentsScope(module);
    final List<Pair<? extends PsiNamedElement, Integer>> classesWithProximity = new ArrayList<>();
    for (final String possibleTestName : allNames) {
        if (possibleTestName.contains(className)) {
            for (final JSQualifiedNamedElement jsElement : JSResolveUtil.findElementsByName(possibleTestName, element.getProject(), scope)) {
                final VirtualFile f = jsElement.getContainingFile().getVirtualFile();
                final boolean inTestSource = f != null && fileIndex.isInTestSourceContent(f);
                if (jsElement instanceof JSClass && jsElement != jsClass && ((inTestSource && possibleTestName.contains("Test")) || flexUnitSupport.isTestClass((JSClass) jsElement, true))) {
                    classesWithProximity.add(Pair.create(jsElement, TestFinderHelper.calcTestNameProximity(className, jsElement.getName())));
                }
            }
        }
    }
    return TestFinderHelper.getSortedElements(classesWithProximity, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with JSClass

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

the class FlexUnitTestFinder method findClassesForTest.

@NotNull
public Collection<PsiElement> findClassesForTest(@NotNull final PsiElement element) {
    final JSClass jsClass = findSourceElement(element);
    final String className = jsClass == null ? null : jsClass.getName();
    final Pair<Module, FlexUnitSupport> moduleAndSupport = FlexUnitSupport.getModuleAndSupport(element);
    final Module module = moduleAndSupport == null ? null : moduleAndSupport.first;
    final FlexUnitSupport flexUnitSupport = moduleAndSupport == null ? null : moduleAndSupport.second;
    if (className == null || module == null || flexUnitSupport == null) {
        return Collections.emptyList();
    }
    final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesScope(module).intersectWith(GlobalSearchScopes.projectProductionScope(module.getProject()));
    final List<Pair<? extends PsiNamedElement, Integer>> classesWithWeights = new ArrayList<>();
    for (Pair<String, Integer> nameWithWeight : TestFinderHelper.collectPossibleClassNamesWithWeights(className)) {
        for (final JSQualifiedNamedElement jsElement : JSResolveUtil.findElementsByName(nameWithWeight.first, module.getProject(), scope)) {
            if (jsElement instanceof JSClass && jsElement != jsClass && !((JSClass) jsElement).isInterface() && !flexUnitSupport.isTestClass((JSClass) jsElement, true)) {
                classesWithWeights.add(Pair.create(jsElement, nameWithWeight.second));
            }
        }
    }
    return TestFinderHelper.getSortedElements(classesWithWeights, false);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with JSClass

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

the class FlexUnitTestFinder method isTest.

public boolean isTest(@NotNull final PsiElement element) {
    final JSClass jsClass = findSourceElement(element);
    final Pair<Module, FlexUnitSupport> moduleAndSupport = FlexUnitSupport.getModuleAndSupport(element);
    final VirtualFile file = element.getContainingFile().getVirtualFile();
    return jsClass != null && moduleAndSupport != null && ((file != null && file.getName().contains("Test") && ModuleRootManager.getInstance(moduleAndSupport.first).getFileIndex().isInTestSourceContent(file)) || moduleAndSupport.second.isTestClass(jsClass, true));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Module(com.intellij.openapi.module.Module) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass)

Example 35 with JSClass

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

the class SwfProjectViewStructureProvider method findDecompiledElement.

@Nullable
private static PsiElement findDecompiledElement(JSQualifiedNamedElement element) {
    if (DumbService.isDumb(element.getProject())) {
        return null;
    }
    JSQualifiedNamedElement mainElement = JSUtils.getMemberContainingClass(element);
    if (mainElement == null) {
        mainElement = element;
    }
    final String qName = mainElement.getQualifiedName();
    if (qName == null) {
        return null;
    }
    VirtualFile elementVFile = mainElement.getContainingFile().getVirtualFile();
    if (elementVFile == null) {
        return null;
    }
    ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(mainElement.getProject()).getFileIndex();
    GlobalSearchScope searchScope = JSResolveUtil.getResolveScope(mainElement);
    Collection<JSQualifiedNamedElement> candidates = StubIndex.getElements(JSQualifiedElementIndex.KEY, qName.hashCode(), mainElement.getProject(), searchScope, JSQualifiedNamedElement.class);
    List<OrderEntry> sourceFileEntries = projectFileIndex.getOrderEntriesForFile(elementVFile);
    for (JSQualifiedNamedElement candidate : candidates) {
        if (candidate == mainElement || !qName.equals(candidate.getQualifiedName())) {
            continue;
        }
        VirtualFile vFile = candidate.getContainingFile().getVirtualFile();
        if (vFile != null && projectFileIndex.getClassRootForFile(vFile) != null) {
            List<OrderEntry> candidateEntries = projectFileIndex.getOrderEntriesForFile(vFile);
            if (ContainerUtil.intersects(sourceFileEntries, candidateEntries)) {
                if (element == mainElement) {
                    return candidate;
                } else {
                    LOG.assertTrue(candidate instanceof JSClass, candidate);
                    if (element instanceof JSVariable) {
                        return ((JSClass) candidate).findFieldByName(element.getName());
                    } else {
                        LOG.assertTrue(element instanceof JSFunction, element);
                        return ((JSClass) candidate).findFunctionByNameAndKind(element.getName(), ((JSFunction) element).getKind());
                    }
                }
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderEntry(com.intellij.openapi.roots.OrderEntry) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)141 PsiElement (com.intellij.psi.PsiElement)65 Nullable (org.jetbrains.annotations.Nullable)27 MxmlJSClass (com.intellij.javascript.flex.mxml.MxmlJSClass)23 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)22 JSFunction (com.intellij.lang.javascript.psi.JSFunction)18 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)18 Module (com.intellij.openapi.module.Module)17 XmlFile (com.intellij.psi.xml.XmlFile)17 Project (com.intellij.openapi.project.Project)16 PsiFile (com.intellij.psi.PsiFile)16 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)15 ArrayList (java.util.ArrayList)14 PsiDirectory (com.intellij.psi.PsiDirectory)11 XmlTag (com.intellij.psi.xml.XmlTag)11 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)8 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)8 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)7