Search in sources :

Example 71 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project android by JetBrains.

the class AndroidPropertyFilesUpdater method createProperty.

// workaround for behavior of Android SDK , which uses non-escaped ':' characters
@NotNull
private static IProperty createProperty(@NotNull Project project, @NotNull String targetPropertyValue) {
    final String text = AndroidUtils.ANDROID_TARGET_PROPERTY + "=" + targetPropertyValue;
    final PropertiesFile dummyFile = PropertiesElementFactory.createPropertiesFile(project, text);
    return dummyFile.getProperties().get(0);
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) NotNull(org.jetbrains.annotations.NotNull)

Example 72 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-plugins by JetBrains.

the class SocketInputHandlerImpl method getResourceBundle.

private void getResourceBundle() throws IOException {
    initResultFile();
    final boolean fromModuleSource = reader.readBoolean();
    final int moduleId = readEntityId();
    final String locale = reader.readUTF();
    final String bundleName = reader.readUTF();
    final ModuleInfo moduleInfo = Client.getInstance().getRegisteredModules().getNullableInfo(moduleId);
    PropertiesFile resourceBundle = null;
    int sourceId = -1;
    if (moduleInfo == null) {
        // project may be closed, but client is not closed yet (AppTest#testCloseAndOpenProject)
        LOG.warn("Skip getResourceBundle(" + locale + ", " + bundleName + ") due to cannot find module with id " + moduleId);
    } else {
        if (fromModuleSource) {
            resourceBundle = getResourceBundleFromModuleSource(moduleInfo.getModule(), bundleName);
            sourceId = moduleId;
        } else {
            Pair<PropertiesFile, Integer> bundleInfo = LibraryManager.getInstance().getResourceBundleFile(locale, bundleName, moduleInfo);
            if (bundleInfo != null) {
                resourceBundle = bundleInfo.first;
                sourceId = bundleInfo.second;
            }
        }
    }
    @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") final FileOutputStream fileOut = new FileOutputStream(resultFile);
    final AccessToken token = ReadAction.start();
    try {
        writeResourceBundle(resourceBundle, fileOut, sourceId);
    } finally {
        token.finish();
        fileOut.close();
    }
}
Also used : AccessToken(com.intellij.openapi.application.AccessToken) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Example 73 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-plugins by JetBrains.

the class SocketInputHandlerImpl method getResourceBundleFromModuleSource.

