Search in sources :

Example 1 with XmlElementFactory

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

the class CreatePatternFix method doFix.

private void doFix() throws IncorrectOperationException {
    final XmlTag tag = PsiTreeUtil.getParentOfType(myReference.getElement(), XmlTag.class);
    assert tag != null;
    final XmlTag defineTag = tag.createChildTag("define", ApplicationLoader.RNG_NAMESPACE, "\n \n", false);
    defineTag.setAttribute("name", myReference.getCanonicalText());
    final RngGrammar grammar = ((DefinitionReference) myReference).getScope();
    if (grammar == null)
        return;
    final XmlTag root = grammar.getXmlTag();
    if (root == null)
        return;
    final XmlTag[] tags = root.getSubTags();
    for (XmlTag xmlTag : tags) {
        if (PsiTreeUtil.isAncestor(xmlTag, tag, false)) {
            final XmlElementFactory ef = XmlElementFactory.getInstance(tag.getProject());
            final XmlText text = ef.createDisplayText(" ");
            final PsiElement e = root.addAfter(text, xmlTag);
            root.addAfter(defineTag, e);
            return;
        }
    }
    root.add(defineTag);
}
Also used : XmlElementFactory(com.intellij.psi.XmlElementFactory) XmlText(com.intellij.psi.xml.XmlText) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) RngGrammar(org.intellij.plugins.relaxNG.xml.dom.RngGrammar)

Example 2 with XmlElementFactory

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

the class RegExResponseHandler method parseIssues.

@NotNull
@Override
public Task[] parseIssues(@NotNull String response, int max) throws Exception {
    final List<String> placeholders = getPlaceholders(myTaskRegex);
    if (!placeholders.contains(ID_PLACEHOLDER) || !placeholders.contains(SUMMARY_PLACEHOLDER)) {
        throw new Exception("Incorrect Task Pattern");
    }
    final String taskPatternWithoutPlaceholders = myTaskRegex.replaceAll("\\{.+?\\}", "");
    Matcher matcher = Pattern.compile(taskPatternWithoutPlaceholders, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL | Pattern.UNICODE_CASE | Pattern.CANON_EQ).matcher(response);
    List<Task> tasks = new ArrayList<>();
    for (int i = 0; i < max && matcher.find(); i++) {
        String id = matcher.group(placeholders.indexOf(ID_PLACEHOLDER) + 1);
        String summary = matcher.group(placeholders.indexOf(SUMMARY_PLACEHOLDER) + 1);
        // temporary workaround to make AssemblaIntegrationTestPass
        final String finalSummary = summary;
        summary = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

            @Override
            public String compute() {
                XmlElementFactory factory = XmlElementFactory.getInstance(ProjectManager.getInstance().getDefaultProject());
                XmlTag text = factory.createTagFromText("<a>" + finalSummary + "</a>");
                String trimmedText = text.getValue().getTrimmedText();
                return XmlUtil.decode(trimmedText);
            }
        });
        tasks.add(new GenericTask(id, summary, myRepository));
    }
    return tasks.toArray(new Task[tasks.size()]);
}
Also used : Task(com.intellij.tasks.Task) Matcher(java.util.regex.Matcher) XmlElementFactory(com.intellij.psi.XmlElementFactory) ArrayList(java.util.ArrayList) Computable(com.intellij.openapi.util.Computable) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with XmlElementFactory

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

the class XsltNamespaceContext method getUnresolvedNamespaceFixesStatic.

public static IntentionAction[] getUnresolvedNamespaceFixesStatic(PsiReference reference, String localName) {
    final XmlElementFactory factory = XmlElementFactory.getInstance(reference.getElement().getProject());
    final XmlTag tag = factory.createTagFromText("<" + reference.getCanonicalText() + ":" + localName + " />", XMLLanguage.INSTANCE);
    final XmlFile xmlFile = PsiTreeUtil.getContextOfType(reference.getElement(), XmlFile.class, true);
    return new IntentionAction[] { new MyCreateNSDeclarationAction(tag, reference.getCanonicalText(), xmlFile) };
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlElementFactory(com.intellij.psi.XmlElementFactory) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) XmlTag(com.intellij.psi.xml.XmlTag)

Example 4 with XmlElementFactory

