Java Code Examples for com.intellij.psi.impl.source.tree.TreeElement#getElementType()

The following examples show how to use com.intellij.psi.impl.source.tree.TreeElement#getElementType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequirejsProjectComponent.java    From WebStormRequireJsPlugin with MIT License 6 votes vote down vote up
protected void parseMapConfigElement(TreeElement mapConfigElement) {
    if (null == mapConfigElement) {
        return;
    }

    if (mapConfigElement.getElementType() == JSElementTypes.PROPERTY) {
        String module = getJSPropertyName(mapConfigElement);

        TreeElement mapAliasesObject = (TreeElement) mapConfigElement
                .findChildByType(JSElementTypes.OBJECT_LITERAL_EXPRESSION);

        if (null != mapAliasesObject) {
            RequireMapModule requireMapModule = new RequireMapModule();

            requireMapModule.module = module;
            TreeElement mapAliasProperty = (TreeElement) mapAliasesObject.findChildByType(JSElementTypes.PROPERTY);
            parseMapAliasProperty(requireMapModule, mapAliasProperty);

            requireMap.addModule(requireMapModule);
        }
    }

    parseMapConfigElement(mapConfigElement.getTreeNext());
}
 
Example 2
Source File: RequirejsProjectComponent.java    From WebStormRequireJsPlugin with MIT License 6 votes vote down vote up
protected void parseMapAliasProperty(RequireMapModule requireMapModule, TreeElement mapAliasProperty) {
    if (null == mapAliasProperty) {
        return;
    }

    if (mapAliasProperty.getElementType() == JSElementTypes.PROPERTY) {
        RequirePathAlias alias = new RequirePathAlias();
        alias.alias = getJSPropertyName(mapAliasProperty);
        alias.path = getJSPropertyLiteralValue(mapAliasProperty);

        if (null != alias.alias && alias.path != null) {
            requireMapModule.addAlias(alias);
        } else {
            LOG.debug("Error parse require js path", alias);
        }
    }

    parseMapAliasProperty(requireMapModule, mapAliasProperty.getTreeNext());
}
 
Example 3
Source File: RequirejsProjectComponent.java    From WebStormRequireJsPlugin with MIT License 6 votes vote down vote up
private void parsePackage(TreeElement node) {
    if (null == node) {
        return;
    }
    if (node.getElementType() == JSElementTypes.OBJECT_LITERAL_EXPRESSION
        || node.getElementType() == JSElementTypes.LITERAL_EXPRESSION
    ) {
        // TODO: Not adding not resolve package
        Package p = new Package();
        packageConfig.packages.add(p);
        if (node.getElementType() == JSElementTypes.OBJECT_LITERAL_EXPRESSION) {
            TreeElement prop = (TreeElement) node.findChildByType(JSElementTypes.PROPERTY);
            parsePackageObject(prop, p);
        } else {
            p.name = dequote(node.getText());
        }
        normalizeParsedPackage(p);
        validatePackage(p);
    }
    TreeElement next = node.getTreeNext();
    parsePackage(next);
}
 
Example 4
Source File: RequirejsProjectComponent.java    From WebStormRequireJsPlugin with MIT License 6 votes vote down vote up
protected void parseRequireJsPaths(TreeElement node) {
    if (null == node) {
        return ;
    }

    if (node.getElementType() == JSElementTypes.PROPERTY) {
        RequirePathAlias pathAlias = new RequirePathAlias();
        pathAlias.alias = getJSPropertyName(node);
        pathAlias.path = getJSPropertyLiteralValue(node);
        requirePaths.addPath(pathAlias);
    }

    TreeElement next = node.getTreeNext();
    if (null != next) {
        parseRequireJsPaths(next);
    }
}
 
Example 5
Source File: RequirejsProjectComponent.java    From WebStormRequireJsPlugin with MIT License 5 votes vote down vote up
public void parseMainJsFile(TreeElement node) {

        TreeElement firstChild = node.getFirstChildNode();
        if (firstChild != null) {
            parseMainJsFile(firstChild);
        }

        TreeElement nextNode = node.getTreeNext();
        if (nextNode != null) {
            parseMainJsFile(nextNode);
        }

        if (node.getElementType() == JSTokenTypes.IDENTIFIER) {
            if (node.getText().equals("requirejs") || node.getText().equals("require")) {
                TreeElement treeParent = node.getTreeParent();

                if (null != treeParent) {
                    ASTNode firstTreeChild = treeParent.findChildByType(JSElementTypes.OBJECT_LITERAL_EXPRESSION);
                    TreeElement nextTreeElement = treeParent.getTreeNext();
                    if (null != firstTreeChild) {
                        parseRequirejsConfig((TreeElement) firstTreeChild
                            .getFirstChildNode()
                        );
                    } else if (null != nextTreeElement && nextTreeElement.getElementType() == JSTokenTypes.DOT) {
                        nextTreeElement = nextTreeElement.getTreeNext();
                        if (null != nextTreeElement && nextTreeElement.getText().equals("config")) {
                            treeParent = nextTreeElement.getTreeParent();
                            findAndParseConfig(treeParent);
                        }
                    } else {
                        findAndParseConfig(treeParent);
                    }
                }
            }
        }
    }
 
