use of com.intellij.psi.xml.XmlText in project android by JetBrains.
the class AndroidBaseXmlRefactoringAction method getExtractableRange.
@Nullable
private static Pair<PsiElement, PsiElement> getExtractableRange(PsiFile file, int start, int end) {
PsiElement startElement = file.findElementAt(start);
PsiElement parent = startElement != null ? startElement.getParent() : null;
while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getStartOffset() == startElement.getTextRange().getStartOffset()) {
startElement = parent;
parent = parent.getParent();
}
PsiElement endElement = file.findElementAt(end - 1);
parent = endElement != null ? endElement.getParent() : null;
while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getEndOffset() == endElement.getTextRange().getEndOffset()) {
endElement = parent;
parent = parent.getParent();
}
if (startElement == null || endElement == null) {
return null;
}
final PsiElement commonParent = startElement.getParent();
if (commonParent == null || !(commonParent instanceof XmlTag) || commonParent != endElement.getParent()) {
return null;
}
PsiElement e = startElement;
boolean containTag = false;
while (e != null) {
if (!(e instanceof XmlText) && !(e instanceof XmlTag) && !(e instanceof PsiWhiteSpace) && !(e instanceof PsiComment)) {
return null;
}
if (e instanceof XmlTag) {
containTag = true;
}
if (e == endElement) {
break;
}
e = e.getNextSibling();
}
return e != null && containTag ? Pair.create(startElement, endElement) : null;
}
use of com.intellij.psi.xml.XmlText 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.xml.XmlText in project intellij-community by JetBrains.
the class InlineXslAttribute method invoke.
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final int offset = editor.getCaretModel().getOffset();
final PsiElement element = file.findElementAt(offset);
final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
assert tag != null;
final StringBuilder sb = new StringBuilder();
final PsiElement[] children = tag.getChildren();
for (PsiElement child : children) {
if (child instanceof XmlText) {
final XmlText text = (XmlText) child;
if (text.getText().trim().length() > 0) {
sb.append(text.getText().replaceAll("\"", """));
}
} else if (child instanceof XmlTag) {
final XmlTag t = (XmlTag) child;
if (XsltSupport.isXsltTag(t)) {
if ("text".equals(t.getLocalName())) {
sb.append(t.getValue().getText().replaceAll("\"", """));
} else if ("value-of".equals(t.getLocalName())) {
sb.append("{").append(t.getAttributeValue("select")).append("}");
} else {
assert false;
}
}
}
}
final XmlTag p = findParent(tag);
if (p != null) {
final String value = tag.getAttributeValue("name");
p.setAttribute(value, sb.toString());
// TODO: deal with namespace prefix mapping
tag.delete();
}
}
use of com.intellij.psi.xml.XmlText in project intellij-community by JetBrains.
the class InlineXslAttribute method isAvailable.
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!XsltSupport.isXsltFile(file))
return false;
final int offset = editor.getCaretModel().getOffset();
final PsiElement element = file.findElementAt(offset);
final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false);
if (tag == null) {
return false;
}
if (!XsltSupport.isXsltTag(tag)) {
return false;
} else if (!"attribute".equals(tag.getLocalName())) {
return false;
} else if (tag.getAttribute("name") == null) {
return false;
} else if (findParent(tag) == null) {
// we cannot "inline" anything that isn't clearly the child of either an xsl:element or a literal result element
return false;
}
final ASTNode node = tag.getNode();
if (node == null)
return false;
final ASTNode nameNode = XmlChildRole.START_TAG_NAME_FINDER.findChild(node);
if (nameNode == null || !nameNode.getTextRange().contains(offset)) {
return false;
}
final XmlTag[] texts = tag.findSubTags("text", XsltSupport.XSLT_NS);
final XmlTag[] exprs = tag.findSubTags("value-of", XsltSupport.XSLT_NS);
final PsiElement[] children = tag.getChildren();
for (PsiElement child : children) {
if (child instanceof XmlText) {
final XmlText text = (XmlText) child;
if (text.getText().trim().length() == 0) {
if (texts.length == 0 && exprs.length == 0) {
return false;
}
}
} else if (child instanceof XmlTag) {
final XmlTag t = (XmlTag) child;
if (XsltSupport.isXsltTag(t)) {
if ("text".equals(t.getLocalName())) {
} else if ("value-of".equals(t.getLocalName())) {
if (t.getAttribute("select") == null) {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
}
return true;
}
use of com.intellij.psi.xml.XmlText in project intellij-community by JetBrains.
the class MavenDependenciesCompletionProvider method fillCompletionVariants.
@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
PsiElement xmlText = parameters.getPosition().getParent();
if (!(xmlText instanceof XmlText))
return;
PsiElement eDependencyTag = xmlText.getParent();
if (!(eDependencyTag instanceof XmlTag))
return;
XmlTag dependencyTag = (XmlTag) eDependencyTag;
if (!"dependency".equals(dependencyTag.getName()))
return;
if (!PsiImplUtil.isLeafElementOfType(xmlText.getPrevSibling(), XmlTokenType.XML_TAG_END) || !PsiImplUtil.isLeafElementOfType(xmlText.getNextSibling(), XmlTokenType.XML_END_TAG_START)) {
return;
}
Project project = dependencyTag.getProject();
DomElement domElement = DomManager.getDomManager(project).getDomElement(dependencyTag);
if (!(domElement instanceof MavenDomDependency)) {
return;
}
MavenProjectIndicesManager indicesManager = MavenProjectIndicesManager.getInstance(project);
for (String groupId : indicesManager.getGroupIds()) {
for (String artifactId : indicesManager.getArtifactIds(groupId)) {
LookupElement builder = LookupElementBuilder.create(groupId + ':' + artifactId).withIcon(AllIcons.Nodes.PpLib).withInsertHandler(MavenDependencyInsertHandler.INSTANCE);
result.addElement(builder);
}
}
}
Aggregations