Search in sources :

Example 61 with PsiWhiteSpace

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

the class LineCommentSelectioner method select.

@Override
public List<TextRange> select(PsiElement element, CharSequence editorText, int cursorOffset, Editor editor) {
    List<TextRange> result = super.select(element, editorText, cursorOffset, editor);
    PsiElement firstComment = element;
    PsiElement e = element;
    while (e.getPrevSibling() != null) {
        if (e instanceof PsiComment) {
            firstComment = e;
        } else if (!(e instanceof PsiWhiteSpace)) {
            break;
        }
        e = e.getPrevSibling();
    }
    PsiElement lastComment = element;
    e = element;
    while (e.getNextSibling() != null) {
        if (e instanceof PsiComment) {
            lastComment = e;
        } else if (!(e instanceof PsiWhiteSpace)) {
            break;
        }
        e = e.getNextSibling();
    }
    result.addAll(expandToWholeLine(editorText, new TextRange(firstComment.getTextRange().getStartOffset(), lastComment.getTextRange().getEndOffset())));
    return result;
}
Also used : PsiComment(com.intellij.psi.PsiComment) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 62 with PsiWhiteSpace

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

the class InspectionUtil method isSuppressedAt.

private static boolean isSuppressedAt(PsiElement anchor, LocalInspectionTool tool) {
    if (anchor == null)
        return false;
    PsiElement prevSibling;
    if (!(anchor instanceof XmlComment)) {
        prevSibling = anchor.getPrevSibling();
        while (prevSibling instanceof PsiWhiteSpace || prevSibling instanceof XmlText) {
            prevSibling = prevSibling.getPrevSibling();
        }
    } else {
        prevSibling = anchor;
    }
    if (prevSibling instanceof XmlProlog) {
        if (prevSibling.getTextLength() > 0 && !"\n".equals(prevSibling.getText())) {
            return isSuppressedAt(prevSibling.getLastChild(), tool);
        } else {
            return isSuppressedAt(prevSibling, tool);
        }
    }
    if (prevSibling instanceof XmlComment) {
        final XmlComment comment = (XmlComment) prevSibling;
        final String text = XmlUtil.getCommentText(comment);
        if (text != null) {
            final Matcher matcher = SUPPRESSION_PATTERN.matcher(text);
            if (matcher.matches()) {
                final String[] strings = matcher.group(1).split(",");
                final String toolId = tool.getID();
                for (String s : strings) {
                    if (s.trim().equals(toolId) || ALL_ID.equals(s.trim()))
                        return true;
                }
            }
        }
    }
    return false;
}
Also used : Matcher(java.util.regex.Matcher) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 63 with PsiWhiteSpace

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

the class SuppressInspectionAction method invoke.

public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final XmlTag anchor = getAnchor(element);
    if (anchor == null)
        return;
    PsiElement prevSibling = anchor.getPrevSibling();
    while (prevSibling instanceof PsiWhiteSpace || prevSibling instanceof XmlText) {
        prevSibling = prevSibling.getPrevSibling();
    }
    if (prevSibling instanceof XmlProlog) {
        prevSibling = prevSibling.getLastChild();
        if (prevSibling != null && !(prevSibling instanceof XmlComment)) {
            prevSibling = PsiTreeUtil.getPrevSiblingOfType(prevSibling, XmlComment.class);
        }
    }
    if (prevSibling instanceof XmlComment) {
        final XmlComment comment = (XmlComment) prevSibling;
        final String text = XmlUtil.getCommentText(comment);
        if (text != null && InspectionUtil.SUPPRESSION_PATTERN.matcher(text).matches()) {
            final String s = text.trim() + ", " + myToolId;
            final XmlComment newComment = createComment(project, s);
            CodeStyleManager.getInstance(PsiManager.getInstance(project).getProject()).reformat(comment.replace(newComment));
        } else {
            addNoinspectionComment(project, anchor);
        }
    } else {
        addNoinspectionComment(project, anchor);
    }
}
Also used : XmlComment(com.intellij.psi.xml.XmlComment) XmlText(com.intellij.psi.xml.XmlText) XmlProlog(com.intellij.psi.xml.XmlProlog) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 64 with PsiWhiteSpace

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

the class ShowXPathAction method isEnabledAt.

