Search in sources :

Example 46 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class InputExpressionDialog method prepareShow.

@SuppressWarnings({ "unchecked" })
private void prepareShow(XmlElement contextElement) {
    final NamespaceCollector.CollectedInfo collectedInfo;
    if (contextElement != null) {
        collectedInfo = NamespaceCollector.collectInfo((XmlFile) contextElement.getContainingFile());
        myNamespaceCache = collectedInfo.namespaces;
    } else {
        collectedInfo = NamespaceCollector.empty();
        myNamespaceCache = null;
    }
    myContextProvider = new InteractiveContextProvider(contextElement, collectedInfo, myModel);
    myContextProvider.attachTo(myXPathFile);
    final HistoryElement historyElement = myModel.getSelectedItem();
    if (historyElement != null) {
        myContextProvider.getNamespaceContext().setMap(asMap(historyElement.namespaces));
    } else {
        myContextProvider.getNamespaceContext().setMap(asMap(null));
    }
    updateOkAction();
}
Also used : HistoryElement(org.intellij.plugins.xpathView.HistoryElement) NamespaceCollector(org.intellij.plugins.xpathView.util.NamespaceCollector) XmlFile(com.intellij.psi.xml.XmlFile)

Example 47 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class RenameXmlAttributeProcessor method doRenameXmlAttributeValue.

