Search in sources :

Example 21 with XmlToken

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

the class UserColorLookup method handleUserSelection.

private static void handleUserSelection(InsertionContext context, @NotNull Function<Color, String> colorToStringConverter) {
    Project project = context.getProject();
    Editor editor = context.getEditor();
    int startOffset = context.getStartOffset();
    context.getDocument().deleteString(startOffset, context.getTailOffset());
    PsiElement element = context.getFile().findElementAt(editor.getCaretModel().getOffset());
    Color myColorAtCaret = element instanceof XmlToken ? getColorFromElement(element) : null;
    context.setLaterRunnable(() -> {
        if (editor.isDisposed() || project.isDisposed())
            return;
        List<ColorPickerListener> listeners = ColorPickerListenerFactory.createListenersFor(element);
        Color color = ColorChooser.chooseColor(WindowManager.getInstance().suggestParentWindow(project), XmlBundle.message("choose.color.dialog.title"), myColorAtCaret, true, listeners, true);
        if (color != null) {
            WriteCommandAction.runWriteCommandAction(project, () -> {
                editor.getCaretModel().moveToOffset(startOffset);
                EditorModificationUtil.insertStringAtCaret(editor, colorToStringConverter.fun(color));
            });
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) ColorPickerListener(com.intellij.ui.ColorPickerListener) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken)

Example 22 with XmlToken

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

the class XmlTokenManipulator method handleContentChange.

@Override
public XmlToken handleContentChange(@NotNull XmlToken xmlToken, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    String oldText = xmlToken.getText();
    String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset());
    IElementType tokenType = xmlToken.getTokenType();
    FileElement holder = DummyHolderFactory.createHolder(xmlToken.getManager(), null).getTreeElement();
    LeafElement leaf = ASTFactory.leaf(tokenType, holder.getCharTable().intern(newText));
    holder.rawAddChildren(leaf);
    return (XmlToken) xmlToken.replace(leaf.getPsi());
}
Also used : IElementType(com.intellij.psi.tree.IElementType) FileElement(com.intellij.psi.impl.source.tree.FileElement) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) XmlToken(com.intellij.psi.xml.XmlToken)

Example 23 with XmlToken

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

the class HtmlClosingTagErrorFilter method shouldHighlightErrorElement.

@Override
public boolean shouldHighlightErrorElement(@NotNull final PsiErrorElement element) {
    final PsiFile psiFile = element.getContainingFile();
    if (psiFile == null || psiFile.getViewProvider().getBaseLanguage() != HTMLLanguage.INSTANCE && HTMLLanguage.INSTANCE != element.getLanguage())
        return true;
    final PsiElement[] children = element.getChildren();
    if (children.length > 0) {
        if (children[0] instanceof XmlToken && XmlTokenType.XML_END_TAG_START == ((XmlToken) children[0]).getTokenType()) {
            if (XmlErrorMessages.message("xml.parsing.closing.tag.matches.nothing").equals(element.getErrorDescription())) {
                return false;
            }
        }
    }
    return true;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken)

Example 24 with XmlToken

use of com.intellij.psi.xml.XmlToken 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 25 with XmlToken

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

the class RemoveExtraClosingTagIntentionAction method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    final int offset = editor.getCaretModel().getOffset();
    final PsiElement psiElement = file.findElementAt(offset);
    if (psiElement == null || !psiElement.isValid() || !(psiElement instanceof XmlToken)) {
        return;
    }
    doFix(psiElement);
}
Also used : PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken)

Aggregations

XmlToken (com.intellij.psi.xml.XmlToken)29 PsiElement (com.intellij.psi.PsiElement)18 XmlTag (com.intellij.psi.xml.XmlTag)12 TextRange (com.intellij.openapi.util.TextRange)6 IElementType (com.intellij.psi.tree.IElementType)6 Nullable (org.jetbrains.annotations.Nullable)5 NotNull (org.jetbrains.annotations.NotNull)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 PsiErrorElement (com.intellij.psi.PsiErrorElement)3 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 ArrayList (java.util.ArrayList)3 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2 ResourceType (com.android.resources.ResourceType)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 HtmlTag (com.intellij.psi.html.HtmlTag)2