|
28 | 28 | import org.eclipse.jface.text.BadLocationException; |
29 | 29 | import org.eclipse.jface.text.Document; |
30 | 30 | import org.eclipse.jface.text.IDocument; |
| 31 | +import org.eclipse.jface.text.IDocumentInformationMapping; |
31 | 32 | import org.eclipse.jface.text.IRegion; |
32 | 33 | import org.eclipse.jface.text.ITextOperationTarget; |
33 | 34 | import org.eclipse.jface.text.ITextSelection; |
34 | 35 | import org.eclipse.jface.text.Position; |
35 | 36 | import org.eclipse.jface.text.Region; |
| 37 | +import org.eclipse.jface.text.projection.ProjectionDocument; |
36 | 38 | import org.eclipse.jface.text.source.AnnotationModel; |
37 | 39 | import org.eclipse.jface.text.source.IOverviewRuler; |
38 | 40 | import org.eclipse.jface.text.source.IVerticalRuler; |
@@ -433,6 +435,71 @@ public void testSetVisibleRegionExpandsBorderingProjectionRegions() { |
433 | 435 | } |
434 | 436 | } |
435 | 437 |
|
| 438 | + @Test |
| 439 | + public void testImageLineStateAfterSettingVisibleRegionsWithProjections() throws BadLocationException { |
| 440 | + Shell shell= new Shell(); |
| 441 | + shell.setLayout(new FillLayout()); |
| 442 | + TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE); |
| 443 | + String documentContent= """ |
| 444 | + public class TM { |
| 445 | + void a() { |
| 446 | + // ... |
| 447 | + } |
| 448 | +
|
| 449 | + void b() { |
| 450 | + // ... |
| 451 | + } |
| 452 | +
|
| 453 | + void c() { |
| 454 | + // ... |
| 455 | + } |
| 456 | + } |
| 457 | + """; |
| 458 | + Document document= new Document(documentContent); |
| 459 | + viewer.setDocument(document, new AnnotationModel()); |
| 460 | + viewer.enableProjection(); |
| 461 | + addAnnotationBetween(viewer, new ProjectionAnnotation(false), "\tvoid a()", "\t}"); |
| 462 | + ProjectionAnnotation annotationToCollapse= new ProjectionAnnotation(false); |
| 463 | + addAnnotationBetween(viewer, annotationToCollapse, "\tvoid b()", "\t}"); |
| 464 | + addAnnotationBetween(viewer, new ProjectionAnnotation(false), "\tvoid c()", "\t}"); |
| 465 | + |
| 466 | + shell.setVisible(true); |
| 467 | + try { |
| 468 | + viewer.getProjectionAnnotationModel().collapse(annotationToCollapse); |
| 469 | + |
| 470 | + Position firstMethod= findPositionFromStartAndEndText(viewer, "\tvoid a()", "}"); |
| 471 | + viewer.setVisibleRegion(firstMethod.getOffset(), firstMethod.getLength() + 1); |
| 472 | + viewer.setVisibleRegion(documentContent.indexOf("class"), documentContent.length() - documentContent.indexOf("class")); |
| 473 | + |
| 474 | + IDocumentInformationMapping mapping= ((ProjectionDocument) viewer.getVisibleDocument()).getDocumentInformationMapping(); |
| 475 | + // toImageLine should not throw exceptions |
| 476 | + for (int i= 0; i < 5; i++) { |
| 477 | + int imageLine= mapping.toImageLine(i);// should not throw exception |
| 478 | + assertEquals(i, imageLine); |
| 479 | + } |
| 480 | + assertEquals(-1, mapping.toImageLine(6), "should still be collapsed"); |
| 481 | + for (int i= 7; i < documentContent.split("\n").length; i++) { |
| 482 | + int imageLine= mapping.toImageLine(i);// should not throw exception |
| 483 | + assertEquals(i - 1, imageLine); |
| 484 | + } |
| 485 | + } finally { |
| 486 | + shell.dispose(); |
| 487 | + } |
| 488 | + } |
| 489 | + |
| 490 | + private void addAnnotationBetween(TestProjectionViewer viewer, ProjectionAnnotation annotationToCollapse, String startText, String endText) { |
| 491 | + Position position= findPositionFromStartAndEndText(viewer, startText, endText); |
| 492 | + viewer.getProjectionAnnotationModel().addAnnotation(annotationToCollapse, position); |
| 493 | + } |
| 494 | + |
| 495 | + private Position findPositionFromStartAndEndText(TestProjectionViewer viewer, String startText, String endText) { |
| 496 | + String documentContent= viewer.getDocument().get(); |
| 497 | + int startIndex= documentContent.indexOf(startText); |
| 498 | + int endIndex= documentContent.indexOf(endText, startIndex + 1); |
| 499 | + Position position= new Position(startIndex, endIndex - startIndex); |
| 500 | + return position; |
| 501 | + } |
| 502 | + |
436 | 503 | @Test |
437 | 504 | public void testProjectionRegionsShownOnlyInVisibleRegion() { |
438 | 505 | Shell shell= new Shell(Display.getCurrent()); |
|
0 commit comments