Skip to content
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

Use a helper method instead of doing lots of Uri building #11346

Merged
merged 2 commits into from
Jan 2, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,9 @@ .. csharpDiagnostics ?? []
{
if (_options.DelegateToCSharpOnDiagnosticPublish)
{
var uriBuilder = new UriBuilder()
{
Scheme = Uri.UriSchemeFile,
Path = document.FilePath,
Host = string.Empty,
};

var delegatedParams = new DocumentDiagnosticParams
{
TextDocument = new TextDocumentIdentifier { Uri = uriBuilder.Uri },
TextDocument = new TextDocumentIdentifier { Uri = VsLspFactory.CreateFilePathUri(document.FilePath) },
};

var delegatedResponse = await _clientConnection
Expand Down Expand Up @@ -257,19 +250,12 @@ private void ClearClosedDocuments()

private void PublishDiagnosticsForFilePath(string filePath, Diagnostic[] diagnostics)
{
var uriBuilder = new UriBuilder()
{
Scheme = Uri.UriSchemeFile,
Path = filePath,
Host = string.Empty,
};

_clientConnection
.SendNotificationAsync(
Methods.TextDocumentPublishDiagnosticsName,
new PublishDiagnosticParams()
{
Uri = uriBuilder.Uri,
Uri = VsLspFactory.CreateFilePathUri(filePath),
Diagnostics = diagnostics,
},
_disposeTokenSource.Token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ internal class CreateComponentCodeActionResolver(LanguageServerFeatureOptions la
var updatedPath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !actionParams.Path.StartsWith("/")
? '/' + actionParams.Path
: actionParams.Path;
var newComponentUri = new UriBuilder()
{
Scheme = Uri.UriSchemeFile,
Path = updatedPath,
Host = string.Empty,
}.Uri;
var newComponentUri = VsLspFactory.CreateFilePathUri(updatedPath);

using var documentChanges = new PooledArrayBuilder<SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>>();
documentChanges.Add(new CreateFile() { Uri = newComponentUri });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ internal class ExtractToCodeBehindCodeActionResolver(
? '/' + codeBehindPath
: codeBehindPath;

var codeBehindUri = new UriBuilder
{
Scheme = Uri.UriSchemeFile,
Path = updatedCodeBehindPath,
Host = string.Empty,
}.Uri;
var codeBehindUri = VsLspFactory.CreateFilePathUri(updatedCodeBehindPath);

var text = await documentContext.GetSourceTextAsync(cancellationToken).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

#if !NET
using System;
#endif
using System.IO;
using System.Text.Json;
using System.Threading;
Expand Down Expand Up @@ -58,12 +60,7 @@ internal class ExtractToComponentCodeActionResolver(
? '/' + componentPath
: componentPath;

var newComponentUri = new UriBuilder
{
Scheme = Uri.UriSchemeFile,
Path = componentPath,
Host = string.Empty,
}.Uri;
var newComponentUri = VsLspFactory.CreateFilePathUri(componentPath);

using var _ = StringBuilderPool.GetPooledObject(out var builder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,7 @@ razorClassName is null ||
cancellationToken).ConfigureAwait(false);
}

var codeBehindUri = new UriBuilder
{
Scheme = Uri.UriSchemeFile,
Path = codeBehindPath,
Host = string.Empty,
}.Uri;
var codeBehindUri = VsLspFactory.CreateFilePathUri(codeBehindPath);

var codeBehindTextDocumentIdentifier = new OptionalVersionedTextDocumentIdentifier() { Uri = codeBehindUri };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ internal class PromoteUsingCodeActionResolver(IFileSystem fileSystem) : IRazorCo
var file = FilePathNormalizer.Normalize(documentContext.Uri.GetAbsoluteOrUNCPath());
var folder = Path.GetDirectoryName(file).AssumeNotNull();
var importsFile = Path.GetFullPath(Path.Combine(folder, "..", importsFileName));
var importFileUri = new UriBuilder
{
Scheme = Uri.UriSchemeFile,
Path = importsFile,
Host = string.Empty,
}.Uri;
var importFileUri = VsLspFactory.CreateFilePathUri(importsFile);

using var edits = new PooledArrayBuilder<SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,7 @@ private Uri BuildUri(string filePath)
var updatedPath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !filePath.StartsWith("/")
? '/' + filePath
: filePath;
var oldUri = new UriBuilder
{
Path = updatedPath,
Host = string.Empty,
Scheme = Uri.UriSchemeFile,
}.Uri;
return oldUri;
return VsLspFactory.CreateFilePathUri(updatedPath);
}

private static string MakeNewPath(string originalPath, string newName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,12 @@ public async Task Resolve_RootUriPrefered()
{
// Arrange
var initialWorkspaceDirectory = "C:\\testpath";
var uriBuilder = new UriBuilder
{
Scheme = "file",
Host = null,
Path = initialWorkspaceDirectory,
};

#pragma warning disable CS0618 // Type or member is obsolete
var initializeParams = new InitializeParams()
{
RootPath = "/somethingelse",
RootUri = uriBuilder.Uri,
RootUri = VsLspFactory.CreateFilePathUri(initialWorkspaceDirectory),
};
#pragma warning restore CS0618 // Type or member is obsolete

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,9 @@ public async Task InitializedAsync_StartsFileChangeDetectors()
// Arrange
var initialWorkspaceDirectory = "testpath";

var uriBuilder = new UriBuilder
{
Scheme = "file",
Host = null,
Path = initialWorkspaceDirectory
};

var initializeParams = new InitializeParams()
{
RootUri = uriBuilder.Uri,
RootUri = VsLspFactory.CreateFilePathUri(initialWorkspaceDirectory),
};

var capabilitiesManager = new CapabilitiesManager(StrictMock.Of<ILspServices>());
Expand Down