Skip to content

Commit

Permalink
Merge pull request #109 from gyorokpeter/search2
Browse files Browse the repository at this point in the history
fix replace button replacing the wrong occurrence on the 2nd press
  • Loading branch information
gyorokpeter authored Apr 18, 2022
2 parents c794635 + 106de11 commit 0375f75
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/studio/ui/SearchPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public SearchPanel(EditorPane editorPane) {
JButton btnFindBack = new JButton(findBackAction);
JButton btnMarkAll = new JButton(markAllAction);
btnReplace = new JButton(replaceAction);
btnReplace.setName("btnReplace");
btnReplaceAll = new JButton(replaceAllAction);
btnReplaceAll.setName("btnReplaceAll");
JButton btnClose = new JButton(closeAction);
Expand Down Expand Up @@ -150,8 +151,13 @@ private void doSearch(SearchContext context, SearchAction action) {
return;
}
}

int pos = context.getSearchForward() ? textArea.getSelectionEnd() : textArea.getSelectionStart();
boolean searchFromSelStart;
if (context.getReplaceWith() != null) {
searchFromSelStart = context.getSearchForward();
} else {
searchFromSelStart = ! context.getSearchForward();
}
int pos = searchFromSelStart ? textArea.getSelectionStart() : textArea.getSelectionEnd();
textArea.setSelectionStart(pos);
textArea.setSelectionEnd(pos);
SearchResult result;
Expand Down
5 changes: 5 additions & 0 deletions src/test-integration/studio/StudioTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public void test() {
window.textBox("txtReplace").setText("aa"); //replacement text should contain the find text as a substring
window.button("btnReplaceAll").click(); //this was an infinite loop with rsyntaxtextarea 3.1.3
tb.requireText("aabc\ndef");
tb.setText("aaa");
window.textBox("txtReplace").setText("c");
window.button("btnReplace").click();
window.button("btnReplace").click();
tb.requireText("cca"); //should replace the second "a", not the third
}

}

0 comments on commit 0375f75

Please sign in to comment.