use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.
the class AddAttributeValueIntentionFix method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
final XmlAttribute attribute = PsiTreeUtil.getNonStrictParentOfType(startElement, XmlAttribute.class);
if (attribute == null || attribute.getValue() != null) {
return;
}
final XmlAttribute attributeWithValue = XmlElementFactory.getInstance(project).createAttribute(attribute.getName(), "", startElement);
final PsiElement newAttribute = attribute.replace(attributeWithValue);
if (editor != null && newAttribute != null && newAttribute instanceof XmlAttribute && newAttribute.isValid()) {
final XmlAttributeValue valueElement = ((XmlAttribute) newAttribute).getValueElement();
if (valueElement != null) {
editor.getCaretModel().moveToOffset(valueElement.getTextOffset());
AutoPopupController.getInstance(newAttribute.getProject()).scheduleAutoPopup(editor);
}
}
}
use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.
the class XmlChooseColorIntentionAction method chooseColor.
public static void chooseColor(JComponent editorComponent, PsiElement element) {
String caption = CodeInsightBundle.message("intention.color.chooser.dialog");
final XmlAttributeValue literal = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class, false);
if (literal == null)
return;
final String text = StringUtil.unquoteString(literal.getValue());
Color oldColor;
try {
oldColor = Color.decode(text);
} catch (NumberFormatException e) {
oldColor = JBColor.GRAY;
}
Color color = ColorChooser.chooseColor(editorComponent, caption, oldColor, true);
if (color == null)
return;
if (!Comparing.equal(color, oldColor)) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(element))
return;
final String newText = "#" + ColorUtil.toHex(color);
final PsiManager manager = literal.getManager();
final XmlAttribute newAttribute = XmlElementFactory.getInstance(manager.getProject()).createAttribute("name", newText, element);
final Runnable replaceRunnable = () -> {
final XmlAttributeValue valueElement = newAttribute.getValueElement();
assert valueElement != null;
literal.replace(valueElement);
};
new WriteCommandAction(element.getProject(), caption) {
@Override
protected void run(@NotNull Result result) throws Throwable {
replaceRunnable.run();
}
}.execute();
}
}
use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.
the class FormReferenceProvider method processReferences.
private static void processReferences(final PsiPlainTextFile file, final PsiReferenceProcessor processor) {
final Project project = file.getProject();
final XmlTag rootTag = ApplicationManager.getApplication().runReadAction(new Computable<XmlTag>() {
public XmlTag compute() {
final XmlFile xmlFile = (XmlFile) PsiFileFactory.getInstance(project).createFileFromText("a.xml", XmlFileType.INSTANCE, file.getViewProvider().getContents());
return xmlFile.getRootTag();
}
});
if (rootTag == null || !Utils.FORM_NAMESPACE.equals(rootTag.getNamespace())) {
return;
}
@NonNls final String name = rootTag.getName();
if (!"form".equals(name)) {
return;
}
PsiReference classReference = null;
final XmlAttribute classToBind = rootTag.getAttribute("bind-to-class", null);
if (classToBind != null) {
// reference to class
final XmlAttributeValue valueElement = classToBind.getValueElement();
if (valueElement == null) {
return;
}
final String className = valueElement.getValue().replace('$', '.');
final PsiReference[] referencesByString = new JavaClassReferenceProvider().getReferencesByString(className, file, valueElement.getTextRange().getStartOffset() + 1);
if (referencesByString.length < 1) {
// There are no references there
return;
}
for (PsiReference aReferencesByString : referencesByString) {
processor.execute(aReferencesByString);
}
classReference = referencesByString[referencesByString.length - 1];
}
final PsiReference finalClassReference = classReference;
ApplicationManager.getApplication().runReadAction(() -> processReferences(rootTag, finalClassReference, file, processor));
}
use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.
the class FormReferenceProvider method getValueRange.
private static TextRange getValueRange(final XmlAttribute classToBind) {
final XmlAttributeValue valueElement = classToBind.getValueElement();
final TextRange textRange = valueElement.getTextRange();
// skip " "
return new TextRange(textRange.getStartOffset() + 1, textRange.getEndOffset() - 1);
}
use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.
the class XsltValidator method isUnused.
private static boolean isUnused(PsiElement obj, Query<PsiReference> query) {
if (obj instanceof XsltParameter) {
final Collection<PsiReference> references = query.findAll();
int n = references.size();
for (PsiReference reference : references) {
final PsiElement element = reference.getElement();
if (element instanceof XmlAttributeValue) {
final XmlAttribute parent = (XmlAttribute) element.getParent();
if ("name".equals(parent.getName())) {
final XmlTag tag = parent.getParent();
if (tag != null && "with-param".equals(tag.getLocalName())) {
n--;
}
}
}
}
return n == 0;
} else {
return query.findFirst() == null;
}
}
Aggregations