Example 6
Source File: RequirejsProjectComponent.java    From WebStormRequireJsPlugin with MIT License 5 votes vote down vote up
private static void parsePackageObject(TreeElement node, Package p) {
    if (null == node) {
        return;
    }

    if (node.getElementType() == JSElementTypes.PROPERTY) {
        TreeElement identifier = (TreeElement) node.findChildByType(JSTokenTypes.IDENTIFIER);
        String identifierName = null;
        if (null != identifier) {
            identifierName = identifier.getText();
        } else {
            TreeElement identifierString = (TreeElement) node.findChildByType(JSTokenTypes.STRING_LITERAL);
            if (null != identifierString) {
                identifierName = dequote(identifierString.getText());
            }
        }
        if (null != identifierName) {
            if (identifierName.equals("name")) {
                p.name = getJSPropertyLiteralValue(node);
            } else if (identifierName.equals("location")) {
                p.location = getJSPropertyLiteralValue(node);
            } else if (identifierName.equals("main")) {
                p.main = getJSPropertyLiteralValue(node);
            }
        }
    }

    TreeElement next = node.getTreeNext();
    parsePackageObject(next, p);
}
 
Example 7
Source File: RequirejsProjectComponent.java    From WebStormRequireJsPlugin with MIT License 4 votes vote down vote up
public void parseRequirejsConfig(TreeElement node) {
    if (null == node) {
        return ;
    }

    try {
        if (node.getElementType() == JSElementTypes.PROPERTY) {
            TreeElement identifier = (TreeElement) node.findChildByType(JSTokenTypes.IDENTIFIER);
            String identifierName = null;
            if (null != identifier) {
                identifierName = identifier.getText();
            } else {
                TreeElement identifierString = (TreeElement) node.findChildByType(JSTokenTypes.STRING_LITERAL);
                if (null != identifierString) {
                    identifierName = dequote(identifierString.getText());
                }
            }
            if (null != identifierName) {
                if (identifierName.equals("baseUrl")) {
                    String baseUrl = null;
                    if (!settings.overrideBaseUrl) {
                        ASTNode baseUrlNode = node
                                .findChildByType(JSElementTypes.LITERAL_EXPRESSION);
                        if (null != baseUrlNode) {
                            baseUrl = dequote(baseUrlNode.getText());
                        }
                    } else {
                        LOG.info("baseUrl override is enabled, overriding with '" + settings.baseUrl + "'");
                        baseUrl = settings.baseUrl;
                    }
                    if (null != baseUrl) {
                        LOG.info("Setting baseUrl to '" + baseUrl + "'");
                        setBaseUrl(baseUrl);
                        packageConfig.baseUrl = baseUrl;
                    } else {
                        LOG.debug("BaseUrl not set");
                    }
                } else if (identifierName.equals("paths")) {
                    ASTNode pathsNode = node
                            .findChildByType(JSElementTypes.OBJECT_LITERAL_EXPRESSION);
                    if (null != pathsNode) {
                        parseRequireJsPaths(
                                (TreeElement) pathsNode.getFirstChildNode()
                        );
                    }
                } else if (identifierName.equals("packages")) {
                    TreeElement packages = (TreeElement) node.findChildByType(JSElementTypes.ARRAY_LITERAL_EXPRESSION);
                    LOG.debug("parsing packages");
                    parsePackages(packages);
                    LOG.debug("parsing packages done, found " + packageConfig.packages.size() + " packages");
                } else if (identifierName.equals("map")) {
                    TreeElement mapElement = (TreeElement) node.findChildByType(JSElementTypes.OBJECT_LITERAL_EXPRESSION);
                    parseMapsConfig(mapElement);
                }
            }
        }
    } catch (NullPointerException exception) {
        LOG.error(exception.getMessage(), exception);
    }

    TreeElement next = node.getTreeNext();
    if (null != next) {
        parseRequirejsConfig(next);
    }
}