public void insertOrUpdateTranslation( String key, String value, final PropertiesFile propertiesFile) throws IncorrectOperationException { final IProperty property = propertiesFile.findPropertyByKey(key); if (property != null) { final String oldValue = property.getValue(); if (!Comparing.equal(oldValue, value)) { property.setValue(value); myCodeStyleManager.reformat(property.getPsiElement()); } return; } if (myOrdered) { if (myAlphaSorted) { myCodeStyleManager.reformat(propertiesFile.addProperty(key, value).getPsiElement()); return; } final Pair<IProperty, Integer> propertyAndPosition = findExistedPrevSiblingProperty(key, propertiesFile); myCodeStyleManager.reformat( propertiesFile .addPropertyAfter( key, value, propertyAndPosition == null ? null : (Property) propertyAndPosition.getFirst()) .getPsiElement()); } else { insertPropertyLast(key, value, propertiesFile); } }
@Nullable public PropertiesFile findPropertiesFile( final Module module, final String bundleName, final Locale locale) { List<PropertiesFile> propFiles = findPropertiesFiles(module, bundleName); if (locale != null) { for (PropertiesFile propFile : propFiles) { if (propFile.getLocale().equals(locale)) { return propFile; } } } // fallback to default locale for (PropertiesFile propFile : propFiles) { if (propFile.getLocale().getLanguage().length() == 0 || propFile.getLocale().equals(Locale.getDefault())) { return propFile; } } // fallback to any file if (!propFiles.isEmpty()) { return propFiles.get(0); } return null; }
private void insertPropertyLast(String key, String value, PropertiesFile propertiesFile) { final List<IProperty> properties = propertiesFile.getProperties(); final IProperty lastProperty = properties.isEmpty() ? null : properties.get(properties.size() - 1); myCodeStyleManager.reformat( propertiesFile.addPropertyAfter(key, value, lastProperty).getPsiElement()); }
public static void createProperty( @NotNull final Project project, @NotNull final PsiElement psiElement, @NotNull final Collection<PropertiesFile> selectedPropertiesFiles, @NotNull final String key, @NotNull final String value) { for (PropertiesFile selectedFile : selectedPropertiesFiles) { if (!FileModificationService.getInstance() .prepareFileForWrite(selectedFile.getContainingFile())) return; } UndoUtil.markPsiFileForUndo(psiElement.getContainingFile()); ApplicationManager.getApplication() .runWriteAction( new Runnable() { public void run() { CommandProcessor.getInstance() .executeCommand( project, new Runnable() { public void run() { try { I18nUtil.createProperty(project, selectedPropertiesFiles, key, value); } catch (IncorrectOperationException e) { LOG.error(e); } } }, CodeInsightBundle.message("quickfix.i18n.command.name"), project); } }); }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final boolean force = myFilesToSort.length == 1; for (PropertiesFile file : myFilesToSort) { if (!force && file.isAlphaSorted()) { continue; } sortPropertiesFile(file); } }
public void insertAfter(@NotNull String key, @NotNull String value, @NotNull String anchor) { if (myAlphaSorted || !myOrdered) { throw new IllegalStateException( "Can't insert new properties by anchor while resource bundle is alpha-sorted"); } final PropertiesFile file = myResourceBundle.getDefaultPropertiesFile(); final IProperty anchorProperty = file.findPropertyByKey(anchor); file.addPropertyAfter(key, value, anchorProperty); final int anchorIndex = myKeysOrder.indexOf(anchor); myKeysOrder.add(anchorIndex + 1, key); }
public static void doI18nSelectedString( final @NotNull Project project, final @NotNull Editor editor, final @NotNull PsiFile psiFile, final @NotNull I18nQuickFixHandler handler) { try { handler.checkApplicability(psiFile, editor); } catch (IncorrectOperationException ex) { CommonRefactoringUtil.showErrorHint( project, editor, ex.getMessage(), CodeInsightBundle.message("i18nize.error.title"), null); return; } final JavaI18nizeQuickFixDialog dialog = handler.createDialog(project, editor, psiFile); if (dialog == null) return; dialog.show(); if (!dialog.isOK()) return; if (!CodeInsightUtilBase.prepareFileForWrite(psiFile)) return; final Collection<PropertiesFile> propertiesFiles = dialog.getAllPropertiesFiles(); for (PropertiesFile file : propertiesFiles) { if (!CodeInsightUtilBase.prepareFileForWrite(file.getContainingFile())) return; } ApplicationManager.getApplication() .runWriteAction( new Runnable() { public void run() { CommandProcessor.getInstance() .executeCommand( project, new Runnable() { public void run() { try { handler.performI18nization( psiFile, editor, dialog.getLiteralExpression(), propertiesFiles, dialog.getKey(), dialog.getValue(), dialog.getI18nizedText(), dialog.getParameters(), dialog.getPropertyCreationHandler()); } catch (IncorrectOperationException e) { LOG.error(e); } } }, CodeInsightBundle.message("quickfix.i18n.command.name"), project); } }); }
@Nullable private BaseNameError checkBaseName(final String baseNameCandidate) { for (PropertiesFile propertiesFile : myPropertiesFiles) { final String name = propertiesFile.getVirtualFile().getName(); if (!name.startsWith(baseNameCandidate) || !PropertiesUtil.BASE_NAME_BORDER_CHAR.contains( name.charAt(baseNameCandidate.length()))) { return new BaseNameError(name); } } return null; }
private static boolean isResourceBundleAlphaSortedExceptOneFile( final @NotNull ResourceBundle resourceBundle, final @NotNull PropertiesFile exceptedFile) { for (PropertiesFile file : resourceBundle.getPropertiesFiles()) { if (!(file instanceof PropertiesFileImpl)) { return true; } if (!file.equals(exceptedFile) && !file.isAlphaSorted()) { return false; } } return true; }
public void insertNewProperty(String key, String value) { if (ApplicationManager.getApplication().isUnitTestMode() && myKeysOrder != null) { LOG.assertTrue(!myKeysOrder.contains(key)); } final PropertiesFile propertiesFile = myResourceBundle.getDefaultPropertiesFile(); if (myAlphaSorted) { myCodeStyleManager.reformat(propertiesFile.addProperty(key, value).getPsiElement()); } else { insertPropertyLast(key, value, propertiesFile); if (myOrdered) { myKeysOrder.add(key); } } }
ResourceBundleKeyRenameValidator( Project project, ResourceBundleEditor editor, ResourceBundle bundle, String oldPropertyName) { myEditor = editor; myOldPropertyName = oldPropertyName; for (PropertiesFile file : bundle.getPropertiesFiles(project)) { for (IProperty property : file.getProperties()) { myExistingProperties.add(property.getKey()); } } myExistingProperties.remove(oldPropertyName); }
@Override public void update(final AnActionEvent e) { final List<PropertiesFile> propertiesFiles = getPropertiesFiles(e); boolean isAvailable = propertiesFiles != null && propertiesFiles.size() > 1; if (isAvailable) { for (PropertiesFile propertiesFile : propertiesFiles) { if (propertiesFile.getResourceBundle().getPropertiesFiles().size() != 1) { isAvailable = false; break; } } } e.getPresentation().setVisible(isAvailable); }
@Nullable static PsiDirectory getResourceBundlePlacementDirectory(ResourceBundle resourceBundle) { PsiDirectory containingDirectory = null; for (PropertiesFile propertiesFile : resourceBundle.getPropertiesFiles()) { if (containingDirectory == null) { containingDirectory = propertiesFile.getContainingFile().getContainingDirectory(); } else if (!containingDirectory.isEquivalentTo( propertiesFile.getContainingFile().getContainingDirectory())) { return null; } } LOG.assertTrue(containingDirectory != null); return containingDirectory; }
public static boolean isPropertyRef( final PsiLiteralExpression expression, final String key, final String resourceBundleName) { if (resourceBundleName == null) { return !PropertiesImplUtil.findPropertiesByKey(expression.getProject(), key).isEmpty(); } else { final List<PropertiesFile> propertiesFiles = propertiesFilesByBundleName(resourceBundleName, expression); boolean containedInPropertiesFile = false; for (PropertiesFile propertiesFile : propertiesFiles) { containedInPropertiesFile |= propertiesFile.findPropertyByKey(key) != null; } return containedInPropertiesFile; } }
public void testPropertiesFileNotAssociatedWhileLanguageCodeNotRecognized() { final PsiFile file = myFixture.addFileToProject("some_property_file.properties", ""); final PsiFile file2 = myFixture.addFileToProject("some_property_fil.properties", ""); final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file); assertNotNull(propertiesFile); final ResourceBundle resourceBundle = propertiesFile.getResourceBundle(); assertSize(1, resourceBundle.getPropertiesFiles()); final PropertiesFile propertiesFile2 = PropertiesImplUtil.getPropertiesFile(file2); assertNotNull(propertiesFile2); final ResourceBundle resourceBundle2 = propertiesFile.getResourceBundle(); assertSize(1, resourceBundle2.getPropertiesFiles()); assertEquals(PropertiesUtil.DEFAULT_LOCALE, propertiesFile.getLocale()); }
@Override public boolean accept(@NotNull final Project project, @NotNull final VirtualFile file) { if (file instanceof ResourceBundleAsVirtualFile) return true; if (!file.isValid()) return false; PsiFile psiFile = ApplicationManager.getApplication() .runReadAction( new Computable<PsiFile>() { @Override public PsiFile compute() { return PsiManager.getInstance(project).findFile(file); } }); PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(psiFile); return propertiesFile != null && propertiesFile.getResourceBundle().getPropertiesFiles().size() > 1; }
public void testResourceBundleManagerUpdatesProperlyWhileDirRemoval() { myFixture.addFileToProject("qwe/asd/p.properties", ""); final PsiFile file = myFixture.addFileToProject("qwe/asd/p_en.properties", ""); final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file); assertNotNull(propertiesFile); final ResourceBundleManager resourceBundleManager = ResourceBundleManager.getInstance(getProject()); resourceBundleManager.dissociateResourceBundle(propertiesFile.getResourceBundle()); final PropertiesFile propFile1 = PropertiesImplUtil.getPropertiesFile( myFixture.addFileToProject("qwe1/asd1/p.properties", "")); final PropertiesFile propFile2 = PropertiesImplUtil.getPropertiesFile( myFixture.addFileToProject("qwe1/asd1/p_abc.properties", "")); assertNotNull(propFile1); assertNotNull(propFile2); resourceBundleManager.combineToResourceBundle( ContainerUtil.newArrayList(propFile1, propFile2), "p"); final PsiFile someFile = myFixture.addFileToProject("to_remove/asd.txt", ""); final PsiDirectory toRemove = someFile.getParent(); assertNotNull(toRemove); ApplicationManager.getApplication().runWriteAction(toRemove::delete); final ResourceBundleManagerState state = resourceBundleManager.getState(); assertNotNull(state); assertSize(1, state.getCustomResourceBundles()); assertSize(2, state.getDissociatedFiles()); final PsiDirectory directory = propertiesFile.getParent().getParent(); assertNotNull(directory); ApplicationManager.getApplication().runWriteAction(directory::delete); assertSize(1, state.getCustomResourceBundles()); assertSize(0, state.getDissociatedFiles()); final PsiDirectory directory1 = propFile1.getParent().getParent(); assertNotNull(directory1); ApplicationManager.getApplication().runWriteAction(directory1::delete); assertSize(0, state.getCustomResourceBundles()); assertSize(0, state.getDissociatedFiles()); }
public void testCustomResourceBundleFilesMovedOrDeleted() throws IOException { final PropertiesFile file = PropertiesImplUtil.getPropertiesFile( myFixture.addFileToProject("resources-dev/my-app-dev.properties", "")); final PropertiesFile file2 = PropertiesImplUtil.getPropertiesFile( myFixture.addFileToProject("resources-dev/my-app-test.properties", "")); final PropertiesFile file3 = PropertiesImplUtil.getPropertiesFile( myFixture.addFileToProject("resources-prod/my-app-prod.properties", "")); assertNotNull(file); assertNotNull(file2); assertNotNull(file3); assertOneElement(file.getResourceBundle().getPropertiesFiles()); assertOneElement(file2.getResourceBundle().getPropertiesFiles()); assertOneElement(file3.getResourceBundle().getPropertiesFiles()); final ResourceBundleManager resourceBundleBaseNameManager = ResourceBundleManager.getInstance(getProject()); resourceBundleBaseNameManager.combineToResourceBundle(list(file, file2, file3), "my-app"); assertSize(3, file.getResourceBundle().getPropertiesFiles()); final PsiDirectory newDir = PsiManager.getInstance(getProject()) .findDirectory(myFixture.getTempDirFixture().findOrCreateDir("new-resources-dir")); new MoveFilesOrDirectoriesProcessor( getProject(), new PsiElement[] {file2.getContainingFile()}, newDir, false, false, null, null) .run(); ApplicationManager.getApplication().runWriteAction(() -> file3.getContainingFile().delete()); assertSize(2, file.getResourceBundle().getPropertiesFiles()); final ResourceBundleManagerState state = ResourceBundleManager.getInstance(getProject()).getState(); assertNotNull(state); assertSize(1, state.getCustomResourceBundles()); assertSize(2, state.getCustomResourceBundles().get(0).getFileUrls()); }
protected static void collectPropertiesFileVariants( @Nullable PropertiesFile file, @Nullable String prefix, List<Object> result) { if (file == null) return; for (IProperty each : file.getProperties()) { String name = each.getKey(); if (name != null) { if (prefix != null) name = prefix + name; result.add(createLookupElement(each, name, PlatformIcons.PROPERTY_ICON)); } } }
private void combineToResourceBundleIfNeed(Collection<PsiFile> files) { Collection<PropertiesFile> createdFiles = ContainerUtil.map( files, (NotNullFunction<PsiFile, PropertiesFile>) dom -> { final PropertiesFile file = PropertiesImplUtil.getPropertiesFile(dom); LOG.assertTrue(file != null, dom.getName()); return file; }); ResourceBundle mainBundle = myResourceBundle; final Set<ResourceBundle> allBundles = new HashSet<>(); if (mainBundle != null) { allBundles.add(mainBundle); } boolean needCombining = false; for (PropertiesFile file : createdFiles) { final ResourceBundle rb = file.getResourceBundle(); if (mainBundle == null) { mainBundle = rb; } else if (!mainBundle.equals(rb)) { needCombining = true; } allBundles.add(rb); } if (needCombining) { final List<PropertiesFile> toCombine = new ArrayList<>(createdFiles); final String baseName = getBaseName(); if (myResourceBundle != null) { toCombine.addAll(myResourceBundle.getPropertiesFiles()); } ResourceBundleManager manager = ResourceBundleManager.getInstance(mainBundle.getProject()); for (ResourceBundle bundle : allBundles) { manager.dissociateResourceBundle(bundle); } manager.combineToResourceBundle(toCombine, baseName); } }
private String canCreateAllFilesForAllLocales() { final String name = getBaseName(); if (name.isEmpty()) { return "Base name is empty"; } final Set<String> files = getFileNamesToCreate(); if (files.isEmpty()) { return "No locales added"; } for (PsiElement element : myDirectory.getChildren()) { if (element instanceof PsiFile) { if (element instanceof PropertiesFile) { PropertiesFile propertiesFile = (PropertiesFile) element; final String propertiesFileName = propertiesFile.getName(); if (files.contains(propertiesFileName)) { return "Some of files already exist"; } } } } return null; }
private Pair<IProperty, Integer> findExistedPrevSiblingProperty(String key, PropertiesFile file) { if (myKeysOrder.isEmpty()) { return null; } final int prevPosition = myKeysOrder.indexOf(key); for (int i = prevPosition; i >= 0; i--) { final String prevKey = myKeysOrder.get(i); final IProperty property = file.findPropertyByKey(prevKey); if (property != null) { return Pair.create(property, prevPosition + 1); } } return null; }
private void updateEditorsFromProperties() { String propertyName = getSelectedPropertyName(); ((CardLayout) myValuesPanel.getLayout()) .show(myValuesPanel, propertyName == null ? NO_PROPERTY_SELECTED : VALUES); if (propertyName == null) return; for (final PropertiesFile propertiesFile : myResourceBundle.getPropertiesFiles(myProject)) { final EditorEx editor = (EditorEx) myEditors.get(propertiesFile); if (editor == null) continue; final IProperty property = propertiesFile.findPropertyByKey(propertyName); final Document document = editor.getDocument(); CommandProcessor.getInstance() .executeCommand( null, new Runnable() { @Override public void run() { ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { updateDocumentFromPropertyValue( getPropertyEditorValue(property), document, propertiesFile); } }); } }, "", this); JPanel titledPanel = myTitledPanels.get(propertiesFile); ((TitledBorder) titledPanel.getBorder()) .setTitleColor(property == null ? JBColor.RED : UIUtil.getLabelTextForeground()); titledPanel.repaint(); } }
public void deletePropertyIfExist(String key, PropertiesFile file) { final IProperty property = file.findPropertyByKey(key); if (property != null && myKeysOrder != null) { boolean keyExistInOtherPropertiesFiles = false; for (PropertiesFile propertiesFile : myResourceBundle.getPropertiesFiles()) { if (!propertiesFile.equals(file) && propertiesFile.findPropertyByKey(key) != null) { keyExistInOtherPropertiesFiles = true; break; } } if (!keyExistInOtherPropertiesFiles) { myKeysOrder.remove(key); } } if (property != null) { PsiElement anElement = property.getPsiElement(); if (anElement instanceof PomTargetPsiElement) { final PomTarget xmlProperty = ((PomTargetPsiElement) anElement).getTarget(); LOG.assertTrue(xmlProperty instanceof XmlProperty); anElement = ((XmlProperty) xmlProperty).getNavigationElement(); } anElement.delete(); } }
public void testLanguageCodeNotRecognized() { final PsiFile file = myFixture.addFileToProject("p.properties", ""); final PsiFile file2 = myFixture.addFileToProject("p_asd.properties", ""); final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file); final PropertiesFile propertiesFile2 = PropertiesImplUtil.getPropertiesFile(file2); assertNotNull(propertiesFile); assertNotNull(propertiesFile2); final ResourceBundle bundle = propertiesFile.getResourceBundle(); final ResourceBundle bundle2 = propertiesFile2.getResourceBundle(); assertSize(1, bundle.getPropertiesFiles()); assertSize(1, bundle2.getPropertiesFiles()); assertEquals("p", bundle.getBaseName()); assertEquals("p_asd", bundle2.getBaseName()); final ResourceBundleManager manager = ResourceBundleManager.getInstance(getProject()); final ArrayList<PropertiesFile> rawBundle = ContainerUtil.newArrayList(propertiesFile, propertiesFile2); final String suggestedBaseName = PropertiesUtil.getDefaultBaseName(rawBundle); assertEquals("p", suggestedBaseName); manager.combineToResourceBundle(rawBundle, suggestedBaseName); assertEquals("asd", propertiesFile2.getLocale().getLanguage()); }
public void testPropertiesFilesDefaultCombiningToResourceBundle() { final PsiFile file = myFixture.addFileToProject("prop_core_en.properties", ""); final PsiFile file2 = myFixture.addFileToProject("prop_core_fi.properties", ""); final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file); final PropertiesFile propertiesFile2 = PropertiesImplUtil.getPropertiesFile(file2); assertNotNull(propertiesFile); assertNotNull(propertiesFile2); final ResourceBundle bundle = propertiesFile.getResourceBundle(); final ResourceBundle bundle2 = propertiesFile2.getResourceBundle(); assertTrue(bundle.equals(bundle2)); assertSize(2, bundle.getPropertiesFiles()); assertTrue(bundle.getDefaultPropertiesFile().equals(bundle2.getDefaultPropertiesFile())); assertEquals("prop_core", bundle.getBaseName()); assertNotSame( propertiesFile.getLocale().getLanguage(), propertiesFile.getLocale().getDisplayLanguage()); assertNotSame( propertiesFile2.getLocale().getLanguage(), propertiesFile2.getLocale().getDisplayLanguage()); }
public void testCombineToCustomResourceBundleAndDissociateAfter() { final PropertiesFile file = PropertiesImplUtil.getPropertiesFile( myFixture.addFileToProject("resources-dev/my-app-dev.properties", "")); final PropertiesFile file2 = PropertiesImplUtil.getPropertiesFile( myFixture.addFileToProject("resources-prod/my-app-prod.properties", "")); assertNotNull(file); assertNotNull(file2); assertOneElement(file.getResourceBundle().getPropertiesFiles()); assertOneElement(file2.getResourceBundle().getPropertiesFiles()); final ResourceBundleManager resourceBundleBaseNameManager = ResourceBundleManager.getInstance(getProject()); final String newBaseName = "my-app"; resourceBundleBaseNameManager.combineToResourceBundle(list(file, file2), newBaseName); final ResourceBundle resourceBundle = file.getResourceBundle(); assertEquals(2, resourceBundle.getPropertiesFiles().size()); assertEquals(newBaseName, resourceBundle.getBaseName()); resourceBundleBaseNameManager.dissociateResourceBundle(resourceBundle); assertOneElement(file.getResourceBundle().getPropertiesFiles()); assertOneElement(file2.getResourceBundle().getPropertiesFiles()); }
private static void sortPropertiesFile(final PropertiesFile file) { final List<IProperty> properties = new ArrayList<IProperty>(file.getProperties()); Collections.sort( properties, new Comparator<IProperty>() { @Override public int compare(IProperty p1, IProperty p2) { return Comparing.compare(p1.getKey(), p2.getKey()); } }); final char delimiter = PropertiesCodeStyleSettings.getInstance(file.getProject()).KEY_VALUE_DELIMITER; final StringBuilder rawText = new StringBuilder(); for (int i = 0; i < properties.size(); i++) { IProperty property = properties.get(i); final String value = property.getUnescapedValue(); rawText.append( PropertiesElementFactory.getPropertyText( property.getKey(), value != null ? value : "", delimiter, null)); if (i != properties.size() - 1) { rawText.append("\n"); } } final PropertiesFile fakeFile = PropertiesElementFactory.createPropertiesFile(file.getProject(), rawText.toString()); final PropertiesList propertiesList = PsiTreeUtil.findChildOfType(file.getContainingFile(), PropertiesList.class); LOG.assertTrue(propertiesList != null); final PropertiesList fakePropertiesList = PsiTreeUtil.findChildOfType(fakeFile.getContainingFile(), PropertiesList.class); LOG.assertTrue(fakePropertiesList != null); propertiesList.replace(fakePropertiesList); }
@Nullable public static IProperty findProperty( @NotNull Project project, @NotNull VirtualFile file, @NotNull String propName) { PropertiesFile propertiesFile = getPropertiesFile(project, file); return propertiesFile == null ? null : propertiesFile.findPropertyByKey(propName); }
@Override public PsiElement fun(PropertiesFile propertiesFile) { return propertiesFile.getContainingFile(); }