use of com.intellij.psi.xml.XmlProlog 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.XmlProlog in project intellij-community by JetBrains.
the class UpdateJspxFileCopyright method scanFile.
protected void scanFile() {
logger.debug("updating " + getFile().getVirtualFile());
XmlDocument doc = ((XmlFile) getFile()).getDocument();
XmlProlog xmlProlog = doc.getProlog();
if (xmlProlog == null) {
return;
}
PsiElement elem = xmlProlog.getFirstChild();
PsiElement docTypeStart = null;
while (elem != null) {
if (elem instanceof XmlDoctype) {
docTypeStart = elem;
break;
}
elem = getNextSibling(elem);
}
PsiElement first = xmlProlog.getFirstChild();
int location = getLanguageOptions().getFileLocation();
if (docTypeStart != null) {
final ArrayList<PsiComment> comments = new ArrayList<>();
collectComments(doc.getFirstChild(), xmlProlog, comments);
collectComments(first, docTypeStart, comments);
checkComments(first, location == XmlOptions.LOCATION_BEFORE_DOCTYPE, comments);
checkComments(docTypeStart, doc.getRootTag(), location == XmlOptions.LOCATION_BEFORE_ROOTTAG);
return;
} else if (location == XmlOptions.LOCATION_BEFORE_DOCTYPE) {
location = XmlOptions.LOCATION_BEFORE_ROOTTAG;
}
final ArrayList<PsiComment> comments = new ArrayList<>();
collectComments(doc.getFirstChild(), xmlProlog, comments);
collectComments(first, doc.getRootTag(), comments);
checkComments(doc.getRootTag(), location == XmlOptions.LOCATION_BEFORE_ROOTTAG, comments);
}
use of com.intellij.psi.xml.XmlProlog in project intellij-community by JetBrains.
the class GenerateDTDAction method getHandler.
@Override
@NotNull
protected CodeInsightActionHandler getHandler() {
return new CodeInsightActionHandler() {
@Override
public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
final XmlDocument document = findSuitableXmlDocument(file);
if (document != null) {
@NonNls final StringBuilder buffer = new StringBuilder();
buffer.append("<!DOCTYPE " + document.getRootTag().getName() + " [\n");
buffer.append(XmlUtil.generateDocumentDTD(document, true));
buffer.append("]>\n");
XmlFile tempFile;
try {
final XmlProlog prolog = document.getProlog();
final PsiElement childOfType = PsiTreeUtil.getChildOfType(prolog, XmlProcessingInstruction.class);
if (childOfType != null) {
final String text = childOfType.getText();
buffer.insert(0, text);
final PsiElement nextSibling = childOfType.getNextSibling();
if (nextSibling instanceof PsiWhiteSpace) {
buffer.insert(text.length(), nextSibling.getText());
}
}
tempFile = (XmlFile) PsiFileFactory.getInstance(file.getProject()).createFileFromText("dummy.xml", buffer.toString());
prolog.replace(tempFile.getDocument().getProlog());
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
};
}
Aggregations