Search in sources :

Example 21 with JSClass

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

the class FlexXmlBackedClassesIndex method getIndexer.

@Override
@NotNull
public DataIndexer<String, Void, FileContent> getIndexer() {
    return new DataIndexer<String, Void, FileContent>() {

        @Override
        @NotNull
        public Map<String, Void> map(@NotNull FileContent inputData) {
            final XmlFile file = (XmlFile) inputData.getPsiFile();
            final Map<String, Void> result = new HashMap<>();
            for (JSClass clazz : XmlBackedJSClassImpl.getClasses(file)) {
                JSReferenceList supers = getSupers(clazz);
                if (supers != null) {
                    final JSExpression[] expressions = supers.getExpressions();
                    for (JSExpression expr : expressions) {
                        String s = expr instanceof JSReferenceExpression ? ((JSReferenceExpression) expr).getReferenceName() : null;
                        if (s != null)
                            result.put(s, null);
                    }
                }
            }
            return result;
        }
    };
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) HashMap(com.intellij.util.containers.HashMap) JSExpression(com.intellij.lang.javascript.psi.JSExpression) NotNull(org.jetbrains.annotations.NotNull) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) JSReferenceList(com.intellij.lang.javascript.psi.ecmal4.JSReferenceList) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with JSClass

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

the class FlexSchemaHandler method getAvailableNamespaces.

@NotNull
@Override
public Set<String> getAvailableNamespaces(@NotNull XmlFile _file, @Nullable @NonNls final String tagName) {
    // tagName == null => tag name completion
    // tagName != null => guess namespace of unresolved tag
    PsiFile originalFile = _file.getOriginalFile();
    if (originalFile instanceof XmlFile)
        _file = (XmlFile) originalFile;
    final XmlFile file = _file;
    final Project project = file.getProject();
    final Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(file.getVirtualFile());
    final Collection<String> illegalNamespaces = getIllegalNamespaces(file);
    final Set<String> result = new THashSet<>();
    final Set<String> componentsThatHaveNotPackageBackedNamespace = new THashSet<>();
    for (final String namespace : CodeContextHolder.getInstance(project).getNamespaces(module)) {
        if (!CodeContext.isPackageBackedNamespace(namespace) && !illegalNamespaces.contains(namespace)) {
            // package backed namespaces will be added later from JSPackageIndex
            if (tagName == null) {
                result.add(namespace);
            } else {
                final XmlElementDescriptor descriptor = CodeContext.getContext(namespace, module).getElementDescriptor(tagName, (XmlTag) null);
                if (descriptor != null) {
                    result.add(namespace);
                    componentsThatHaveNotPackageBackedNamespace.add(descriptor.getQualifiedName());
                }
            }
        }
    }
    if (tagName == null && !illegalNamespaces.contains(JavaScriptSupportLoader.MXML_URI)) {
        result.add(JavaScriptSupportLoader.MXML_URI);
    }
    if (XmlBackedJSClassImpl.SCRIPT_TAG_NAME.equals(tagName) || "Style".equals(tagName))
        return result;
    if (DumbService.isDumb(project))
        return result;
    if (tagName == null) {
        FileBasedIndex.getInstance().processAllKeys(JSPackageIndex.INDEX_ID, packageName -> {
            result.add(StringUtil.isEmpty(packageName) ? "*" : packageName + ".*");
            return true;
        }, project);
    } else {
        final GlobalSearchScope scopeWithLibs = module != null ? GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module) : GlobalSearchScope.allScope(project);
        for (JSQualifiedNamedElement element : JSResolveUtil.findElementsByName(tagName, project, scopeWithLibs, false)) {
            if (element instanceof JSClass && CodeContext.hasDefaultConstructor((JSClass) element)) {
                final String packageName = StringUtil.getPackageName(element.getQualifiedName());
                if (!componentsThatHaveNotPackageBackedNamespace.contains(StringUtil.getQualifiedName(packageName, tagName))) {
                    result.add(StringUtil.isEmpty(packageName) ? "*" : packageName + ".*");
                }
            }
        }
    }
    final GlobalSearchScope scopeWithoutLibs = module != null ? GlobalSearchScope.moduleWithDependenciesScope(module) : GlobalSearchScope.allScope(project);
    // packages that contain *.mxml files and do not contain *.as are not retrieved from JSPackageIndex
    FlexResolveHelper.processAllMxmlAndFxgFiles(scopeWithoutLibs, project, new FlexResolveHelper.MxmlAndFxgFilesProcessor() {

        public void addDependency(final PsiDirectory directory) {
        }

        public boolean processFile(final VirtualFile file, final VirtualFile root) {
            if (tagName == null || tagName.equals(file.getNameWithoutExtension())) {
                final String packageName = VfsUtilCore.getRelativePath(file.getParent(), root, '.');
                if (packageName != null && (tagName == null || !componentsThatHaveNotPackageBackedNamespace.contains(StringUtil.getQualifiedName(packageName, tagName)))) {
                    result.add(StringUtil.isEmpty(packageName) ? "*" : packageName + ".*");
                }
            }
            return true;
        }
    }, tagName);
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) FlexResolveHelper(com.intellij.javascript.flex.resolve.FlexResolveHelper) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with JSClass

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

