Skip to content

Commit 712c3ee

Browse files
committed
Implement "Mark occurrences" by double-click and Error Strip
The Mark All' occurrences feature is restricted to certain identifiers defined on individually for each language. This makes it so, that in addition to whatever is being specified, it is also possible to double-click on any string to highlight all occurrences. In addition, this adds an error strip to the right side of the text area, marking the positioning of lines associated with syntax errors and 'Mark all'/'Find all' occurrences. Related to @acardona's report, #56 (comment)
1 parent f78a3fe commit 712c3ee

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

src/main/java/org/scijava/ui/swing/script/EditorPane.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929

3030
package org.scijava.ui.swing.script;
3131

32+
import java.awt.Color;
3233
import java.awt.Component;
3334
import java.awt.Container;
3435
import java.awt.Dimension;
3536
import java.awt.Font;
3637
import java.awt.Graphics2D;
3738
import java.awt.event.ActionEvent;
39+
import java.awt.event.MouseAdapter;
40+
import java.awt.event.MouseEvent;
3841
import java.awt.image.BufferedImage;
3942
import java.io.BufferedReader;
4043
import java.io.BufferedWriter;
@@ -71,6 +74,8 @@
7174
import org.fife.ui.rtextarea.RTextArea;
7275
import org.fife.ui.rtextarea.RTextScrollPane;
7376
import org.fife.ui.rtextarea.RecordableTextAction;
77+
import org.fife.ui.rtextarea.SearchContext;
78+
import org.fife.ui.rtextarea.SearchEngine;
7479
import org.scijava.Context;
7580
import org.scijava.log.LogService;
7681
import org.scijava.platform.PlatformService;
@@ -164,6 +169,42 @@ public void hyperlinkUpdate(final HyperlinkEvent hle) {
164169
wordMovement(-1, true));
165170
ToolTipManager.sharedInstance().registerComponent(this);
166171
getDocument().addDocumentListener(this);
172+
173+
addMouseListener(new MouseAdapter() {
174+
175+
SearchContext context;
176+
177+
@Override
178+
public void mousePressed(final MouseEvent me) {
179+
180+
// 2022.02 TF: 'Mark All' occurrences is quite awkward. What is
181+
// marked is language-specific and the defaults are restricted
182+
// to certain identifiers. We'll hack things so that it works
183+
// for any selection by double-click. See
184+
// https://github.com/bobbylight/RSyntaxTextArea/issues/88
185+
if (getMarkOccurrences() && 2 == me.getClickCount()) {
186+
187+
// Do nothing if getMarkOccurrences() is unset or no selection exists
188+
final String str = getSelectedText();
189+
if (str == null) return;
190+
191+
if (context != null && str.equals(context.getSearchFor())) {
192+
// Selection is the previously 'marked all' scope. Clear it
193+
SearchEngine.markAll(EditorPane.this, new SearchContext());
194+
context = null;
195+
} else {
196+
// Use SearchEngine for 'mark all'
197+
final Color stashedColor = getMarkAllHighlightColor();
198+
setMarkAllHighlightColor(getMarkOccurrencesColor());
199+
context = new SearchContext(str, true);
200+
context.setMarkAll(true);
201+
context.setWholeWord(true);
202+
SearchEngine.markAll(EditorPane.this, context);
203+
setMarkAllHighlightColor(stashedColor);
204+
}
205+
}
206+
}
207+
});
167208
}
168209

169210
@Override

src/main/java/org/scijava/ui/swing/script/TextEditorTab.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
package org.scijava.ui.swing.script;
3131

32+
import java.awt.BorderLayout;
3233
import java.awt.Dimension;
3334
import java.awt.GridBagConstraints;
3435
import java.awt.GridBagLayout;
@@ -56,6 +57,7 @@
5657
import javax.swing.SwingUtilities;
5758
import javax.swing.text.JTextComponent;
5859

60+
import org.fife.ui.rsyntaxtextarea.ErrorStrip;
5961
import org.scijava.ui.swing.script.TextEditor.Executer;
6062

6163
/**
@@ -297,8 +299,19 @@ public void actionPerformed(final ActionEvent e) {
297299
});
298300

299301
screenAndPromptSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, bottom, prompt_panel);
300-
301-
super.setLeftComponent(editorPane.wrappedInScrollbars());
302+
303+
// Enable ErrorSrip à la Eclipse. This will keep track of lines with 'Mark All'
304+
// occurrences as well as lines associated with ParserNotice.Level.WARNING and
305+
// ParserNotice.Level.ERROR. NB: As is, the end of the strip corresponds to the
306+
// last line of text in the text area: E.g., for a text area with just 3 lines,
307+
// line 2 will be marked at the strip's half height
308+
final ErrorStrip es = new ErrorStrip(editorPane);
309+
es.setShowMarkAll(true);
310+
es.setShowMarkedOccurrences(true);
311+
final JPanel holder = new JPanel(new BorderLayout());
312+
holder.add(editorPane.wrappedInScrollbars());
313+
holder.add(es, BorderLayout.LINE_END);
314+
super.setLeftComponent(holder);
302315
super.setRightComponent(screenAndPromptSplit);
303316
screenAndPromptSplit.setDividerLocation(1.0);
304317

0 commit comments

Comments
 (0)