Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2014-08-15 02:54:18 +0000
committerSergey Prigogin2014-08-18 18:47:57 +0000
commit90d747a70773d55259e1fb0a14800a495d503761 (patch)
tree068fd698abd25dc98fa4d716a6793f21ba543712
parenta6c3fcd0d01212acee000c36c1f9d18f9224978f (diff)
downloadeclipse.platform.text-90d747a70773d55259e1fb0a14800a495d503761.tar.gz
eclipse.platform.text-90d747a70773d55259e1fb0a14800a495d503761.tar.xz
eclipse.platform.text-90d747a70773d55259e1fb0a14800a495d503761.zip
Bug 441827 - TextViewer.ViewerState.restore method looses caret position
Change-Id: I89be1f8a6712f390f2cd8e6a5b8ea3d329c5fb4d Signed-off-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
-rw-r--r--org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java1
-rw-r--r--org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java60
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java17
3 files changed, 67 insertions, 11 deletions
diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java
index e8dae5280..18f225fa0 100644
--- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java
+++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/JFaceTextTestSuite.java
@@ -30,6 +30,7 @@ public class JFaceTextTestSuite extends TestSuite {
suite.addTest(TextHoverPopupTest.suite());
suite.addTest(TextPresentationTest.suite());
suite.addTest(DefaultUndoManagerTest.suite());
+ suite.addTest(TextViewerTest.suite());
suite.addTest(TextViewerUndoManagerTest.suite());
suite.addTest(RulesTestSuite.suite());
suite.addTest(ReconcilerTestSuite.suite());
diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java
new file mode 100644
index 000000000..3b529ff08
--- /dev/null
+++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Google, Inc and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Sergey Prigogin (Google) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jface.text.tests;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Shell;
+
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.TextViewer;
+
+/**
+ * Basic tests for TextViewer.
+ */
+public class TextViewerTest extends TestCase {
+ public static Test suite() {
+ return new TestSuite(TextViewerTest.class);
+ }
+
+ private Shell fShell;
+ private TextViewer fTextViewer;
+ private Document fDocument;
+
+ protected void setUp() {
+ fShell= new Shell();
+ fTextViewer= new TextViewer(fShell, SWT.NONE);
+ }
+
+ protected void tearDown() {
+ fShell.dispose();
+ }
+
+ public void testSetRedraw_Bug441827() throws Exception {
+ fDocument= new Document("abc");
+ fTextViewer.setDocument(fDocument);
+ int len= fDocument.getLength();
+ // Select the whole document with the caret at the beginning.
+ fTextViewer.setSelectedRange(len, -len);
+ assertEquals(0, fTextViewer.getSelectedRange().x);
+ assertEquals(len, fTextViewer.getSelectedRange().y);
+ assertEquals(0, fTextViewer.getTextWidget().getCaretOffset());
+ fTextViewer.setRedraw(false);
+ fTextViewer.setRedraw(true);
+ // Check that the selection and the caret position are preserved.
+ assertEquals(0, fTextViewer.getSelectedRange().x);
+ assertEquals(len, fTextViewer.getSelectedRange().y);
+ assertEquals(0, fTextViewer.getTextWidget().getCaretOffset());
+ }
+}
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java
index 595988aeb..de4b96018 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java
@@ -9,6 +9,7 @@
* IBM Corporation - initial API and implementation
* Tom Eicher (Avaloq Evolution AG) - block selection mode
* Markus Schorn <markus.schorn@windriver.com> - shift with trailing empty line - https://bugs.eclipse.org/325438
+ * Sergey Prigogin (Google) - Bug 441827 - TextViewer.ViewerState.restore method looses caret position
*******************************************************************************/
package org.eclipse.jface.text;
@@ -1231,7 +1232,7 @@ public class TextViewer extends Viewer implements
int offset= fSelection.getOffset();
int length= fSelection.getLength();
if (fReverseSelection) {
- offset-= length;
+ offset+= length;
length= -length;
}
setSelectedRange(offset, length);
@@ -1289,17 +1290,11 @@ public class TextViewer extends Viewer implements
int endVirtual= Math.max(0, bts.getEndColumn() - document.getLineInformationOfOffset(bts.getOffset() + bts.getLength()).getLength());
fSelection= new ColumnPosition(bts.getOffset(), bts.getLength(), startVirtual, endVirtual);
} else {
+ Point range= fTextWidget.getSelectionRange();
+ int caretOffset= fTextWidget.getCaretOffset();
+ fReverseSelection= caretOffset == range.x;
Point selectionRange= getSelectedRange();
- fReverseSelection= selectionRange.y < 0;
- int offset, length;
- if (fReverseSelection) {
- offset= selectionRange.x + selectionRange.y;
- length= -selectionRange.y;
- } else {
- offset= selectionRange.x;
- length= selectionRange.y;
- }
- fSelection= new Position(offset, length);
+ fSelection= new Position(selectionRange.x, selectionRange.y);
}
fSelectionSet= false;

Back to the top