Skip to content

Commit

Permalink
TextViewer: use Throttler for firePostSelectionChanged #1695
Browse files Browse the repository at this point in the history
Avoids a 500ms delay after changing the selection, like higlighting the
selected word.

#1695
  • Loading branch information
EcljpseB0T committed Feb 20, 2024
1 parent a768a39 commit 7abf4fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*******************************************************************************/
package org.eclipse.jface.text;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -73,6 +74,7 @@
import org.eclipse.jface.internal.text.StickyHoverManager;
import org.eclipse.jface.util.Geometry;
import org.eclipse.jface.util.OpenStrategy;
import org.eclipse.jface.util.Throttler;
import org.eclipse.jface.viewers.IPostSelectionProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
Expand Down Expand Up @@ -1456,6 +1458,7 @@ public void documentRewriteSessionChanged(DocumentRewriteSessionEvent event) {

/** The viewer's text widget */
private StyledText fTextWidget;
private Throttler throttledPostSelection;
/** The viewer's input document */
private IDocument fDocument;
/** The viewer's visible document */
Expand Down Expand Up @@ -1716,7 +1719,7 @@ protected IDocumentAdapter createDocumentAdapter() {
* @param styles the SWT style bits for the viewer's control
*/
protected void createControl(Composite parent, int styles) {

throttledPostSelection= new Throttler(parent.getDisplay(), Duration.ofMillis(getEmptySelectionChangedEventDelay()), this::postSelectionChanged);
fTextWidget= createTextWidget(parent, styles);

// Support scroll page upon MOD1+MouseWheel
Expand Down Expand Up @@ -2613,26 +2616,22 @@ private void queuePostSelectionChanged(final boolean fireEqualSelection) {

fNumberOfPostSelectionChangedEvents[0]++;
fFireEqualPostSelectionChange|= fireEqualSelection;
display.timerExec(getEmptySelectionChangedEventDelay(), new Runnable() {
final int id= fNumberOfPostSelectionChangedEvents[0];
@Override
public void run() {
if (id == fNumberOfPostSelectionChangedEvents[0]) {
// Check again because this is executed after the delay
if (getDisplay() != null) {
Point selection= fTextWidget.getSelectionRange();
if (selection != null) {
IRegion r= widgetRange2ModelRange(new Region(selection.x, selection.y));
if (fFireEqualPostSelectionChange || (r != null && !r.equals(fLastSentPostSelectionChange)) || r == null) {
fLastSentPostSelectionChange= r;
fFireEqualPostSelectionChange= false;
firePostSelectionChanged(selection.x, selection.y);
}
}
}
}
throttledPostSelection.throttledExec();
}

private void postSelectionChanged() {
if (fTextWidget == null || fTextWidget.isDisposed()) {
return;
}
Point selection= fTextWidget.getSelectionRange();
if (selection != null) {
IRegion r= widgetRange2ModelRange(new Region(selection.x, selection.y));
if (fFireEqualPostSelectionChange || (r != null && !r.equals(fLastSentPostSelectionChange)) || r == null) {
fLastSentPostSelectionChange= r;
fFireEqualPostSelectionChange= false;
firePostSelectionChanged(selection.x, selection.y);
}
});
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ public void testPostSelection() throws Throwable {
try {
postProvider.addPostSelectionChangedListener(listener);
editor.setPage(1);
assertEquals(1, fPostCalled);
editor.setPage(0);
assertEquals(2, fPostCalled);
editor.setPage(0);
assertEquals(3, fPostCalled);
} finally {
postProvider.removePostSelectionChangedListener(listener);
}
Expand Down

0 comments on commit 7abf4fa

Please sign in to comment.