@Override @NotNull public TextRange getRangeInElement(@NotNull final XmlTag tag) { if (tag.getSubTags().length > 0) { // Text range in tag with subtags is not supported, return empty range, consider making this function nullable. return TextRange.EMPTY_RANGE; } final XmlTagValue value = tag.getValue(); final XmlText[] texts = value.getTextElements(); switch (texts.length) { case 0: return value.getTextRange().shiftRight(-tag.getTextOffset()); case 1: return getValueRange(texts[0]); default: return TextRange.EMPTY_RANGE; } }
@Nullable private static String getPackageInfoText(XmlFile xmlFile) { final XmlTag rootTag = xmlFile.getRootTag(); if (rootTag == null) { return null; } final PsiElement[] children = rootTag.getChildren(); for (PsiElement child : children) { if (!(child instanceof HtmlTag)) { continue; } final HtmlTag htmlTag = (HtmlTag)child; @NonNls final String name = htmlTag.getName(); if ("body".equalsIgnoreCase(name)) { final XmlTagValue value = htmlTag.getValue(); return value.getText(); } } return null; }
public TextRange getRangeInElement(final XmlTag tag) { if (tag.getSubTags().length > 0) { // Text range in tag with subtags is not supported, return empty range, consider making this function nullable. return TextRange.EMPTY_RANGE; } final XmlTagValue value = tag.getValue(); final XmlText[] texts = value.getTextElements(); switch (texts.length) { case 0: return value.getTextRange().shiftRight(-tag.getTextOffset()); case 1: return getValueRange(texts[0]); default: return TextRange.EMPTY_RANGE; } }
@Nullable private static String getPackageInfoText(XmlFile xmlFile) { final XmlTag rootTag = xmlFile.getRootTag(); if (rootTag == null) { return null; } final PsiElement[] children = rootTag.getChildren(); for (PsiElement child : children) { if (!(child instanceof HtmlTag)) { continue; } final HtmlTag htmlTag = (HtmlTag)child; @NonNls final String name = htmlTag.getName(); if ("body".equals(name)) { final XmlTagValue value = htmlTag.getValue(); return value.getText(); } } return null; }
@Override @NotNull public TextRange getRangeInElement(@NotNull final XmlTag tag) { if(tag.getSubTags().length > 0) { // Text range in tag with subtags is not supported, return empty range, consider making this function nullable. return TextRange.EMPTY_RANGE; } final XmlTagValue value = tag.getValue(); final XmlText[] texts = value.getTextElements(); switch(texts.length) { case 0: return value.getTextRange().shiftRight(-tag.getTextOffset()); case 1: return getValueRange(texts[0]); default: return TextRange.EMPTY_RANGE; } }
public static TextRange[] getValueRanges(@NotNull final XmlTag tag) { final XmlTagValue value = tag.getValue(); final XmlText[] texts = value.getTextElements(); if(texts.length == 0) { return new TextRange[]{value.getTextRange().shiftRight(-tag.getTextOffset())}; } else { final TextRange[] ranges = new TextRange[texts.length]; for(int i = 0; i < texts.length; i++) { ranges[i] = getValueRange(texts[i]); } return ranges; } }
@Nullable private static String getPackageInfoText(XmlFile xmlFile) { final XmlTag rootTag = xmlFile.getRootTag(); if(rootTag == null) { return null; } final PsiElement[] children = rootTag.getChildren(); for(PsiElement child : children) { if(!(child instanceof HtmlTag)) { continue; } final HtmlTag htmlTag = (HtmlTag) child; @NonNls final String name = htmlTag.getName(); if("body".equals(name)) { final XmlTagValue value = htmlTag.getValue(); return value.getText(); } } return null; }
private String localizeDescription(final DomElement dom) { if (dom instanceof Description) { final XmlElement xmlElement = dom.getXmlElement(); if (xmlElement instanceof XmlTag) { final XmlTagValue value = ((XmlTag) xmlElement).getValue(); if (value != null) { return value.getTrimmedText(); } } } return null; }
@Nullable public static DomTarget getTarget(DomElement element, GenericDomValue nameElement) { if (nameElement instanceof GenericAttributeValue) { final GenericAttributeValue genericAttributeValue = (GenericAttributeValue)nameElement; final XmlAttributeValue attributeValue = genericAttributeValue.getXmlAttributeValue(); if (attributeValue == null) { return null; } final int length = attributeValue.getTextLength(); if (length >= 2) { return new DomTarget(element, attributeValue, new TextRange(1, length - 1), nameElement); } } final XmlTag tag = nameElement.getXmlTag(); if (tag == null) { return null; } XmlTagValue tagValue = tag.getValue(); if (StringUtil.isEmpty(tagValue.getTrimmedText())) { return null; } return new DomTarget(element, tag, XmlTagUtil.getTrimmedValueRange(tag), nameElement); }
public static TextRange[] getValueRanges(@NotNull final XmlTag tag) { final XmlTagValue value = tag.getValue(); final XmlText[] texts = value.getTextElements(); if (texts.length == 0) { return new TextRange[] { value.getTextRange().shiftRight(-tag.getTextOffset()) }; } else { final TextRange[] ranges = new TextRange[texts.length]; for (int i = 0; i < texts.length; i++) { ranges[i] = getValueRange(texts[i]); } return ranges; } }
public void testPsiPrimitiveTypeArray() throws Throwable { final MyElement element = createElement("<a><psi-type>int[]</psi-type></a>"); final GenericDomValue value = element.getPsiType(); final XmlTagValue tagValue = value.getXmlTag().getValue(); final int i = tagValue.getText().indexOf(value.getStringValue()); assertReference(value, value.getXmlTag(), tagValue.getTextRange().getStartOffset() + i + "int".length()); }
protected PsiReference assertReference(final GenericDomValue value, PsiElement resolveTo) { final XmlTagValue tagValue = value.getXmlTag().getValue(); final TextRange textRange = tagValue.getTextRange(); final String s = value.getStringValue(); assertNotNull(s); final int i = tagValue.getText().indexOf(s); return assertReference(value, resolveTo, textRange.getStartOffset() + i + s.length()); }
@NotNull public static TextRange getTrimmedValueRange(final @NotNull XmlTag tag) { XmlTagValue tagValue = tag.getValue(); final String text = tagValue.getText(); final String trimmed = text.trim(); final int index = text.indexOf(trimmed); final int startOffset = tagValue.getTextRange().getStartOffset() - tag.getTextRange().getStartOffset() + index; return new TextRange(startOffset, startOffset + trimmed.length()); }
protected PsiReference assertReference(final GenericDomValue value, PsiElement resolveTo) { final XmlTagValue tagValue = value.getXmlTag().getValue(); final TextRange textRange = tagValue.getTextRange(); final String s = value.getStringValue(); final int i = tagValue.getText().indexOf(s); return assertReference(value, resolveTo, textRange.getStartOffset() + i + s.length()); }
public static TextRange[] getValueRanges(final @NotNull XmlTag tag) { final XmlTagValue value = tag.getValue(); final XmlText[] texts = value.getTextElements(); if (texts.length == 0) { return new TextRange[] { value.getTextRange().shiftRight(-tag.getTextOffset()) }; } else { final TextRange[] ranges = new TextRange[texts.length]; for (int i = 0; i < texts.length; i++) { ranges[i] = getValueRange(texts[i]); } return ranges; } }
/** * "_controller" and "_route" * "/_profiler/242e61?panel=request" * * <tr> * <th>_route</th> * <td>foo_route</td> * </tr> */ @NotNull public static Map<String, String> getRequestAttributes(@NotNull Project project, @NotNull String html) { HtmlFileImpl htmlFile = (HtmlFileImpl) PsiFileFactory.getInstance(project).createFileFromText(HTMLLanguage.INSTANCE, html); String[] keys = new String[] {"_controller", "_route"}; Map<String, String> map = new HashMap<>(); PsiTreeUtil.processElements(htmlFile, psiElement -> { if(!(psiElement instanceof XmlTag) || !"th".equals(((XmlTag) psiElement).getName())) { return true; } XmlTagValue keyTag = ((XmlTag) psiElement).getValue(); String key = StringUtils.trim(keyTag.getText()); if(!ArrayUtils.contains(keys, key)) { return true; } XmlTag tdTag = PsiTreeUtil.getNextSiblingOfType(psiElement, XmlTag.class); if(tdTag == null || !"td".equals(tdTag.getName())) { return true; } XmlTagValue valueTag = tdTag.getValue(); String value = valueTag.getText(); if(StringUtils.isBlank(value)) { return true; } // Symfony 3.2 profiler debug? strip html map.put(key, stripHtmlTags(value)); // exit if all item found return map.size() != keys.length; }); return map; }
@Override public PsiReference[] getValueReferences(XmlTag xmlTag, @NotNull String text) { XmlTagValue value = xmlTag.getValue(); XmlText[] elements = value.getTextElements(); if(elements.length == 0 || xmlTag.getSubTags().length > 0) { return PsiReference.EMPTY_ARRAY; } return new PsiReference[]{ new XmlEnumeratedValueReference(xmlTag, this, ElementManipulators.getValueTextRange(xmlTag)) }; }
public static XmlTagValue createXmlTagValue(XmlTag tag) { final List<XmlTagChild> bodyElements = new ArrayList<XmlTagChild>(); tag.processElements(new PsiElementProcessor() { boolean insideBody = false; @Override public boolean execute(@NotNull PsiElement element) { final ASTNode treeElement = element.getNode(); if(insideBody) { if(treeElement != null && treeElement.getElementType() == XmlTokenType.XML_END_TAG_START) { return false; } if(!(element instanceof XmlTagChild)) { return true; } bodyElements.add((XmlTagChild) element); } else if(treeElement != null && treeElement.getElementType() == XmlTokenType.XML_TAG_END) { insideBody = true; } return true; } }, tag); XmlTagChild[] tagChildren = ContainerUtil.toArray(bodyElements, new XmlTagChild[bodyElements.size()]); return new XmlTagValueImpl(tagChildren, tag); }
private boolean doInjectTo(final XmlTag tag) { final XmlTagValue value = tag.getValue(); final XmlTagChild[] tagChildren = value.getChildren(); return tagChildren.length == 1 && (tagChildren[0].getNode().getElementType() == XmlElementType.XML_CDATA || !tagChildren[0].textContains('<')); }
@Override @NotNull public XmlTagValue getValue() { return xmlTag.getValue(); }
@Override @NotNull public XmlTagValue getValue() { return XmlTagValueImpl.createXmlTagValue(this); }
public void test() throws Exception { VirtualFile file1 = myFixture.copyFileToProject("render/layout1.xml", "res/layout/layout1.xml"); VirtualFile file2 = myFixture.copyFileToProject("render/layout2.xml", "res/layout/layout2.xml"); VirtualFile file3 = myFixture.copyFileToProject("javadoc/strings/strings.xml", "res/values/strings.xml"); assertNotNull(file1); assertNotNull(file2); assertNotNull(file3); AndroidFacet facet = AndroidFacet.getInstance(myModule); assertNotNull(facet); Project project = getProject(); PsiFile psiFile1 = PsiManager.getInstance(project).findFile(file1); assertNotNull(psiFile1); PsiFile psiFile2 = PsiManager.getInstance(project).findFile(file2); assertNotNull(psiFile2); final PsiFile psiFile3 = PsiManager.getInstance(project).findFile(file3); assertNotNull(psiFile3); ConfigurationManager configurationManager = facet.getConfigurationManager(); assertNotNull(configurationManager); final Configuration configuration1 = configurationManager.getConfiguration(file1); Configuration configuration2 = configurationManager.getConfiguration(file2); assertNotNull(configuration1.getTheme()); assertEquals(configuration2.getTheme(), configuration1.getTheme()); ResourceResolver resolver1 = configuration1.getResourceResolver(); ResourceResolver resolver2 = configuration2.getResourceResolver(); assertSame(resolver1, resolver2); assertSame(resolver1, configuration1.getResourceResolver()); configuration1.setTheme("Theme.Light"); final ResourceResolver resolver1b = configuration1.getResourceResolver(); assertNotSame(resolver1b, resolver1); assertNotSame(resolver1b, resolver2); assertSame(resolver1b, configuration1.getResourceResolver()); configuration2.setTheme("Theme.Light"); assertSame(resolver1b, configuration2.getResourceResolver()); // Test project resource changes, should invalidate final LocalResourceRepository resources = myFacet.getModuleResources(true); assertNotNull(resources); final long generation = resources.getModificationCount(); assertEquals("Cancel", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue()); WriteCommandAction.runWriteCommandAction(null, new Runnable() { @Override public void run() { //noinspection ConstantConditions XmlTagValue value = ((XmlFile)psiFile3).getRootTag().getSubTags()[1].getValue(); assertEquals("Cancel", value.getTrimmedText()); value.setText("\"FooBar\""); } }); assertTrue(resources.isScanPending(psiFile3)); ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { assertTrue(generation < resources.getModificationCount()); assertNotSame(resolver1b, configuration1.getResourceResolver()); assertEquals("FooBar", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue()); } }); ResourceResolverCache cache = configuration1.getConfigurationManager().getResolverCache(); assertSame(cache, configuration2.getConfigurationManager().getResolverCache()); ResourceRepository frameworkResources = cache.getFrameworkResources(configuration1.getFullConfig(), configuration1.getTarget()); assertTrue(frameworkResources instanceof FrameworkResourceLoader.IdeFrameworkResources); assertTrue(((FrameworkResourceLoader.IdeFrameworkResources)frameworkResources).getSkippedLocales()); }
/** * ["foo/foo.html.twig": 1] * * <tr> * <td>@Twig/Exception/traces_text.html.twig</td> * <td class="font-normal">1</td> * </tr> */ public static Map<String, Integer> getRenderedElementTwigTemplates(@NotNull Project project, @NotNull String html) { HtmlFileImpl htmlFile = (HtmlFileImpl) PsiFileFactory.getInstance(project).createFileFromText(HTMLLanguage.INSTANCE, html); final XmlTag[] xmlTag = new XmlTag[1]; PsiTreeUtil.processElements(htmlFile, psiElement -> { if(!(psiElement instanceof XmlTag) || !"h2".equals(((XmlTag) psiElement).getName())) { return true; } XmlTagValue keyTag = ((XmlTag) psiElement).getValue(); String contents = StringUtils.trim(keyTag.getText()); if(!"Rendered Templates".equalsIgnoreCase(contents)) { return true; } xmlTag[0] = (XmlTag) psiElement; return true; }); if(xmlTag[0] == null) { return Collections.emptyMap(); } XmlTag tableTag = PsiTreeUtil.getNextSiblingOfType(xmlTag[0], XmlTag.class); if(tableTag == null || !"table".equals(tableTag.getName())) { return Collections.emptyMap(); } XmlTag tbody = tableTag.findFirstSubTag("tbody"); if(tbody == null) { return Collections.emptyMap(); } Map<String, Integer> templates = new HashMap<>(); for (XmlTag tag : PsiTreeUtil.getChildrenOfTypeAsList(tbody, XmlTag.class)) { if(!"tr".equals(tag.getName())) { continue; } XmlTag[] tds = tag.findSubTags("td"); if(tds.length < 2) { continue; } String template = stripHtmlTags(StringUtils.trim(tds[0].getValue().getText())); if(StringUtils.isBlank(template)) { continue; } Integer count; try { count = Integer.valueOf(stripHtmlTags(StringUtils.trim(tds[1].getValue().getText()))); } catch (NumberFormatException e) { count = 0; } templates.put(template, count); } return templates; }
boolean process(ComponentType type, XmlTag component, @Nullable XmlTagValue impl, @Nullable XmlTagValue intf);