| 
10 | 10 |  *******************************************************************************/  | 
11 | 11 | package org.eclipse.jface.text.tests;  | 
12 | 12 | 
 
  | 
 | 13 | +import static org.junit.Assert.assertTrue;  | 
13 | 14 | import static org.junit.jupiter.api.Assertions.assertEquals;  | 
 | 15 | +import static org.junit.jupiter.api.Assertions.assertFalse;  | 
 | 16 | +import static org.junit.jupiter.api.Assertions.assertThrows;  | 
14 | 17 | 
 
  | 
15 | 18 | import org.junit.jupiter.api.Test;  | 
16 | 19 | 
 
  | 
 | 
19 | 22 | import org.eclipse.swt.dnd.TextTransfer;  | 
20 | 23 | import org.eclipse.swt.layout.FillLayout;  | 
21 | 24 | import org.eclipse.swt.widgets.Composite;  | 
 | 25 | +import org.eclipse.swt.widgets.Display;  | 
22 | 26 | import org.eclipse.swt.widgets.Shell;  | 
23 | 27 | 
 
  | 
24 | 28 | import org.eclipse.jface.text.BadLocationException;  | 
@@ -368,4 +372,148 @@ public void testRemoveEntireVisibleRegion() throws BadLocationException {  | 
368 | 372 | 			shell.dispose();  | 
369 | 373 | 		}  | 
370 | 374 | 	}  | 
 | 375 | + | 
 | 376 | +	@Test  | 
 | 377 | +	public void testSetVisibleRegionDoesNotExpandOutsideProjectionRegions() {  | 
 | 378 | +		Shell shell= new Shell();  | 
 | 379 | +		shell.setLayout(new FillLayout());  | 
 | 380 | +		TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE);  | 
 | 381 | +		String documentContent= """  | 
 | 382 | +				Hello  | 
 | 383 | +				World  | 
 | 384 | +				abc  | 
 | 385 | +				123  | 
 | 386 | +				456  | 
 | 387 | +				789  | 
 | 388 | +				""";  | 
 | 389 | +		Document document= new Document(documentContent);  | 
 | 390 | +		viewer.setDocument(document, new AnnotationModel());  | 
 | 391 | +		viewer.enableProjection();  | 
 | 392 | +		ProjectionAnnotation firstAnnotation= new ProjectionAnnotation(true);  | 
 | 393 | +		ProjectionAnnotation secondAnnotation= new ProjectionAnnotation(true);  | 
 | 394 | +		viewer.getProjectionAnnotationModel().addAnnotation(firstAnnotation, new Position(0, documentContent.indexOf("World")));  | 
 | 395 | +		viewer.getProjectionAnnotationModel().addAnnotation(secondAnnotation, new Position(documentContent.indexOf("456"), documentContent.length() - documentContent.indexOf("456")));  | 
 | 396 | + | 
 | 397 | +		viewer.setVisibleRegion(documentContent.indexOf("abc"), documentContent.indexOf("123") - documentContent.indexOf("abc"));  | 
 | 398 | +		shell.setVisible(true);  | 
 | 399 | +		try {  | 
 | 400 | +			assertTrue(firstAnnotation.isCollapsed());  | 
 | 401 | +			assertTrue(secondAnnotation.isCollapsed());  | 
 | 402 | +		} finally {  | 
 | 403 | +			shell.dispose();  | 
 | 404 | +		}  | 
 | 405 | +	}  | 
 | 406 | + | 
 | 407 | +	@Test  | 
 | 408 | +	public void testSetVisibleRegionExpandsBorderingProjectionRegions() {  | 
 | 409 | +		Shell shell= new Shell();  | 
 | 410 | +		shell.setLayout(new FillLayout());  | 
 | 411 | +		TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, false, SWT.NONE);  | 
 | 412 | +		String documentContent= """  | 
 | 413 | +				Hello  | 
 | 414 | +				World  | 
 | 415 | +				123  | 
 | 416 | +				456  | 
 | 417 | +				""";  | 
 | 418 | +		Document document= new Document(documentContent);  | 
 | 419 | +		viewer.setDocument(document, new AnnotationModel());  | 
 | 420 | +		viewer.enableProjection();  | 
 | 421 | +		ProjectionAnnotation firstAnnotation= new ProjectionAnnotation(true);  | 
 | 422 | +		ProjectionAnnotation secondAnnotation= new ProjectionAnnotation(true);  | 
 | 423 | +		viewer.getProjectionAnnotationModel().addAnnotation(firstAnnotation, new Position(0, documentContent.indexOf("123")));  | 
 | 424 | +		viewer.getProjectionAnnotationModel().addAnnotation(secondAnnotation, new Position(documentContent.indexOf("123"), documentContent.length() - documentContent.indexOf("123")));  | 
 | 425 | + | 
 | 426 | +		viewer.setVisibleRegion(documentContent.indexOf("World"), documentContent.indexOf("456") - documentContent.indexOf("World"));  | 
 | 427 | +		shell.setVisible(true);  | 
 | 428 | +		try {  | 
 | 429 | +			assertFalse(firstAnnotation.isCollapsed());  | 
 | 430 | +			assertFalse(secondAnnotation.isCollapsed());  | 
 | 431 | +		} finally {  | 
 | 432 | +			shell.dispose();  | 
 | 433 | +		}  | 
 | 434 | +	}  | 
 | 435 | + | 
 | 436 | +	@Test  | 
 | 437 | +	public void testProjectionRegionsShownOnlyInVisibleRegion() {  | 
 | 438 | +		Shell shell= new Shell(Display.getCurrent());  | 
 | 439 | +		shell.setLayout(new FillLayout());  | 
 | 440 | +		TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, true, SWT.ALL);  | 
 | 441 | +		String documentContent= """  | 
 | 442 | +
  | 
 | 443 | +				visible_region_start  | 
 | 444 | +
  | 
 | 445 | +				projection_start  | 
 | 446 | +
  | 
 | 447 | +				visible_region_end  | 
 | 448 | +
  | 
 | 449 | +				projection_end  | 
 | 450 | +
  | 
 | 451 | +				""";  | 
 | 452 | +		Document document= new Document(documentContent);  | 
 | 453 | +		viewer.setDocument(document, new AnnotationModel());  | 
 | 454 | +		ProjectionAnnotation annotation= addVisibleRegionAndProjection(viewer, documentContent);  | 
 | 455 | +		try {  | 
 | 456 | +			assertEquals("""  | 
 | 457 | +					visible_region_start  | 
 | 458 | +
  | 
 | 459 | +					projection_start  | 
 | 460 | +
  | 
 | 461 | +					visible_region_end  | 
 | 462 | +					""", viewer.getVisibleDocument().get());  | 
 | 463 | + | 
 | 464 | +			annotation.paint(null, null, null); //should exit early and not throw NPE  | 
 | 465 | +		} finally {  | 
 | 466 | +			shell.dispose();  | 
 | 467 | +		}  | 
 | 468 | +	}  | 
 | 469 | + | 
 | 470 | +	@Test  | 
 | 471 | +	public void testProjectionRegionsShownWithinVisibleRegion() {  | 
 | 472 | +		Shell shell= new Shell(Display.getCurrent());  | 
 | 473 | +		shell.setLayout(new FillLayout());  | 
 | 474 | +		TestProjectionViewer viewer= new TestProjectionViewer(shell, null, null, true, SWT.ALL);  | 
 | 475 | +		String documentContent= """  | 
 | 476 | +
  | 
 | 477 | +				visible_region_start  | 
 | 478 | +
  | 
 | 479 | +				projection_start  | 
 | 480 | +
  | 
 | 481 | +				projection_end  | 
 | 482 | +
  | 
 | 483 | +				visible_region_end  | 
 | 484 | +
  | 
 | 485 | +				""";  | 
 | 486 | +		Document document= new Document(documentContent);  | 
 | 487 | +		viewer.setDocument(document, new AnnotationModel());  | 
 | 488 | +		ProjectionAnnotation annotation= addVisibleRegionAndProjection(viewer, documentContent);  | 
 | 489 | +		try {  | 
 | 490 | +			assertEquals("""  | 
 | 491 | +					visible_region_start  | 
 | 492 | +
  | 
 | 493 | +					projection_start  | 
 | 494 | +
  | 
 | 495 | +					projection_end  | 
 | 496 | +
  | 
 | 497 | +					visible_region_end  | 
 | 498 | +					""", viewer.getVisibleDocument().get());  | 
 | 499 | + | 
 | 500 | +			assertThrows(NullPointerException.class, () -> annotation.paint(null, null, null), "expected to run painting logic");  | 
 | 501 | +		} finally {  | 
 | 502 | +			shell.dispose();  | 
 | 503 | +		}  | 
 | 504 | +	}  | 
 | 505 | + | 
 | 506 | +	private ProjectionAnnotation addVisibleRegionAndProjection(TestProjectionViewer viewer, String documentContent) {  | 
 | 507 | +		int visibleRegionStart= documentContent.indexOf("visible_region_start");  | 
 | 508 | +		int visibleRegionEnd= documentContent.indexOf("\n", documentContent.indexOf("visible_region_end")) + 1;  | 
 | 509 | + | 
 | 510 | +		int projectionStart= documentContent.indexOf("projection_start");  | 
 | 511 | +		int projectionEnd= documentContent.indexOf("\n", documentContent.indexOf("projection_end")) + 1;  | 
 | 512 | + | 
 | 513 | +		viewer.setVisibleRegion(visibleRegionStart, visibleRegionEnd - visibleRegionStart);  | 
 | 514 | +		viewer.enableProjection();  | 
 | 515 | +		ProjectionAnnotation annotation= new ProjectionAnnotation();  | 
 | 516 | +		viewer.getProjectionAnnotationModel().addAnnotation(annotation, new Position(projectionStart, projectionEnd - projectionStart));  | 
 | 517 | +		return annotation;  | 
 | 518 | +	}  | 
371 | 519 | }  | 
0 commit comments