Skip to content

DocumentUri adjustments #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/Protocol/DocumentUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ public DocumentUri(string url)
var delimiterIndex = url.IndexOf(SchemeDelimiter, StringComparison.Ordinal);
if (delimiterIndex == -1)
{
url = Uri.UnescapeDataString(url).Replace('\\', '/');
// Unc path
if (url.StartsWith("\\\\"))
{
var authorityEndIndex = url.IndexOf('\\', 2);
Authority = url.Substring(2, authorityEndIndex - 2);
url = url.Substring(authorityEndIndex);
// Path = Uri.UnescapeDataString(url);
}
else
{
Authority = string.Empty;
}

url = url.Replace('\\', '/');

Scheme = UriSchemeFile;
Authority = string.Empty;
Query = string.Empty;
Fragment = string.Empty;
Path = Uri.UnescapeDataString(url).TrimStart('/');
Path = Uri.UnescapeDataString(url.StartsWith("/") ? url : "/" + url);

return;
}
Expand All @@ -40,6 +52,15 @@ public DocumentUri(string url)
Authority = url.Substring(delimiterIndex + SchemeDelimiter.Length,
authorityIndex - (delimiterIndex + SchemeDelimiter.Length));

// this is a possible windows path without the proper tripple slash
// file://c:/some/path.file.cs
// We need deal with this case.
if (Authority.IndexOf(':') > -1 || Authority.IndexOf("%3a", StringComparison.OrdinalIgnoreCase) > -1)
{
Authority = string.Empty;
authorityIndex = delimiterIndex + SchemeDelimiter.Length;
}

var fragmentIndex = url.IndexOf('#');
if (fragmentIndex > -1)
{
Expand All @@ -62,8 +83,7 @@ public DocumentUri(string url)
queryIndex = fragmentIndex;
}

Path = Uri.UnescapeDataString(url.Substring(authorityIndex + 1, queryIndex - (authorityIndex)))
.TrimStart('/');
Path = Uri.UnescapeDataString(url.Substring(authorityIndex, queryIndex - (authorityIndex) + 1));
}

/// <summary>
Expand Down Expand Up @@ -133,7 +153,7 @@ public Uri ToUri()
/// <returns></returns>
/// <remarks>This will not a uri encode asian and cyrillic characters</remarks>
public override string ToString() =>
$"{Scheme}{SchemeDelimiter}{Authority}/{Path}{(string.IsNullOrWhiteSpace(Query) ? "" : "?" + Query)}{(string.IsNullOrWhiteSpace(Fragment) ? "" : "#" + Fragment)}";
$"{Scheme}{SchemeDelimiter}{Authority}{Path}{(string.IsNullOrWhiteSpace(Query) ? "" : "?" + Query)}{(string.IsNullOrWhiteSpace(Fragment) ? "" : "#" + Fragment)}";

/// <summary>
/// Gets the file system path prefixed with / for unix platforms
Expand All @@ -143,7 +163,11 @@ public override string ToString() =>
public string GetFileSystemPath()
{
// The language server protocol represents "C:\Foo\Bar" as "file:///c:/foo/bar".
return Path.IndexOf(':') == -1 ? "/" + Path : Path.Replace('/', '\\');
if (Path.IndexOf(':') == -1 && !(Scheme == UriSchemeFile && !string.IsNullOrWhiteSpace(Authority)))
return Path;
if (!string.IsNullOrWhiteSpace(Authority))
return $"\\\\{Authority}{Path}".Replace('/', '\\');
return Path.TrimStart('/').Replace('/', '\\');
}

/// <inheritdoc />
Expand Down
8 changes: 4 additions & 4 deletions src/Protocol/Models/DocumentFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,19 @@ public bool IsMatch(TextDocumentAttributes attributes)
{
if (HasLanguage && HasPattern && HasScheme)
{
return Language == attributes.LanguageId && Scheme == attributes.Scheme && _minimatcher.IsMatch(attributes.Uri.Path);
return Language == attributes.LanguageId && Scheme == attributes.Scheme && _minimatcher.IsMatch(attributes.Uri.ToString());
}
if (HasLanguage && HasPattern)
{
return Language == attributes.LanguageId && _minimatcher.IsMatch(attributes.Uri.Path);
return Language == attributes.LanguageId && _minimatcher.IsMatch(attributes.Uri.ToString());
}
if (HasLanguage && HasScheme)
{
return Language == attributes.LanguageId && Scheme == attributes.Scheme;
}
if (HasPattern && HasScheme)
{
return Scheme == attributes.Scheme && _minimatcher.IsMatch(attributes.Uri.Path);
return Scheme == attributes.Scheme && _minimatcher.IsMatch(attributes.Uri.ToString());
}
if (HasLanguage)
{
Expand All @@ -102,7 +102,7 @@ public bool IsMatch(TextDocumentAttributes attributes)
}
if (HasPattern)
{
return _minimatcher.IsMatch(attributes.Uri.Path);
return _minimatcher.IsMatch(attributes.Uri.ToString());
}

return false;
Expand Down
10 changes: 8 additions & 2 deletions test/Lsp.Tests/AbsoluteUriConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public AbsoluteUriConverterTests(ITestOutputHelper testOutputHelper)
}

[Theory]
[ClassData(typeof(DocumentUriTestData.StringUris))]
[ClassData(typeof(DocumentUriTests.WindowsPathStringUris))]
[ClassData(typeof(DocumentUriTests.WindowsPathAltStringUris))]
[ClassData(typeof(DocumentUriTests.UncPathStringUris))]
[ClassData(typeof(DocumentUriTests.UnixPathStringUris))]
public void Should_Deserialize_VSCode_Style_Uris(string uri, DocumentUri expected)
{
_testOutputHelper.WriteLine($"Given: {uri}");
Expand All @@ -29,7 +32,10 @@ public void Should_Deserialize_VSCode_Style_Uris(string uri, DocumentUri expecte
}

[Theory]
[ClassData(typeof(DocumentUriTestData.StringUris))]
[ClassData(typeof(DocumentUriTests.WindowsPathStringUris))]
[ClassData(typeof(DocumentUriTests.WindowsPathAltStringUris))]
[ClassData(typeof(DocumentUriTests.UncPathStringUris))]
[ClassData(typeof(DocumentUriTests.UnixPathStringUris))]
public void Should_Serialize_VSCode_Style_Uris(string uri, DocumentUri expected)
{
_testOutputHelper.WriteLine($"Given: {uri}");
Expand Down
Loading