private static void doRenameXmlAttributeValue(@NotNull XmlAttributeValue value, String newName, UsageInfo[] infos, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    LOG.assertTrue(value.isValid());
    renameAll(value, infos, newName, value.getValue());
    PsiManager psiManager = value.getManager();
    LOG.assertTrue(psiManager != null);
    XmlFile file = (XmlFile) PsiFileFactory.getInstance(psiManager.getProject()).createFileFromText("dummy.xml", XMLLanguage.INSTANCE, "<a attr=\"" + newName + "\"/>");
    @SuppressWarnings("ConstantConditions") PsiElement element = value.replace(file.getRootTag().getAttributes()[0].getValueElement());
    if (listener != null) {
        listener.elementRenamed(element);
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiManager(com.intellij.psi.PsiManager) PsiElement(com.intellij.psi.PsiElement)

Example 48 with XmlFile

use of com.intellij.psi.xml.XmlFile 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 49 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class ExtendedTagInsertHandler method handleInsert.

@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
    final XmlFile contextFile = (XmlFile) context.getFile();
    final XmlExtension extension = XmlExtension.getExtension(contextFile);
    final XmlFile file = extension.getContainingFile(contextFile);
    final Project project = context.getProject();
    assert file != null;
    final PsiElement psiElement = file.findElementAt(context.getStartOffset());
    assert psiElement != null;
    if (isNamespaceBound(psiElement)) {
        doDefault(context, item);
        return;
    }
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    final int caretOffset = editor.getCaretModel().getOffset();
    final RangeMarker caretMarker = document.createRangeMarker(caretOffset, caretOffset);
    caretMarker.setGreedyToRight(true);
    final XmlNamespaceHelper.Runner<String, IncorrectOperationException> runAfter = new XmlNamespaceHelper.Runner<String, IncorrectOperationException>() {

        @Override
        public void run(final String namespacePrefix) {
            PsiDocumentManager.getInstance(project).commitDocument(document);
            final PsiElement element = file.findElementAt(context.getStartOffset());
            if (element != null) {
                qualifyWithPrefix(namespacePrefix, element, document);
                PsiDocumentManager.getInstance(project).commitDocument(document);
            }
            editor.getCaretModel().moveToOffset(caretMarker.getEndOffset());
            PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
            doDefault(context, item);
        }
    };
    try {
        final String prefixByNamespace = getPrefixByNamespace(file, myNamespace);
        if (myNamespacePrefix != null || StringUtil.isEmpty(prefixByNamespace)) {
            final String nsPrefix = myNamespacePrefix == null ? suggestPrefix(file, myNamespace) : myNamespacePrefix;
            XmlNamespaceHelper.getHelper(file).insertNamespaceDeclaration(file, editor, Collections.singleton(myNamespace), nsPrefix, runAfter);
            FeatureUsageTracker.getInstance().triggerFeatureUsed(XmlCompletionContributor.TAG_NAME_COMPLETION_FEATURE);
        } else {
            // qualify && complete
            runAfter.run(prefixByNamespace);
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project) XmlExtension(com.intellij.xml.XmlExtension) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 50 with XmlFile

use of com.intellij.psi.xml.XmlFile in project intellij-community by JetBrains.

the class XmlAttributeInsertHandler method handleInsert.

@Override
public void handleInsert(final InsertionContext context, final LookupElement item) {
    final Editor editor = context.getEditor();
    final Document document = editor.getDocument();
    final int caretOffset = editor.getCaretModel().getOffset();
    final PsiFile file = context.getFile();
    final CharSequence chars = document.getCharsSequence();
    final String quote = getAttributeQuote(HtmlUtil.hasHtml(file) || HtmlUtil.supportsXmlTypedHandlers(file));
    final boolean insertQuotes = WebEditorOptions.getInstance().isInsertQuotesForAttributeValue() && StringUtil.isNotEmpty(quote);
    final boolean hasQuotes = CharArrayUtil.regionMatches(chars, caretOffset, "=\"") || CharArrayUtil.regionMatches(chars, caretOffset, "='");
    if (!hasQuotes) {
        PsiElement fileContext = file.getContext();
        String toInsert = null;
        if (fileContext != null) {
            if (fileContext.getText().startsWith("\""))
                toInsert = "=''";
            if (fileContext.getText().startsWith("\'"))
                toInsert = "=\"\"";
        }
        if (toInsert == null) {
            toInsert = "=" + quote + quote;
        }
        if (!insertQuotes)
            toInsert = "=";
        if (caretOffset >= document.getTextLength() || "/> \n\t\r".indexOf(document.getCharsSequence().charAt(caretOffset)) < 0) {
            document.insertString(caretOffset, toInsert + " ");
        } else {
            document.insertString(caretOffset, toInsert);
        }
        if ('=' == context.getCompletionChar()) {
            // IDEA-19449
            context.setAddCompletionChar(false);
        }
    }
    editor.getCaretModel().moveToOffset(caretOffset + (insertQuotes || hasQuotes ? 2 : 1));
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    editor.getSelectionModel().removeSelection();
    AutoPopupController.getInstance(editor.getProject()).scheduleAutoPopup(editor);
    if (myNamespaceToInsert != null && file instanceof XmlFile) {
        final PsiElement element = file.findElementAt(context.getStartOffset());
        final XmlTag tag = element != null ? PsiTreeUtil.getParentOfType(element, XmlTag.class) : null;
        if (tag != null) {
            String prefix = ExtendedTagInsertHandler.suggestPrefix((XmlFile) file, myNamespaceToInsert);
            if (prefix != null) {
                prefix = makePrefixUnique(prefix, tag);
                final XmlNamespaceHelper helper = XmlNamespaceHelper.getHelper(context.getFile());
                if (helper != null) {
                    final Project project = context.getProject();
                    PsiDocumentManager.getInstance(project).commitDocument(document);
                    qualifyWithPrefix(prefix, element);
                    helper.insertNamespaceDeclaration((XmlFile) file, editor, Collections.singleton(myNamespaceToInsert), prefix, null);
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) XmlNamespaceHelper(com.intellij.xml.XmlNamespaceHelper) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlFile (com.intellij.psi.xml.XmlFile)409 XmlTag (com.intellij.psi.xml.XmlTag)155 PsiFile (com.intellij.psi.PsiFile)121 VirtualFile (com.intellij.openapi.vfs.VirtualFile)102 Nullable (org.jetbrains.annotations.Nullable)74 Project (com.intellij.openapi.project.Project)69 NotNull (org.jetbrains.annotations.NotNull)66 PsiElement (com.intellij.psi.PsiElement)64 XmlAttribute (com.intellij.psi.xml.XmlAttribute)39 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)34 Module (com.intellij.openapi.module.Module)34 XmlDocument (com.intellij.psi.xml.XmlDocument)32 Result (com.intellij.openapi.application.Result)28 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)23 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)22 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)21 ArrayList (java.util.ArrayList)20 Document (com.intellij.openapi.editor.Document)19 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)18 Editor (com.intellij.openapi.editor.Editor)15