Skip to content

Commit b25ed1c

Browse files
Use a separate RSyntaxTextArea for each editor tab
RSyntaxTextArea appears to support using a single instance and replacing the underlying text and document when switching between tabs, but in practice this support is not complete and even though the RSyntaxTextArea developers did some work to improve the situation, they recommend to just use a seperate instance for each tab. This commit implements exactly that. A new class EditorTab is introduce to wrap the RSyntaxTextArea and containing scroll pane, and to encapsulate the code related to handling the text area itself. Doing so removes some quirks and prepares for some later additions. In particular, error highlights are now no longer shared between all tabs, which was previously the case. This commit mostly moves code from Editor into EditorTab, and updates the callers to use getCurrentTab() and call methods on the result instead of calling them on Editor. Some code is added to take care of creating multiple EditorTab objects and switching between them. Some small changes have been made to make the flow of opening files work, though these are mostly a bit hacky. While moving code, changes to the rest of the code were kept minimal, retaining existing interfaces as much as possible. This sometimes result in less than ideal code, which should be cleaned up in subsequent commits. The SketchCodeDocument class has been pretty much emptied out, since it was mostly used to store things for tabs in the background, which are now just stored in each RSyntaxTextArea separately. The last remaining bits of this class can probably be moved or implemented differently later, so it can be removed. The entire flow of working with sketches and files needs to be cleaned up next, so no thorough attempt at testing this commit was done. It is likely that there are plenty of corner cases and race conditions, which will be fixed once the reset of the code is cleaned up. Fixes #3441
1 parent b5bf07b commit b25ed1c

13 files changed

+634
-601
lines changed

app/src/cc/arduino/packages/formatter/AStyle.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,22 @@ public void init(Editor editor) {
7878

7979
@Override
8080
public void run() {
81-
String originalText = editor.getText();
81+
String originalText = editor.getCurrentTab().getText();
8282
String formattedText = aStyleInterface.AStyleMain(originalText, formatterConfiguration);
8383

8484
if (formattedText.equals(originalText)) {
8585
editor.statusNotice(tr("No changes necessary for Auto Format."));
8686
return;
8787
}
8888

89-
SketchTextArea textArea = editor.getTextArea();
89+
SketchTextArea textArea = editor.getCurrentTab().getTextArea();
9090

9191
int line = getLineOfOffset(textArea);
9292
int lineOffset = getLineOffset(textArea, line);
9393

9494
textArea.getUndoManager().beginInternalAtomicEdit();
9595
editor.removeAllLineHighlights();
96-
editor.setText(formattedText);
96+
editor.getCurrentTab().setText(formattedText);
9797
editor.getSketch().setModified(true);
9898
textArea.getUndoManager().endInternalAtomicEdit();
9999

app/src/cc/arduino/view/GoToLineNumber.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
127127
private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
128128
try {
129129
int line = Integer.parseInt(lineNumber.getText());
130-
editor.goToLine(line);
130+
editor.getCurrentTab().goToLine(line);
131131
cancelActionPerformed(evt);
132132
} catch (Exception e) {
133133
// ignore

app/src/cc/arduino/view/findreplace/FindReplace.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or
292292
return false;
293293
}
294294

295-
String text = editor.getText();
295+
String text = editor.getCurrentTab().getText();
296296

297297
if (ignoreCaseBox.isSelected()) {
298298
search = search.toLowerCase();
@@ -302,7 +302,7 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or
302302
int nextIndex;
303303
if (!backwards) {
304304
// int selectionStart = editor.textarea.getSelectionStart();
305-
int selectionEnd = editor.getSelectionStop();
305+
int selectionEnd = editor.getCurrentTab().getSelectionStop();
306306

307307
nextIndex = text.indexOf(search, selectionEnd);
308308
if (wrap && nextIndex == -1) {
@@ -311,7 +311,7 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or
311311
}
312312
} else {
313313
// int selectionStart = editor.textarea.getSelectionStart();
314-
int selectionStart = editor.getSelectionStart() - 1;
314+
int selectionStart = editor.getCurrentTab().getSelectionStart() - 1;
315315

316316
if (selectionStart >= 0) {
317317
nextIndex = text.lastIndexOf(search, selectionStart);
@@ -346,12 +346,12 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or
346346
if (backwards) {
347347
sketch.handlePrevCode();
348348
this.setVisible(true);
349-
int l = editor.getText().length() - 1;
350-
editor.setSelection(l, l);
349+
int l = editor.getCurrentTab().getText().length() - 1;
350+
editor.getCurrentTab().setSelection(l, l);
351351
} else {
352352
sketch.handleNextCode();
353353
this.setVisible(true);
354-
editor.setSelection(0, 0);
354+
editor.getCurrentTab().setSelection(0, 0);
355355
}
356356

357357
return find(wrap, backwards, true, originTab);
@@ -365,7 +365,7 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or
365365
}
366366

367367
if (nextIndex != -1) {
368-
editor.setSelection(nextIndex, nextIndex + search.length());
368+
editor.getCurrentTab().setSelection(nextIndex, nextIndex + search.length());
369369
return true;
370370
}
371371

@@ -381,17 +381,17 @@ private void replace() {
381381
return;
382382
}
383383

384-
int newpos = editor.getSelectionStart() - findField.getText().length();
384+
int newpos = editor.getCurrentTab().getSelectionStart() - findField.getText().length();
385385
if (newpos < 0) {
386386
newpos = 0;
387387
}
388-
editor.setSelection(newpos, newpos);
388+
editor.getCurrentTab().setSelection(newpos, newpos);
389389

390390
boolean foundAtLeastOne = false;
391391

392392
if (find(false, false, searchAllFilesBox.isSelected(), -1)) {
393393
foundAtLeastOne = true;
394-
editor.setSelectedText(replaceField.getText());
394+
editor.getCurrentTab().setSelectedText(replaceField.getText());
395395
editor.getSketch().setModified(true); // TODO is this necessary?
396396
}
397397

@@ -423,13 +423,13 @@ private void replaceAll() {
423423
editor.getSketch().setCurrentCode(0); // select the first tab
424424
}
425425

426-
editor.setSelection(0, 0); // move to the beginning
426+
editor.getCurrentTab().setSelection(0, 0); // move to the beginning
427427

428428
boolean foundAtLeastOne = false;
429429
while (true) {
430430
if (find(false, false, searchAllFilesBox.isSelected(), -1)) {
431431
foundAtLeastOne = true;
432-
editor.setSelectedText(replaceField.getText());
432+
editor.getCurrentTab().setSelectedText(replaceField.getText());
433433
editor.getSketch().setModified(true); // TODO is this necessary?
434434
} else {
435435
break;

app/src/processing/app/Base.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -610,10 +610,10 @@ protected void handleActivated(Editor whichEditor) {
610610
activeEditor.rebuildRecentSketchesMenu();
611611
if (PreferencesData.getBoolean("editor.external")) {
612612
try {
613-
int previousCaretPosition = activeEditor.getTextArea().getCaretPosition();
613+
int previousCaretPosition = activeEditor.getCurrentTab().getTextArea().getCaretPosition();
614614
activeEditor.getSketch().load(true);
615-
if (previousCaretPosition < activeEditor.getText().length()) {
616-
activeEditor.getTextArea().setCaretPosition(previousCaretPosition);
615+
if (previousCaretPosition < activeEditor.getCurrentTab().getText().length()) {
616+
activeEditor.getCurrentTab().getTextArea().setCaretPosition(previousCaretPosition);
617617
}
618618
} catch (IOException e) {
619619
// noop

0 commit comments

Comments
 (0)