Search in sources :

Example 11 with OuterLanguageElement

use of com.intellij.psi.templateLanguages.OuterLanguageElement in project intellij-community by JetBrains.

the class GroovyBlockGenerator method getGroovyChildren.

private static ASTNode[] getGroovyChildren(final ASTNode node) {
    PsiElement psi = node.getPsi();
    if (psi instanceof OuterLanguageElement) {
        TextRange range = node.getTextRange();
        ArrayList<ASTNode> childList = new ArrayList<>();
        PsiFile groovyFile = psi.getContainingFile().getViewProvider().getPsi(GroovyLanguage.INSTANCE);
        if (groovyFile instanceof GroovyFileBase) {
            addChildNodes(groovyFile, childList, range, psi);
        }
        return childList.toArray(new ASTNode[childList.size()]);
    }
    return node.getChildren(null);
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) ArrayList(java.util.ArrayList) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 12 with OuterLanguageElement

use of com.intellij.psi.templateLanguages.OuterLanguageElement in project intellij-community by JetBrains.

the class XmlBlock method createLeafBlocks.

private void createLeafBlocks(ASTNode node, List<Block> result) {
    if (node instanceof OuterLanguageElement) {
        processChild(result, node, null, null, null);
        return;
    }
    ASTNode child = node.getFirstChildNode();
    if (child == null && !(node instanceof PsiWhiteSpace) && node.getElementType() != TokenType.ERROR_ELEMENT && node.getTextLength() > 0) {
        result.add(new ReadOnlyBlock(node));
        return;
    }
    while (child != null) {
        createLeafBlocks(child, result);
        child = child.getTreeNext();
    }
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) ASTNode(com.intellij.lang.ASTNode) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 13 with OuterLanguageElement

use of com.intellij.psi.templateLanguages.OuterLanguageElement in project intellij-community by JetBrains.

the class XmlUnboundNsPrefixInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new XmlElementVisitor() {

        private Boolean isXml;

        private boolean isXmlFile(XmlElement element) {
            if (isXml == null) {
                final PsiFile file = element.getContainingFile();
                isXml = file instanceof XmlFile && !InjectedLanguageManager.getInstance(element.getProject()).isInjectedFragment(file);
            }
            return isXml.booleanValue();
        }

        @Override
        public void visitXmlToken(final XmlToken token) {
            if (isXmlFile(token) && token.getTokenType() == XmlTokenType.XML_NAME) {
                PsiElement element = token.getPrevSibling();
                while (element instanceof PsiWhiteSpace) element = element.getPrevSibling();
                if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_START_TAG_START) {
                    PsiElement parent = element.getParent();
                    if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) {
                        XmlTag tag = (XmlTag) parent;
                        checkUnboundNamespacePrefix(tag, tag, tag.getNamespacePrefix(), token, holder, isOnTheFly);
                    }
                }
            }
        }

        @Override
        public void visitXmlAttribute(final XmlAttribute attribute) {
            if (!isXmlFile(attribute)) {
                return;
            }
            final String namespace = attribute.getNamespace();
            if (attribute.isNamespaceDeclaration() || XmlUtil.XML_SCHEMA_INSTANCE_URI.equals(namespace)) {
                return;
            }
            XmlTag tag = attribute.getParent();
            if (tag == null)
                return;
            XmlElementDescriptor elementDescriptor = tag.getDescriptor();
            if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
                return;
            }
            final String name = attribute.getName();
            checkUnboundNamespacePrefix(attribute, tag, XmlUtil.findPrefixByQualifiedName(name), null, holder, isOnTheFly);
        }

        @Override
        public void visitXmlAttributeValue(XmlAttributeValue value) {
            PsiReference[] references = value.getReferences();
            for (PsiReference reference : references) {
                if (reference instanceof SchemaPrefixReference) {
                    if (!XML.equals(((SchemaPrefixReference) reference).getNamespacePrefix()) && reference.resolve() == null) {
                        holder.registerProblem(reference, XmlErrorMessages.message("unbound.namespace", ((SchemaPrefixReference) reference).getNamespacePrefix()), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
                    }
                }
            }
        }
    };
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with OuterLanguageElement

use of com.intellij.psi.templateLanguages.OuterLanguageElement in project intellij-plugins by JetBrains.

