diff options
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 b4390a743a5..0e5a865e52b 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 00000000000..6f9a11e01bf --- /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 7c3a171f1c1..08aac2c3c39 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 ac12e9cc0d2..5ab2dafd55e 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; + } + } }; |