use of org.jetbrains.android.facet.IdeaSourceProvider in project android by JetBrains.
the class FmGetAppManifestDirMethod method exec.
@Override
public Object exec(List arguments) throws TemplateModelException {
Module module = findAppModuleIfAny();
if (module == null) {
return null;
}
AndroidFacet facet = AndroidFacet.getInstance(module);
if (facet == null) {
return null;
}
IdeaSourceProvider provider = facet.getMainIdeaSourceProvider();
VirtualFile file = provider.getManifestFile();
if (file == null) {
return null;
}
return file.getParent().getCanonicalPath();
}
use of org.jetbrains.android.facet.IdeaSourceProvider in project android by JetBrains.
the class ManifestPanel method getErrorRemoveHtml.
@NotNull
private static String getErrorRemoveHtml(@NotNull final AndroidFacet facet, @NotNull String message, @NotNull final SourceFilePosition position, @NotNull HtmlLinkManager htmlLinkManager, @Nullable final VirtualFile currentlyOpenFile) {
/*
Example Input:
ERROR Overlay manifest:package attribute declared at AndroidManifest.xml:3:5-49
value=(com.foo.manifestapplication.debug) has a different value=(com.foo.manifestapplication)
declared in main manifest at AndroidManifest.xml:5:5-43 Suggestion: remove the overlay
declaration at AndroidManifest.xml and place it in the build.gradle: flavorName
{ applicationId = "com.foo.manifestapplication.debug" } AndroidManifest.xml (debug)
*/
HtmlBuilder sb = new HtmlBuilder();
int start = message.indexOf('{');
int end = message.indexOf('}', start + 1);
final String declaration = message.substring(start + 1, end).trim();
if (!declaration.startsWith("applicationId")) {
throw new IllegalArgumentException("unexpected remove suggestion format " + message);
}
final GradleBuildFile buildFile = GradleBuildFile.get(facet.getModule());
Runnable link = null;
if (buildFile != null) {
final String applicationId = declaration.substring(declaration.indexOf('"') + 1, declaration.lastIndexOf('"'));
final File manifestOverlayFile = position.getFile().getSourceFile();
assert manifestOverlayFile != null;
VirtualFile manifestOverlayVirtualFile = LocalFileSystem.getInstance().findFileByIoFile(manifestOverlayFile);
assert manifestOverlayVirtualFile != null;
IdeaSourceProvider sourceProvider = ManifestUtils.findManifestSourceProvider(facet, manifestOverlayVirtualFile);
assert sourceProvider != null;
final String name = sourceProvider.getName();
AndroidModuleModel androidModuleModel = AndroidModuleModel.get(facet.getModule());
assert androidModuleModel != null;
final XmlFile manifestOverlayPsiFile = (XmlFile) PsiManager.getInstance(facet.getModule().getProject()).findFile(manifestOverlayVirtualFile);
assert manifestOverlayPsiFile != null;
if (androidModuleModel.getBuildTypeNames().contains(name)) {
final String packageName = MergedManifest.get(facet).getPackage();
assert packageName != null;
if (applicationId.startsWith(packageName)) {
link = () -> new WriteCommandAction.Simple(facet.getModule().getProject(), "Apply manifest suggestion", buildFile.getPsiFile(), manifestOverlayPsiFile) {
@Override
protected void run() throws Throwable {
if (currentlyOpenFile != null) {
// We mark this action as affecting the currently open file, so the Undo is available in this editor
CommandProcessor.getInstance().addAffectedFiles(facet.getModule().getProject(), currentlyOpenFile);
}
removePackageAttribute(manifestOverlayPsiFile);
final String applicationIdSuffix = applicationId.substring(packageName.length());
@SuppressWarnings("unchecked") List<NamedObject> buildTypes = (List<NamedObject>) buildFile.getValue(BuildFileKey.BUILD_TYPES);
if (buildTypes == null) {
buildTypes = new ArrayList<>();
}
NamedObject buildType = find(buildTypes, name);
if (buildType == null) {
buildType = new NamedObject(name);
buildTypes.add(buildType);
}
buildType.setValue(BuildFileKey.APPLICATION_ID_SUFFIX, applicationIdSuffix);
buildFile.setValue(BuildFileKey.BUILD_TYPES, buildTypes);
GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(facet.getModule().getProject(), null);
}
}.execute();
}
} else if (androidModuleModel.getProductFlavorNames().contains(name)) {
link = () -> new WriteCommandAction.Simple(facet.getModule().getProject(), "Apply manifest suggestion", buildFile.getPsiFile(), manifestOverlayPsiFile) {
@Override
protected void run() throws Throwable {
if (currentlyOpenFile != null) {
// We mark this action as affecting the currently open file, so the Undo is available in this editor
CommandProcessor.getInstance().addAffectedFiles(facet.getModule().getProject(), currentlyOpenFile);
}
removePackageAttribute(manifestOverlayPsiFile);
@SuppressWarnings("unchecked") List<NamedObject> flavors = (List<NamedObject>) buildFile.getValue(BuildFileKey.FLAVORS);
assert flavors != null;
NamedObject flavor = find(flavors, name);
assert flavor != null;
flavor.setValue(BuildFileKey.APPLICATION_ID, applicationId);
buildFile.setValue(BuildFileKey.FLAVORS, flavors);
GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(facet.getModule().getProject(), null);
}
}.execute();
}
}
if (link != null) {
sb.addLink(message.substring(0, end + 1), htmlLinkManager.createRunnableLink(link));
sb.add(message.substring(end + 1));
} else {
sb.add(message);
}
return sb.getHtml();
}
use of org.jetbrains.android.facet.IdeaSourceProvider in project android by JetBrains.
the class AndroidManifestFileNode method update.
@Override
public void update(PresentationData data) {
super.update(data);
PsiFile file = getValue();
// could be null if the file was deleted
if (file == null) {
return;
}
// if it is not part of the main source set, then append the provider name
IdeaSourceProvider sourceProvider = getSourceProvider(myFacet, file);
if (sourceProvider != null && !SdkConstants.FD_MAIN.equals(sourceProvider.getName())) {
data.addText(file.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
data.addText(" (" + sourceProvider.getName() + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
data.setPresentableText(file.getName());
}
}
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);
}
Aggregations