use of com.intellij.xml.XmlElementDescriptor in project intellij-community by JetBrains.
the class GenerateXmlTagAction method invoke.
@Override
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {
if (!EditorModificationUtil.checkModificationAllowed(editor))
return;
try {
final XmlTag contextTag = getContextTag(editor, file);
if (contextTag == null) {
throw new CommonRefactoringUtil.RefactoringErrorHintException("Caret should be positioned inside a tag");
}
XmlElementDescriptor currentTagDescriptor = contextTag.getDescriptor();
assert currentTagDescriptor != null;
final XmlElementDescriptor[] descriptors = currentTagDescriptor.getElementsDescriptors(contextTag);
Arrays.sort(descriptors, Comparator.comparing(PsiMetaData::getName));
final JBList list = new JBList(descriptors);
list.setCellRenderer(new MyListCellRenderer());
Runnable runnable = () -> {
final XmlElementDescriptor selected = (XmlElementDescriptor) list.getSelectedValue();
new WriteCommandAction.Simple(project, "Generate XML Tag", file) {
@Override
protected void run() {
if (selected == null)
return;
XmlTag newTag = createTag(contextTag, selected);
PsiElement anchor = getAnchor(contextTag, editor, selected);
if (anchor == null) {
// insert it in the cursor position
int offset = editor.getCaretModel().getOffset();
Document document = editor.getDocument();
document.insertString(offset, newTag.getText());
PsiDocumentManager.getInstance(project).commitDocument(document);
newTag = PsiTreeUtil.getParentOfType(file.findElementAt(offset + 1), XmlTag.class, false);
} else {
newTag = (XmlTag) contextTag.addAfter(newTag, anchor);
}
if (newTag != null) {
generateTag(newTag, editor);
}
}
}.execute();
};
if (ApplicationManager.getApplication().isUnitTestMode()) {
XmlElementDescriptor descriptor = ContainerUtil.find(descriptors, xmlElementDescriptor -> xmlElementDescriptor.getName().equals(TEST_THREAD_LOCAL.get()));
list.setSelectedValue(descriptor, false);
runnable.run();
} else {
JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle("Choose Tag Name").setItemChoosenCallback(runnable).setFilteringEnabled(o -> ((XmlElementDescriptor) o).getName()).createPopup().showInBestPositionFor(editor);
}
} catch (CommonRefactoringUtil.RefactoringErrorHintException e) {
HintManager.getInstance().showErrorHint(editor, e.getMessage());
}
}
use of com.intellij.xml.XmlElementDescriptor in project intellij-community by JetBrains.
the class Xsd2InstanceUtils method addVariantsFromRootTag.
public static List<String> addVariantsFromRootTag(XmlTag rootTag) {
PsiMetaData metaData = rootTag.getMetaData();
if (metaData instanceof XmlNSDescriptorImpl) {
XmlNSDescriptorImpl nsDescriptor = (XmlNSDescriptorImpl) metaData;
List<String> elementDescriptors = new ArrayList<>();
XmlElementDescriptor[] rootElementsDescriptors = nsDescriptor.getRootElementsDescriptors(PsiTreeUtil.getParentOfType(rootTag, XmlDocument.class));
for (XmlElementDescriptor e : rootElementsDescriptors) {
elementDescriptors.add(e.getName());
}
return elementDescriptors;
}
return Collections.emptyList();
}
use of com.intellij.xml.XmlElementDescriptor in project intellij-community by JetBrains.
the class XmlInsightTest method testAttributeDescriptor2.
public void testAttributeDescriptor2() throws Exception {
XmlFile file = createFile("<root><a c='' a=''></a></root>");
XmlNSDescriptor descriptor = createDescriptor(file);
XmlTag rootTag = file.getDocument().getRootTag();
XmlElementDescriptor element = descriptor.getElementDescriptor(rootTag);
element = element.getElementsDescriptors(rootTag)[0];
XmlAttributeDescriptor[] attributes = element.getAttributesDescriptors(rootTag);
assertEquals("c", attributes[0].getName());
assertTrue(attributes[0].isRequired());
assertEquals("a", attributes[1].getName());
assertTrue(attributes[1].isRequired());
}
use of com.intellij.xml.XmlElementDescriptor in project intellij-community by JetBrains.
the class XmlInsightTest method testAttributeDescriptor1.
public void testAttributeDescriptor1() throws Exception {
XmlFile file = createFile("<root><a attr1=''></a><a attr2='' attr1=''></a></root>");
XmlNSDescriptor descriptor = createDescriptor(file);
XmlTag rootTag = file.getDocument().getRootTag();
XmlElementDescriptor element = descriptor.getElementDescriptor(rootTag);
element = element.getElementsDescriptors(rootTag)[0];
XmlAttributeDescriptor[] attributes = element.getAttributesDescriptors(rootTag);
assertEquals("attr1", attributes[0].getName());
assertTrue(attributes[0].isRequired());
assertEquals("attr2", attributes[1].getName());
assertTrue(!attributes[1].isRequired());
}
use of com.intellij.xml.XmlElementDescriptor in project intellij-community by JetBrains.
the class XmlInsightTest method testElementDescriptor2.
public void testElementDescriptor2() throws Exception {
XmlFile file = createFile("<root><a><b/></a><a><c/></a></root>");
XmlNSDescriptor descriptor = createDescriptor(file);
XmlTag rootTag = file.getDocument().getRootTag();
XmlElementDescriptor element = descriptor.getElementDescriptor(rootTag);
element = element.getElementsDescriptors(rootTag)[0];
XmlElementDescriptor[] elements = element.getElementsDescriptors(rootTag.getSubTags()[0]);
assertEquals(2, elements.length);
assertEquals("b", elements[0].getName());
assertEquals("c", elements[1].getName());
}
Aggregations