use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class I18nizeQuickFixDialog method suggestSelectedFileUrl.
private String suggestSelectedFileUrl(List<String> paths) {
if (myDefaultPropertyValue != null) {
for (String path : paths) {
VirtualFile file = LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(path));
if (file == null)
continue;
PsiFile psiFile = myContext.getManager().findFile(file);
if (!(psiFile instanceof PropertiesFile))
continue;
for (IProperty property : ((PropertiesFile) psiFile).getProperties()) {
if (property.getValue().equals(myDefaultPropertyValue))
return path;
}
}
}
return LastSelectedPropertiesFileStore.getInstance().suggestLastSelectedPropertiesFileUrl(myContext);
}
use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class ResourceBundleKeyReference method isReferenceTo.
public boolean isReferenceTo(final PsiElement element) {
if (!(element instanceof IProperty)) {
return false;
}
IProperty property = (IProperty) element;
String baseName = ResourceBundleManager.getInstance(element.getProject()).getFullName(property.getPropertiesFile());
return baseName != null && myBundleName.equals(baseName.replace('.', '/')) && getRangeText().equals(property.getUnescapedKey());
}
use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class PropertyFoldingBuilder method checkLiteral.
private static void checkLiteral(PsiLiteralExpression expression, List<FoldingDescriptor> result) {
if (isI18nProperty(expression)) {
final IProperty property = getI18nProperty(expression);
final HashSet<Object> set = new HashSet<>();
set.add(property != null ? property : PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
final String msg = formatI18nProperty(expression, property);
final PsiElement parent = expression.getParent();
if (!msg.equals(expression.getText()) && parent instanceof PsiExpressionList && ((PsiExpressionList) parent).getExpressions()[0] == expression) {
final PsiExpressionList expressions = (PsiExpressionList) parent;
final int count = JavaI18nUtil.getPropertyValueParamsMaxCount(expression);
final PsiExpression[] args = expressions.getExpressions();
if (args.length == 1 + count && parent.getParent() instanceof PsiMethodCallExpression) {
boolean ok = true;
for (int i = 1; i < count + 1; i++) {
Object value = JavaConstantExpressionEvaluator.computeConstantExpression(args[i], false);
if (value == null) {
if (!(args[i] instanceof PsiReferenceExpression)) {
ok = false;
break;
}
}
}
if (ok) {
result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(parent.getParent().getNode()), parent.getParent().getTextRange(), null, set));
return;
}
}
}
result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(expression.getNode()), expression.getTextRange(), null, set));
}
}
use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class PropertyFoldingBuilder method isI18nProperty.
public static boolean isI18nProperty(@NotNull PsiLiteralExpression expr) {
if (!isStringLiteral(expr))
return false;
final IProperty property = expr.getUserData(CACHE);
if (property == NULL)
return false;
if (property != null)
return true;
final Map<String, Object> annotationParams = new HashMap<>();
annotationParams.put(AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER, null);
final boolean isI18n = JavaI18nUtil.mustBePropertyKey(expr, annotationParams);
if (!isI18n) {
expr.putUserData(CACHE, NULL);
}
return isI18n;
}
use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.
the class InlinePropertyHandler method inlineElement.
public void inlineElement(final Project project, Editor editor, PsiElement psiElement) {
if (!(psiElement instanceof IProperty))
return;
IProperty property = (IProperty) psiElement;
final String propertyValue = property.getValue();
if (propertyValue == null)
return;
final List<PsiElement> occurrences = Collections.synchronizedList(ContainerUtil.<PsiElement>newArrayList());
final Collection<PsiFile> containingFiles = Collections.synchronizedSet(new HashSet<PsiFile>());
containingFiles.add(psiElement.getContainingFile());
boolean result = ReferencesSearch.search(psiElement).forEach(psiReference -> {
PsiElement element = psiReference.getElement();
PsiElement parent = element.getParent();
if (parent instanceof PsiExpressionList && parent.getParent() instanceof PsiMethodCallExpression) {
if (((PsiExpressionList) parent).getExpressions().length == 1) {
occurrences.add(parent.getParent());
containingFiles.add(element.getContainingFile());
return true;
}
}
return false;
});
if (!result) {
CommonRefactoringUtil.showErrorHint(project, editor, "Property has non-method usages", REFACTORING_NAME, null);
}
if (occurrences.isEmpty()) {
CommonRefactoringUtil.showErrorHint(project, editor, "Property has no usages", REFACTORING_NAME, null);
return;
}
if (!ApplicationManager.getApplication().isUnitTestMode()) {
String occurrencesString = RefactoringBundle.message("occurrences.string", occurrences.size());
String question = PropertiesBundle.message("inline.property.confirmation", property.getName(), propertyValue) + " " + occurrencesString;
RefactoringMessageDialog dialog = new RefactoringMessageDialog(REFACTORING_NAME, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
if (!dialog.showAndGet()) {
return;
}
}
final RefactoringEventData data = new RefactoringEventData();
data.addElement(psiElement.copy());
new WriteCommandAction.Simple(project, REFACTORING_NAME, containingFiles.toArray(new PsiFile[containingFiles.size()])) {
@Override
protected void run() throws Throwable {
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_ID, data);
PsiLiteral stringLiteral = (PsiLiteral) JavaPsiFacade.getInstance(getProject()).getElementFactory().createExpressionFromText("\"" + StringUtil.escapeStringCharacters(propertyValue) + "\"", null);
for (PsiElement occurrence : occurrences) {
occurrence.replace(stringLiteral.copy());
}
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_ID, null);
}
}.execute();
}
Aggregations