ruby.refactoring/src/org/netbeans/modules/refactoring/ruby/plugins/RenameRefactoringPlugin.java
changeset 4559 7a0a8afa3e90
parent 4542 be97a5e85907
     1.1 --- a/ruby.refactoring/src/org/netbeans/modules/refactoring/ruby/plugins/RenameRefactoringPlugin.java	Thu Nov 21 16:40:13 2013 -0600
     1.2 +++ b/ruby.refactoring/src/org/netbeans/modules/refactoring/ruby/plugins/RenameRefactoringPlugin.java	Tue Apr 22 15:36:21 2014 -0500
     1.3 @@ -351,6 +351,13 @@
     1.4      }
     1.5  
     1.6      private Set<RubyElementCtx> allMethods;
     1.7 +
     1.8 +    private static final Comparator<Difference> COMPARATOR = new Comparator<Difference>() {
     1.9 +        public int compare(Difference d1, Difference d2) {
    1.10 +            return d1.getStartPosition().getOffset() - d2.getStartPosition().getOffset();
    1.11 +
    1.12 +        };
    1.13 +    };
    1.14      
    1.15      public Problem prepare(RefactoringElementsBag elements) {
    1.16          if (treePathHandle == null) {
    1.17 @@ -367,25 +374,30 @@
    1.18                      rt.setWorkingCopy(parserResult);
    1.19                      rt.scan();
    1.20                      ModificationResult mr = new ModificationResult();
    1.21 -                    mr.addDifferences(parserResult.getSnapshot().getSource().getFileObject(), rt.diffs);
    1.22 +
    1.23 +                    mr.addDifferences(parserResult.getSnapshot().getSource().getFileObject(), cullDifferences(rt.diffs));
    1.24 +
    1.25                      return Collections.singleton(mr);
    1.26                  }
    1.27              };
    1.28  
    1.29              final Collection<ModificationResult> results = processFiles(files, transform);
    1.30 -            elements.registerTransaction(new RetoucheCommit(results));
    1.31 -            for (ModificationResult result:results) {
    1.32 +
    1.33 +            // We don't want retouche to look at all results since we are finding the same nodes
    1.34 +            // repeated in our tree (e.g. @a += 1 has two nodes for @a for the assign and the references).
    1.35 +            for (ModificationResult result: results) {
    1.36                  for (FileObject jfo : result.getModifiedFileObjects()) {
    1.37 +
    1.38                      for (Difference diff: result.getDifferences(jfo)) {
    1.39                          String old = diff.getOldText();
    1.40 -                        if (old!=null) {
    1.41 -                            //TODO: workaround
    1.42 -                            //generator issue?
    1.43 +                        if (old!=null) {  //TODO: workaround. generator issue?
    1.44                              elements.add(refactoring,DiffElement.create(diff, jfo, result));
    1.45                          }
    1.46                      }
    1.47                  }
    1.48              }
    1.49 +
    1.50 +            elements.registerTransaction(new RetoucheCommit(results));
    1.51          }
    1.52          // see #126733. need to set a correct new name for the file rename plugin
    1.53          // that gets invoked after this plugin when the refactoring is invoked on a file.
    1.54 @@ -399,6 +411,29 @@
    1.55          return problem;
    1.56      }
    1.57  
    1.58 +    // At NB 8.0 it gets really unhappy if we submit duplicate Differences which overlap.
    1.59 +    // This method sorts and then removes any which happen to have the same start offset.
    1.60 +    // Start offset might not be perfect but in the cases on improper overlaps it should get
    1.61 +    // repaired by the thing supplying the differences.
    1.62 +    private static List<Difference> cullDifferences(List<Difference> oldDiffs) {
    1.63 +        List<Difference> diffs = new ArrayList<Difference>();
    1.64 +        Difference lastDiff = null;
    1.65 +
    1.66 +        if (oldDiffs.size() > 0) Collections.sort(oldDiffs, COMPARATOR);
    1.67 +
    1.68 +        for (Difference diff: oldDiffs) {
    1.69 +            if (lastDiff == null ||
    1.70 +                    (diff.getStartPosition().getOffset() != lastDiff.getStartPosition().getOffset() &&
    1.71 +                    diff.getEndPosition().getOffset() != lastDiff.getEndPosition().getOffset())) {
    1.72 +                diffs.add(diff);
    1.73 +            }
    1.74 +
    1.75 +            lastDiff = diff;
    1.76 +        }
    1.77 +
    1.78 +        return diffs;
    1.79 +    }
    1.80 +
    1.81      private static final String getString(String key) {
    1.82          return NbBundle.getMessage(RenameRefactoringPlugin.class, key);
    1.83      }
    1.84 @@ -871,7 +906,7 @@
    1.85                  case CLASSVARASGNNODE:
    1.86                  case CLASSVARDECLNODE:
    1.87                      if (((INameNode)node).getName().equals(name)) {
    1.88 -                        rename(node, name, null, null);
    1.89 +                        rename(node, ((INameNode)node).getLexicalName(), null, null);
    1.90                      }
    1.91                      break;
    1.92                  }