Search in sources :

Example 6 with IdeaSourceProvider

use of org.jetbrains.android.facet.IdeaSourceProvider in project android by JetBrains.

the class ManifestPanel method describePosition.

private void describePosition(@NotNull HtmlBuilder sb, @NotNull AndroidFacet facet, @NotNull SourceFilePosition sourceFilePosition) {
    SourceFile sourceFile = sourceFilePosition.getFile();
    SourcePosition sourcePosition = sourceFilePosition.getPosition();
    File file = sourceFile.getSourceFile();
    if (file == GRADLE_MODEL_MARKER_FILE) {
        VirtualFile gradleBuildFile = GradleUtil.getGradleBuildFile(facet.getModule());
        if (gradleBuildFile != null) {
            file = VfsUtilCore.virtualToIoFile(gradleBuildFile);
            sb.addHtml("<a href=\"");
            sb.add(file.toURI().toString());
            sb.addHtml("\">");
            sb.add(file.getName());
            sb.addHtml("</a>");
            sb.add(" injection");
        } else {
            sb.add("build.gradle injection (source location unknown)");
        }
        return;
    }
    AndroidLibrary library;
    if (file != null) {
        String source = null;
        Module libraryModule = null;
        Module[] modules = ModuleManager.getInstance(facet.getModule().getProject()).getModules();
        VirtualFile vFile = LocalFileSystem.getInstance().findFileByIoFile(file);
        if (vFile != null) {
            Module module = ModuleUtilCore.findModuleForFile(vFile, facet.getModule().getProject());
            if (module != null) {
                if (modules.length >= 2) {
                    source = module.getName();
                }
                // AAR Library?
                if (file.getPath().contains(EXPLODED_AAR)) {
                    AndroidModuleModel androidModel = AndroidModuleModel.get(module);
                    if (androidModel != null) {
                        library = GradleUtil.findLibrary(file.getParentFile(), androidModel.getSelectedVariant(), androidModel.getModelVersion());
                        if (library != null) {
                            if (library.getProject() != null) {
                                libraryModule = GradleUtil.findModuleByGradlePath(facet.getModule().getProject(), library.getProject());
                                if (libraryModule != null) {
                                    module = libraryModule;
                                    source = module.getName();
                                } else {
                                    source = library.getProject();
                                    source = StringUtil.trimStart(source, ":");
                                }
                            } else {
                                MavenCoordinates coordinates = library.getResolvedCoordinates();
                                source = /*coordinates.getGroupId() + ":" +*/
                                coordinates.getArtifactId() + ":" + coordinates.getVersion();
                            }
                        }
                    }
                }
            }
            IdeaSourceProvider provider = ManifestUtils.findManifestSourceProvider(facet, vFile);
            if (provider != null) /*&& !provider.equals(facet.getMainIdeaSourceProvider())*/
            {
                String providerName = provider.getName();
                if (source == null) {
                    source = providerName;
                } else {
                    // "the app main manifest" - "app" is the module name, "main" is the source provider name
                    source = source + " " + providerName;
                }
            }
        }
        if (source == null) {
            source = file.getName();
            if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
                source += ":" + String.valueOf(sourcePosition);
            }
        }
        sb.addHtml("<a href=\"");
        boolean redirected = false;
        if (libraryModule != null) {
            AndroidFacet libraryFacet = AndroidFacet.getInstance(libraryModule);
            if (libraryFacet != null) {
                File manifestFile = libraryFacet.getMainSourceProvider().getManifestFile();
                if (manifestFile.exists()) {
                    sb.add(manifestFile.toURI().toString());
                    redirected = true;
                    // Line numbers probably aren't right
                    sourcePosition = SourcePosition.UNKNOWN;
                // TODO: Set URL which points to the element/attribute path
                }
            }
        }
        if (!redirected) {
            sb.add(file.toURI().toString());
            if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
                sb.add(":");
                sb.add(String.valueOf(sourcePosition.getStartLine()));
                sb.add(":");
                sb.add(String.valueOf(sourcePosition.getStartColumn()));
            }
        }
        sb.addHtml("\">");
        sb.add(source);
        sb.addHtml("</a>");
        sb.add(" manifest");
        if (FileUtil.filesEqual(file, VfsUtilCore.virtualToIoFile(myFile))) {
            sb.add(" (this file)");
        }
        if (!SourcePosition.UNKNOWN.equals(sourcePosition)) {
            sb.add(", line ");
            sb.add(Integer.toString(sourcePosition.getStartLine()));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) MavenCoordinates(com.android.builder.model.MavenCoordinates) AndroidLibrary(com.android.builder.model.AndroidLibrary) SourcePosition(com.android.ide.common.blame.SourcePosition) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) SourceFile(com.android.ide.common.blame.SourceFile) Module(com.intellij.openapi.module.Module) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SourceFile(com.android.ide.common.blame.SourceFile) GradleBuildFile(com.android.tools.idea.gradle.parser.GradleBuildFile) File(java.io.File)

Example 7 with IdeaSourceProvider

use of org.jetbrains.android.facet.IdeaSourceProvider in project android by JetBrains.