the class AngularJSInjector method getLanguagesToInject.

@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    // check that we have angular directives indexed before injecting
    final Project project = context.getProject();
    if (!AngularIndexUtil.hasAngularJS(project))
        return;
    final PsiElement parent = context.getParent();
    if (context instanceof XmlAttributeValueImpl && parent instanceof XmlAttribute) {
        final String value = context.getText();
        final int start = value.startsWith("'") || value.startsWith("\"") ? 1 : 0;
        final int end = value.endsWith("'") || value.endsWith("\"") ? 1 : 0;
        final int length = value.length();
        if (AngularAttributesRegistry.isAngularExpressionAttribute((XmlAttribute) parent) && length > 1) {
            registrar.startInjecting(AngularJSLanguage.INSTANCE).addPlace(null, null, (PsiLanguageInjectionHost) context, new TextRange(start, length - end)).doneInjecting();
            return;
        }
        if (AngularAttributesRegistry.isJSONAttribute((XmlAttribute) parent) && length > 1) {
            registrar.startInjecting(JsonLanguage.INSTANCE).addPlace(null, null, (PsiLanguageInjectionHost) context, new TextRange(start, length - end)).doneInjecting();
            return;
        }
    }
    if (context instanceof XmlTextImpl || context instanceof XmlAttributeValueImpl) {
        final String start = AngularJSBracesUtil.getInjectionStart(project);
        final String end = AngularJSBracesUtil.getInjectionEnd(project);
        if (AngularJSBracesUtil.hasConflicts(start, end, context))
            return;
        final String text = context.getText();
        int endIndex = -1;
        int afterStart = -1;
        while (true) {
            final int startIdx = text.indexOf(start, Math.max(endIndex, afterStart));
            afterStart = startIdx < 0 ? -1 : (startIdx + start.length());
            if (afterStart < 0)
                return;
            endIndex = afterStart;
            endIndex = InjectorMatchingEndFinder.findMatchingEnd(start, end, text, endIndex);
            endIndex = endIndex > 0 ? endIndex : ElementManipulators.getValueTextRange(context).getEndOffset();
            final PsiElement injectionCandidate = afterStart >= 0 ? context.findElementAt(afterStart) : null;
            if (injectionCandidate != null && injectionCandidate.getNode().getElementType() != XmlTokenType.XML_COMMENT_CHARACTERS && !(injectionCandidate instanceof OuterLanguageElement)) {
                if (afterStart > endIndex) {
                    if (ApplicationManager.getApplication().isInternal()) {
                        LOG.error("Braces: " + start + "," + end + "\n" + "Text: \"" + text + "\"" + "\n" + "Interval: (" + afterStart + "," + endIndex + ")" + "\n" + "File: " + context.getContainingFile().getName() + ", language:" + context.getContainingFile().getLanguage());
                    }
                    return;
                }
                registrar.startInjecting(AngularJSLanguage.INSTANCE).addPlace(null, null, (PsiLanguageInjectionHost) context, new TextRange(afterStart, endIndex)).doneInjecting();
            }
        }
    }
}
Also used : XmlTextImpl(com.intellij.psi.impl.source.xml.XmlTextImpl) Project(com.intellij.openapi.project.Project) OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) XmlAttributeValueImpl(com.intellij.psi.impl.source.xml.XmlAttributeValueImpl)

Aggregations

OuterLanguageElement (com.intellij.psi.templateLanguages.OuterLanguageElement)14 ASTNode (com.intellij.lang.ASTNode)5 PsiElement (com.intellij.psi.PsiElement)5 TextRange (com.intellij.openapi.util.TextRange)4 IElementType (com.intellij.psi.tree.IElementType)4 ArrayList (java.util.ArrayList)4 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 NotNull (org.jetbrains.annotations.NotNull)3 XMLLanguage (com.intellij.lang.xml.XMLLanguage)2 PsiFile (com.intellij.psi.PsiFile)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XmlTag (com.intellij.psi.xml.XmlTag)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)2 Nullable (org.jetbrains.annotations.Nullable)2 WebEditorOptions (com.intellij.application.options.editor.WebEditorOptions)1 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 Language (com.intellij.lang.Language)1 Document (com.intellij.openapi.editor.Document)1 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)1