use of com.intellij.xml.index.XsdNamespaceBuilder in project intellij-community by JetBrains.
the class DefaultXmlTagNameProvider method getRootTagsVariants.
private static List<LookupElement> getRootTagsVariants(final XmlTag tag, final List<LookupElement> elements) {
elements.add(LookupElementBuilder.create("?xml version=\"1.0\" encoding=\"\" ?>").withPresentableText("<?xml version=\"1.0\" encoding=\"\" ?>").withInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
int offset = context.getEditor().getCaretModel().getOffset();
context.getEditor().getCaretModel().moveToOffset(offset - 4);
AutoPopupController.getInstance(context.getProject()).scheduleAutoPopup(context.getEditor());
}
}));
final FileBasedIndex fbi = FileBasedIndex.getInstance();
Collection<String> result = new ArrayList<>();
Processor<String> processor = Processors.cancelableCollectProcessor(result);
fbi.processAllKeys(XmlNamespaceIndex.NAME, processor, tag.getProject());
final GlobalSearchScope scope = new EverythingGlobalScope();
for (final String ns : result) {
if (ns.startsWith("file://"))
continue;
fbi.processValues(XmlNamespaceIndex.NAME, ns, null, new FileBasedIndex.ValueProcessor<XsdNamespaceBuilder>() {
@Override
public boolean process(final VirtualFile file, XsdNamespaceBuilder value) {
List<String> tags = value.getRootTags();
for (String s : tags) {
elements.add(LookupElementBuilder.create(s).withTypeText(ns).withInsertHandler(new XmlTagInsertHandler() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
final Editor editor = context.getEditor();
final Document document = context.getDocument();
final int caretOffset = editor.getCaretModel().getOffset();
final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
caretMarker.setGreedyToRight(true);
XmlFile psiFile = (XmlFile) context.getFile();
boolean incomplete = XmlUtil.getTokenOfType(tag, XmlTokenType.XML_TAG_END) == null && XmlUtil.getTokenOfType(tag, XmlTokenType.XML_EMPTY_ELEMENT_END) == null;
XmlNamespaceHelper.getHelper(psiFile).insertNamespaceDeclaration(psiFile, editor, Collections.singleton(ns), null, null);
editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
XmlTag rootTag = psiFile.getRootTag();
if (incomplete) {
XmlToken token = XmlUtil.getTokenOfType(rootTag, XmlTokenType.XML_EMPTY_ELEMENT_END);
// remove tag end added by smart attribute adder :(
if (token != null)
token.delete();
PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document);
super.handleInsert(context, item);
}
}
}));
}
return true;
}
}, scope);
}
return elements;
}
use of com.intellij.xml.index.XsdNamespaceBuilder in project intellij-community by JetBrains.
the class MapExternalResourceDialog method setupSchemasTree.
private void setupSchemasTree(String uri, @NotNull Project project, @Nullable PsiFile file, @Nullable String location) {
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
mySchemasTree.setModel(new DefaultTreeModel(root));
ConfigFileSearcher searcher = new ConfigFileSearcher(file == null ? null : ModuleUtilCore.findModuleForPsiElement(file), project) {
@Override
public Set<PsiFile> search(@Nullable Module module, @NotNull Project project) {
List<IndexedRelevantResource<String, XsdNamespaceBuilder>> resources = XmlNamespaceIndex.getAllResources(module, project, null);
HashSet<PsiFile> files = new HashSet<>();
PsiManager psiManager = PsiManager.getInstance(project);
for (IndexedRelevantResource<String, XsdNamespaceBuilder> resource : resources) {
VirtualFile file = resource.getFile();
PsiFile psiFile = psiManager.findFile(file);
ContainerUtil.addIfNotNull(files, psiFile);
}
return files;
}
};
searcher.search();
new ConfigFilesTreeBuilder(mySchemasTree).buildTree(root, searcher);
TreeUtil.expandAll(mySchemasTree);
mySchemasTree.setRootVisible(false);
mySchemasTree.setShowsRootHandles(true);
ColoredTreeCellRenderer renderer = new ColoredTreeCellRenderer() {
@Override
public void customizeCellRenderer(@NotNull JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
ConfigFilesTreeBuilder.renderNode(value, expanded, this);
}
};
renderer.setFont(EditorColorsManager.getInstance().getGlobalScheme().getFont(EditorFontType.PLAIN));
mySchemasTree.setCellRenderer(renderer);
MouseAdapter mouseAdapter = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() > 1 && isOKActionEnabled()) {
doOKAction();
}
}
};
mySchemasTree.addMouseListener(mouseAdapter);
mySchemasTree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
validateInput();
}
});
mySchemasTree.setSelectionRow(0);
PsiFile schema = null;
if (file != null) {
schema = XmlUtil.findNamespaceByLocation(file, uri);
} else if (location != null) {
VirtualFile virtualFile = VfsUtilCore.findRelativeFile(location, null);
if (virtualFile != null) {
schema = PsiManager.getInstance(project).findFile(virtualFile);
}
}
if (schema != null) {
DefaultMutableTreeNode node = TreeUtil.findNodeWithObject(root, schema);
if (node != null) {
mySchemaFound = true;
TreeUtil.selectNode(mySchemasTree, node);
}
myFileTextField.setText(schema.getVirtualFile().getCanonicalPath());
}
}
Aggregations