Skip to content

Commit

Permalink
bug fix for sequential replace action
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Jan 19, 2024
1 parent c86845c commit 01a1bd8
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 5 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ sourceSets {
java {
srcDirs 'test-integration'
}
resources {
srcDirs 'test-resources'
}
compileClasspath += main.output
runtimeClasspath += main.output
}
Expand Down
4 changes: 3 additions & 1 deletion notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
`dz4.0` 2024.01.09
* Fixed bug with sequential replace action in the editor

`dz4.0` 2024.01.09
-----
* Drill down for complex results (with double click or popup menu)
* Rework kdb connection logic
Expand Down
3 changes: 0 additions & 3 deletions src/studio/ui/EditorPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ public void search(SearchContext context, SearchAction action) {
}
}

int pos = context.getSearchForward() ? textArea.getSelectionEnd() : textArea.getSelectionStart();
textArea.setSelectionStart(pos);
textArea.setSelectionEnd(pos);
SearchResult result;
if (action == SearchAction.Find) {
result = SearchEngine.find(textArea, context);
Expand Down
3 changes: 2 additions & 1 deletion src/studio/ui/StudioWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -1318,11 +1318,12 @@ public StudioWindow(Workspace.TopWindow workspaceWindow) {

toolbar = createToolbar();
editorSearchPanel = new SearchPanel( () -> editor.getPane() );
editorSearchPanel.setName("SearchPanel");
mainStatusBar = new MainStatusBar();
tabbedPane = initResultPane();
resultSearchPanel = initResultSearchPanel();

// We need to have some editor initialize to prevent NPE
// We need to have some editor initialized to prevent NPE
editor = new EditorTab(this);
initToolbar();
topPanel = new JPanel(new BorderLayout());
Expand Down
5 changes: 5 additions & 0 deletions src/studio/ui/search/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public boolean equals(Object obj) {
Position p = (Position) obj;
return column == p.getColumn() && row == p.getRow();
}

@Override
public String toString() {
return "[" + row + ":" + column + "]";
}
}
4 changes: 4 additions & 0 deletions src/studio/ui/search/SearchPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public SearchPanel(EditorPaneLocator editorPaneLocator) {
txtFind = new JTextField();
txtFind.setName("FindField");
txtReplace = new JTextField();
txtReplace.setName("ReplaceField");

JLabel lblFind = new JLabel("Find: ");
lblReplace = new JLabel("Replace: " );
Expand All @@ -62,9 +63,12 @@ public SearchPanel(EditorPaneLocator editorPaneLocator) {
JButton btnFind = new JButton(findAction);
btnFind.setName("FindButton");
JButton btnFindBack = new JButton(findBackAction);
btnFindBack.setName("FindBackButton");
JButton btnMarkAll = new JButton(markAllAction);
btnReplace = new JButton(replaceAction);
btnReplace.setName("ReplaceButton");
btnReplaceAll = new JButton(replaceAllAction);
btnReplaceAll.setName("ReplaceAllButton");
JButton btnClose = new JButton(closeAction);

ActionMap am = txtFind.getActionMap();
Expand Down
158 changes: 158 additions & 0 deletions test-integration/studio/ui/SearchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package studio.ui;

import org.assertj.swing.fixture.JPanelFixture;
import org.assertj.swing.fixture.JTextComponentFixture;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.junit.Before;
import org.junit.Test;
import studio.ui.search.Position;

import javax.swing.text.BadLocationException;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import static org.assertj.swing.edt.GuiActionRunner.execute;
import static org.junit.Assert.assertEquals;

public class SearchTest extends StudioTest {

static class Selection {
Position start, end;
Selection(int startRow, int startColumn, int endRow, int endColumn) {
start = new Position(startRow, startColumn);
end = new Position(endRow, endColumn);
}
Selection(Position start, Position end) {
this.start = start;
this.end = end;
}
@Override
public boolean equals(Object obj) {
if (! (obj instanceof Selection)) return false;
Selection s = (Selection) obj;
return start.equals(s.start) && end.equals(s.end);
}
}

private Position getPositionFromOffset(RSyntaxTextArea textArea, int offset) throws BadLocationException {
int line = textArea.getLineOfOffset(offset);
int col = offset - textArea.getLineStartOffset(line);
return new Position(line, col);
}

private Selection getSelection(RSyntaxTextArea textArea) throws BadLocationException {
int startOffset = textArea.getSelectionStart();
int endOffset = textArea.getSelectionEnd();
return new Selection(
getPositionFromOffset(textArea, startOffset),
getPositionFromOffset(textArea, endOffset) );
}

private Selection getSelection(JTextComponentFixture editor) throws BadLocationException {
return execute( () -> getSelection((RSyntaxTextArea) editor.target()) );
}

private String content;
private JPanelFixture searchPanel;
private JTextComponentFixture editor;

@Before
public void openFile() throws IOException {
File textFile = new File(getClass().getClassLoader().getResource("searchText.q").getFile());
content = new String(Files.readAllBytes(textFile.toPath()), StandardCharsets.UTF_8);

openFile(textFile);
editor = frameFixture.textBox("editor2");
frameFixture.menuItem("Replace...").click();
searchPanel = frameFixture.panel("SearchPanel");
}

@Test
public void testSequentialSearch() throws BadLocationException {
searchPanel.textBox("FindField").setText("select");

searchPanel.button("FindButton").click();
Selection selection = getSelection(editor);
assertEquals(new Selection(1,0,1,6), selection);

searchPanel.button("FindButton").click();
selection = getSelection(editor);
assertEquals(new Selection(3,0,3,6), selection);

searchPanel.button("FindButton").click();
selection = getSelection(editor);
assertEquals(new Selection(4,4,4,10), selection);

searchPanel.button("FindButton").click();
selection = getSelection(editor);
assertEquals(new Selection(1,0,1,6), selection);
}

@Test
public void testSequentialSearchBack() throws BadLocationException {
editor.selectText(70, 70);

JPanelFixture searchPanel = frameFixture.panel("SearchPanel");
searchPanel.textBox("FindField").setText("select");

searchPanel.button("FindBackButton").click();
Selection selection = getSelection(editor);
assertEquals(new Selection(1,0,1,6), selection);

searchPanel.button("FindBackButton").click();
selection = getSelection(editor);
assertEquals(new Selection(4,4,4,10), selection);

searchPanel.button("FindBackButton").click();
selection = getSelection(editor);
assertEquals(new Selection(3,0,3,6), selection);

searchPanel.button("FindBackButton").click();
selection = getSelection(editor);
assertEquals(new Selection(1,0,1,6), selection);
}


private String replace(String src, String what, String to, int at) {
int index = src.indexOf(what, at);
if (index == -1) return src;
return src.substring(0, index) + to + src.substring(index + what.length());
}

@Test
public void testSequentialReplace() throws BadLocationException, IOException {
editor.selectText(70, 70);

searchPanel.textBox("FindField").setText("select");
searchPanel.textBox("ReplaceField").setText("xxx");

searchPanel.button("ReplaceButton").click();
content = replace(content, "select", "xxx", 70);
editor.requireText(content);

searchPanel.button("ReplaceButton").click();
content = replace(content, "select", "xxx", 70);
editor.requireText(content);

searchPanel.button("ReplaceButton").click();
content = replace(content, "select", "xxx", 0);
editor.requireText(content);

searchPanel.button("ReplaceButton").click();
editor.requireText(content);
}

@Test(timeout = 3000)
public void testSequentialReplaceAllRecursive() throws InterruptedException {
frameFixture.menuItem("Replace...").click();

searchPanel.textBox("FindField").setText("select");
searchPanel.textBox("ReplaceField").setText("select i,");

searchPanel.button("ReplaceAllButton").click();
editor.requireText(content.replace("select", "select i,"));
}

}
6 changes: 6 additions & 0 deletions test-resources/searchText.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/ comment
select date, time, sym, price from table where date=.z.d-1;

select count time by date from table where date>.z.d-100;
100#select from table where date=.z.d;
/ the end

0 comments on commit 01a1bd8

Please sign in to comment.