use of com.intellij.psi.xml.XmlTagValue in project android by JetBrains.
the class ConvertToDpQuickFix method apply.
@Override
public void apply(@NotNull PsiElement startElement, @NotNull PsiElement endElement, @NotNull AndroidQuickfixContexts.Context context) {
if (context instanceof AndroidQuickfixContexts.BatchContext) {
return;
}
final XmlTag tag = PsiTreeUtil.getParentOfType(startElement, XmlTag.class);
final List<Density> densities = new ArrayList<Density>();
for (Density density : Density.values()) {
if (density.getDpiValue() > 0) {
densities.add(density);
}
}
final String[] densityPresentableNames = new String[densities.size()];
String defaultValue = null;
String initialValue = null;
for (int i = 0; i < densities.size(); i++) {
final Density density = densities.get(i);
densityPresentableNames[i] = getLabelForDensity(density);
final int dpi = density.getDpiValue();
if (dpi == 0) {
continue;
}
if (dpi == ourPrevDpi) {
initialValue = densityPresentableNames[i];
} else if (dpi == Density.DEFAULT_DENSITY) {
defaultValue = densityPresentableNames[i];
}
}
if (initialValue == null) {
initialValue = defaultValue;
}
if (initialValue == null) {
return;
}
final int dpi;
if (ApplicationManager.getApplication().isUnitTestMode()) {
dpi = Density.DEFAULT_DENSITY;
} else {
final int selectedIndex = Messages.showChooseDialog("What is the screen density the current px value works with?", "Choose density", densityPresentableNames, initialValue, null);
if (selectedIndex < 0) {
return;
}
dpi = densities.get(selectedIndex).getDpiValue();
}
//noinspection AssignmentToStaticFieldFromInstanceMethod
ourPrevDpi = dpi;
for (XmlAttribute attribute : tag.getAttributes()) {
final String value = attribute.getValue();
if (value != null && value.endsWith("px")) {
final String newValue = convertToDp(value, dpi);
if (newValue != null) {
attribute.setValue(newValue);
}
}
}
final XmlTagValue tagValueElement = tag.getValue();
final String tagValue = tagValueElement.getText();
if (tagValue.endsWith("px")) {
final String newValue = convertToDp(tagValue, dpi);
if (newValue != null) {
tagValueElement.setText(newValue);
}
}
}
use of com.intellij.psi.xml.XmlTagValue in project android by JetBrains.
the class AndroidColorAnnotator method annotate.
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (element instanceof XmlTag) {
XmlTag tag = (XmlTag) element;
String tagName = tag.getName();
if ((ResourceType.COLOR.getName().equals(tagName) || ResourceType.DRAWABLE.getName().equals(tagName) || ResourceType.MIPMAP.getName().equals(tagName))) {
DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
if (domElement instanceof ResourceElement || ApplicationManager.getApplication().isUnitTestMode()) {
String value = tag.getValue().getText().trim();
annotateXml(element, holder, value);
}
} else if (TAG_ITEM.equals(tagName)) {
XmlTagValue value = tag.getValue();
String text = value.getText();
annotateXml(element, holder, text);
}
} else if (element instanceof XmlAttributeValue) {
XmlAttributeValue v = (XmlAttributeValue) element;
String value = v.getValue();
if (value == null || value.isEmpty()) {
return;
}
annotateXml(element, holder, value);
} else if (element instanceof PsiReferenceExpression) {
ResourceReferenceType referenceType = AndroidPsiUtils.getResourceReferenceType(element);
if (referenceType != ResourceReferenceType.NONE) {
// (isResourceReference will return true for both "R.drawable.foo" and the foo literal leaf in the
// same expression, which would result in both elements getting annotated and the icon showing up
// in the gutter twice. Instead we only count the outer one.
ResourceType type = AndroidPsiUtils.getResourceType(element);
if (type == ResourceType.COLOR || type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) {
String name = AndroidPsiUtils.getResourceName(element);
annotateResourceReference(type, holder, element, name, referenceType == ResourceReferenceType.FRAMEWORK);
}
}
}
}
use of com.intellij.psi.xml.XmlTagValue in project android by JetBrains.
the class ResourceResolverCacheTest method test.
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());
}
use of com.intellij.psi.xml.XmlTagValue in project intellij-community by JetBrains.
the class JspxIncludePathReferenceProvider method getReferencesByElement.
@Override
@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
if (element instanceof XmlAttributeValue) {
final XmlAttributeValue attributeValue = ((XmlAttributeValue) element);
String valueString = attributeValue.getValue();
if (valueString.indexOf('?') >= 0)
return getReferencesByString(valueString.substring(0, valueString.indexOf('?')), attributeValue, 1);
return getReferencesByString(valueString, attributeValue, 1);
} else if (element instanceof XmlTag) {
final XmlTag tag = ((XmlTag) element);
final XmlTagValue value = tag.getValue();
final String text = value.getText();
String trimmedText = text.trim();
return getReferencesByString(trimmedText, tag, value.getTextRange().getStartOffset() + text.indexOf(trimmedText) - element.getTextOffset());
}
return PsiReference.EMPTY_ARRAY;
}
use of com.intellij.psi.xml.XmlTagValue in project intellij-community by JetBrains.
the class CheckValidXmlInScriptBodyInspectionBase method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new XmlElementVisitor() {
@Override
public void visitXmlTag(final XmlTag tag) {
if (HtmlUtil.isHtmlTag(tag))
return;
if (HtmlUtil.SCRIPT_TAG_NAME.equals(tag.getName()) || tag instanceof HtmlTag && HtmlUtil.SCRIPT_TAG_NAME.equalsIgnoreCase(tag.getName())) {
final PsiFile psiFile = tag.getContainingFile();
final FileType fileType = psiFile.getFileType();
if (fileType instanceof XmlLikeFileType) {
synchronized (CheckValidXmlInScriptBodyInspectionBase.class) {
if (myXmlLexer == null)
myXmlLexer = new XmlLexer();
final XmlTagValue tagValue = tag.getValue();
final String tagBodyText = tagValue.getText();
if (!tagBodyText.isEmpty()) {
myXmlLexer.start(tagBodyText);
while (myXmlLexer.getTokenType() != null) {
IElementType tokenType = myXmlLexer.getTokenType();
if (tokenType == XmlTokenType.XML_CDATA_START) {
while (tokenType != null && tokenType != XmlTokenType.XML_CDATA_END) {
myXmlLexer.advance();
tokenType = myXmlLexer.getTokenType();
}
if (tokenType == null)
break;
}
if (tokenType == XmlTokenType.XML_BAD_CHARACTER && "&".equals(TreeUtil.getTokenText(myXmlLexer)) || tokenType == XmlTokenType.XML_START_TAG_START) {
final int valueStart = tagValue.getTextRange().getStartOffset();
final int offset = valueStart + myXmlLexer.getTokenStart();
final PsiElement psiElement = psiFile.findElementAt(offset);
final TextRange elementRange = psiElement.getTextRange();
final int offsetInElement = offset - elementRange.getStartOffset();
holder.registerProblem(psiElement, XmlBundle.message("unescaped.xml.character"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, createFix(psiElement, offsetInElement));
int endOfElementInScriptTag = elementRange.getEndOffset() - valueStart;
while (myXmlLexer.getTokenEnd() < endOfElementInScriptTag) {
myXmlLexer.advance();
if (myXmlLexer.getTokenType() == null)
break;
}
}
myXmlLexer.advance();
}
}
}
}
}
}
};
}
Aggregations