protected boolean isEnabledAt(XmlFile xmlFile, int offset) {
    final PsiElement element = xmlFile.findElementAt(offset);
    if (!(element instanceof XmlElement || element instanceof PsiWhiteSpace)) {
        return false;
    }
    final PsiElement node = XPathExpressionGenerator.transformToValidShowPathNode(element);
    return node != null;
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 65 with PsiWhiteSpace

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

the class ShowXPathAction method actionPerformed.

public void actionPerformed(AnActionEvent e) {
    final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    if (editor == null) {
        return;
    }
    final Project project = editor.getProject();
    if (project == null) {
        return;
    }
    final PsiDocumentManager docmgr = PsiDocumentManager.getInstance(project);
    final Document document = editor.getDocument();
    docmgr.commitDocument(document);
    final PsiFile psiFile = docmgr.getPsiFile(document);
    if (!(psiFile instanceof XmlFile)) {
        return;
    }
    final PsiElement element = psiFile.findElementAt(editor.getCaretModel().getOffset());
    if (!(element instanceof XmlElement || element instanceof PsiWhiteSpace)) {
        XPathAppComponent.showEditorHint("No suitable context for an XPath-expression selected.", editor);
        return;
    }
    final PsiElement node = XPathExpressionGenerator.transformToValidShowPathNode(element);
    if (node == null) {
        XPathAppComponent.showEditorHint("No suitable context for an XPath-expression selected.", editor);
        return;
    }
    final Config cfg = XPathAppComponent.getInstance().getConfig();
    final RangeHighlighter h = HighlighterUtil.highlightNode(editor, node, cfg.getContextAttributes(), cfg);
    final String path = XPathSupport.getInstance().getUniquePath((XmlElement) node, null);
    final JTextField label = new JTextField(path);
    label.setPreferredSize(new Dimension(label.getPreferredSize().width + new JLabel("M").getPreferredSize().width, label.getPreferredSize().height));
    label.setOpaque(false);
    label.setEditable(false);
    label.setBorder(null);
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
    final JPanel p = new NonOpaquePanel(new BorderLayout());
    final JLabel l = new JLabel("XPath:");
    p.add(l, BorderLayout.WEST);
    p.add(label, BorderLayout.CENTER);
    InplaceButton copy = new InplaceButton(ActionsBundle.message("action.EditorCopy.text"), PlatformIcons.COPY_ICON, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            CopyPasteManager.getInstance().setContents(new StringSelection(path));
        }
    });
    p.add(copy, BorderLayout.EAST);
    final LightweightHint hint = new LightweightHint(p) {

        public void hide() {
            super.hide();
            HighlighterUtil.removeHighlighter(editor, h);
        }
    };
    final Point point = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
    point.y += editor.getLineHeight() / 2;
    HintHint hintHint = new HintHint(editor, point).setAwtTooltip(true).setContentActive(true).setExplicitClose(true).setShowImmediately(true);
    HintManagerImpl.getInstanceImpl().showEditorHint(hint, editor, point, HintManager.HIDE_BY_ANY_KEY, 0, false, hintHint);
}
Also used : ActionEvent(java.awt.event.ActionEvent) Document(com.intellij.openapi.editor.Document) StringSelection(java.awt.datatransfer.StringSelection) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) NonOpaquePanel(com.intellij.ui.components.panels.NonOpaquePanel) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) XmlFile(com.intellij.psi.xml.XmlFile) LightweightHint(com.intellij.ui.LightweightHint) InplaceButton(com.intellij.ui.InplaceButton) Project(com.intellij.openapi.project.Project) HintHint(com.intellij.ui.HintHint) ActionListener(java.awt.event.ActionListener) XmlElement(com.intellij.psi.xml.XmlElement) Editor(com.intellij.openapi.editor.Editor) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)90 PsiElement (com.intellij.psi.PsiElement)82 TextRange (com.intellij.openapi.util.TextRange)23 ASTNode (com.intellij.lang.ASTNode)15 PsiFile (com.intellij.psi.PsiFile)15 NotNull (org.jetbrains.annotations.NotNull)15 Nullable (org.jetbrains.annotations.Nullable)14 PsiComment (com.intellij.psi.PsiComment)13 IElementType (com.intellij.psi.tree.IElementType)13 Document (com.intellij.openapi.editor.Document)12 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)6 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)5 Editor (com.intellij.openapi.editor.Editor)5 Language (com.intellij.lang.Language)4 UnfairTextRange (com.intellij.openapi.util.UnfairTextRange)4 XmlTag (com.intellij.psi.xml.XmlTag)4 XMLLanguage (com.intellij.lang.xml.XMLLanguage)3 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)3 XmlFile (com.intellij.psi.xml.XmlFile)3