(PreprocessedSketch ps, IBinding binding, String newName)
| 240 | |
| 241 | // Thread: EDT (we can't allow user to mess with sketch while renaming) |
| 242 | void rename(PreprocessedSketch ps, IBinding binding, String newName) { |
| 243 | CompilationUnit root = ps.compilationUnit; |
| 244 | |
| 245 | // Renaming constructor should rename class |
| 246 | if (binding.getKind() == IBinding.METHOD) { |
| 247 | IMethodBinding method = (IMethodBinding) binding; |
| 248 | if (method.isConstructor()) { |
| 249 | binding = method.getDeclaringClass(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | ASTNode decl = root.findDeclaringNode(binding.getKey()); |
| 254 | if (decl == null) return; |
| 255 | |
| 256 | showUsage.hide(); |
| 257 | |
| 258 | List<SimpleName> occurrences = new ArrayList<>(); |
| 259 | occurrences.addAll(findAllOccurrences(root, binding.getKey())); |
| 260 | |
| 261 | // Renaming class should rename all constructors |
| 262 | if (binding.getKind() == IBinding.TYPE) { |
| 263 | ITypeBinding type = (ITypeBinding) binding; |
| 264 | //type = type.getErasure(); |
| 265 | IMethodBinding[] methods = type.getDeclaredMethods(); |
| 266 | Arrays.stream(methods) |
| 267 | .filter(IMethodBinding::isConstructor) |
| 268 | .flatMap(c -> findAllOccurrences(root, c.getKey()).stream()) |
| 269 | .forEach(occurrences::add); |
| 270 | } |
| 271 | |
| 272 | Map<Integer, List<SketchInterval>> mappedNodes = occurrences.stream() |
| 273 | .map(ps::mapJavaToSketch) |
| 274 | .filter(ps::inRange) |
| 275 | .collect(Collectors.groupingBy(interval -> interval.tabIndex)); |
| 276 | |
| 277 | Sketch sketch = ps.sketch; |
| 278 | |
| 279 | editor.startCompoundEdit(); |
| 280 | |
| 281 | mappedNodes.entrySet().forEach(entry -> { |
| 282 | int tabIndex = entry.getKey(); |
| 283 | SketchCode sketchCode = sketch.getCode(tabIndex); |
| 284 | |
| 285 | SyntaxDocument document = (SyntaxDocument) sketchCode.getDocument(); |
| 286 | |
| 287 | List<SketchInterval> nodes = entry.getValue(); |
| 288 | nodes.stream() |
| 289 | // Replace from the end so all unprocess offsets stay valid |
| 290 | .sorted(Comparator.comparing((SketchInterval si) -> si.startTabOffset).reversed()) |
| 291 | .forEach(si -> { |
| 292 | // Make sure offsets are in bounds |
| 293 | int documentLength = document.getLength(); |
| 294 | if (si.startTabOffset >= 0 && si.startTabOffset <= documentLength && |
| 295 | si.stopTabOffset >= 0 && si.stopTabOffset <= documentLength) { |
| 296 | // Replace the code |
| 297 | int length = si.stopTabOffset - si.startTabOffset; |
| 298 | try { |
| 299 | document.remove(si.startTabOffset, length); |
no test coverage detected