Search in sources :

Example 1 with XsdNamespaceBuilder

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) EverythingGlobalScope(com.intellij.psi.search.EverythingGlobalScope) XsdNamespaceBuilder(com.intellij.xml.index.XsdNamespaceBuilder) RangeMarker(com.intellij.openapi.editor.RangeMarker) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Document(com.intellij.openapi.editor.Document) XmlToken(com.intellij.psi.xml.XmlToken) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) XmlTagInsertHandler(com.intellij.codeInsight.completion.XmlTagInsertHandler) Editor(com.intellij.openapi.editor.Editor) FileBasedIndex(com.intellij.util.indexing.FileBasedIndex) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with XsdNamespaceBuilder

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());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreeSelectionListener(javax.swing.event.TreeSelectionListener) NotNull(org.jetbrains.annotations.NotNull) IndexedRelevantResource(com.intellij.xml.index.IndexedRelevantResource) ConfigFilesTreeBuilder(com.intellij.xml.config.ConfigFilesTreeBuilder) PsiFile(com.intellij.psi.PsiFile) HashSet(java.util.HashSet) ColoredTreeCellRenderer(com.intellij.ui.ColoredTreeCellRenderer) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) PsiManager(com.intellij.psi.PsiManager) XsdNamespaceBuilder(com.intellij.xml.index.XsdNamespaceBuilder) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) Project(com.intellij.openapi.project.Project) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable) ConfigFileSearcher(com.intellij.xml.config.ConfigFileSearcher)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 XsdNamespaceBuilder (com.intellij.xml.index.XsdNamespaceBuilder)2 InsertHandler (com.intellij.codeInsight.completion.InsertHandler)1 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)1 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)1 XmlTagInsertHandler (com.intellij.codeInsight.completion.XmlTagInsertHandler)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 PsiFile (com.intellij.psi.PsiFile)1 PsiManager (com.intellij.psi.PsiManager)1 EverythingGlobalScope (com.intellij.psi.search.EverythingGlobalScope)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 XmlToken (com.intellij.psi.xml.XmlToken)1 ColoredTreeCellRenderer (com.intellij.ui.ColoredTreeCellRenderer)1