diff options
author | Mickael Istria | 2021-09-13 13:50:27 +0000 |
---|---|---|
committer | Mickael Istria | 2021-09-14 15:09:22 +0000 |
commit | e57e5530453240142e96f9755a4f5b66e3a07a9e (patch) | |
tree | f71ed042e84521bb3f7452ad40d7d0ccb232dc7c | |
parent | e0c27e6ed1c36e22b5d75c0ab470ace2d1a6125f (diff) | |
download | eclipse.platform.text-e57e5530453240142e96f9755a4f5b66e3a07a9e.tar.gz eclipse.platform.text-e57e5530453240142e96f9755a4f5b66e3a07a9e.tar.xz eclipse.platform.text-e57e5530453240142e96f9755a4f5b66e3a07a9e.zip |
Bug 575520 - Fix CodeFormatterTest tests
Change-Id: I259ed992d4754a7eb72fe9216270ad1d214cd27d
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/185400
Tested-by: Platform Bot <platform-bot@eclipse.org>
Reviewed-by: Mickael Istria <mistria@redhat.com>
-rw-r--r-- | org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java | 464 | ||||
-rw-r--r-- | org.eclipse.jface.text/src/org/eclipse/jface/text/TextViewer.java | 25 |
2 files changed, 256 insertions, 233 deletions
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 index c9d83eb92f8..53e9c55f65c 100644 --- 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 @@ -24,6 +24,7 @@ import java.util.Arrays; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; +import org.junit.After; import org.junit.Assume; import org.junit.Rule; import org.junit.Test; @@ -73,94 +74,87 @@ import org.eclipse.jface.text.tests.util.DisplayHelper; public class TextViewerTest { @Rule public ScreenshotOnFailureRule screenshotRule = new ScreenshotOnFailureRule(); + private Shell fShell; + + @After + public void tearDown() { + if (fShell != null && !fShell.isDisposed()) { + fShell.dispose(); + } + fShell= null; + } @Test public void testSetRedraw_Bug441827() throws Exception { - Shell shell= new Shell(); - try { - TextViewer textViewer= new TextViewer(shell, SWT.NONE); - Document document= new Document("abc"); - textViewer.setDocument(document); - int len= document.getLength(); - // Select the whole document with the caret at the beginning. - textViewer.setSelectedRange(len, -len); - assertEquals(0, textViewer.getSelectedRange().x); - assertEquals(len, textViewer.getSelectedRange().y); - assertEquals(0, textViewer.getTextWidget().getCaretOffset()); - textViewer.setRedraw(false); - textViewer.setRedraw(true); - // Check that the selection and the caret position are preserved. - assertEquals(0, textViewer.getSelectedRange().x); - assertEquals(len, textViewer.getSelectedRange().y); - assertEquals(0, textViewer.getTextWidget().getCaretOffset()); - } finally { - shell.dispose(); - } + fShell= new Shell(); + TextViewer textViewer= new TextViewer(fShell, SWT.NONE); + Document document= new Document("abc"); + textViewer.setDocument(document); + int len= document.getLength(); + // Select the whole document with the caret at the beginning. + textViewer.setSelectedRange(len, -len); + assertEquals(0, textViewer.getSelectedRange().x); + assertEquals(len, textViewer.getSelectedRange().y); + assertEquals(0, textViewer.getTextWidget().getCaretOffset()); + textViewer.setRedraw(false); + textViewer.setRedraw(true); + // Check that the selection and the caret position are preserved. + assertEquals(0, textViewer.getSelectedRange().x); + assertEquals(len, textViewer.getSelectedRange().y); + assertEquals(0, textViewer.getTextWidget().getCaretOffset()); } @Test public void testCaretMoveChangesSelection() throws Exception { - Shell shell= new Shell(); - try { - TextViewer textViewer= new TextViewer(shell, SWT.NONE); - Document document= new Document("abc"); - textViewer.setDocument(document); - int len= document.getLength(); - // Select the whole document with the caret at the beginning. - textViewer.setSelectedRange(0, len); - ITextSelection selection = (ITextSelection)textViewer.getSelectionProvider().getSelection(); - assertEquals(0, selection.getOffset()); - assertEquals(len, selection.getLength()); - textViewer.getTextWidget().setCaretOffset(1); - selection = (ITextSelection)textViewer.getSelectionProvider().getSelection(); - assertEquals(1, selection.getOffset()); - assertEquals(0, selection.getLength()); - } finally { - shell.dispose(); - } + fShell= new Shell(); + TextViewer textViewer= new TextViewer(fShell, SWT.NONE); + Document document= new Document("abc"); + textViewer.setDocument(document); + int len= document.getLength(); + // Select the whole document with the caret at the beginning. + textViewer.setSelectedRange(0, len); + ITextSelection selection= (ITextSelection) textViewer.getSelectionProvider().getSelection(); + assertEquals(0, selection.getOffset()); + assertEquals(len, selection.getLength()); + textViewer.getTextWidget().setCaretOffset(1); + selection= (ITextSelection) textViewer.getSelectionProvider().getSelection(); + assertEquals(1, selection.getOffset()); + assertEquals(0, selection.getLength()); } @Test public void testGetCachedSelection() throws Exception { - Shell shell= new Shell(); - try { - TextViewer textViewer= new TextViewer(shell, SWT.NONE); - Document document= new Document("abc"); - textViewer.setDocument(document); - int len= document.getLength(); - // Select the whole document with the caret at the beginning. - textViewer.setSelectedRange(0, len); - checkInAndOutUIThread(() -> { - ITextSelection selection = textViewer.getLastKnownSelection(); - assertEquals(0, selection.getOffset()); - assertEquals(len, selection.getLength()); - }); - } finally { - shell.dispose(); - } + fShell= new Shell(); + TextViewer textViewer= new TextViewer(fShell, SWT.NONE); + Document document= new Document("abc"); + textViewer.setDocument(document); + int len= document.getLength(); + // Select the whole document with the caret at the beginning. + textViewer.setSelectedRange(0, len); + checkInAndOutUIThread(() -> { + ITextSelection selection= textViewer.getLastKnownSelection(); + assertEquals(0, selection.getOffset()); + assertEquals(len, selection.getLength()); + }); } @Test public void testBlockSelectionAccessors() throws Exception { - Shell shell= new Shell(); - try { - ITextViewer textViewer= new TextViewer(shell, SWT.NONE); - Document document= new Document("0123\n4567\n89ab\ncdef"); - textViewer.setDocument(document); - // Select the whole document with the caret at the beginning. - StyledText textWidget= textViewer.getTextWidget(); - textWidget.setBlockSelection(true); - shell.setLayout(new FillLayout()); - shell.open(); - textViewer.getSelectionProvider().setSelection(new BlockTextSelection(textViewer.getDocument(), 1, 1, 2, 2, textWidget.getTabs())); - BlockTextSelection sel = (BlockTextSelection)textViewer.getSelectionProvider().getSelection(); - assertEquals(1, sel.getStartLine()); - assertEquals(2, sel.getEndLine()); - assertEquals(1, sel.getStartColumn()); - assertEquals(2, sel.getEndColumn()); - } finally { - shell.dispose(); - } + fShell= new Shell(); + ITextViewer textViewer= new TextViewer(fShell, SWT.NONE); + Document document= new Document("0123\n4567\n89ab\ncdef"); + textViewer.setDocument(document); + // Select the whole document with the caret at the beginning. + StyledText textWidget= textViewer.getTextWidget(); + textWidget.setBlockSelection(true); + fShell.setLayout(new FillLayout()); + fShell.open(); + textViewer.getSelectionProvider().setSelection(new BlockTextSelection(textViewer.getDocument(), 1, 1, 2, 2, textWidget.getTabs())); + BlockTextSelection sel= (BlockTextSelection) textViewer.getSelectionProvider().getSelection(); + assertEquals(1, sel.getStartLine()); + assertEquals(2, sel.getEndLine()); + assertEquals(1, sel.getStartColumn()); + assertEquals(2, sel.getEndColumn()); } @@ -191,54 +185,45 @@ public class TextViewerTest { @Test public void testCtrlHomeViewportListener() { Assume.assumeFalse("See bug 541415. For whatever reason, this shortcut doesn't work on Mac", Util.isMac()); - Shell shell= new Shell(); - try { - shell.setLayout(new FillLayout()); - shell.setSize(500, 200); - SourceViewer textViewer= new SourceViewer(shell, null, SWT.NONE); - textViewer.setDocument(new Document(generate5000Lines())); - shell.open(); - textViewer.revealRange(4000, 1); - AtomicBoolean notifyHomeReached = new AtomicBoolean(); - ctrlEnd(textViewer); - DisplayHelper.sleep(textViewer.getTextWidget().getDisplay(), 1000); - textViewer.addViewportListener(offset -> notifyHomeReached.set(offset == 0)); - ctrlHome(textViewer); - assertTrue(new DisplayHelper() { - @Override - protected boolean condition() { - return notifyHomeReached.get(); - } - }.waitForCondition(textViewer.getTextWidget().getDisplay(), 3000)); - } finally { - shell.dispose(); - } + fShell= new Shell(); + fShell.setLayout(new FillLayout()); + fShell.setSize(500, 200); + SourceViewer textViewer= new SourceViewer(fShell, null, SWT.NONE); + textViewer.setDocument(new Document(generate5000Lines())); + fShell.open(); + textViewer.revealRange(4000, 1); + AtomicBoolean notifyHomeReached= new AtomicBoolean(); + ctrlEnd(textViewer); + DisplayHelper.sleep(textViewer.getTextWidget().getDisplay(), 1000); + textViewer.addViewportListener(offset -> notifyHomeReached.set(offset == 0)); + ctrlHome(textViewer); + assertTrue(new DisplayHelper() { + @Override + protected boolean condition() { + return notifyHomeReached.get(); + } + }.waitForCondition(textViewer.getTextWidget().getDisplay(), 3000)); } @Test public void testCtrlEndViewportListener() { Assume.assumeFalse("See bug 541415. For whatever reason, this shortcut doesn't work on Mac", Util.isMac()); - Shell shell= new Shell(); - try { - shell.setLayout(new FillLayout()); - shell.setSize(500, 200); - SourceViewer textViewer= new SourceViewer(shell, null, SWT.NONE); - Document document= new Document(generate5000Lines()); - textViewer.setDocument(document); - shell.open(); - AtomicBoolean notifyEndReached = new AtomicBoolean(); - textViewer.addViewportListener(offset -> - notifyEndReached.set(offset > 4000)); - ctrlEnd(textViewer); - assertTrue(new DisplayHelper() { - @Override - protected boolean condition() { - return notifyEndReached.get(); - } - }.waitForCondition(textViewer.getControl().getDisplay(), 3000)); - } finally { - shell.dispose(); - } + fShell= new Shell(); + fShell.setLayout(new FillLayout()); + fShell.setSize(500, 200); + SourceViewer textViewer= new SourceViewer(fShell, null, SWT.NONE); + Document document= new Document(generate5000Lines()); + textViewer.setDocument(document); + fShell.open(); + AtomicBoolean notifyEndReached= new AtomicBoolean(); + textViewer.addViewportListener(offset -> notifyEndReached.set(offset > 4000)); + ctrlEnd(textViewer); + assertTrue(new DisplayHelper() { + @Override + protected boolean condition() { + return notifyEndReached.get(); + } + }.waitForCondition(textViewer.getControl().getDisplay(), 3000)); } /** @@ -247,57 +232,53 @@ public class TextViewerTest { */ @Test public void testDefaultContentImplementation() { - final Shell shell= new Shell(); + fShell= new Shell(); + final StyledTextContent content; try { - final StyledTextContent content; - try { - final TextViewer textViewer= new TextViewer(shell, SWT.NONE); - textViewer.setDocument(new Document()); - content= textViewer.getTextWidget().getContent(); - } catch (Exception ex) { - fail("Failed to obtain default instance of TextViewers document adapter. " + ex.getMessage()); - return; - } - assumeNotNull(content); - - final String line0= "Hello "; - final String line1= ""; - final String line2= "World!"; - final String text= line0 + "\n" + line1 + "\r\n" + line2; - content.setText(text); - assertEquals("Get text range failed.", "H", content.getTextRange(0, 1)); - assertEquals("Get text range failed.", "ll", content.getTextRange(2, 2)); - assertEquals("Adapter content length wrong.", text.length(), content.getCharCount()); - assertEquals("Adapter returned wrong content.", line0, content.getLine(0)); - assertEquals("Adapter returned wrong content.", line1, content.getLine(1)); - assertEquals("Adapter returned wrong content.", line2, content.getLine(2)); - - content.setText("\r\n\r\n"); - assertEquals("Wrong line for offset.", 0, content.getLineAtOffset(0)); - assertEquals("Wrong line for offset.", 0, content.getLineAtOffset(1)); - assertEquals("Wrong line for offset.", 1, content.getLineAtOffset(2)); - assertEquals("Wrong line for offset.", 1, content.getLineAtOffset(3)); - assertEquals("Wrong line for offset.", 2, content.getLineAtOffset(4)); - assertEquals("Wrong line for offset.", content.getLineCount() - 1, content.getLineAtOffset(content.getCharCount())); - - content.setText(null); - assertEquals("Adapter returned wrong line count.", 1, content.getLineCount()); - content.setText(""); - assertEquals("Adapter returned wrong line count.", 1, content.getLineCount()); - content.setText("a\n"); - assertEquals("Adapter returned wrong line count.", 2, content.getLineCount()); - content.setText("\n\n"); - assertEquals("Adapter returned wrong line count.", 3, content.getLineCount()); - - content.setText("\r\ntest\r\n"); - assertEquals("Wrong offset for line.", 0, content.getOffsetAtLine(0)); - assertEquals("Wrong offset for line.", 2, content.getOffsetAtLine(1)); - assertEquals("Wrong offset for line.", 8, content.getOffsetAtLine(2)); - content.setText(""); - assertEquals("Wrong offset for line.", 0, content.getOffsetAtLine(0)); - } finally { - shell.dispose(); + final TextViewer textViewer= new TextViewer(fShell, SWT.NONE); + textViewer.setDocument(new Document()); + content= textViewer.getTextWidget().getContent(); + } catch (Exception ex) { + fail("Failed to obtain default instance of TextViewers document adapter. " + ex.getMessage()); + return; } + assumeNotNull(content); + + final String line0= "Hello "; + final String line1= ""; + final String line2= "World!"; + final String text= line0 + "\n" + line1 + "\r\n" + line2; + content.setText(text); + assertEquals("Get text range failed.", "H", content.getTextRange(0, 1)); + assertEquals("Get text range failed.", "ll", content.getTextRange(2, 2)); + assertEquals("Adapter content length wrong.", text.length(), content.getCharCount()); + assertEquals("Adapter returned wrong content.", line0, content.getLine(0)); + assertEquals("Adapter returned wrong content.", line1, content.getLine(1)); + assertEquals("Adapter returned wrong content.", line2, content.getLine(2)); + + content.setText("\r\n\r\n"); + assertEquals("Wrong line for offset.", 0, content.getLineAtOffset(0)); + assertEquals("Wrong line for offset.", 0, content.getLineAtOffset(1)); + assertEquals("Wrong line for offset.", 1, content.getLineAtOffset(2)); + assertEquals("Wrong line for offset.", 1, content.getLineAtOffset(3)); + assertEquals("Wrong line for offset.", 2, content.getLineAtOffset(4)); + assertEquals("Wrong line for offset.", content.getLineCount() - 1, content.getLineAtOffset(content.getCharCount())); + + content.setText(null); + assertEquals("Adapter returned wrong line count.", 1, content.getLineCount()); + content.setText(""); + assertEquals("Adapter returned wrong line count.", 1, content.getLineCount()); + content.setText("a\n"); + assertEquals("Adapter returned wrong line count.", 2, content.getLineCount()); + content.setText("\n\n"); + assertEquals("Adapter returned wrong line count.", 3, content.getLineCount()); + + content.setText("\r\ntest\r\n"); + assertEquals("Wrong offset for line.", 0, content.getOffsetAtLine(0)); + assertEquals("Wrong offset for line.", 2, content.getOffsetAtLine(1)); + assertEquals("Wrong offset for line.", 8, content.getOffsetAtLine(2)); + content.setText(""); + assertEquals("Wrong offset for line.", 0, content.getOffsetAtLine(0)); } public static void ctrlEnd(ITextViewer viewer) { @@ -337,47 +318,43 @@ public class TextViewerTest { @Test public void testShiftLeft() { - Shell shell= new Shell(); - try { - TextViewer textViewer= new TextViewer(shell, SWT.NONE); - { - // Normal case, both lines match prefix - Document document= new Document("//line1\n//line2"); - textViewer.setDocumentPartitioning(IDocumentExtension3.DEFAULT_PARTITIONING); - textViewer.setDocument(document); - textViewer.setDefaultPrefixes(new String[] { "//" }, IDocument.DEFAULT_CONTENT_TYPE); - - textViewer.doOperation(ITextOperationTarget.SELECT_ALL); - textViewer.doOperation(ITextOperationTarget.STRIP_PREFIX); - - assertEquals("line1\nline2", textViewer.getDocument().get()); - } - { - // Don't shift anything, as 2nd line does not match any prefix - Document document= new Document("//line1\nline2"); - textViewer.setDocumentPartitioning(IDocumentExtension3.DEFAULT_PARTITIONING); - textViewer.setDocument(document); - textViewer.setDefaultPrefixes(new String[] { "//" }, IDocument.DEFAULT_CONTENT_TYPE); + fShell= new Shell(); + TextViewer textViewer= new TextViewer(fShell, SWT.NONE); + { + // Normal case, both lines match prefix + Document document= new Document("//line1\n//line2"); + textViewer.setDocumentPartitioning(IDocumentExtension3.DEFAULT_PARTITIONING); + textViewer.setDocument(document); + textViewer.setDefaultPrefixes(new String[] { "//" }, IDocument.DEFAULT_CONTENT_TYPE); - textViewer.doOperation(ITextOperationTarget.SELECT_ALL); - textViewer.doOperation(ITextOperationTarget.STRIP_PREFIX); + textViewer.doOperation(ITextOperationTarget.SELECT_ALL); + textViewer.doOperation(ITextOperationTarget.STRIP_PREFIX); - assertEquals("//line1\nline2", textViewer.getDocument().get()); - } - { - // Shift line1, since line2 matches the allowed empty prefix - Document document= new Document("//line1\nline2"); - textViewer.setDocumentPartitioning(IDocumentExtension3.DEFAULT_PARTITIONING); - textViewer.setDocument(document); - textViewer.setDefaultPrefixes(new String[] { "//", "" }, IDocument.DEFAULT_CONTENT_TYPE); + assertEquals("line1\nline2", textViewer.getDocument().get()); + } + { + // Don't shift anything, as 2nd line does not match any prefix + Document document= new Document("//line1\nline2"); + textViewer.setDocumentPartitioning(IDocumentExtension3.DEFAULT_PARTITIONING); + textViewer.setDocument(document); + textViewer.setDefaultPrefixes(new String[] { "//" }, IDocument.DEFAULT_CONTENT_TYPE); - textViewer.doOperation(ITextOperationTarget.SELECT_ALL); - textViewer.doOperation(ITextOperationTarget.STRIP_PREFIX); + textViewer.doOperation(ITextOperationTarget.SELECT_ALL); + textViewer.doOperation(ITextOperationTarget.STRIP_PREFIX); - assertEquals("line1\nline2", textViewer.getDocument().get()); - } - } finally { - shell.dispose(); + assertEquals("//line1\nline2", textViewer.getDocument().get()); + } + { + // Shift line1, since line2 matches the allowed empty prefix + Document document= new Document("//line1\nline2"); + textViewer.setDocumentPartitioning(IDocumentExtension3.DEFAULT_PARTITIONING); + textViewer.setDocument(document); + textViewer.setDefaultPrefixes(new String[] { "//", "" }, IDocument.DEFAULT_CONTENT_TYPE); + + textViewer.doOperation(ITextOperationTarget.SELECT_ALL); + textViewer.doOperation(ITextOperationTarget.STRIP_PREFIX); + + assertEquals("line1\nline2", textViewer.getDocument().get()); } } @@ -412,47 +389,72 @@ public class TextViewerTest { @Test public void testURLHyperlinkDetector() { - Shell shell = new Shell(); - - try { - TextViewer textViewer= new TextViewer(shell, SWT.NONE); - checkHyperlink(textViewer, 3, "https://foo ", "[https://foo]"); - checkHyperlink(textViewer, 0, "", "[]"); - checkHyperlink(textViewer, 3, "https", "[]"); - checkHyperlink(textViewer, 3, "https://", "[]"); - checkHyperlink(textViewer, 3, "https:// ", "[]"); - checkHyperlink(textViewer, 3, "https:// foo", "[]"); - checkHyperlink(textViewer, 3, "https://foo bar", "[https://foo]"); - checkHyperlink(textViewer, 3, "\"https://\" foo bar", "[]"); - checkHyperlink(textViewer, 3, "\"https:// \" foo bar", "[]"); - checkHyperlink(textViewer, 3, "\"https:// foo\" bar", "[]"); - checkHyperlink(textViewer, 15, "https:// foo https://bar bar", "[https://bar]"); - checkHyperlink(textViewer, 24, "https:// foo https://bar bar", "[https://bar]"); - checkHyperlink(textViewer, 15, "<a href=\"test:https://bugs.eclipse.org/bugs\"></a>", "[https://bugs.eclipse.org/bugs]"); - checkHyperlink(textViewer, 19, "<a href=\"scm:git:https://bugs.eclipse.org/bugs\"></a>", "[https://bugs.eclipse.org/bugs]"); - checkHyperlink(textViewer, 40, "Find more information at https://www.eclipse.org.", "[https://www.eclipse.org]"); - checkHyperlink(textViewer, 3, "http://... links should not be used anymore; use https://... instead.", "[]"); - checkHyperlink(textViewer, 50, "http://... links should not be used anymore; use https://... instead.", "[]"); - } finally { - shell.dispose(); - } + fShell= new Shell(); + TextViewer textViewer= new TextViewer(fShell, SWT.NONE); + checkHyperlink(textViewer, 3, "https://foo ", "[https://foo]"); + checkHyperlink(textViewer, 0, "", "[]"); + checkHyperlink(textViewer, 3, "https", "[]"); + checkHyperlink(textViewer, 3, "https://", "[]"); + checkHyperlink(textViewer, 3, "https:// ", "[]"); + checkHyperlink(textViewer, 3, "https:// foo", "[]"); + checkHyperlink(textViewer, 3, "https://foo bar", "[https://foo]"); + checkHyperlink(textViewer, 3, "\"https://\" foo bar", "[]"); + checkHyperlink(textViewer, 3, "\"https:// \" foo bar", "[]"); + checkHyperlink(textViewer, 3, "\"https:// foo\" bar", "[]"); + checkHyperlink(textViewer, 15, "https:// foo https://bar bar", "[https://bar]"); + checkHyperlink(textViewer, 24, "https:// foo https://bar bar", "[https://bar]"); + checkHyperlink(textViewer, 15, "<a href=\"test:https://bugs.eclipse.org/bugs\"></a>", "[https://bugs.eclipse.org/bugs]"); + checkHyperlink(textViewer, 19, "<a href=\"scm:git:https://bugs.eclipse.org/bugs\"></a>", "[https://bugs.eclipse.org/bugs]"); + checkHyperlink(textViewer, 40, "Find more information at https://www.eclipse.org.", "[https://www.eclipse.org]"); + checkHyperlink(textViewer, 3, "http://... links should not be used anymore; use https://... instead.", "[]"); + checkHyperlink(textViewer, 50, "http://... links should not be used anymore; use https://... instead.", "[]"); } @Test public void testPasteMultiLines() { - Shell shell= new Shell(); - TextViewer textViewer= new TextViewer(shell, SWT.NONE); + fShell= new Shell(); + TextViewer textViewer= new TextViewer(fShell, SWT.NONE); textViewer.setDocument(new Document()); - new Clipboard(shell.getDisplay()).setContents(new Object[] { "a\na" }, new Transfer[] { TextTransfer.getInstance() }, DND.CLIPBOARD); + new Clipboard(fShell.getDisplay()).setContents(new Object[] { "a\na" }, new Transfer[] { TextTransfer.getInstance() }, DND.CLIPBOARD); textViewer.doOperation(ITextOperationTarget.PASTE); assertEquals("a\na", textViewer.getTextWidget().getText()); } @Test public void testSetSelectionNoDoc() { - Shell shell= new Shell(); - TextViewer textViewer= new TextViewer(shell, SWT.NONE); + fShell= new Shell(); + TextViewer textViewer= new TextViewer(fShell, SWT.NONE); textViewer.setSelection(TextSelection.emptySelection()); // assert no exception is thrown } + + @Test + public void testSelectionFromViewerState() { + fShell= new Shell(); + TextViewer textViewer= new TextViewer(fShell, SWT.NONE); + textViewer.setDocument(new Document( + "/**\n" + + " *\n" + + " * HEADER\n" + + " */\n" + + "package pack;\n" + + "\n" + + "public final class C {\n" + + " /** \n" + + "* javadoc\n" + + " */\n" + + " public void method() {\n" + + "/* a\n" + + "comment */\n" + + "int local;\n" + + " }\n" + + "}\n")); + textViewer.setSelectedRange(118, 0); + ITextSelection textSelection= (ITextSelection) textViewer.getSelection(); + assertEquals(118, textSelection.getOffset()); + textViewer.setRedraw(false); // switch to usage of ViewerState + textViewer.setSelectedRange(113, 15); + textSelection= (ITextSelection) textViewer.getSelection(); + assertEquals(113, textSelection.getOffset()); + } } 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 58768f9dff1..7938bc5888e 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 @@ -1120,8 +1120,12 @@ public class TextViewer extends Viewer implements */ public void updateSelection(Position[] selections) { fSelectionSet= true; - if (fSelections == null) { - fSelections= Arrays.copyOf(selections, selections.length); + if (selections == null) { + fSelections = new Position[0]; + } else if (fSelections != null && fSelections.length == selections.length) { + for (int i = 0; i < fSelections.length; i++) { + updatePosition(fSelections[i], selections[i].getOffset(), selections[i].getLength()); + } } else { fSelections= Arrays.stream(selections) .map(position -> new Position(position.getOffset(), position.getLength())) /*force deleted=false*/ @@ -1252,6 +1256,20 @@ public class TextViewer extends Viewer implements } /** + * Updates a position with the given information and clears its deletion state. + * + * @param position the position to update + * @param offset the new selection offset + * @param length the new selection length + */ + private void updatePosition(Position position, int offset, int length) { + position.setOffset(offset); + position.setLength(length); + // http://bugs.eclipse.org/bugs/show_bug.cgi?id=32795 + position.isDeleted= false; + } + + /** * Returns the document line to keep visually stable. If the caret line is (partially) * visible, it is returned, otherwise the topmost (partially) visible line is returned. * @@ -2496,6 +2514,9 @@ public class TextViewer extends Viewer implements } } + if (!redraws() && fViewerState != null) { + return toSelection(Arrays.stream(fViewerState.getSelection()).map(point -> new Region(point.x, point.y)).toArray(IRegion[]::new)); + } int[] ranges= fTextWidget.getSelectionRanges(); IRegion[] selectedRanges= new IRegion[ranges.length / 2]; for (int i= 0; i < selectedRanges.length; i++) { |