use of com.intellij.psi.xml.XmlAttribute 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.XmlAttribute in project intellij-community by JetBrains.
the class Html5CustomAttributeDescriptorsProvider method getAttributeDescriptors.
@Override
public XmlAttributeDescriptor[] getAttributeDescriptors(XmlTag tag) {
if (tag == null || !HtmlUtil.isHtml5Context(tag)) {
return XmlAttributeDescriptor.EMPTY;
}
final List<String> currentAttrs = new ArrayList<>();
for (XmlAttribute attribute : tag.getAttributes()) {
currentAttrs.add(attribute.getName());
}
final Project project = tag.getProject();
final Collection<String> keys = CachedValuesManager.getManager(project).getCachedValue(project, () -> {
final Collection<String> keys1 = FileBasedIndex.getInstance().getAllKeys(Html5CustomAttributesIndex.INDEX_ID, project);
final GlobalSearchScope scope = GlobalSearchScope.allScope(project);
return CachedValueProvider.Result.<Collection<String>>create(ContainerUtil.filter(keys1, key -> !FileBasedIndex.getInstance().processValues(Html5CustomAttributesIndex.INDEX_ID, key, null, (file, value) -> false, scope)), PsiModificationTracker.MODIFICATION_COUNT);
});
if (keys.isEmpty())
return XmlAttributeDescriptor.EMPTY;
final List<XmlAttributeDescriptor> result = new ArrayList<>();
for (String key : keys) {
boolean add = true;
for (String attr : currentAttrs) {
if (attr.startsWith(key)) {
add = false;
}
}
if (add) {
result.add(new AnyXmlAttributeDescriptor(key));
}
}
return result.toArray(new XmlAttributeDescriptor[result.size()]);
}
use of com.intellij.psi.xml.XmlAttribute 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.XmlAttribute 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.XmlAttribute in project intellij-community by JetBrains.
the class FormReferenceProvider method processNestedFormReference.
private static void processNestedFormReference(final XmlTag tag, final PsiReferenceProcessor processor, final PsiPlainTextFile file) {
final XmlAttribute formFileAttribute = tag.getAttribute(UIFormXmlConstants.ATTRIBUTE_FORM_FILE, null);
if (formFileAttribute != null) {
processPackageReferences(file, processor, formFileAttribute);
processor.execute(new ResourceFileReference(file, getValueRange(formFileAttribute)));
}
}
Aggregations