Search in sources :

Example 56 with AndroidFacet

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

the class AndroidCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet resultSet) {
    PsiElement position = parameters.getPosition();
    PsiElement originalPosition = parameters.getOriginalPosition();
    AndroidFacet facet = AndroidFacet.getInstance(position);
    if (facet == null) {
        return;
    }
    PsiElement parent = position.getParent();
    PsiElement originalParent = originalPosition != null ? originalPosition.getParent() : null;
    if (parent instanceof XmlTag) {
        XmlTag tag = (XmlTag) parent;
        if (tag.getParentTag() != null) {
            return;
        }
        final ASTNode startTagName = XmlChildRole.START_TAG_NAME_FINDER.findChild(tag.getNode());
        if (startTagName == null || startTagName.getPsi() != position) {
            return;
        }
        final PsiFile file = tag.getContainingFile();
        if (!(file instanceof XmlFile)) {
            return;
        }
        final PsiReference reference = file.findReferenceAt(parameters.getOffset());
        if (reference != null) {
            final PsiElement element = reference.getElement();
            if (element != null) {
                final int refOffset = element.getTextRange().getStartOffset() + reference.getRangeInElement().getStartOffset();
                if (refOffset != position.getTextRange().getStartOffset()) {
                    // do not provide completion if we're inside some reference starting in the middle of tag name
                    return;
                }
            }
        }
        if (!completeTagNames(facet, (XmlFile) file, resultSet)) {
            resultSet.stopHere();
        }
    } else if (parent instanceof XmlAttribute) {
        final ASTNode attrName = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild(parent.getNode());
        if (attrName == null || attrName.getPsi() != position) {
            return;
        }
        addAndroidPrefixElement(position, parent, resultSet);
        final XmlAttribute attribute = (XmlAttribute) parent;
        final String namespace = attribute.getNamespace();
        // has already been typed
        if (SdkConstants.TOOLS_URI.equals(namespace)) {
            addDesignTimeAttributes(attribute.getNamespacePrefix(), position, facet, attribute, resultSet);
        }
        customizeAddedAttributes(facet, parameters, attribute, resultSet);
    } else if (originalParent instanceof XmlAttributeValue) {
        completeTailsInFlagAttribute(parameters, resultSet, (XmlAttributeValue) originalParent);
        completeDataBindingTypeAttr(parameters, resultSet, (XmlAttributeValue) originalParent);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 57 with AndroidFacet

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

the class AndroidImplicitUsagesProvider method isImplicitWrite.

@Override
public boolean isImplicitWrite(PsiElement element) {
    if (!(element instanceof PsiField)) {
        return false;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(element);
    if (facet == null) {
        return false;
    }
    final PsiField field = (PsiField) element;
    final PsiModifierList modifierList = field.getModifierList();
    if (modifierList == null) {
        return false;
    }
    for (PsiAnnotation annotation : modifierList.getAnnotations()) {
        for (PsiNameValuePair pair : annotation.getParameterList().getAttributes()) {
            final PsiAnnotationMemberValue value = pair.getValue();
            if (isResourceReference(value)) {
                return true;
            }
        }
    }
    return false;
}
Also used : AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 58 with AndroidFacet

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

the class AndroidProjectComponent method projectOpened.

@Override
public void projectOpened() {
    final CompilerManager manager = CompilerManager.getInstance(myProject);
    manager.addBeforeTask(new AndroidPrecompileTask());
    myDisposable = Disposer.newDisposable(getClass().getName());
    if (!ApplicationManager.getApplication().isUnitTestMode() && !ApplicationManager.getApplication().isHeadlessEnvironment()) {
        if (ProjectFacetManager.getInstance(myProject).hasFacets(AndroidFacet.ID)) {
            createAndroidSpecificComponents();
        } else {
            final MessageBusConnection connection = myProject.getMessageBus().connect(myDisposable);
            connection.subscribe(FacetManager.FACETS_TOPIC, new FacetManagerAdapter() {

                @Override
                public void facetAdded(@NotNull Facet facet) {
                    if (facet instanceof AndroidFacet) {
                        createAndroidSpecificComponents();
                        connection.disconnect();
                    }
                }
            });
        }
    }
}
Also used : AndroidPrecompileTask(org.jetbrains.android.compiler.AndroidPrecompileTask) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) CompilerManager(com.intellij.openapi.compiler.CompilerManager) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) FacetManagerAdapter(com.intellij.facet.FacetManagerAdapter) Facet(com.intellij.facet.Facet) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 59 with AndroidFacet

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

the class AndroidPropertyFilesUpdater method updatePropertyFilesIfNecessary.

private void updatePropertyFilesIfNecessary() {
    if (myProject.isDisposed())
        return;
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    final List<VirtualFile> toAskFiles = new ArrayList<VirtualFile>();
    final List<AndroidFacet> toAskFacets = new ArrayList<AndroidFacet>();
    final List<Runnable> toAskChanges = new ArrayList<Runnable>();
    final List<VirtualFile> files = new ArrayList<VirtualFile>();
    final List<Runnable> changes = new ArrayList<Runnable>();
    for (Module module : ModuleManager.getInstance(myProject).getModules()) {
        final AndroidFacet facet = AndroidFacet.getInstance(module);
        if (facet != null && !facet.requiresAndroidModel()) {
            final String updatePropertyFiles = facet.getProperties().UPDATE_PROPERTY_FILES;
            final boolean ask = updatePropertyFiles.isEmpty();
            if (!ask && !Boolean.parseBoolean(updatePropertyFiles)) {
                continue;
            }
            final Pair<VirtualFile, List<Runnable>> pair = updateProjectPropertiesIfNecessary(facet);
            if (pair != null) {
                if (ask) {
                    toAskFacets.add(facet);
                    toAskFiles.add(pair.getFirst());
                    toAskChanges.addAll(pair.getSecond());
                } else {
                    files.add(pair.getFirst());
                    changes.addAll(pair.getSecond());
                }
            }
        }
    }
    /* We should expire old notification even if there are no properties to update in current event.
     For example, user changed "is library" setting to 'true', the notification was shown, but user ignored it.
     Then he changed the setting to 'false' again. New notification won't be shown, because the value of
     "android.library" in project.properties is correct. However if the old notification was not expired,
     user may press on it, and "android.library" property will be changed to 'false'. */
    if (myNotification != null && !myNotification.isExpired()) {
        myNotification.expire();
    }
    if (changes.size() > 0 || toAskChanges.size() > 0) {
        if (toAskChanges.size() > 0) {
            askUserIfUpdatePropertyFile(myProject, toAskFacets, new Processor<MyResult>() {

                @Override
                public boolean process(MyResult result) {
                    if (result == MyResult.NEVER) {
                        for (AndroidFacet facet : toAskFacets) {
                            facet.getProperties().UPDATE_PROPERTY_FILES = Boolean.FALSE.toString();
                        }
                        return true;
                    } else if (result == MyResult.ALWAYS) {
                        for (AndroidFacet facet : toAskFacets) {
                            facet.getProperties().UPDATE_PROPERTY_FILES = Boolean.TRUE.toString();
                        }
                    }
                    if (ReadonlyStatusHandler.ensureFilesWritable(myProject, toAskFiles.toArray(new VirtualFile[toAskFiles.size()]))) {
                        CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {

                            @Override
                            public void run() {
                                ApplicationManager.getApplication().runWriteAction(new Runnable() {

                                    @Override
                                    public void run() {
                                        for (Runnable change : toAskChanges) {
                                            change.run();
                                        }
                                    }
                                });
                            }
                        }, "Update Android property files", null);
                    }
                    return true;
                }
            });
        }
        if (changes.size() > 0 && ReadonlyStatusHandler.ensureFilesWritable(myProject, files.toArray(new VirtualFile[files.size()]))) {
            CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {

                @Override
                public void run() {
                    ApplicationManager.getApplication().runWriteAction(new Runnable() {

                        @Override
                        public void run() {
                            for (Runnable change : changes) {
                                change.run();
                            }
                        }
                    });
                    CommandProcessor.getInstance().markCurrentCommandAsGlobal(myProject);
                }
            }, "Update Android property files", null);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Module(com.intellij.openapi.module.Module)

Example 60 with AndroidFacet

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

the class ResourceNameConverter method createReferences.

@NotNull
@Override
public PsiReference[] createReferences(GenericDomValue<String> value, PsiElement element, ConvertContext context) {
    final Module module = context.getModule();
    if (module == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    final DomElement parent = value.getParent();
    if (parent instanceof Style) {
        return getReferencesInStyleName((Style) parent, value, facet);
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

AndroidFacet (org.jetbrains.android.facet.AndroidFacet)299 Module (com.intellij.openapi.module.Module)122 VirtualFile (com.intellij.openapi.vfs.VirtualFile)73 NotNull (org.jetbrains.annotations.NotNull)61 Nullable (org.jetbrains.annotations.Nullable)51 Project (com.intellij.openapi.project.Project)39 File (java.io.File)29 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)28 PsiFile (com.intellij.psi.PsiFile)24 XmlFile (com.intellij.psi.xml.XmlFile)20 PsiElement (com.intellij.psi.PsiElement)17 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)16 XmlTag (com.intellij.psi.xml.XmlTag)16 ArrayList (java.util.ArrayList)16 Manifest (org.jetbrains.android.dom.manifest.Manifest)14 IAndroidTarget (com.android.sdklib.IAndroidTarget)13 ResourceFolderType (com.android.resources.ResourceFolderType)11 Configuration (com.android.tools.idea.configurations.Configuration)10 PsiClass (com.intellij.psi.PsiClass)10 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)10