@@ -306,27 +306,23 @@ public void CloseFile(ScriptFile scriptFile)
306
306
/// <summary>
307
307
/// Gets the workspace-relative path of the given file path.
308
308
/// </summary>
309
- /// <param name="filePath">The original full file path.</param>
310
309
/// <returns>A relative file path</returns>
311
- public string GetRelativePath ( string filePath )
310
+ public string GetRelativePath ( ScriptFile scriptFile )
312
311
{
313
- string resolvedPath = filePath ;
314
-
315
- if ( ! IsPathInMemory ( filePath ) && ! string . IsNullOrEmpty ( InitialWorkingDirectory ) )
312
+ if ( ! scriptFile . IsInMemory )
316
313
{
317
- Uri workspaceUri = new ( InitialWorkingDirectory ) ;
318
- Uri fileUri = new ( filePath ) ;
319
-
320
- resolvedPath = workspaceUri . MakeRelativeUri ( fileUri ) . ToString ( ) ;
321
-
322
- // Convert the directory separators if necessary
323
- if ( Path . DirectorySeparatorChar == '\\ ' )
314
+ Uri fileUri = scriptFile . DocumentUri . ToUri ( ) ;
315
+ foreach ( WorkspaceFolder workspaceFolder in WorkspaceFolders )
324
316
{
325
- resolvedPath = resolvedPath . Replace ( '/' , '\\ ' ) ;
317
+ Uri workspaceUri = workspaceFolder . Uri . ToUri ( ) ;
318
+ if ( workspaceUri . IsBaseOf ( fileUri ) )
319
+ {
320
+ return workspaceUri . MakeRelativeUri ( fileUri ) . ToString ( ) ;
321
+ }
326
322
}
327
323
}
328
324
329
- return resolvedPath ;
325
+ return scriptFile . FilePath ;
330
326
}
331
327
332
328
/// <summary>
@@ -407,42 +403,6 @@ internal static string ReadFileContents(DocumentUri uri)
407
403
return reader . ReadToEnd ( ) ;
408
404
}
409
405
410
- internal static bool IsPathInMemory ( string filePath )
411
- {
412
- bool isInMemory = false ;
413
-
414
- // In cases where a "virtual" file is displayed in the editor,
415
- // we need to treat the file differently than one that exists
416
- // on disk. A virtual file could be something like a diff
417
- // view of the current file or an untitled file.
418
- try
419
- {
420
- // File system absolute paths will have a URI scheme of file:.
421
- // Other schemes like "untitled:" and "gitlens-git:" will return false for IsFile.
422
- Uri uri = new ( filePath ) ;
423
- isInMemory = ! uri . IsFile ;
424
- }
425
- catch ( UriFormatException )
426
- {
427
- // Relative file paths cause a UriFormatException.
428
- // In this case, fallback to using Path.GetFullPath().
429
- try
430
- {
431
- Path . GetFullPath ( filePath ) ;
432
- }
433
- catch ( Exception ex ) when ( ex is ArgumentException or NotSupportedException )
434
- {
435
- isInMemory = true ;
436
- }
437
- catch ( PathTooLongException )
438
- {
439
- // If we ever get here, it should be an actual file so, not in memory
440
- }
441
- }
442
-
443
- return isInMemory ;
444
- }
445
-
446
406
internal string ResolveWorkspacePath ( string path ) => ResolveRelativeScriptPath ( InitialWorkingDirectory , path ) ;
447
407
448
408
internal string ResolveRelativeScriptPath ( string baseFilePath , string relativePath )
0 commit comments