57
57
import org .openide .filesystems .URLMapper ;
58
58
import org .openide .loaders .DataFolder ;
59
59
import org .openide .loaders .DataObject ;
60
+ import org .openide .loaders .DataObjectNotFoundException ;
60
61
import org .openide .text .Line ;
61
62
import org .openide .text .NbDocument ;
62
63
import org .openide .util .Exceptions ;
@@ -71,6 +72,23 @@ public static String toURI(FileObject file) {
71
72
return file .toURI ().toString ().replace ("file:/" , "file:///" );
72
73
}
73
74
75
+ public static String uriReplaceFilename (String inputUriString , String filename ) {
76
+ try {
77
+ URI inputUri = URI .create (inputUriString );
78
+ URI newUri = inputUri .resolve (new URI (null , null , filename , null ));
79
+ if ("file" .equals (newUri .getScheme ())) {
80
+ // By default the java URI routines drop empty hostnames from
81
+ // file URIs. Normalize this to the empty hostname. See also
82
+ // #toURI
83
+ return new URI ("file" , "" , newUri .getPath (), null ).toString ();
84
+ } else {
85
+ return newUri .toString ();
86
+ }
87
+ } catch (URISyntaxException ex ) {
88
+ throw new IllegalArgumentException (ex );
89
+ }
90
+ }
91
+
74
92
public static Position createPosition (Document doc , int offset ) throws BadLocationException {
75
93
return new Position (LineDocumentUtils .getLineIndex ((LineDocument ) doc , offset ),
76
94
offset - LineDocumentUtils .getLineStart ((LineDocument ) doc , offset ));
@@ -137,7 +155,7 @@ public static void applyWorkspaceEdit(WorkspaceEdit edit) {
137
155
private static void applyEdits (String uri , List <TextEdit > edits ) {
138
156
try {
139
157
FileObject file = URLMapper .findFileObject (new URI (uri ).toURL ());
140
- EditorCookie ec = file . getLookup (). lookup ( EditorCookie .class );
158
+ EditorCookie ec = lookupForFile ( file , EditorCookie .class );
141
159
Document doc = ec != null ? ec .openDocument () : null ;
142
160
if (doc == null ) {
143
161
return ;
@@ -274,7 +292,7 @@ public static void open(String targetUri, Range targetRange) {
274
292
FileObject targetFile = fromURI (targetUri );
275
293
276
294
if (targetFile != null ) {
277
- LineCookie lc = targetFile . getLookup (). lookup ( LineCookie .class );
295
+ LineCookie lc = lookupForFile ( targetFile , LineCookie .class );
278
296
279
297
//TODO: expecting lc != null!
280
298
@@ -288,6 +306,25 @@ public static void open(String targetUri, Range targetRange) {
288
306
}
289
307
}
290
308
309
+ public static <T > T lookupForFile (FileObject targetFile , Class <T > clazz ) {
310
+ if (targetFile == null ) {
311
+ return null ;
312
+ }
313
+ T lc = null ;
314
+ try {
315
+ if (lc == null ) {
316
+ DataObject dataObject = DataObject .find (targetFile );
317
+ lc = dataObject .getLookup ().lookup (clazz );
318
+ }
319
+ } catch (DataObjectNotFoundException ex ) {
320
+ // Ignore
321
+ }
322
+ if (lc == null ) {
323
+ lc = targetFile .getLookup ().lookup (clazz );
324
+ }
325
+ return lc ;
326
+ }
327
+
291
328
private static final Comparator <TextEdit > rangeReverseSort = (s1 , s2 ) -> {
292
329
int l1 = s1 .getRange ().getEnd ().getLine ();
293
330
int l2 = s2 .getRange ().getEnd ().getLine ();
0 commit comments