the class FlexUndefinedElementFixProvider method createFixes.

@Nullable
@Override
public IntentionAction[] createFixes(@NotNull XmlAttribute attribute) {
    if (!JavaScriptSupportLoader.isFlexMxmFile(attribute.getContainingFile()))
        return null;
    final String name = attribute.getName();
    if (!JSRefactoringUtil.isValidIdentifier(name, attribute.getProject())) {
        return IntentionAction.EMPTY_ARRAY;
    }
    final XmlElementDescriptor descriptor = attribute.getParent().getDescriptor();
    final PsiElement declaration = descriptor instanceof ClassBackedElementDescriptor ? descriptor.getDeclaration() : null;
    final VirtualFile virtualFile = declaration == null ? null : declaration.getContainingFile().getVirtualFile();
    if (virtualFile == null || ProjectRootManager.getInstance(declaration.getProject()).getFileIndex().getSourceRootForFile(virtualFile) == null) {
        return IntentionAction.EMPTY_ARRAY;
    }
    if (declaration instanceof JSClass || declaration instanceof XmlFile && JavaScriptSupportLoader.isFlexMxmFile((XmlFile) declaration)) {
        final String attributeValue = attribute.getValue();
        final FixAndIntentionAction fix1 = new CreateFieldByMxmlAttributeFix(name, attributeValue);
        fix1.registerElementRefForFix(attribute, null);
        final FixAndIntentionAction fix2 = new CreateSetterByMxmlAttributeFix(name, attributeValue);
        fix2.registerElementRefForFix(attribute, null);
        final FixAndIntentionAction fix3 = new CreateEventMetadataByMxmlAttributeFix(name);
        fix3.registerElementRefForFix(attribute, null);
        return new IntentionAction[] { fix1, fix2, fix3 };
    }
    return IntentionAction.EMPTY_ARRAY;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) FixAndIntentionAction(com.intellij.lang.javascript.validation.fixes.FixAndIntentionAction) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) FixAndIntentionAction(com.intellij.lang.javascript.validation.fixes.FixAndIntentionAction) CreateFieldByMxmlAttributeFix(com.intellij.lang.javascript.validation.fixes.CreateFieldByMxmlAttributeFix) CreateEventMetadataByMxmlAttributeFix(com.intellij.lang.javascript.validation.fixes.CreateEventMetadataByMxmlAttributeFix) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) CreateSetterByMxmlAttributeFix(com.intellij.lang.javascript.validation.fixes.CreateSetterByMxmlAttributeFix) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with JSClass

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

the class AngularAttributeDescriptor method getFieldBasedDescriptors.

public static XmlAttributeDescriptor[] getFieldBasedDescriptors(JSImplicitElement declaration, String decorator, NullableFunction<Pair<PsiElement, String>, XmlAttributeDescriptor> factory) {
    final JSClass clazz = PsiTreeUtil.getContextOfType(declaration, JSClass.class);
    if (clazz != null) {
        JSField[] fields = clazz.getFields();
        final List<XmlAttributeDescriptor> result = new ArrayList<>(fields.length);
        for (JSField field : fields) {
            String decoratedName = getDecoratedName(field, decorator);
            if (decoratedName == null)
                continue;
            ContainerUtil.addIfNotNull(result, factory.fun(Pair.create(field, decoratedName)));
        }
        for (JSFunction function : clazz.getFunctions()) {
            String decoratedName = getDecoratedName(function, decorator);
            if (decoratedName == null)
                continue;
            ContainerUtil.addIfNotNull(result, factory.fun(Pair.create(function, decoratedName)));
        }
        return result.toArray(new XmlAttributeDescriptor[result.size()]);
    }
    if (!DialectDetector.isTypeScript(declaration)) {
        return getCompiledFieldBasedDescriptors(declaration, decorator, factory);
    }
    return EMPTY;
}
Also used : BasicXmlAttributeDescriptor(com.intellij.xml.impl.BasicXmlAttributeDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) ArrayList(java.util.ArrayList) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass)

Example 25 with JSClass

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

the class ActionScriptHighlightingTest method testNamespaceElementReferences2.

public void testNamespaceElementReferences2() throws Exception {
    defaultTest();
    JSReferenceExpression expr = PsiTreeUtil.getParentOfType(myFile.findElementAt(myEditor.getCaretModel().getOffset()), JSReferenceExpression.class);
    assertNotNull(expr);
    PsiElement exprResolve = expr.resolve();
    assertTrue(exprResolve instanceof JSFunction);
    JSClass clazz = PsiTreeUtil.getParentOfType(exprResolve, JSClass.class);
    assertNotNull(clazz);
    assertEquals("Foo", clazz.getQualifiedName());
}
Also used : JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSFunction(com.intellij.lang.javascript.psi.JSFunction) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass)

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