Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEtienne Reichenbach2018-07-13 14:15:46 +0000
committerEtienne Reichenbach2018-07-18 13:54:48 +0000
commit982b689404fbe86e68a1a36dd5460d4183fe9097 (patch)
tree8ad536a69e3c3e068fd54b4f51a16a58b20d4642
parent70121cc291f51eed06c87255a67b5dd2c61d64b7 (diff)
downloadeclipse.platform.text-982b689404fbe86e68a1a36dd5460d4183fe9097.tar.gz
eclipse.platform.text-982b689404fbe86e68a1a36dd5460d4183fe9097.tar.xz
eclipse.platform.text-982b689404fbe86e68a1a36dd5460d4183fe9097.zip
Bug 536952 - Avoid QuickDiff re-enabling in DocumentRewriteSessionEvent
* Only resume the DocumentLineDiffer if if was previously suspended by a DocumentRewriteSessionEvent. * Add a null check at AbstractDocument.stopRewriteSession(DocumentRewriteSession) to avoid a NullPointerException if startRewriteSession is not called before calling stopRewriteSession. The javadoc of stopRewriteSession says "This method has only any effect if startRewriteSession has been called before.", without the null-check the method had an effect if startRewriteSession was not called before, namely an NPE. Change-Id: Ia6f92c686c934e1c7cbb514e2c32ac055b557935 Signed-off-by: Etienne Reichenbach <etienner@gmail.com>
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java2
-rw-r--r--org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/DocumentLineDifferTest.java159
-rw-r--r--org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/WorkbenchTextEditorTestSuite.java3
-rw-r--r--org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/texteditor/quickdiff/DocumentLineDiffer.java20
4 files changed, 180 insertions, 4 deletions
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java b/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java
index b4390a743..0e5a865e5 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java
+++ b/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java
@@ -1497,7 +1497,7 @@ public abstract class AbstractDocument implements IDocument, IDocumentExtension,
@Override
public void stopRewriteSession(DocumentRewriteSession session) {
- if (fDocumentRewriteSession == session) {
+ if (fDocumentRewriteSession != null && fDocumentRewriteSession == session) {
if (DEBUG)
System.out.println("AbstractDocument: Stopping rewrite session: " + session); //$NON-NLS-1$
diff --git a/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/DocumentLineDifferTest.java b/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/DocumentLineDifferTest.java
new file mode 100644
index 000000000..6f9a11e01
--- /dev/null
+++ b/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/DocumentLineDifferTest.java
@@ -0,0 +1,159 @@
+package org.eclipse.ui.workbench.texteditor.tests;
+
+import static org.eclipse.jface.text.DocumentRewriteSessionType.SEQUENTIAL;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.IDocument;
+
+import org.eclipse.ui.internal.texteditor.quickdiff.DocumentLineDiffer;
+
+/**
+ * Tests for the {@link DocumentLineDiffer}.
+ */
+public class DocumentLineDifferTest {
+
+ /** The document to connect to the {@link #fLineDiffer}. */
+ private Document fDocument= new Document();
+
+
+ /** The {@link DocumentLineDiffer line differ} under test. */
+ private DocumentLineDiffer fLineDiffer= new DocumentLineDiffer();
+
+
+ /**
+ * Test that when a document is {@link DocumentLineDiffer#connect(IDocument) connected} the
+ * differ is neither {@link DocumentLineDiffer#isSuspended() suspended} nor
+ * {@link DocumentLineDiffer#isSynchronized() synchronized} (i.e. it is initializing).
+ *
+ * @throws Exception unexpected exception
+ */
+ @Test
+ public void testLineDifferStateAfterConnectingDocument() throws Exception {
+ // when
+ fLineDiffer.connect(fDocument);
+
+ // then
+ assertFalse(fLineDiffer.isSuspended());
+ assertFalse(fLineDiffer.isSynchronized());
+ }
+
+ /**
+ * Test that when {@link DocumentLineDiffer#suspend()} is called the differ is
+ * {@link DocumentLineDiffer#isSuspended() suspended}.
+ *
+ * @throws Exception unexpected exception
+ */
+ @Test
+ public void suspendSuspendsLineDiffer() throws Exception {
+ // given
+ fLineDiffer.connect(fDocument);
+
+ // when
+ fLineDiffer.suspend();
+
+ // then
+ assertTrue(fLineDiffer.isSuspended());
+ }
+
+ /**
+ * Test that after a suspended {@link DocumentLineDiffer line differ} is
+ * {@link DocumentLineDiffer#resume() resumed} is not {@link DocumentLineDiffer#isSuspended()
+ * suspended} anymore.
+ *
+ * @throws Exception unexpected exception
+ */
+ @Test
+ public void lineDifferNotSuspendedAfterResumeIsCalled() throws Exception {
+ // given
+ fLineDiffer.connect(fDocument);
+ fLineDiffer.suspend();
+
+ // when
+ fLineDiffer.resume();
+
+ // then
+ assertFalse(fLineDiffer.isSuspended());
+ assertFalse(fLineDiffer.isSynchronized());
+ }
+
+ /**
+ * Test that when the document connected to a non suspended {@link DocumentLineDiffer line
+ * differ} starts a rewrite session the differ gets suspended.
+ *
+ * @throws Exception unexpected exception
+ */
+ @Test
+ public void nonSuspendedLineDifferNotSuspendedAfterStartRewriteSession() throws Exception {
+ // given
+ fLineDiffer.connect(fDocument);
+
+ // when
+ fDocument.startRewriteSession(SEQUENTIAL);
+
+ // then
+ assertTrue(fLineDiffer.isSuspended());
+ }
+
+ /**
+ * Test that when a document connected to a suspended {@link DocumentLineDiffer line differ}
+ * stops the rewrite session, the differ gets suspended.
+ *
+ * @throws Exception unexpected exception
+ */
+ @Test
+ public void suspendedLineDifferStillSuspendedAfterStopRewriteSession() throws Exception {
+ // given
+ fLineDiffer.connect(fDocument);
+ fLineDiffer.suspend();
+
+ // when
+ fDocument.stopRewriteSession(fDocument.getActiveRewriteSession());
+
+ // then
+ assertTrue(fLineDiffer.isSuspended());
+ }
+
+ /**
+ * Test that when the document connected to a suspended {@link DocumentLineDiffer line differ}
+ * goes through a rewrite session, the differ stays suspended.
+ *
+ * @throws Exception unexpected exception
+ */
+ @Test
+ public void suspendedLineDifferStaysSuspendedAfterDocumentRewriteSession() throws Exception {
+ // given
+ fLineDiffer.connect(fDocument);
+ fLineDiffer.suspend();
+
+ // when
+ fDocument.startRewriteSession(SEQUENTIAL);
+ fDocument.stopRewriteSession(fDocument.getActiveRewriteSession());
+
+ // then
+ assertTrue(fLineDiffer.isSuspended());
+ }
+
+ /**
+ * Test that when a document connected to a non suspended {@link DocumentLineDiffer line differ}
+ * goes through a rewrite session, the differ stays in the non suspended state.
+ *
+ * @throws Exception unexpected exception
+ */
+ @Test
+ public void nonSuspendedLineDifferStaysNonSuspendedAfterDocumentRewriteSession() throws Exception {
+ // given
+ fLineDiffer.connect(fDocument);
+
+ // when
+ fDocument.startRewriteSession(SEQUENTIAL);
+ fDocument.stopRewriteSession(fDocument.getActiveRewriteSession());
+
+ // then
+ assertFalse(fLineDiffer.isSuspended());
+ }
+
+}
diff --git a/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/WorkbenchTextEditorTestSuite.java b/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/WorkbenchTextEditorTestSuite.java
index 7c3a171f1..08aac2c3c 100644
--- a/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/WorkbenchTextEditorTestSuite.java
+++ b/org.eclipse.ui.workbench.texteditor.tests/src/org/eclipse/ui/workbench/texteditor/tests/WorkbenchTextEditorTestSuite.java
@@ -34,7 +34,8 @@ import org.eclipse.ui.workbench.texteditor.tests.rulers.RulerTestSuite;
RulerTestSuite.class,
HunkComputerTest.class,
ScreenshotTest.class,
- AbstractTextZoomHandlerTest.class
+ AbstractTextZoomHandlerTest.class,
+ DocumentLineDifferTest.class
})
public class WorkbenchTextEditorTestSuite {
// see @SuiteClasses
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/texteditor/quickdiff/DocumentLineDiffer.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/texteditor/quickdiff/DocumentLineDiffer.java
index ac12e9cc0..5ab2dafd5 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/texteditor/quickdiff/DocumentLineDiffer.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/internal/texteditor/quickdiff/DocumentLineDiffer.java
@@ -213,14 +213,30 @@ public class DocumentLineDiffer implements ILineDiffer, IDocumentListener, IAnno
* @since 3.2
*/
private final IDocumentRewriteSessionListener fSessionListener= new IDocumentRewriteSessionListener() {
+
+ /**
+ * <code>true</code> if this line differ was suspended because of a
+ * {@link DocumentRewriteSessionEvent#SESSION_START session start} and
+ * needs to be resumed after a
+ * {@link DocumentRewriteSessionEvent#SESSION_STOP session stop} event,
+ * <code>false</code> otherwise.
+ */
+ private boolean fResumeOnRewriteSessionStop;
+
@Override
public void documentRewriteSessionChanged(DocumentRewriteSessionEvent event) {
if (event.getSession().getSessionType() == DocumentRewriteSessionType.UNRESTRICTED_SMALL)
return;
- if (DocumentRewriteSessionEvent.SESSION_START.equals(event.getChangeType()))
+ if (DocumentRewriteSessionEvent.SESSION_START.equals(event.getChangeType())) {
+ fResumeOnRewriteSessionStop = !isSuspended();
suspend();
- else if (DocumentRewriteSessionEvent.SESSION_STOP.equals(event.getChangeType()))
+ }
+ else if (fResumeOnRewriteSessionStop
+ && DocumentRewriteSessionEvent.SESSION_STOP.equals(event.getChangeType())) {
resume();
+ fResumeOnRewriteSessionStop= false;
+ }
+
}
};

Back to the top