@@ -37,25 +37,36 @@ public static string GetAbsoluteOrUNCPath(this Uri uri)
37
37
// Absolute paths are usually encoded.
38
38
var absolutePath = uri . AbsolutePath . Contains ( "%" ) ? WebUtility . UrlDecode ( uri . AbsolutePath ) : uri . AbsolutePath ;
39
39
40
- if ( string . Equals ( uri . Scheme , "git" , StringComparison . OrdinalIgnoreCase ) )
41
- {
42
- // return a normalized path when we want to add to a fake git directory path (hacky fix for https://github.com/dotnet/razor/issues/9365)
43
- return AddToFakeGitDirectoryAtRoot ( absolutePath ) ;
44
- }
45
-
46
- return absolutePath ;
40
+ return EnsureUniquePathForScheme ( uri . Scheme , absolutePath ) ;
47
41
}
48
42
49
- private static string AddToFakeGitDirectoryAtRoot ( string decodedAbsolutePath )
43
+ private static string EnsureUniquePathForScheme ( string scheme , string decodedAbsolutePath )
50
44
{
45
+ // File Uris we leave untouched, as they represent the actual files on disk
46
+ if ( scheme == Uri . UriSchemeFile )
47
+ {
48
+ return decodedAbsolutePath ;
49
+ }
50
+
51
51
var normalizedPath = FilePathNormalizer . Normalize ( decodedAbsolutePath ) ;
52
52
var firstSeparatorIndex = normalizedPath . IndexOf ( '/' ) ;
53
53
if ( firstSeparatorIndex < 0 )
54
54
{
55
- // no-op
55
+ // A path without a separator is unlikely, but we can't add our unique-ness marker anyway, so just return as is
56
+ // and hope for the best.
56
57
return decodedAbsolutePath ;
57
58
}
58
59
59
- return normalizedPath . Insert ( firstSeparatorIndex + 1 , "_git_/" ) ;
60
+ // For any non-file Uri, we add a marker to the path to ensure things won't conflict with the real file on disk.
61
+ // This is a somewhat hacky fix to ensure things like a git diff view will work, where the left hand side is a
62
+ // Uri like "git://path/to/file.razor" and the right hand side is "file://path/to/file.razor". If we mapped both
63
+ // of those to the same file path, then one side would be sending line and character positions that don't match
64
+ // our understanding of the document.
65
+ // A true fix would be to move away from using file paths in the first place, but instead use the Uris as provided
66
+ // by the LSP client as the source of truth.
67
+ // See https://github.com/dotnet/razor/issues/9365 and https://github.com/microsoft/vscode-dotnettools/issues/2151
68
+ // for examples.
69
+
70
+ return normalizedPath . Insert ( firstSeparatorIndex + 1 , $ "_{ scheme } _/") ;
60
71
}
61
72
}
0 commit comments