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()));
}
}
}
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);
}
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;
}
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();
}
Aggregations