Search in sources :

Example 16 with PsiErrorElement

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

the class XmlTextContextType method isInContext.

@Override
public boolean isInContext(@NotNull PsiFile file, int offset) {
    if (!XmlContextType.isInXml(file, offset))
        return false;
    PsiElement element = file.findElementAt(offset);
    if (element == null)
        return false;
    if (PsiTreeUtil.getParentOfType(element, XmlText.class, false) != null) {
        return true;
    }
    PsiElement parent = element.getParent();
    if (parent instanceof PsiErrorElement) {
        parent = parent.getParent();
    }
    return parent instanceof XmlDocument;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) XmlText(com.intellij.psi.xml.XmlText) XmlDocument(com.intellij.psi.xml.XmlDocument) PsiElement(com.intellij.psi.PsiElement)

Example 17 with PsiErrorElement

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

the class XmlWrongClosingTagNameInspection method annotate.

@Override
public void annotate(@NotNull final PsiElement psiElement, @NotNull final AnnotationHolder holder) {
    if (psiElement instanceof XmlToken) {
        final PsiElement parent = psiElement.getParent();
        if (parent instanceof XmlTag) {
            final XmlTag tag = (XmlTag) parent;
            final XmlToken start = XmlTagUtil.getStartTagNameElement(tag);
            XmlToken endTagName = XmlTagUtil.getEndTagNameElement(tag);
            if (start == psiElement) {
                if (endTagName != null && !(tag instanceof HtmlTag) && !tag.getName().equals(endTagName.getText())) {
                    registerProblemStart(holder, tag, start, endTagName);
                } else if (endTagName == null && !(tag instanceof HtmlTag && HtmlUtil.isSingleHtmlTag(tag.getName()))) {
                    final PsiErrorElement errorElement = PsiTreeUtil.getChildOfType(tag, PsiErrorElement.class);
                    endTagName = findEndTagName(errorElement);
                    if (endTagName != null) {
                        registerProblemStart(holder, tag, start, endTagName);
                    }
                }
            } else if (endTagName == psiElement) {
                if (!(tag instanceof HtmlTag) && !tag.getName().equals(endTagName.getText())) {
                    registerProblemEnd(holder, tag, endTagName);
                }
            }
        } else if (parent instanceof PsiErrorElement) {
            if (XmlTokenType.XML_NAME == ((XmlToken) psiElement).getTokenType()) {
                final PsiFile psiFile = psiElement.getContainingFile();
                if (psiFile != null && (HTMLLanguage.INSTANCE == psiFile.getViewProvider().getBaseLanguage() || HTMLLanguage.INSTANCE == parent.getLanguage())) {
                    final String message = XmlErrorMessages.message("xml.parsing.closing.tag.matches.nothing");
                    if (message.equals(((PsiErrorElement) parent).getErrorDescription()) && psiFile.getContext() == null) {
                        final Annotation annotation = holder.createWarningAnnotation(parent, message);
                        annotation.registerFix(new RemoveExtraClosingTagIntentionAction());
                    }
                }
            }
        }
    }
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) HtmlTag(com.intellij.psi.html.HtmlTag) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Annotation(com.intellij.lang.annotation.Annotation) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Example 18 with PsiErrorElement

use of com.intellij.psi.PsiErrorElement in project kotlin by JetBrains.

the class CodegenTestFiles method create.

@NotNull
public static CodegenTestFiles create(@NotNull String fileName, @NotNull String contentWithDiagnosticMarkup, @NotNull Project project) {
    String content = CheckerTestUtil.parseDiagnosedRanges(contentWithDiagnosticMarkup, new ArrayList<CheckerTestUtil.DiagnosedRange>());
    KtFile file = KotlinTestUtils.createFile(fileName, content, project);
    List<PsiErrorElement> ranges = AnalyzingUtils.getSyntaxErrorRanges(file);
    assert ranges.isEmpty() : "Syntax errors found in " + file + ": " + ranges;
    List<Pair<String, String>> expectedValues = Lists.newArrayList();
    Matcher matcher = Pattern.compile("// expected: (\\S+): (.*)").matcher(content);
    while (matcher.find()) {
        String fieldName = matcher.group(1);
        String expectedValue = matcher.group(2);
        expectedValues.add(Pair.create(fieldName, expectedValue));
    }
    List<Object> scriptParameterValues = Lists.newArrayList();
    if (file.isScript()) {
        Pattern scriptParametersPattern = Pattern.compile("param: (\\S.*)");
        Matcher scriptParametersMatcher = scriptParametersPattern.matcher(file.getText());
        if (scriptParametersMatcher.find()) {
            String valueString = scriptParametersMatcher.group(1);
            String[] values = valueString.split(" ");
            scriptParameterValues.add(values);
        } else {
            scriptParameterValues.add(ArrayUtil.EMPTY_STRING_ARRAY);
        }
    }
    KotlinScriptDefinitionProvider.getInstance(project).addScriptDefinition(StandardScriptDefinition.INSTANCE);
    return new CodegenTestFiles(Collections.singletonList(file), expectedValues, scriptParameterValues);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PsiErrorElement(com.intellij.psi.PsiErrorElement) KtFile(org.jetbrains.kotlin.psi.KtFile) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with PsiErrorElement

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

the class HtmlMissingClosingTagInspection method checkTag.

@Override
protected void checkTag(@NotNull XmlTag tag, @NotNull ProblemsHolder holder, boolean isOnTheFly) {
    if (!(tag instanceof HtmlTag) || !XmlHighlightVisitor.shouldBeValidated(tag)) {
        return;
    }
    final PsiElement child = tag.getLastChild();
    if (child instanceof PsiErrorElement) {
        return;
    }
    final XmlToken tagNameElement = XmlTagUtil.getStartTagNameElement(tag);
    if (tagNameElement == null) {
        return;
    }
    final String tagName = tagNameElement.getText();
    if (HtmlUtil.isSingleHtmlTag(tagName) || XmlTagUtil.getEndTagNameElement(tag) != null) {
        return;
    }
    holder.registerProblem(tagNameElement, XmlErrorMessages.message("element.missing.end.tag"), new MissingClosingTagFix(tagName));
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) HtmlTag(com.intellij.psi.html.HtmlTag) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken)

