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;
}
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;
}
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);
}
}
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;
}
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);
}
Aggregations