41
41
import org .eclipse .jdt .core .ILocalVariable ;
42
42
import org .eclipse .jdt .core .IMember ;
43
43
import org .eclipse .jdt .core .IMemberValuePair ;
44
+ import org .eclipse .jdt .core .IOpenable ;
44
45
import org .eclipse .jdt .core .ISourceRange ;
45
46
import org .eclipse .jdt .core .ISourceReference ;
46
47
import org .eclipse .jdt .core .ITypeParameter ;
@@ -308,20 +309,29 @@ private static ISourceRange getNameRange(IJavaElement element) throws JavaModelE
308
309
* @throws JavaModelException
309
310
*/
310
311
public static Location toLocation (ICompilationUnit unit , int offset , int length ) throws JavaModelException {
311
- Location result = new Location ();
312
- result .setUri (getFileURI (unit ));
313
- int [] loc = JsonRpcHelpers .toLine (unit .getBuffer (), offset );
314
- int [] endLoc = JsonRpcHelpers .toLine (unit .getBuffer (), offset + length );
315
-
316
- Range range = new Range ();
317
- if (loc != null ) {
318
- range .setStart (new Position (loc [0 ],loc [1 ]));
319
- }
320
- if (endLoc != null ) {
321
- range .setEnd (new Position (endLoc [0 ],endLoc [1 ]));
322
- }
323
- result .setRange (range );
324
- return result ;
312
+ return new Location (getFileURI (unit ), toRange (unit , offset , length ));
313
+ }
314
+
315
+ /**
316
+ * Creates a default location for the class file.
317
+ *
318
+ * @param classFile
319
+ * @return location
320
+ * @throws JavaModelException
321
+ */
322
+ public static Location toLocation (IClassFile classFile ) throws JavaModelException {
323
+ return toLocation (classFile , 0 , 0 );
324
+ }
325
+
326
+ /**
327
+ * Creates a default location for the uri.
328
+ *
329
+ * @param classFile
330
+ * @return location
331
+ * @throws JavaModelException
332
+ */
333
+ public static Location toLocation (String uri ) {
334
+ return new Location (uri , newRange ());
325
335
}
326
336
327
337
/**
@@ -330,55 +340,66 @@ public static Location toLocation(ICompilationUnit unit, int offset, int length)
330
340
* @param unit
331
341
* @param offset
332
342
* @param length
333
- * @return location or null
343
+ * @return location
334
344
* @throws JavaModelException
335
345
*/
336
- public static Location toLocation (IClassFile unit , int offset , int length ) throws JavaModelException {
337
- Location result = new Location ();
338
- String packageName = unit .getParent ().getElementName ();
339
- String jarName = unit .getParent ().getParent ().getElementName ();
346
+ public static Location toLocation (IClassFile classFile , int offset , int length ) throws JavaModelException {
347
+ String packageName = classFile .getParent ().getElementName ();
348
+ String jarName = classFile .getParent ().getParent ().getElementName ();
340
349
String uriString = null ;
341
350
try {
342
- uriString = new URI (JDT_SCHEME , "contents" , "/" + jarName + "/" + packageName + "/" + unit .getElementName (), unit .getHandleIdentifier (), null ).toASCIIString ();
351
+ uriString = new URI (JDT_SCHEME , "contents" , "/" + jarName + "/" + packageName + "/" + classFile .getElementName (), classFile .getHandleIdentifier (), null ).toASCIIString ();
343
352
} catch (URISyntaxException e ) {
344
353
JavaLanguageServerPlugin .logException ("Error generating URI for class " , e );
345
354
}
346
- result .setUri (uriString );
347
- IBuffer buffer = unit .getBuffer ();
348
- int [] loc = JsonRpcHelpers .toLine (buffer , offset );
349
- int [] endLoc = JsonRpcHelpers .toLine (buffer , offset + length );
350
-
351
- Range range = new Range ();
352
- if (loc != null ) {
353
- range .setStart (new Position (loc [0 ], loc [1 ]));
354
- }
355
- if (endLoc != null ) {
356
- range .setEnd (new Position (endLoc [0 ],endLoc [1 ]));
357
- }
358
- result .setRange (range );
359
- return result ;
355
+ Range range = toRange (classFile , offset , length );
356
+ return new Location (uriString , range );
360
357
}
361
358
362
359
/**
363
- * Creates a range for the given offset and length for a compilation unit
360
+ * Creates a range for the given offset and length for an {@link IOpenable}
364
361
*
365
- * @param unit
362
+ * @param openable
366
363
* @param offset
367
364
* @param length
368
365
* @return
369
366
* @throws JavaModelException
370
367
*/
371
- public static Range toRange (ICompilationUnit unit , int offset , int length ) throws JavaModelException {
372
- Range result = new Range ();
373
- final IBuffer buffer = unit .getBuffer ();
374
- int [] loc = JsonRpcHelpers .toLine (buffer , offset );
375
- int [] endLoc = JsonRpcHelpers .toLine (buffer , offset + length );
376
-
377
- if (loc != null && endLoc != null ) {
378
- result .setStart (new Position (loc [0 ],loc [1 ]));
379
- result .setEnd (new Position (endLoc [0 ],endLoc [1 ]));
368
+ public static Range toRange (IOpenable openable , int offset , int length ) throws JavaModelException {
369
+ Range range = newRange ();
370
+ if (offset > 0 || length > 0 ) {
371
+ int [] loc = null ;
372
+ int [] endLoc = null ;
373
+ IBuffer buffer = openable .getBuffer ();
374
+ if (buffer != null ) {
375
+ loc = JsonRpcHelpers .toLine (buffer , offset );
376
+ endLoc = JsonRpcHelpers .toLine (buffer , offset + length );
377
+ }
378
+ if (loc == null ) {
379
+ loc = new int [2 ];
380
+ }
381
+ if (endLoc == null ) {
382
+ endLoc = new int [2 ];
383
+ }
384
+ setPosition (range .getStart (), loc );
385
+ setPosition (range .getEnd (), endLoc );
380
386
}
381
- return result ;
387
+ return range ;
388
+ }
389
+
390
+ /**
391
+ * Creates a new {@link Range} with its start and end {@link Position}s set to line=0, character=0
392
+ *
393
+ * @return a new {@link Range};
394
+ */
395
+ public static Range newRange () {
396
+ return new Range (new Position (), new Position ());
397
+ }
398
+
399
+ private static void setPosition (Position position , int [] coords ) {
400
+ assert coords .length == 2 ;
401
+ position .setLine (coords [0 ]);
402
+ position .setCharacter (coords [1 ]);
382
403
}
383
404
384
405
/**
0 commit comments