1212package org .eclipse .lsp4e .test .linkedediting ;
1313
1414import static org .eclipse .lsp4e .test .utils .TestUtils .waitForAndAssertCondition ;
15- import static org .junit .Assert .*;
15+ import static org .junit .Assert .assertEquals ;
16+ import static org .junit .Assert .assertFalse ;
17+ import static org .junit .Assert .assertNotNull ;
18+ import static org .junit .Assert .assertTrue ;
1619
1720import java .util .ArrayList ;
1821import java .util .HashMap ;
@@ -39,38 +42,13 @@ public class LinkedEditingTest extends AbstractTestWithProject {
3942
4043 @ Test
4144 public void testLinkedEditing () throws CoreException {
42- final var ranges = new ArrayList <Range >();
43- ranges .add (new Range (new Position (1 , 3 ), new Position (1 , 7 )));
44- ranges .add (new Range (new Position (3 , 4 ), new Position (3 , 8 )));
45-
46- final var linkkedEditingRanges = new LinkedEditingRanges (ranges );
47- MockLanguageServer .INSTANCE .setLinkedEditingRanges (linkkedEditingRanges );
48-
49- IFile testFile = TestUtils .createUniqueTestFile (project , "<html>\n <body>\n a body text\n </body>\n </html>" );
50- ITextViewer viewer = TestUtils .openTextViewer (testFile );
51-
52- viewer .getTextWidget ().setCaretOffset (11 );
53-
54- if (!(viewer instanceof ISourceViewer )) {
55- Assert .fail ();
56- }
45+ final var sourceViewer = setupSimpleHtmlPageViewer ();
5746
58- final var sourceViewer = (ISourceViewer ) viewer ;
59-
60- viewer .getTextWidget ().setSelection (11 ); // 10-14 <body|>
47+ sourceViewer .getTextWidget ().setSelection (11 ); // 10-14 <body|>
6148
6249 final var annotations = new HashMap <org .eclipse .jface .text .Position , Annotation >();
6350
64- waitForAndAssertCondition (3_000 , () -> {
65- Iterator <Annotation > iterator = sourceViewer .getAnnotationModel ().getAnnotationIterator ();
66- while (iterator .hasNext ()) {
67- Annotation annotation = iterator .next ();
68- if (annotation .getType ().startsWith ("org.eclipse.ui.internal.workbench.texteditor.link" )) {
69- return true ;
70- }
71- }
72- return false ;
73- });
51+ waitForAnnotationsPresent (sourceViewer );
7452
7553 IAnnotationModel model = sourceViewer .getAnnotationModel ();
7654 final Iterator <Annotation > iterator = model .getAnnotationIterator ();
@@ -86,36 +64,12 @@ public void testLinkedEditing() throws CoreException {
8664
8765 @ Test
8866 public void testLinkedEditingExitPolicy () throws CoreException {
89- final var ranges = new ArrayList <Range >();
90- ranges .add (new Range (new Position (1 , 3 ), new Position (1 , 7 )));
91- ranges .add (new Range (new Position (3 , 4 ), new Position (3 , 8 )));
92-
93- final var linkkedEditingRanges = new LinkedEditingRanges (ranges , "[:A-Z_a-z]*\\ Z" );
94- MockLanguageServer .INSTANCE .setLinkedEditingRanges (linkkedEditingRanges );
95-
96- IFile testFile = TestUtils .createUniqueTestFile (project , "<html>\n <body class=\" test\" >\n a body text\n </body>\n </html>" );
97- ITextViewer viewer = TestUtils .openTextViewer (testFile );
98-
99- if (!(viewer instanceof ISourceViewer )) {
100- Assert .fail ();
101- }
102-
103- final var sourceViewer = (ISourceViewer ) viewer ;
67+ final var sourceViewer = setupSimpleHtmlPageViewer ();
10468
10569 // Test linked editing annotation in a tag name position
106- viewer .getTextWidget ().setCaretOffset (14 );
107- viewer .getTextWidget ().setSelection (14 ); // 10-14 <body| class="test">
108- waitForAndAssertCondition (3_000 , () -> {
109- Iterator <Annotation > iterator = sourceViewer .getAnnotationModel ().getAnnotationIterator ();
110- while (iterator .hasNext ()) {
111- Annotation annotation = iterator .next ();
112- if (annotation .getType ().startsWith ("org.eclipse.ui.internal.workbench.texteditor.link" )) {
113- // System.out.println("testLinkedEditingExitPolicy: Wait for annotation succeeded: " + annotation.getType());
114- return true ;
115- }
116- }
117- return false ;
118- });
70+ sourceViewer .getTextWidget ().setCaretOffset (14 );
71+ sourceViewer .getTextWidget ().setSelection (14 ); // 10-14 <body| class="test">
72+ waitForAnnotationsPresent (sourceViewer );
11973
12074 IAnnotationModel model = sourceViewer .getAnnotationModel ();
12175 List <Annotation > annotations = findAnnotations (sourceViewer , 14 ).stream ().filter (a -> a .getType ().startsWith ("org.eclipse.ui.internal.workbench.texteditor.link" )).toList ();
@@ -129,12 +83,47 @@ public void testLinkedEditingExitPolicy() throws CoreException {
12983 assertNotNull (slaveAnnotation );
13084 org .eclipse .jface .text .Position slavePosition = model .getPosition (slaveAnnotation );
13185 assertNotNull (slavePosition );
132- assertEquals (viewer .getTextWidget ().getTextRange (masterPosition .getOffset (), masterPosition .getLength ()),
133- viewer .getTextWidget ().getTextRange (slavePosition .getOffset (), slavePosition .getLength ()));
86+ assertEquals (sourceViewer .getTextWidget ().getTextRange (masterPosition .getOffset (), masterPosition .getLength ()),
87+ sourceViewer .getTextWidget ().getTextRange (slavePosition .getOffset (), slavePosition .getLength ()));
13488
13589 // Test linked editing annotation out of a tag name position (should be absent)
136- viewer .getTextWidget ().setCaretOffset (15 );
137- viewer .getTextWidget ().setSelection (15 ); // 10-14 <body |class="test">
90+ sourceViewer .getTextWidget ().setCaretOffset (15 );
91+ sourceViewer .getTextWidget ().setSelection (15 ); // 10-14 <body |class="test">
92+ waitForAnnotationsPresent (sourceViewer );
93+
94+ model = sourceViewer .getAnnotationModel ();
95+ // No "linked" annotation is to be found at this offset
96+ assertFalse (findAnnotations (sourceViewer , 15 ).stream ().anyMatch (a -> a .getType ().startsWith ("org.eclipse.ui.internal.workbench.texteditor.link" )));
97+ }
98+
99+ @ Test
100+ public void testSelectionChange () throws CoreException {
101+
102+ final var ranges = new ArrayList <Range >();
103+ ranges .add (new Range (new Position (0 , 0 ), new Position (0 , 5 )));
104+ ranges .add (new Range (new Position (0 , 6 ), new Position (0 , 12 )));
105+
106+ final var linkedEditingRanges = new LinkedEditingRanges (ranges , "[:A-Z_a-z]*\\ b" );
107+ MockLanguageServer .INSTANCE .setLinkedEditingRanges (linkedEditingRanges );
108+
109+ IFile testFile = TestUtils .createUniqueTestFile (project , "first second" );
110+ ITextViewer viewer = TestUtils .openTextViewer (testFile );
111+
112+ if (!(viewer instanceof ISourceViewer )) {
113+ Assert .fail ();
114+ }
115+
116+ var sourceViewer = (ISourceViewer ) viewer ;
117+
118+ sourceViewer .getTextWidget ().setSelection (9 ,3 ); //selection range with caret at beginning
119+
120+ waitForAndAssertCondition (3_000 , () -> findAnnotations (sourceViewer , 1 ).size () != 0 );
121+
122+ assertEquals (3 , sourceViewer .getTextWidget ().getCaretOffset ());
123+
124+ }
125+
126+ private void waitForAnnotationsPresent (final ISourceViewer sourceViewer ) {
138127 waitForAndAssertCondition (3_000 , () -> {
139128 Iterator <Annotation > iterator = sourceViewer .getAnnotationModel ().getAnnotationIterator ();
140129 while (iterator .hasNext ()) {
@@ -145,12 +134,28 @@ public void testLinkedEditingExitPolicy() throws CoreException {
145134 }
146135 return false ;
147136 });
137+ }
148138
149- model = sourceViewer .getAnnotationModel ();
150- // No "linked" annotation is to be found at this offset
151- assertFalse (findAnnotations (sourceViewer , 15 ).stream ().anyMatch (a -> a .getType ().startsWith ("org.eclipse.ui.internal.workbench.texteditor.link" )));
139+
140+ private ISourceViewer setupSimpleHtmlPageViewer () throws CoreException {
141+ final var ranges = new ArrayList <Range >();
142+ ranges .add (new Range (new Position (1 , 3 ), new Position (1 , 7 )));
143+ ranges .add (new Range (new Position (3 , 4 ), new Position (3 , 8 )));
144+
145+ final var linkkedEditingRanges = new LinkedEditingRanges (ranges , "[:A-Z_a-z]*\\ Z" );
146+ MockLanguageServer .INSTANCE .setLinkedEditingRanges (linkkedEditingRanges );
147+
148+ IFile testFile = TestUtils .createUniqueTestFile (project , "<html>\n <body class=\" test\" >\n a body text\n </body>\n </html>" );
149+ ITextViewer viewer = TestUtils .openTextViewer (testFile );
150+
151+ if (!(viewer instanceof ISourceViewer )) {
152+ Assert .fail ();
153+ }
154+
155+ return (ISourceViewer ) viewer ;
152156 }
153157
158+
154159 private List <Annotation > findAnnotations (ISourceViewer sourceViewer , int offset ) {
155160 final var annotations = new ArrayList <Annotation >();
156161 IAnnotationModel model = sourceViewer .getAnnotationModel ();
0 commit comments