Skip to content

Commit 6199e76

Browse files
committed
PR Feedback
1 parent 06ef015 commit 6199e76

File tree

12 files changed

+50
-74
lines changed

12 files changed

+50
-74
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/CreateComponentCodeActionResolver.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ internal class CreateComponentCodeActionResolver(LanguageServerFeatureOptions la
3939
}
4040

4141
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
42-
43-
// VS Code in Windows expects path to start with '/'
44-
var updatedPath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !actionParams.Path.StartsWith("/")
45-
? '/' + actionParams.Path
46-
: actionParams.Path;
47-
var newComponentUri = LspFactory.CreateFilePathUri(updatedPath);
42+
var newComponentUri = LspFactory.CreateFilePathUri(actionParams.Path, _languageServerFeatureOptions);
4843

4944
using var documentChanges = new PooledArrayBuilder<SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>>();
5045
documentChanges.Add(new CreateFile() { DocumentUri = new(newComponentUri) });

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCodeBehindCodeActionResolver.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,7 @@ internal class ExtractToCodeBehindCodeActionResolver(
4141

4242
var path = FilePathNormalizer.Normalize(documentContext.Uri.GetAbsoluteOrUNCPath());
4343
var codeBehindPath = FileUtilities.GenerateUniquePath(path, $"{Path.GetExtension(path)}.cs");
44-
45-
// VS Code in Windows expects path to start with '/'
46-
var updatedCodeBehindPath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !codeBehindPath.StartsWith("/")
47-
? $"/{codeBehindPath}"
48-
: codeBehindPath;
49-
50-
var codeBehindUri = LspFactory.CreateFilePathUri(updatedCodeBehindPath);
44+
var codeBehindUri = LspFactory.CreateFilePathUri(codeBehindPath, _languageServerFeatureOptions);
5145

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

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToComponentCodeActionResolver.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@ internal class ExtractToComponentCodeActionResolver(
5050
var templatePath = Path.Combine(directoryName, "Component.razor");
5151
var componentPath = FileUtilities.GenerateUniquePath(templatePath, ".razor");
5252
var componentName = Path.GetFileNameWithoutExtension(componentPath);
53-
54-
// VS Code in Windows expects path to start with '/'
55-
componentPath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !componentPath.StartsWith('/')
56-
? $"/{componentPath}"
57-
: componentPath;
58-
59-
var newComponentUri = new DocumentUri(LspFactory.CreateFilePathUri(componentPath));
53+
var newComponentUri = new DocumentUri(LspFactory.CreateFilePathUri(componentPath, _languageServerFeatureOptions));
6054

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

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ public Task<ImmutableArray<RazorVSInternalCodeAction>> ProvideAsync(RazorCodeAct
6565
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
6666
}
6767

68-
if (textLiteral.LiteralTokens.All(t => t.IsWhitespace()))
68+
if (textLiteral.LiteralTokens.All(static t => t.IsWhitespace()))
6969
{
7070
// If the text literal is all whitespace, we don't want to offer the action.
7171
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
7272
}
7373

7474
// If there are diagnostics, we can't trust the tree to be what we expect.
75-
if (markupElement.GetDiagnostics().Any(d => d.Severity == RazorDiagnosticSeverity.Error))
75+
if (markupElement.GetDiagnostics().Any(static d => d.Severity == RazorDiagnosticSeverity.Error))
7676
{
7777
return SpecializedTasks.EmptyImmutableArray<RazorVSInternalCodeAction>();
7878
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCssCodeActionResolver.cs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@ internal class ExtractToCssCodeActionResolver(
3838
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
3939

4040
var cssFilePath = $"{FilePathNormalizer.Normalize(documentContext.Uri.GetAbsoluteOrUNCPath())}.css";
41-
42-
// VS Code in Windows expects path to start with '/'
43-
cssFilePath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !cssFilePath.StartsWith("/")
44-
? $"/{cssFilePath}"
45-
: cssFilePath;
46-
47-
var cssFileUri = LspFactory.CreateFilePathUri(cssFilePath);
41+
var cssFileUri = LspFactory.CreateFilePathUri(cssFilePath, _languageServerFeatureOptions);
4842

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

@@ -97,7 +91,7 @@ internal class ExtractToCssCodeActionResolver(
9791

9892
private void GetLastLineNumberAndLength(string cssFilePath, out int lastLineNumber, out int lastLineLength)
9993
{
100-
using var stream = _fileSystem.ReadStream(cssFilePath);
94+
using var stream = _fileSystem.OpenReadStream(cssFilePath);
10195
GetLastLineNumberAndLength(stream, bufferSize: 4096, out lastLineNumber, out lastLineLength);
10296
}
10397

@@ -107,38 +101,35 @@ private static void GetLastLineNumberAndLength(Stream stream, int bufferSize, ou
107101
lastLineLength = 0;
108102

109103
using var _ = ArrayPool<char>.Shared.GetPooledArray(bufferSize, out var buffer);
110-
using (var reader = new StreamReader(stream))
111-
{
112-
var currLineLength = 0;
113-
var currLineNumber = 0;
104+
using var reader = new StreamReader(stream);
105+
106+
var currLineLength = 0;
107+
var currLineNumber = 0;
114108

115-
int charsRead;
116-
while ((charsRead = reader.Read(buffer, 0, buffer.Length)) > 0)
109+
int charsRead;
110+
while ((charsRead = reader.Read(buffer, 0, buffer.Length)) > 0)
111+
{
112+
var chunk = buffer.AsSpan(0, charsRead);
113+
while (true)
117114
{
118-
var chunk = buffer.AsSpan()[..charsRead];
119-
while (true)
115+
// Since we're only concerned with the last line length, we don't need to worry about \r\n. Strictly speaking,
116+
// we're incorrectly counting the \r in the line length, but since the last line can't end with a \n (since that
117+
// starts a new line) it doesn't actually change the output of the method.
118+
var index = chunk.IndexOf('\n');
119+
if (index == -1)
120120
{
121-
// Since we're only concerned with the last line length, we don't need to worry about \r\n. Strictly speaking,
122-
// we're incorrectly counting the \r in the line length, but since the last line can't end with a \n (since that
123-
// starts a new line) it doesn't actually change the output of the method.
124-
var index = chunk.IndexOf('\n');
125-
if (index == -1)
126-
{
127-
currLineLength += chunk.Length;
128-
break;
129-
}
130-
else
131-
{
132-
currLineNumber++;
133-
currLineLength = 0;
134-
chunk = chunk[(index + 1)..];
135-
}
121+
currLineLength += chunk.Length;
122+
break;
136123
}
137-
}
138124

139-
lastLineNumber = currLineNumber;
140-
lastLineLength = currLineLength;
125+
currLineNumber++;
126+
currLineLength = 0;
127+
chunk = chunk[(index + 1)..];
128+
}
141129
}
130+
131+
lastLineNumber = currLineNumber;
132+
lastLineLength = currLineLength;
142133
}
143134

144135
internal readonly struct TestAccessor

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/LspFactory.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using Microsoft.CodeAnalysis.Razor.Workspaces;
67
using Microsoft.CodeAnalysis.Text;
78

89
namespace Roslyn.LanguageServer.Protocol;
@@ -205,6 +206,16 @@ public static TextEdit CreateTextEdit(LinePosition position, string newText)
205206
public static TextEdit CreateTextEdit((int line, int character) position, string newText)
206207
=> CreateTextEdit(CreateZeroWidthRange(position), newText);
207208

209+
public static Uri CreateFilePathUri(string filePath, LanguageServerFeatureOptions options)
210+
{
211+
// VS Code in Windows expects path to start with '/'
212+
var updateFilePath = options.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !filePath.StartsWith('/')
213+
? $"/{filePath}"
214+
: filePath;
215+
216+
return CreateFilePathUri(updateFilePath);
217+
}
218+
208219
public static Uri CreateFilePathUri(string filePath)
209220
{
210221
var builder = new UriBuilder

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/FileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public bool FileExists(string filePath)
2020
public string ReadFile(string filePath)
2121
=> File.ReadAllText(filePath);
2222

23-
public Stream ReadStream(string filePath)
23+
public Stream OpenReadStream(string filePath)
2424
=> new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
2525
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/IFileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ internal interface IFileSystem
1616

1717
string ReadFile(string filePath);
1818

19-
Stream ReadStream(string filePath);
19+
Stream OpenReadStream(string filePath);
2020
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Rename/RenameService.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,10 @@ private static ImmutableArray<IDocumentSnapshot> GetAllDocumentSnapshots(string
136136
private RenameFile GetFileRenameForComponent(IDocumentSnapshot documentSnapshot, string newPath)
137137
=> new RenameFile
138138
{
139-
OldDocumentUri = BuildUri(documentSnapshot.FilePath),
140-
NewDocumentUri = BuildUri(newPath),
139+
OldDocumentUri = new(LspFactory.CreateFilePathUri(documentSnapshot.FilePath, _languageServerFeatureOptions)),
140+
NewDocumentUri = new(LspFactory.CreateFilePathUri(newPath, _languageServerFeatureOptions)),
141141
};
142142

143-
private DocumentUri BuildUri(string filePath)
144-
{
145-
// VS Code in Windows expects path to start with '/'
146-
var updatedPath = _languageServerFeatureOptions.ReturnCodeActionAndRenamePathsWithPrefixedSlash && !filePath.StartsWith("/")
147-
? '/' + filePath
148-
: filePath;
149-
return new(LspFactory.CreateFilePathUri(updatedPath));
150-
}
151-
152143
private static string MakeNewPath(string originalPath, string newName)
153144
{
154145
var newFileName = $"{newName}{Path.GetExtension(originalPath)}";
@@ -171,7 +162,7 @@ private async Task AddEditsForCodeDocumentAsync(
171162
var codeDocument = await documentSnapshot.GetGeneratedOutputAsync(cancellationToken).ConfigureAwait(false);
172163

173164
// VS Code in Windows expects path to start with '/'
174-
var uri = BuildUri(documentSnapshot.FilePath);
165+
var uri = new DocumentUri(LspFactory.CreateFilePathUri(documentSnapshot.FilePath, _languageServerFeatureOptions));
175166

176167
AddEditsForCodeDocument(documentChanges, originTagHelpers, newName, uri, codeDocument);
177168
}

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/RemoteFileSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public bool FileExists(string filePath)
1919
public string ReadFile(string filePath)
2020
=> _fileSystem.ReadFile(filePath);
2121

22-
public Stream ReadStream(string filePath)
23-
=> _fileSystem.ReadStream(filePath);
22+
public Stream OpenReadStream(string filePath)
23+
=> _fileSystem.OpenReadStream(filePath);
2424

2525
public IEnumerable<string> GetDirectories(string workspaceDirectory)
2626
=> _fileSystem.GetDirectories(workspaceDirectory);

0 commit comments

Comments
 (0)