FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

search for classes · dzaima/processing@97dd4aa · GitHub

Repository navigation

Commit 97dd4aa

Browse files
committed
search for classes
1 parent a46249c commit 97dd4aa

6 files changed

Lines changed: 131 additions & 33 deletions

File tree

‎app/src/processing/app/ui/Editor.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,10 @@ public void addErrorTable(EditorFooter ef) {
444444
public EditorState getEditorState() {
445445
return state;
446446
}
447-
448-
447+
448+
public void extraSearch(Search search) { } // by default don't require anything
449+
450+
449451
/**
450452
* Handles files dragged & dropped from the desktop and into the editor
451453
* window. Dragging files into the editor window is the same as using

‎app/src/processing/app/ui/Search.java‎

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import javax.swing.event.*;
3030
import javax.swing.text.*;
3131
import java.awt.*;
32+
import java.awt.Dimension;
3233
import java.awt.event.*;
3334
import java.util.ArrayList;
3435

@@ -92,7 +93,7 @@ public void mouseClicked(MouseEvent evt) {
9293
public void insertUpdate(DocumentEvent e) { call(); }
9394

9495
private void call() {
95-
updateList(searchField.getText());
96+
updateList();
9697
}
9798
});
9899
searchField.addActionListener(e -> {
@@ -154,63 +155,78 @@ private void call() {
154155

155156

156157
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
157-
addWindowListener(new WindowAdapter() {
158-
public void windowClosing(WindowEvent e) {
159-
handleClose();
160-
}
161-
});
162158

163159
Toolkit.registerWindowCloseKeys(getRootPane(), actionEvent -> handleClose());
164160
Toolkit.setIcon(this);
165161

166-
// hack to to get first field to focus properly on osx
167162
addWindowListener(new WindowAdapter() {
163+
// hack to to get first field to focus properly on osx
168164
public void windowActivated(WindowEvent e) {
169165
searchField.requestFocusInWindow();
170166
searchField.selectAll();
171167
}
168+
@Override public void windowDeactivated(WindowEvent e) {
169+
System.out.println(2);handleClose();
170+
}
171+
@Override public void windowClosing(WindowEvent e) {
172+
System.out.println(3);handleClose();
173+
}
172174
});
173175
pack();
174176
setResizable(true);
175177
setLocationRelativeTo(null);
176178

177-
updateList("");
179+
updateList();
178180
}
179181

180-
ArrayList<SearchResult> results;
181-
private void updateList(String search) {
182-
search = search.toLowerCase();
182+
private ArrayList<SearchResult> results;
183+
private void updateList() {
183184
results = new ArrayList<>();
184185
for (SketchCode c : editor.sketch.getCode()) {
185-
if (c.getFileName().toLowerCase().contains(search)) results.add(new SearchResult(c));
186+
if (match(c.getFileName())) results.add(new FileSearchResult(c));
186187
}
188+
189+
editor.extraSearch(this);
190+
191+
reModel();
192+
}
193+
194+
private void reModel() {
187195
list.setModel(new AbstractListModel<SearchResult>() {
188196
@Override
189197
public int getSize() {
190198
return results.size();
191199
}
192-
200+
193201
@Override
194202
public SearchResult getElementAt(int i) {
195203
return results.get(i);
196204
}
197205
});
206+
198207
if (results.size() > 0) list.setSelectedIndex(0);
199208
}
200209

201-
class SearchResult {
210+
public String getText() {
211+
return searchField.getText().toLowerCase();
212+
}
202213

203-
private final SketchCode tab;
214+
public void add(ArrayList<SearchResult> names) {
215+
results.addAll(names);
216+
EventQueue.invokeLater(this::reModel); // idk if invokeLater is needed but i did it so
217+
}
204218

205-
public SearchResult(SketchCode tab) {
219+
class FileSearchResult extends SearchResult {
220+
private final SketchCode tab;
221+
222+
FileSearchResult(SketchCode tab) {
223+
super(Search.this);
206224
this.tab = tab;
207225
}
208-
209226
@Override public String toString() {
210-
return tab.getFileName();
227+
return tab.getPrettyName();
211228
}
212-
213-
public void select() {
229+
@Override public void select() {
214230
handleClose();
215231
editor.sketch.setCurrentCode(tab);
216232
}
@@ -295,14 +311,9 @@ private boolean find(boolean wrap) {
295311
}
296312

297313

314+
public boolean match(String str) {
315+
return str.toLowerCase().contains(getText());
316+
}
298317

299318

300-
301-
/**
302-
* Returns true if find next/previous will work, for graying-
303-
* out of menu items.
304-
*/
305-
public boolean canFindNext() {
306-
return searchField.getText().length() != 0;
307-
}
308-
}
319+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package processing.app.ui;
2+
3+
public abstract class SearchResult {
4+
5+
private final Search s;
6+
7+
public SearchResult(Search s) {
8+
this.s = s;
9+
}
10+
11+
public abstract void select();
12+
public void close() {
13+
s.handleClose();
14+
}
15+
}