use of com.intellij.psi.XmlElementFactory in project android by JetBrains.

the class NlModel method createComponent.

/**
   * Creates a new component of the given type. It will optionally insert it as a child of the given parent (and optionally
   * right before the given sibling or null to append at the end.)
   * <p/>
   * Note: This operation can only be called when the caller is already holding a write lock. This will be the
   * case from {@link ViewHandler} callbacks such as {@link ViewHandler#onCreate} and {@link DragHandler#commit}.
   *
   * @param screenView The target screen, if known. Used to handle pixel to dp computations in view handlers, etc.
   * @param fqcn       The fully qualified name of the widget to insert, such as {@code android.widget.LinearLayout}.
   *                   You can also pass XML tags here (this is typically the same as the fully qualified class name
   *                   of the custom view, but for Android framework views in the android.view or android.widget packages,
   *                   you can omit the package.)
   * @param parent     The optional parent to add this component to
   * @param before     The sibling to insert immediately before, or null to append
   * @param insertType The type of insertion
   */
public NlComponent createComponent(@Nullable ScreenView screenView, @NotNull String fqcn, @Nullable NlComponent parent, @Nullable NlComponent before, @NotNull InsertType insertType) {
    String tagName = NlComponent.viewClassToTag(fqcn);
    XmlTag tag;
    if (parent != null) {
        // Creating a component intended to be inserted into an existing layout
        tag = parent.getTag().createChildTag(tagName, null, null, false);
    } else {
        // Creating a component not yet inserted into a layout. Typically done when trying to perform
        // a drag from palette, etc.
        XmlElementFactory elementFactory = XmlElementFactory.getInstance(getProject());
        // SIZES?
        String text = "<" + fqcn + " xmlns:android=\"http://schemas.android.com/apk/res/android\"/>";
        tag = elementFactory.createTagFromText(text);
    }
    return createComponent(screenView, tag, parent, before, insertType);
}
Also used : XmlElementFactory(com.intellij.psi.XmlElementFactory) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with XmlElementFactory

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

the class AddXsiSchemaLocationForExtResourceAction method doIt.

private static void doIt(final PsiFile file, final Editor editor, final String uri, final XmlTag tag, final String s) throws IncorrectOperationException {
    if (!FileModificationService.getInstance().prepareFileForWrite(file))
        return;
    final XmlElementFactory elementFactory = XmlElementFactory.getInstance(file.getProject());
    if (tag.getAttributeValue(XMLNS_XSI_ATTR_NAME) == null) {
        tag.add(elementFactory.createXmlAttribute(XMLNS_XSI_ATTR_NAME, XmlUtil.XML_SCHEMA_INSTANCE_URI));
    }
    final XmlAttribute locationAttribute = tag.getAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME);
    final String toInsert = uri + " " + s;
    int offset = s.length();
    if (locationAttribute == null) {
        tag.add(elementFactory.createXmlAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME, toInsert));
    } else {
        final String newValue = locationAttribute.getValue() + "\n" + toInsert;
        locationAttribute.setValue(newValue);
    }
    PsiDocumentManager.getInstance(file.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument());
    CodeStyleManager.getInstance(file.getProject()).reformat(tag);
    @SuppressWarnings("ConstantConditions") final TextRange range = tag.getAttribute(XSI_SCHEMA_LOCATION_ATTR_NAME).getValueElement().getTextRange();
    final TextRange textRange = new TextRange(range.getEndOffset() - offset - 1, range.getEndOffset() - 1);
    editor.getCaretModel().moveToOffset(textRange.getStartOffset());
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlElementFactory(com.intellij.psi.XmlElementFactory) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

XmlElementFactory (com.intellij.psi.XmlElementFactory)12 XmlTag (com.intellij.psi.xml.XmlTag)9 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 HashMap (com.intellij.util.containers.HashMap)2 Map (java.util.Map)2 NotNull (org.jetbrains.annotations.NotNull)2 ViewInfo (com.android.ide.common.rendering.api.ViewInfo)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 Project (com.intellij.openapi.project.Project)1 Computable (com.intellij.openapi.util.Computable)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlText (com.intellij.psi.xml.XmlText)1 Task (com.intellij.tasks.Task)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1