Skip to content

Feature/search and replace improved dialog #2070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 81 additions & 14 deletions app/src/processing/app/FindReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public class FindReplace extends JFrame implements ActionListener {
JCheckBox wrapAroundBox;
static boolean wrapAround = true;

JCheckBox searchAllFilesBox;
static boolean searchAllFiles = false;

public FindReplace(Editor editor) {
super("Find");
setResizable(false);
Expand All @@ -88,6 +91,10 @@ public FindReplace(Editor editor) {
pain.add(replaceField = new JTextField(20));
int fieldHeight = findField.getPreferredSize().height;

// Fill the findString with selected text if no previous value
if(editor.getSelectedText()!=null && editor.getSelectedText().length()>0)
findString = editor.getSelectedText();

if (findString != null) findField.setText(findString);
if (replaceString != null) replaceField.setText(replaceString);
//System.out.println("setting find str to " + findString);
Expand All @@ -110,6 +117,15 @@ public void actionPerformed(ActionEvent e) {
});
wrapAroundBox.setSelected(wrapAround);
pain.add(wrapAroundBox);

searchAllFilesBox = new JCheckBox(_("Search all Sketch Tabs"));
searchAllFilesBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchAllFiles = searchAllFilesBox.isSelected();
}
});
searchAllFilesBox.setSelected(searchAllFiles);
pain.add(searchAllFilesBox);

JPanel buttons = new JPanel();

Expand Down Expand Up @@ -180,9 +196,13 @@ public void focusLost(FocusEvent e) {

ignoreCaseBox.setBounds(EDGE + labelDimension.width + SMALL,
ypos,
(fieldWidth-SMALL)/2, fieldHeight);
(fieldWidth-SMALL)/4, fieldHeight);

wrapAroundBox.setBounds(EDGE + labelDimension.width + SMALL + (fieldWidth-SMALL)/2 + SMALL,
wrapAroundBox.setBounds(EDGE + labelDimension.width + SMALL + (fieldWidth-SMALL)/4 + SMALL,
ypos,
(fieldWidth-SMALL)/4, fieldHeight);

searchAllFilesBox.setBounds(EDGE + labelDimension.width + SMALL + (int)((fieldWidth-SMALL)/1.9) + SMALL,
ypos,
(fieldWidth-SMALL)/2, fieldHeight);

Expand Down Expand Up @@ -291,8 +311,9 @@ public void actionPerformed(ActionEvent e) {
// look for the next instance of the find string to be found
// once found, select it (and go to that line)

private boolean find(boolean wrap,boolean backwards ) {

private boolean find(boolean wrap,boolean backwards,boolean searchTabs,int originTab) {
//System.out.println("Find: " + originTab);
boolean wrapNeeded = false;
String search = findField.getText();
//System.out.println("finding for " + search + " " + findString);
// this will catch "find next" being called when no search yet
Expand All @@ -313,7 +334,7 @@ private boolean find(boolean wrap,boolean backwards ) {
nextIndex = text.indexOf(search, selectionEnd);
if (wrap && nextIndex == -1) {
// if wrapping, a second chance is ok, start from beginning
nextIndex = text.indexOf(search, 0);
wrapNeeded = true;
}
} else {
//int selectionStart = editor.textarea.getSelectionStart();
Expand All @@ -326,16 +347,58 @@ private boolean find(boolean wrap,boolean backwards ) {
}
if (wrap && nextIndex == -1) {
// if wrapping, a second chance is ok, start from the end
nextIndex = text.lastIndexOf(search);
wrapNeeded = true;
}
}

if (nextIndex != -1) {
editor.setSelection(nextIndex, nextIndex + search.length());
} else {
//Toolkit.getDefaultToolkit().beep();
if (nextIndex == -1) {
//Nothing found on this tab: Search other tabs if required
if(searchTabs)
{
//editor.
Sketch sketch = editor.getSketch();
if(sketch.getCodeCount()>1)
{
int realCurrentTab = sketch.getCodeIndex(sketch.getCurrentCode());

if(originTab!=realCurrentTab)
{
if(originTab <0)
originTab = realCurrentTab;

if(!wrap)
if((!backwards && realCurrentTab+1 >= sketch.getCodeCount()) || (backwards && realCurrentTab-1 < 0))
return false; // Can't continue without wrap

if(backwards)
{
sketch.handlePrevCode();
this.setVisible(true);
int l = editor.getText().length()-1;
editor.setSelection(l,l);
}
else
{
sketch.handleNextCode();
this.setVisible(true);
editor.setSelection(0,0);
}

return find(wrap,backwards,searchTabs,originTab);
}
}
}

if(wrapNeeded)
nextIndex = backwards? text.lastIndexOf(search):text.indexOf(search, 0);
}
return nextIndex != -1;

if (nextIndex != -1) {
editor.setSelection(nextIndex, nextIndex + search.length());
return true;
}

return false;
}


Expand All @@ -344,6 +407,8 @@ private boolean find(boolean wrap,boolean backwards ) {
* replacement text field.
*/
public void replace() {
if(findField.getText().length()==0)
return;
editor.setSelectedText(replaceField.getText());
editor.getSketch().setModified(true); // TODO is this necessary?
}
Expand All @@ -362,12 +427,14 @@ public void replaceAndFindNext() {
* alternately until nothing more found.
*/
public void replaceAll() {
if(findField.getText().length()==0)
return;
// move to the beginning
editor.setSelection(0, 0);

boolean foundAtLeastOne = false;
while ( true ) {
if ( find(false,false) ) {
if ( find(false,false,searchAllFiles,-1)) {
foundAtLeastOne = true;
replace();
} else {
Expand All @@ -385,13 +452,13 @@ public void setFindText( String t ) {
}

public void findNext() {
if ( !find( wrapAround, false ) ) {
if ( !find( wrapAround, false, searchAllFiles,-1 ) ) {
Toolkit.getDefaultToolkit().beep();
}
}

public void findPrevious() {
if ( !find( wrapAround, true ) ) {
if ( !find( wrapAround, true, searchAllFiles,-1 ) ) {
Toolkit.getDefaultToolkit().beep();
}
}
Expand Down