private static PropertiesFile getResourceBundleFromModuleSource(Module module, final String bundleName) {
    final AccessToken token = ReadAction.start();
    try {
        final PsiManager psiManager = PsiManager.getInstance(module.getProject());
        final List<VirtualFile> result = new ArrayList<>();
        FileTypeIndex.processFiles(PropertiesFileType.INSTANCE, file -> {
            if (file.getNameWithoutExtension().equals(bundleName)) {
                result.add(file);
                if (file.getParent().getName().equals("en_US")) {
                    return false;
                }
            }
            return true;
        }, module.getModuleScope(false));
        PropertiesFile defaultResourceBundle = null;
        for (VirtualFile file : result) {
            PsiFile psiFile = psiManager.findFile(file);
            if (psiFile != null) {
                if (file.getParent().getName().equals("en_US")) {
                    defaultResourceBundle = (PropertiesFile) psiFile;
                } else {
                    return (PropertiesFile) psiFile;
                }
            }
        }
        return defaultResourceBundle;
    } finally {
        token.finish();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AccessToken(com.intellij.openapi.application.AccessToken) ArrayList(java.util.ArrayList) PsiManager(com.intellij.psi.PsiManager) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PsiFile(com.intellij.psi.PsiFile)

Example 74 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-plugins by JetBrains.

the class InjectedPsiVisitor method processResourceDirective.

private ValueWriter processResourceDirective(JSAttribute attribute) {
    String key = null;
    PropertiesFile bundle = null;
    for (JSAttributeNameValuePair p : attribute.getValues()) {
        final String name = p.getName();
        if ("key".equals(name)) {
            key = p.getSimpleValue();
        } else if ("bundle".equals(name)) {
            try {
                // IDEA-74868
                final PsiFileSystemItem referencedPsiFile = InjectionUtil.getReferencedPsiFile(p);
                if (referencedPsiFile instanceof PropertiesFile) {
                    bundle = (PropertiesFile) referencedPsiFile;
                } else {
                    LOG.warn("skip resource directive, referenced file is not properties file " + host.getText());
                }
            } catch (InvalidPropertyException e) {
                invalidPropertyException = e;
                return InjectedASWriter.IGNORE;
            }
        }
    }
    if (key == null || key.isEmpty() || bundle == null) {
        LOG.warn("skip resource directive, one of the required attributes is missed " + host.getText());
        return InjectedASWriter.IGNORE;
    }
    final IProperty property = bundle.findPropertyByKey(key);
    if (property == null) {
        LOG.warn("skip resource directive, key not found " + host.getText());
        return InjectedASWriter.IGNORE;
    }
    return new ResourceDirectiveValueWriter(property.getUnescapedValue());
}
Also used : IProperty(com.intellij.lang.properties.IProperty) JSAttributeNameValuePair(com.intellij.lang.javascript.psi.ecmal4.JSAttributeNameValuePair) InvalidPropertyException(com.intellij.flex.uiDesigner.InvalidPropertyException) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem)

Example 75 with PropertiesFile

use of com.intellij.lang.properties.psi.PropertiesFile in project intellij-community by JetBrains.

the class AntResolveInspection method checkReferences.

private static void checkReferences(final XmlElement xmlElement, @NonNls final DomElementAnnotationHolder holder, DomElement domElement) {
    if (xmlElement == null) {
        return;
    }
    Set<PsiReference> processed = null;
    // to be initialized lazily
    Collection<PropertiesFile> propertyFiles = null;
    for (final PsiReference ref : xmlElement.getReferences()) {
        if (!(ref instanceof AntDomReference)) {
            continue;
        }
        final AntDomReference antDomRef = (AntDomReference) ref;
        if (antDomRef.shouldBeSkippedByAnnotator()) {
            continue;
        }
        if (processed != null && processed.contains(ref)) {
            continue;
        }
        if (!isResolvable(ref)) {
            final List<LocalQuickFix> quickFixList = new SmartList<>();
            quickFixList.add(new AntChangeContextLocalFix());
            if (ref instanceof AntDomPropertyReference) {
                final String canonicalText = ref.getCanonicalText();
                quickFixList.add(new AntCreatePropertyFix(canonicalText, null));
                final PsiFile containingFile = xmlElement.getContainingFile();
                if (containingFile != null) {
                    if (propertyFiles == null) {
                        propertyFiles = getPropertyFiles(AntSupport.getAntDomProject(containingFile), xmlElement);
                    }
                    for (PropertiesFile propertyFile : propertyFiles) {
                        quickFixList.add(new AntCreatePropertyFix(canonicalText, propertyFile));
                    }
                }
            } else if (ref instanceof AntDomTargetReference) {
                quickFixList.add(new AntCreateTargetFix(ref.getCanonicalText()));
            }
            holder.createProblem(domElement, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, antDomRef.getUnresolvedMessagePattern(), ref.getRangeInElement(), quickFixList.toArray((new LocalQuickFix[quickFixList.size()])));
            if (ref instanceof AntDomFileReference) {
                if (processed == null) {
                    processed = new HashSet<>();
                }
                ContainerUtil.addAll(processed, ((AntDomFileReference) ref).getFileReferenceSet().getAllReferences());
            }
        }
    }
}
Also used : AntCreateTargetFix(com.intellij.lang.ant.quickfix.AntCreateTargetFix) AntChangeContextLocalFix(com.intellij.lang.ant.quickfix.AntChangeContextLocalFix) AntCreatePropertyFix(com.intellij.lang.ant.quickfix.AntCreatePropertyFix) PsiReference(com.intellij.psi.PsiReference) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PsiFile(com.intellij.psi.PsiFile) SmartList(com.intellij.util.SmartList)

Aggregations

PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)143 IProperty (com.intellij.lang.properties.IProperty)44 PsiFile (com.intellij.psi.PsiFile)42 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 ResourceBundle (com.intellij.lang.properties.ResourceBundle)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)19 Nullable (org.jetbrains.annotations.Nullable)18 Property (com.intellij.lang.properties.psi.Property)15 Project (com.intellij.openapi.project.Project)10 XmlPropertiesFile (com.intellij.lang.properties.xml.XmlPropertiesFile)9 PsiDirectory (com.intellij.psi.PsiDirectory)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 THashSet (gnu.trove.THashSet)8 ArrayList (java.util.ArrayList)7 Module (com.intellij.openapi.module.Module)6 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)6 HashSet (com.intellij.util.containers.HashSet)6 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5