Search in sources :

Example 6 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class PsiResourceFile method removeItem.

@Override
public void removeItem(ResourceItem item) {
    String key = item.getKey();
    if (myDuplicates != null) {
        Collection<ResourceItem> prev = myDuplicates.get(key);
        if (prev != null && prev.contains(item)) {
            myDuplicates.remove(key, item);
            if (myDuplicates.isEmpty()) {
                myDuplicates = null;
            }
            item.setSource(null);
            return;
        }
    }
    mItems.remove(key);
    item.setSource(null);
    // If we removed an item and we have duplicates in the wings, shift one of those into the prime position
    if (myDuplicates != null) {
        Collection<ResourceItem> prev = myDuplicates.get(key);
        if (prev != null && !prev.isEmpty()) {
            ResourceItem first = prev.iterator().next();
            myDuplicates.remove(key, first);
            mItems.put(key, first);
            if (myDuplicates.isEmpty()) {
                myDuplicates = null;
            }
        }
    }
}
Also used : ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 7 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class PsiResourceItem method getSource.

@Nullable
@Override
public ResourceFile getSource() {
    ResourceFile source = super.getSource();
    // Temporary safety workaround
    if (source == null && myFile != null && myFile.getParent() != null) {
        PsiDirectory parent = myFile.getParent();
        if (parent != null) {
            String name = parent.getName();
            ResourceFolderType folderType = ResourceFolderType.getFolderType(name);
            FolderConfiguration configuration = FolderConfiguration.getConfigForFolder(name);
            int index = name.indexOf('-');
            String qualifiers = index == -1 ? "" : name.substring(index + 1);
            source = new PsiResourceFile(myFile, Collections.<ResourceItem>singletonList(this), qualifiers, folderType, configuration);
            setSource(source);
        }
    }
    return source;
}
Also used : ResourceFile(com.android.ide.common.res2.ResourceFile) ResourceFolderType(com.android.resources.ResourceFolderType) PsiDirectory(com.intellij.psi.PsiDirectory) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class LocalResourceRepository method getItemPsiFile.

/** Returns the {@link PsiFile} corresponding to the source of the given resource item, if possible */
@Nullable
public static PsiFile getItemPsiFile(@NonNull Project project, @NonNull ResourceItem item) {
    if (project.isDisposed()) {
        return null;
    }
    if (item instanceof PsiResourceItem) {
        PsiResourceItem psiResourceItem = (PsiResourceItem) item;
        return psiResourceItem.getPsiFile();
    }
    ResourceFile source = item.getSource();
    if (source == null) {
        // most likely a dynamically defined value
        return null;
    }
    if (source instanceof PsiResourceFile) {
        PsiResourceFile prf = (PsiResourceFile) source;
        return prf.getPsiFile();
    }
    File file = source.getFile();
    VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file);
    if (virtualFile != null) {
        PsiManager psiManager = PsiManager.getInstance(project);
        return psiManager.findFile(virtualFile);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFile(com.android.ide.common.res2.ResourceFile) PsiManager(com.intellij.psi.PsiManager) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) ResourceFile(com.android.ide.common.res2.ResourceFile) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class LayoutDataBindingInfo method getViewsWithIds.

@Override
public List<ViewWithId> getViewsWithIds() {
    Collection<ResourceItem> resourceItems = myPsiResourceFile.getItems();
    List<ViewWithId> result = Lists.newArrayList();
    for (ResourceItem item : resourceItems) {
        if (!ResourceType.ID.equals(item.getType())) {
            continue;
        }
        if (!(item instanceof PsiResourceItem)) {
            continue;
        }
        PsiResourceItem psiResourceItem = (PsiResourceItem) item;
        if (psiResourceItem.getTag() == null) {
            continue;
        }
        String name = item.getName();
        if (StringUtil.isEmpty(name)) {
            continue;
        }
        result.add(new ViewWithId(DataBindingUtil.convertToJavaFieldName(name.trim()), psiResourceItem.getTag()));
    }
    return result;
}
Also used : ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 10 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class GradleDependencyManagerTest method ignore_testDependencyAarIsExplodedForLayoutLib.

@SuppressWarnings("unused")
public void ignore_testDependencyAarIsExplodedForLayoutLib() throws Exception {
    loadSimpleApplication();
    List<GradleCoordinate> dependencies = Collections.singletonList(RECYCLER_VIEW_DEPENDENCY);
    GradleDependencyManager dependencyManager = GradleDependencyManager.getInstance(getProject());
    assertThat(dependencyManager.findMissingDependencies(myModules.getAppModule(), dependencies)).isNotEmpty();
    Messages.setTestDialog(new TestMessagesDialog(Messages.OK));
    boolean found = dependencyManager.ensureLibraryIsIncluded(myModules.getAppModule(), dependencies, null);
    assertThat(found).isTrue();
    List<ResourceItem> items = AppResourceRepository.getAppResources(myAndroidFacet, true).getResourceItem(ResourceType.DECLARE_STYLEABLE, "RecyclerView");
    assertThat(items).isNotEmpty();
    assertThat(dependencyManager.findMissingDependencies(myModules.getAppModule(), dependencies)).isEmpty();
}
Also used : TestMessagesDialog(com.android.tools.idea.testing.TestMessagesDialog) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) ResourceItem(com.android.ide.common.res2.ResourceItem)

Aggregations

ResourceItem (com.android.ide.common.res2.ResourceItem)83 VirtualFile (com.intellij.openapi.vfs.VirtualFile)44 PsiFile (com.intellij.psi.PsiFile)35 Document (com.intellij.openapi.editor.Document)26 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)26 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)11 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)11 XmlTag (com.intellij.psi.xml.XmlTag)11 NotNull (org.jetbrains.annotations.NotNull)11 IOException (java.io.IOException)10 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)8 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)8 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)7 ResourceFile (com.android.ide.common.res2.ResourceFile)7 ResourceType (com.android.resources.ResourceType)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)7 Nullable (org.jetbrains.annotations.Nullable)7 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)6 Project (com.intellij.openapi.project.Project)6 File (java.io.File)6