1616import static org .junit .jupiter .api .Assertions .assertNull ;
1717import static org .junit .jupiter .api .Assertions .assertThrows ;
1818import static org .junit .jupiter .api .Assertions .assertTrue ;
19+ import static org .mockito .Mockito .mock ;
1920
2021import java .util .List ;
2122
3233import org .eclipse .jface .text .BadLocationException ;
3334import org .eclipse .jface .text .Document ;
3435import org .eclipse .jface .text .IDocument ;
36+ import org .eclipse .jface .text .IDocumentExtension3 ;
3537import org .eclipse .jface .text .IDocumentPartitioner ;
3638import org .eclipse .jface .text .Region ;
3739import org .eclipse .jface .text .TextAttribute ;
@@ -74,7 +76,9 @@ public void tearDown() {
7476 @ Test
7577 public void testBasicStyleRanges () throws Exception {
7678 SourceViewer viewer = createConfiguredViewer ();
77- viewer .setDocument (new Document ("original content" ));
79+ var document = new Document ("original content" );
80+ setupDefaultPartitioning (document );
81+ viewer .setDocument (document );
7882
7983 Document externalDoc = new Document ("some 'highlighted' text" );
8084 List <StyleRange > styles = viewer .computeStyleRanges (externalDoc , new Region (0 , externalDoc .getLength ()));
@@ -91,7 +95,9 @@ public void testBasicStyleRanges() throws Exception {
9195 @ Test
9296 public void testNoMatchingContent () throws Exception {
9397 SourceViewer viewer = createConfiguredViewer ();
94- viewer .setDocument (new Document ("original content" ));
98+ var document = new Document ("original content" );
99+ setupDefaultPartitioning (document );
100+ viewer .setDocument (document );
95101
96102 Document externalDoc = new Document ("no special content here" );
97103 List <StyleRange > styles = viewer .computeStyleRanges (externalDoc , new Region (0 , externalDoc .getLength ()));
@@ -107,7 +113,9 @@ public void testNoMatchingContent() throws Exception {
107113 @ Test
108114 public void testRegionSubset () throws Exception {
109115 SourceViewer viewer = createConfiguredViewer ();
110- viewer .setDocument (new Document ("original content" ));
116+ var document = new Document ("original content" );
117+ setupDefaultPartitioning (document );
118+ viewer .setDocument (document );
111119
112120 // Put quoted text at position 10..22
113121 Document externalDoc = new Document ("0123456789'highlighted'rest" );
@@ -129,28 +137,31 @@ public void testOriginalDocumentNotAffected() throws Exception {
129137 SourceViewer viewer = createConfiguredViewer ();
130138 String originalContent = "original content" ;
131139 Document originalDoc = new Document (originalContent );
132- IDocumentPartitioner originalPartitioner = setupPartitioning (originalDoc );
140+ IDocumentPartitioner originalPartitioner = setupDefaultPartitioning (originalDoc );
133141 assertNotNull (originalPartitioner );
134142 viewer .setDocument (originalDoc );
135143
136144 Document externalDoc = new Document ("some 'highlighted' text" );
137- IDocumentPartitioner externalPartitioner = setupPartitioning (externalDoc );
145+ IDocumentPartitioner externalPartitioner = setupDefaultPartitioning (externalDoc );
138146 assertNotNull (externalPartitioner );
139147 viewer .computeStyleRanges (externalDoc , new Region (0 , externalDoc .getLength ()));
140148
141149 assertEquals (originalContent , originalDoc .get (), "Original document content must not change" );
142- assertEquals (originalPartitioner , originalDoc .getDocumentPartitioner (),
150+ assertEquals (originalPartitioner , originalDoc .getDocumentPartitioner (IDocumentExtension3 . DEFAULT_PARTITIONING ),
143151 "Original document partitioner must be restored" );
144- assertEquals (externalPartitioner , externalDoc .getDocumentPartitioner (),
152+ assertEquals (externalPartitioner , externalDoc .getDocumentPartitioner (IDocumentExtension3 . DEFAULT_PARTITIONING ),
145153 "External document partitioner must be restored" );
146154 }
147155
148156 @ Test
149157 public void testEmptyRegion () throws Exception {
150- SourceViewer viewer = createConfiguredViewer ();
151- viewer .setDocument (new Document ("original content" ));
158+ SourceViewer viewer = createConfiguredViewerWithNamedPartitioning ();
159+ var document = new Document ("original content" );
160+ setupNamedPartitioning (document );
161+ viewer .setDocument (document );
152162
153163 Document externalDoc = new Document ("some 'highlighted' text" );
164+ setupNamedPartitioning (externalDoc );
154165 List <StyleRange > styles = viewer .computeStyleRanges (externalDoc , new Region (0 , 0 ));
155166
156167 assertNotNull (styles );
@@ -163,8 +174,10 @@ public void testEmptyRegion() throws Exception {
163174
164175 @ Test
165176 public void testMultipleStyleRanges () throws Exception {
166- SourceViewer viewer = createConfiguredViewer ();
167- viewer .setDocument (new Document ("original content" ));
177+ SourceViewer viewer = createConfiguredViewerWithNamedPartitioning ();
178+ var document = new Document ("original content" );
179+ setupNamedPartitioning (document );
180+ viewer .setDocument (document );
168181
169182 Document externalDoc = new Document ("'first' normal 'second' end" );
170183 List <StyleRange > styles = viewer .computeStyleRanges (externalDoc , new Region (0 , externalDoc .getLength ()));
@@ -179,7 +192,9 @@ public void testMultipleStyleRanges() throws Exception {
179192 @ Test
180193 public void testBadLocationExceptionForOutOfBoundsRegion () throws Exception {
181194 SourceViewer viewer = createConfiguredViewer ();
182- viewer .setDocument (new Document ("original content" ));
195+ var document = new Document ("original content" );
196+ setupDefaultPartitioning (document );
197+ viewer .setDocument (document );
183198
184199 Document externalDoc = new Document ("short" );
185200 assertThrows (BadLocationException .class ,
@@ -248,6 +263,22 @@ public void testNamedPartitioning() throws Exception {
248263 assertEquals (externalPartitioner , restoredExternalPartitioner );
249264 }
250265
266+ @ Test
267+ public void testNonDocumentExtension3ReturnsEmpty () throws Exception {
268+ SourceViewer viewer = createConfiguredViewer ();
269+ Document originalDoc = new Document ("original content" );
270+ setupDefaultPartitioning (originalDoc );
271+ viewer .setDocument (originalDoc );
272+
273+ // Create a document that does NOT implement IDocumentExtension3
274+ // so that computeStyleRanges takes the early-return path
275+ IDocument document = mock (IDocument .class );
276+ List <StyleRange > styles = viewer .computeStyleRanges (document , new Region (0 , document .getLength ()));
277+
278+ assertNotNull (styles );
279+ assertTrue (styles .isEmpty (), "Expected empty style ranges for non-IDocumentExtension3 document" );
280+ }
281+
251282 private SourceViewer createConfiguredViewer () {
252283 SourceViewer viewer = new SourceViewer (shell , null , SWT .NONE );
253284 viewer .configure (new SourceViewerConfiguration () {
@@ -297,10 +328,10 @@ private IDocumentPartitioner setupNamedPartitioning(Document document) {
297328 return partitioner ;
298329 }
299330
300- private IDocumentPartitioner setupPartitioning (Document document ) {
331+ private IDocumentPartitioner setupDefaultPartitioning (Document document ) {
301332 IPartitionTokenScanner partitionScanner = new RuleBasedPartitionScanner ();
302333 IDocumentPartitioner partitioner = new FastPartitioner (partitionScanner , new String [] {});
303- document .setDocumentPartitioner (partitioner );
334+ document .setDocumentPartitioner (IDocumentExtension3 . DEFAULT_PARTITIONING , partitioner );
304335 partitioner .connect (document );
305336 return partitioner ;
306337 }
0 commit comments