the class CreateMissingClassQuickFix method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiPackage aPackage = myPackage.getElement();
    if (aPackage == null) {
        return;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(myModule);
    if (facet == null) {
        return;
    }
    final List<IdeaSourceProvider> providerList = IdeaSourceProvider.getCurrentSourceProviders(facet);
    final List<VirtualFile> javaDirectories = Lists.newArrayList();
    for (IdeaSourceProvider provider : providerList) {
        javaDirectories.addAll(provider.getJavaDirectories());
    }
    final PsiDirectory[] directories = aPackage.getDirectories();
    final List<PsiDirectory> filteredDirectories = Lists.newArrayListWithExpectedSize(directories.length);
    for (PsiDirectory directory : directories) {
        for (VirtualFile file : javaDirectories) {
            if (VfsUtilCore.isAncestor(file, directory.getVirtualFile(), true)) {
                filteredDirectories.add(directory);
                break;
            }
        }
    }
    final PsiDirectory directory;
    switch(filteredDirectories.size()) {
        case 0:
            directory = null;
            break;
        case 1:
            directory = filteredDirectories.get(0);
            break;
        default:
            // There are several directories, present a dialog window for a user to choose a particular destination directory
            final PsiDirectory[] array = filteredDirectories.toArray(new PsiDirectory[filteredDirectories.size()]);
            directory = DirectoryChooserUtil.selectDirectory(aPackage.getProject(), array, filteredDirectories.get(0), "");
    }
    if (directory == null) {
        return;
    }
    final RunResult<PsiClass> result = new WriteCommandAction<PsiClass>(project) {

        @Override
        protected void run(@NotNull Result<PsiClass> result) throws Throwable {
            // Create a new class
            final PsiClass psiClass = JavaDirectoryService.getInstance().createClass(directory, myClassName);
            final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
            // Add a base class to "extends" list
            final PsiReferenceList list = psiClass.getExtendsList();
            if (list != null && myBaseClassFqcn != null) {
                final PsiClass parentClass = facade.findClass(myBaseClassFqcn, GlobalSearchScope.allScope(project));
                if (parentClass != null) {
                    list.add(facade.getElementFactory().createClassReferenceElement(parentClass));
                }
            }
            // Add a "public" modifier, which is absent by default. Required because classes references in AndroidManifest
            // have to point to public classes.
            final PsiModifierList modifierList = psiClass.getModifierList();
            if (modifierList != null) {
                modifierList.setModifierProperty(PsiModifier.PUBLIC, true);
            }
            result.setResult(psiClass);
        }
    }.execute();
    PsiClass aClass = result.getResultObject();
    OpenFileDescriptor fileDescriptor = new OpenFileDescriptor(project, aClass.getContainingFile().getVirtualFile());
    FileEditorManager.getInstance(project).openEditor(fileDescriptor, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 8 with IdeaSourceProvider

use of org.jetbrains.android.facet.IdeaSourceProvider in project android by JetBrains.

the class AndroidTestsNode method contains.

@Override
public boolean contains(@NotNull VirtualFile file) {
    Module module = getValue().getModule();
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null || facet.getAndroidModel() == null) {
        return false;
    }
    for (IdeaSourceProvider provider : IdeaSourceProvider.getCurrentTestSourceProviders(facet)) {
        if (provider.containsFile(file)) {
            return true;
        }
    }
    return false;
}
Also used : IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 9 with IdeaSourceProvider

use of org.jetbrains.android.facet.IdeaSourceProvider in project android by JetBrains.

the class AndroidResFileNode method getQualifier.

@Nullable
String getQualifier() {
    PsiFile resFile = getValue();
    if (resFile == null) {
        // happens if psiFile becomes invalid
        return null;
    }
    PsiDirectory resTypeFolder = resFile.getParent();
    if (resTypeFolder == null) {
        // cannot happen
        return null;
    }
    String folderName = resTypeFolder.getName();
    int index = folderName.indexOf(ResourceConstants.RES_QUALIFIER_SEP);
    String qualifier = index < 0 ? null : folderName.substring(index + 1);
    String providerName = null;
    PsiDirectory resFolder = resTypeFolder.getParent();
    if (resFolder != null) {
        IdeaSourceProvider ideaSourceProvider = findSourceProviderForResFolder(resFolder);
        if (ideaSourceProvider != null) {
            providerName = ideaSourceProvider.getName();
            if (SdkConstants.FD_MAIN.equals(providerName)) {
                providerName = null;
            }
        }
    }
    if (qualifier == null && providerName == null) {
        return null;
    }
    StringBuilder sb = new StringBuilder(10);
    sb.append(" (");
    sb.append(Joiner.on(", ").skipNulls().join(qualifier, providerName));
    sb.append(')');
    return sb.toString();
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) IdeaSourceProvider(org.jetbrains.android.facet.IdeaSourceProvider) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IdeaSourceProvider (org.jetbrains.android.facet.IdeaSourceProvider)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 PsiFile (com.intellij.psi.PsiFile)4 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4 Module (com.intellij.openapi.module.Module)3 Nullable (org.jetbrains.annotations.Nullable)3 SourceFile (com.android.ide.common.blame.SourceFile)2 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)2 XmlFile (com.intellij.psi.xml.XmlFile)2 File (java.io.File)2 AndroidLibrary (com.android.builder.model.AndroidLibrary)1 MavenCoordinates (com.android.builder.model.MavenCoordinates)1 SourcePosition (com.android.ide.common.blame.SourcePosition)1 NamedObject (com.android.tools.idea.gradle.parser.NamedObject)1 HtmlBuilder (com.android.utils.HtmlBuilder)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiManager (com.intellij.psi.PsiManager)1