summaryrefslogtreecommitdiff
path: root/plugins/properties/src/com/intellij/lang/properties/refactoring/rename/RenamePropertyProcessor.java
blob: 6f0d52f1c2942f84420baadb2d38ccd710a43252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.lang.properties.refactoring.rename;

import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.PropertiesUtil;
import com.intellij.lang.properties.ResourceBundle;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.lang.properties.refactoring.PropertiesRefactoringSettings;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.rename.RenamePsiElementProcessor;
import com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo;
import com.intellij.usageView.UsageInfo;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RenamePropertyProcessor extends RenamePsiElementProcessor {
  public boolean canProcessElement(@NotNull final PsiElement element) {
    return element instanceof IProperty;
  }

  public void prepareRenaming(final PsiElement element, final String newName,
                              final Map<PsiElement, String> allRenames) {
    final Project project = element.getProject();
    ResourceBundle resourceBundle = ((IProperty) element).getPropertiesFile().getResourceBundle();

    final Map<PsiElement, String> allRenamesCopy = new LinkedHashMap<PsiElement, String>(allRenames);
    allRenames.clear();
    for (final Map.Entry<PsiElement, String> e : allRenamesCopy.entrySet()) {
      final IProperty property = (IProperty) e.getKey();
      final List<IProperty> properties = PropertiesUtil.findAllProperties(resourceBundle, property.getUnescapedKey());
      for (final IProperty toRename : properties) {
        allRenames.put(toRename.getPsiElement(), e.getValue());
      }
    }
  }

  @Override
  public void findCollisions(PsiElement element,
                             final String newName,
                             Map<? extends PsiElement, String> allRenames,
                             List<UsageInfo> result) {
    for (final Map.Entry<? extends PsiElement, String> e: allRenames.entrySet()) {
      for (IProperty property : ((PropertiesFile)e.getKey().getContainingFile()).getProperties()) {
        if (Comparing.strEqual(e.getValue(), property.getKey())) {
          result.add(new UnresolvableCollisionUsageInfo(property.getPsiElement(), e.getKey()) {
            @Override
            public String getDescription() {
              return "New property name \'" + e.getValue() + "\' hides existing property";
            }
          });
        }
      }
    }
  }

  @Override
   public boolean isToSearchInComments(PsiElement element) {
     return PropertiesRefactoringSettings.getInstance().RENAME_SEARCH_IN_COMMENTS;
   }
 
   @Override
   public void setToSearchInComments(PsiElement element, boolean enabled) {
     PropertiesRefactoringSettings.getInstance().RENAME_SEARCH_IN_COMMENTS = enabled;
   }
}