mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-26 20:54:22 +01:00
Merge 08ed2cb83dcaea670c0410ee08237261330a24d0 into 3278173ef810935e07808deed58783c1bc7ca4cf
This commit is contained in:
commit
7ce09de91e
@ -33,6 +33,7 @@ import processing.app.Base;
|
|||||||
import processing.app.Editor;
|
import processing.app.Editor;
|
||||||
import processing.app.EditorTab;
|
import processing.app.EditorTab;
|
||||||
import processing.app.helpers.OSUtils;
|
import processing.app.helpers.OSUtils;
|
||||||
|
import processing.app.syntax.SketchTextArea;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
@ -40,7 +41,13 @@ import java.awt.event.WindowEvent;
|
|||||||
import javax.swing.JPopupMenu;
|
import javax.swing.JPopupMenu;
|
||||||
import javax.swing.Action;
|
import javax.swing.Action;
|
||||||
import javax.swing.text.DefaultEditorKit;
|
import javax.swing.text.DefaultEditorKit;
|
||||||
|
import javax.swing.text.BadLocationException;
|
||||||
|
import javax.swing.text.DefaultHighlighter;
|
||||||
|
import javax.swing.text.Highlighter;
|
||||||
|
import javax.swing.text.Highlighter.Highlight;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static processing.app.I18n.tr;
|
import static processing.app.I18n.tr;
|
||||||
@ -81,6 +88,10 @@ public class FindReplace extends javax.swing.JFrame {
|
|||||||
findField.requestFocusInWindow();
|
findField.requestFocusInWindow();
|
||||||
findField.selectAll();
|
findField.selectAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void windowDeactivated(WindowEvent windowEvent) {
|
||||||
|
clearHighlights();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
restoreFindDialogState(state);
|
restoreFindDialogState(state);
|
||||||
@ -303,38 +314,85 @@ public class FindReplace extends javax.swing.JFrame {
|
|||||||
private javax.swing.JCheckBox wrapAroundBox;
|
private javax.swing.JCheckBox wrapAroundBox;
|
||||||
// End of variables declaration//GEN-END:variables
|
// End of variables declaration//GEN-END:variables
|
||||||
|
|
||||||
private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int originTab) {
|
private List<Highlight> foundHighlights = new ArrayList<Highlight>();
|
||||||
String search = findField.getText();
|
private Highlight selectedHighlight = null;
|
||||||
|
private int selectedHighlightIndex = -1;
|
||||||
|
private String lastSearchText = "";
|
||||||
|
private String lastText = "";
|
||||||
|
|
||||||
if (search.length() == 0) {
|
private void clearHighlights() {
|
||||||
|
Highlighter highlighter = editor.getCurrentTab().getTextArea().getHighlighter();
|
||||||
|
for (Highlight highlight : foundHighlights) {
|
||||||
|
editor.getCurrentTab().getTextArea().getHighlighter().removeHighlight(highlight);
|
||||||
|
}
|
||||||
|
foundHighlights.clear();
|
||||||
|
selectedHighlight = null;
|
||||||
|
lastSearchText = "";
|
||||||
|
lastText = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int originTab) {
|
||||||
|
String searchText = findField.getText();
|
||||||
|
int searchTextLength = searchText.length();
|
||||||
|
|
||||||
|
if (searchTextLength == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String text = editor.getCurrentTab().getText();
|
EditorTab currentTab = editor.getCurrentTab();
|
||||||
|
SketchTextArea textarea = currentTab.getTextArea();
|
||||||
|
String text = currentTab.getText();
|
||||||
|
|
||||||
if (ignoreCaseBox.isSelected()) {
|
if (ignoreCaseBox.isSelected()) {
|
||||||
search = search.toLowerCase();
|
searchText = searchText.toLowerCase();
|
||||||
text = text.toLowerCase();
|
text = text.toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
int nextIndex;
|
// search all indexes
|
||||||
|
if (!lastSearchText.equals(searchText) || !lastText.equals(text)) {
|
||||||
|
clearHighlights();
|
||||||
|
|
||||||
|
int index = text.indexOf(searchText, 0);
|
||||||
|
while (index >= 0) {
|
||||||
|
int endIndex = index + searchTextLength;
|
||||||
|
|
||||||
|
try {
|
||||||
|
foundHighlights.add((Highlight)textarea.getHighlighter().addHighlight(index, endIndex,
|
||||||
|
new DefaultHighlighter.DefaultHighlightPainter(Color.yellow)));
|
||||||
|
} catch (BadLocationException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
index = text.indexOf(searchText, endIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastSearchText = searchText;
|
||||||
|
lastText = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
Highlight nextHighlight = null;
|
||||||
if (!backwards) {
|
if (!backwards) {
|
||||||
// int selectionStart = editor.textarea.getSelectionStart();
|
|
||||||
int selectionEnd = editor.getCurrentTab().getSelectionStop();
|
int selectionEnd = editor.getCurrentTab().getSelectionStop();
|
||||||
|
|
||||||
nextIndex = text.indexOf(search, selectionEnd);
|
for (Highlight h : foundHighlights) {
|
||||||
|
if (h.getStartOffset() < selectionEnd) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
nextHighlight = h;
|
||||||
|
break;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// int selectionStart = editor.textarea.getSelectionStart();
|
|
||||||
int selectionStart = editor.getCurrentTab().getSelectionStart() - 1;
|
int selectionStart = editor.getCurrentTab().getSelectionStart() - 1;
|
||||||
|
|
||||||
if (selectionStart >= 0) {
|
for (Highlight h : foundHighlights) {
|
||||||
nextIndex = text.lastIndexOf(search, selectionStart);
|
if (h.getStartOffset() > selectionStart) {
|
||||||
} else {
|
break;
|
||||||
nextIndex = -1;
|
}
|
||||||
|
nextHighlight = h;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nextIndex == -1) {
|
if (nextHighlight == null) {
|
||||||
// Nothing found on this tab: Search other tabs if required
|
// Nothing found on this tab: Search other tabs if required
|
||||||
if (searchTabs) {
|
if (searchTabs) {
|
||||||
int numTabs = editor.getTabs().size();
|
int numTabs = editor.getTabs().size();
|
||||||
@ -353,6 +411,8 @@ public class FindReplace extends javax.swing.JFrame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clearHighlights();
|
||||||
|
|
||||||
if (backwards) {
|
if (backwards) {
|
||||||
editor.selectPrevTab();
|
editor.selectPrevTab();
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
@ -370,15 +430,31 @@ public class FindReplace extends javax.swing.JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (wrap) {
|
if (wrap) {
|
||||||
nextIndex = backwards ? text.lastIndexOf(search) : text.indexOf(search, 0);
|
nextHighlight = backwards ? foundHighlights.get(foundHighlights.size() - 1) : foundHighlights.get(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nextIndex != -1) {
|
if (nextHighlight != null) {
|
||||||
EditorTab currentTab = editor.getCurrentTab();
|
Highlighter highlighter = textarea.getHighlighter();
|
||||||
currentTab.getTextArea().getFoldManager().ensureOffsetNotInClosedFold(nextIndex);
|
int nextIndex = nextHighlight.getStartOffset();
|
||||||
currentTab.setSelection(nextIndex, nextIndex + search.length());
|
|
||||||
currentTab.getTextArea().getCaret().setSelectionVisible(true);
|
try {
|
||||||
|
if (selectedHighlight != null) {
|
||||||
|
foundHighlights.add(selectedHighlightIndex, (Highlight)highlighter.addHighlight(selectedHighlight.getStartOffset(), selectedHighlight.getEndOffset(), selectedHighlight.getPainter()));
|
||||||
|
}
|
||||||
|
selectedHighlight = nextHighlight;
|
||||||
|
selectedHighlightIndex = foundHighlights.indexOf(selectedHighlight);
|
||||||
|
foundHighlights.remove(selectedHighlight);
|
||||||
|
|
||||||
|
highlighter.removeHighlight(selectedHighlight);
|
||||||
|
|
||||||
|
} catch (BadLocationException bl) {
|
||||||
|
bl.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
textarea.getFoldManager().ensureOffsetNotInClosedFold(nextIndex);
|
||||||
|
currentTab.setSelection(nextIndex, nextIndex + searchTextLength);
|
||||||
|
textarea.getCaret().setSelectionVisible(true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user