Example 20 with PsiErrorElement

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

the class CfmlTypedHandler method insertCloseTagIfNeeded.

public static boolean insertCloseTagIfNeeded(Editor editor, PsiFile file, Project project) {
    final Document document = editor.getDocument();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    int offset = editor.getCaretModel().getOffset();
    documentManager.commitDocument(document);
    char charAtOffset = DocumentUtils.getCharAt(document, offset);
    if (charAtOffset != '>') {
        EditorModificationUtil.insertStringAtCaret(editor, ">", true, 0);
    }
    EditorModificationUtil.moveCaretRelatively(editor, 1);
    ++offset;
    if (DocumentUtils.getCharAt(document, offset - 2) == '/') {
        return false;
    }
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 2);
    while (!iterator.atEnd() && !iterator.getTokenType().equals(CfmlTokenTypes.CF_TAG_NAME)) {
        if (CfmlUtil.isControlToken(iterator.getTokenType())) {
            return false;
        }
        iterator.retreat();
    }
    if (!iterator.atEnd()) {
        iterator.retreat();
        if (!iterator.atEnd() && iterator.getTokenType().equals(CfmlTokenTypes.LSLASH_ANGLEBRACKET)) {
            return false;
        }
        iterator.advance();
    }
    if (iterator.atEnd()) {
        return false;
    }
    String tagName = document.getCharsSequence().subSequence(iterator.getStart(), iterator.getEnd()).toString();
    if (CfmlUtil.isSingleCfmlTag(tagName, project) || CfmlUtil.isUserDefined(tagName)) {
        return false;
    }
    PsiElement tagElement = file.findElementAt(iterator.getStart());
    while (tagElement != null && !(tagElement instanceof CfmlTag)) {
        tagElement = tagElement.getParent();
    }
    if (tagElement == null) {
        return false;
    }
    boolean doInsertion = false;
    if (tagElement.getLastChild() instanceof PsiErrorElement) {
        doInsertion = true;
    } else {
        iterator = ((EditorEx) editor).getHighlighter().createIterator(0);
        while (!iterator.atEnd() && iterator.getStart() < offset) {
            if (iterator.getTokenType() == CfmlTokenTypes.CF_TAG_NAME) {
                String currentTagName = document.getCharsSequence().subSequence(iterator.getStart(), iterator.getEnd()).toString();
                if (tagName.equals(currentTagName)) {
                    PsiElement currentTagElement = file.findElementAt(iterator.getStart());
                    currentTagElement = PsiTreeUtil.getParentOfType(currentTagElement, CfmlTag.class);
                    if (currentTagElement.getLastChild() instanceof PsiErrorElement) {
                        doInsertion = true;
                        break;
                    }
                }
            }
            iterator.advance();
        }
    }
    // tag name in lowercase
    String tagNameFromPsi = ((CfmlTag) tagElement).getTagName();
    if (doInsertion && CfmlUtil.isEndTagRequired(tagNameFromPsi, project)) {
        if (!Comparing.equal(tagNameFromPsi, tagName, false)) {
            // use tagName because it has proper case
            tagName = tagNameFromPsi;
        }
        EditorModificationUtil.insertStringAtCaret(editor, "</" + tagName + ">", true, 0);
        return true;
    }
    return false;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CfmlTag(com.intellij.coldFusion.model.psi.CfmlTag) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Aggregations

PsiErrorElement (com.intellij.psi.PsiErrorElement)20 PsiElement (com.intellij.psi.PsiElement)12 XmlTag (com.intellij.psi.xml.XmlTag)4 PsiFile (com.intellij.psi.PsiFile)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)3 XmlToken (com.intellij.psi.xml.XmlToken)3 NotNull (org.jetbrains.annotations.NotNull)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 HtmlTag (com.intellij.psi.html.HtmlTag)2 IElementType (com.intellij.psi.tree.IElementType)2 XmlDocument (com.intellij.psi.xml.XmlDocument)2 XmlText (com.intellij.psi.xml.XmlText)2 CfmlTag (com.intellij.coldFusion.model.psi.CfmlTag)1 EquivalenceDescriptorProvider (com.intellij.dupLocator.equivalence.EquivalenceDescriptorProvider)1 Language (com.intellij.lang.Language)1 Annotation (com.intellij.lang.annotation.Annotation)1 HTMLLanguage (com.intellij.lang.html.HTMLLanguage)1 XMLLanguage (com.intellij.lang.xml.XMLLanguage)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1