‎java/src/processing/mode/java/JavaEditor.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import javax.swing.text.BadLocationException;
1717
import javax.swing.text.Document;
1818

19+
import org.eclipse.jdt.core.dom.SimpleName;
1920
import processing.core.PApplet;
2021
import processing.data.StringList;
2122
import processing.app.*;
@@ -1816,7 +1817,6 @@ protected void downloadImports() {
18161817
* Returns a list of AvailableContributions of those libraries that the user
18171818
* wants imported, but that are not installed.
18181819
*
1819-
* @param importHeaders
18201820
*/
18211821
private List<AvailableContribution> getNotInstalledAvailableLibs(ArrayList<String> importHeadersList) {
18221822
Map<String, Contribution> importMap =
@@ -2812,4 +2812,10 @@ static private int howManyFloats(List<List<Handle>> handles) {
28122812
}
28132813
return count;
28142814
}
2815+
2816+
2817+
2818+
public void extraSearch(Search search) {
2819+
pdex.getSearcher().search(search, search::add);
2820+
}
28152821
}

‎java/src/processing/mode/java/pdex/PDEX.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class PDEX {
1919
private ShowUsage usage;
2020
private Rename rename;
2121
private DebugTree debugTree;
22+
private Searcher searcher;
2223

2324
private PreprocessingService pps;
2425

@@ -29,8 +30,9 @@ public PDEX(JavaEditor editor, PreprocessingService pps) {
2930
this.enabled = !editor.hasJavaTabs();
3031

3132
errorChecker = new ErrorChecker(editor, pps);
32-
33+
3334
usage = new ShowUsage(editor, pps);
35+
searcher = new Searcher(editor, pps);
3436
inspect = new InspectMode(editor, pps, usage);
3537
rename = new Rename(editor, pps, usage);
3638

@@ -104,4 +106,8 @@ public void dispose() {
104106
public void documentChanged(Document newDoc) {
105107
addDocumentListener(newDoc);
106108
}
109+
110+
public Searcher getSearcher() {
111+
return searcher;
112+
}
107113
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package processing.mode.java.pdex;
2+
3+
import org.eclipse.jdt.core.dom.*;
4+
import processing.app.ui.*;
5+
import processing.mode.java.JavaEditor;
6+
7+
import java.awt.*;
8+
import java.util.ArrayList;
9+
import java.util.function.Consumer;
10+
11+
public class Searcher {
12+
private final JavaEditor editor;
13+
private final PreprocessingService pps;
14+
15+
Searcher(JavaEditor editor, PreprocessingService pps) {
16+
17+
this.editor = editor;
18+
this.pps = pps;
19+
}
20+
public void search(Search s, Consumer<ArrayList<SearchResult>> callback) {
21+
pps.whenDoneBlocking(ps -> handleSearch(ps, s, callback));
22+
}
23+
24+
private void handleSearch(PreprocessedSketch ps, Search search, Consumer<ArrayList<SearchResult>> callback) {
25+
ArrayList<SearchResult> occurrences = new ArrayList<>();
26+
ps.compilationUnit.getRoot().accept(new ASTVisitor() {
27+
@Override
28+
public boolean visit(TypeDeclaration name) {
29+
if (search.match(name.getName().getIdentifier())) occurrences.add(new SNSearchResult(search, name, ps));
30+
return super.visit(name);
31+
}
32+
});
33+
callback.accept(occurrences);
34+
}
35+
36+
class SNSearchResult extends SearchResult {
37+
38+
private final TypeDeclaration item;
39+
private final PreprocessedSketch ps;
40+
41+
SNSearchResult(Search s, TypeDeclaration item, PreprocessedSketch ps) {
42+
super(s);
43+
this.item = item;
44+
this.ps = ps;
45+
}
46+
47+
public void select() {
48+
PreprocessedSketch.SketchInterval si = ps.mapJavaToSketch(item.getName());
49+
if (!ps.inRange(si)) return;
50+
EventQueue.invokeLater(() -> editor.highlight(si.tabIndex, si.startTabOffset, si.stopTabOffset));
51+
close();
52+
}
53+
54+
@Override public String toString() {
55+
return item.getName().getIdentifier();
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL