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);
}
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);
}
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);
}
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));
}
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;
}
Aggregations