|
51 | 51 | import javax.swing.JScrollPane; |
52 | 52 | import javax.swing.JTextField; |
53 | 53 | import javax.swing.JTree; |
| 54 | +import javax.swing.text.BadLocationException; |
54 | 55 | import javax.swing.tree.DefaultMutableTreeNode; |
55 | 56 | import javax.swing.tree.DefaultTreeCellRenderer; |
56 | 57 | import javax.swing.tree.DefaultTreeModel; |
57 | 58 | import javax.swing.tree.TreeModel; |
58 | 59 |
|
59 | 60 | import processing.app.Messages; |
60 | | -import processing.app.Preferences; |
61 | 61 | import processing.app.Sketch; |
| 62 | +import processing.app.SketchCode; |
| 63 | +import processing.app.syntax.SyntaxDocument; |
62 | 64 | import processing.app.ui.EditorStatus; |
63 | 65 | import processing.app.ui.Toolkit; |
64 | 66 | import processing.mode.java.JavaEditor; |
@@ -657,24 +659,33 @@ void rename(PreprocessedSketch ps, IBinding binding, String newName) { |
657 | 659 | final int currentOffset = editor.getCaretOffset(); |
658 | 660 | mappedNodes.entrySet().forEach(entry -> { |
659 | 661 | int tabIndex = entry.getKey(); |
660 | | - sketch.setCurrentCode(tabIndex); |
| 662 | + SketchCode sketchCode = sketch.getCode(tabIndex); |
| 663 | + |
| 664 | + SyntaxDocument document = (SyntaxDocument) sketchCode.getDocument(); |
661 | 665 |
|
662 | 666 | List<SketchInterval> nodes = entry.getValue(); |
663 | 667 | nodes.stream() |
664 | 668 | // Replace from the end so all unprocess offsets stay valid |
665 | 669 | .sorted(Comparator.comparing((SketchInterval si) -> si.startTabOffset).reversed()) |
666 | 670 | .forEach(si -> { |
667 | 671 | // Make sure offsets are in bounds |
668 | | - int length = editor.getTextArea().getDocumentLength(); |
669 | | - if (si.startTabOffset >= 0 && si.startTabOffset <= length && |
670 | | - si.stopTabOffset >= 0 && si.stopTabOffset <= length) { |
| 672 | + int documentLength = document.getLength(); |
| 673 | + if (si.startTabOffset >= 0 && si.startTabOffset <= documentLength && |
| 674 | + si.stopTabOffset >= 0 && si.stopTabOffset <= documentLength) { |
671 | 675 | // Replace the code |
672 | | - editor.getTextArea().select(si.startTabOffset, si.stopTabOffset); |
673 | | - editor.getTextArea().setSelectedText(newName); |
| 676 | + int length = si.stopTabOffset - si.startTabOffset; |
| 677 | + try { |
| 678 | + document.remove(si.startTabOffset, length); |
| 679 | + document.insertString(si.startTabOffset, newName, null); |
| 680 | + } catch (BadLocationException e) { /* Whatever */ } |
674 | 681 | } |
675 | 682 | }); |
676 | 683 |
|
677 | | - sketch.setModified(true); |
| 684 | + try { |
| 685 | + sketchCode.setProgram(document.getText(0, document.getLength())); |
| 686 | + } catch (BadLocationException e) { /* Whatever */ } |
| 687 | + sketchCode.setModified(true); |
| 688 | + editor.repaintHeader(); |
678 | 689 | }); |
679 | 690 |
|
680 | 691 | int precedingIntervals = |
|
0 commit comments