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

Clean up extension methods in MS.CA.Razor.Workspaces #10670

Merged
merged 47 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
16b0b00
Use ArgHelper in SourceTextExtensions
DustinCampbell Jul 19, 2024
d5ff78b
Unify SourceText parameter name in SourceTextExtensions
DustinCampbell Jul 19, 2024
5a828ee
Use ArgHelper in TextSpanExtensions
DustinCampbell Jul 19, 2024
76893d7
Unify SourceText parameter name in TextSpanExtensions
DustinCampbell Jul 19, 2024
c5755ad
Unify SourceText parmater name in SourceSpanExtensions
DustinCampbell Jul 19, 2024
b86d9ca
Add SourceText.GetLineAndOffset(...) extension that returns a tuple
DustinCampbell Jul 19, 2024
edfdb2e
Use tuple-variant of GetLineAndOffset in GetLinesAndOffsets
DustinCampbell Jul 19, 2024
8786444
Change SourceText.GetLinesAndOffsets(...) extensions to return tuple
DustinCampbell Jul 19, 2024
5393616
Update all SourceText.GetLinesAndOffsets(...) calls to use tuple variant
DustinCampbell Jul 19, 2024
3082ee6
Use ArrayPool<char> in GetSubTextString(...) rather than creating array
DustinCampbell Jul 19, 2024
066a42f
Convert SourceText.GetFirstNonWhitespaceOffset extensions to TryGet form
DustinCampbell Jul 19, 2024
fe56a18
Convert SourceText.GetLastNonWhitespaceOffset extensions to TryGet form
DustinCampbell Jul 19, 2024
436d578
Use ArgHelper in RangeExtensions
DustinCampbell Jul 19, 2024
87b510e
Use ArgHelper in PositionExtensions
DustinCampbell Jul 19, 2024
05dcdca
Unify SourceText parameter name in PositionExtentions & RangeExtensions
DustinCampbell Jul 19, 2024
10399da
Use ThrowHelper in SourceTextExtensions
DustinCampbell Jul 19, 2024
f6738df
Unify SourceText parameter name in LinePositionExtensions
DustinCampbell Jul 19, 2024
0af3f90
Add several missing methods to ILoggerExtensions
DustinCampbell Jul 19, 2024
e4c8fb2
Reword TryGetAbsoluteIndex and GetRequiredAbsoluteIndex
DustinCampbell Jul 19, 2024
540718a
Move extensions methods for MS.CA.Text types to MS.CA.Text namespace
DustinCampbell Jul 19, 2024
5bf3239
Change GetLineAndOffset to GetLinePosition
DustinCampbell Jul 19, 2024
d67e43b
Change GetLinesAndOffsets to GetLinePositionSpan
DustinCampbell Jul 19, 2024
425009a
Clean up and normalize more extension methods
DustinCampbell Jul 20, 2024
c23484e
Move all MS.VS.LS.Protocol extension methods into VsLspExtensions
DustinCampbell Jul 22, 2024
e313b8c
Clean up RazorSyntax(Node|Token|Tree)Extensions
DustinCampbell Jul 22, 2024
20f68f9
Move all Roslyn.LS.Protocol extension methods into RoslynLspExtensions
DustinCampbell Jul 23, 2024
da767fc
Clean up VsLspFactory and RoslynLspFactory
DustinCampbell Jul 23, 2024
82f2030
Move IDocumentSnapshotExtensions to ProjectSystem folder
DustinCampbell Jul 23, 2024
5e72b6d
Clean up EnumerableExtensions
DustinCampbell Jul 23, 2024
684af14
Clean up Project|SolutionExtensions
DustinCampbell Jul 23, 2024
7a291ab
Clean up RazorCodeDocumentExtensions
DustinCampbell Jul 24, 2024
6993bab
Clean up StringExtensions and move to System namespace
DustinCampbell Jul 24, 2024
8f7162f
Clean up remaining extensions
DustinCampbell Jul 24, 2024
d1c06ac
Don't add global usings for MS.VS.LS.Protocol.Range in .csproj files
DustinCampbell Jul 24, 2024
2cc8b2d
Change "Collapse" to "ZeroWidth"
DustinCampbell Jul 24, 2024
2c62095
Add Range.IsZeroWidth() extension method
DustinCampbell Jul 24, 2024
da1c0ac
Use IsSingleLine() and IsZeroWidth() in a couple of places
DustinCampbell Jul 24, 2024
e6cc7cb
Rename EmptyPosition and EmptyRange to DefaultPosition and DefaultRange
DustinCampbell Jul 24, 2024
418ede4
Add CreatePosition and CreateRange extension methods that take tuples
DustinCampbell Jul 24, 2024
b24c361
Add CreateTextEdit methods to RoslynLspFactory
DustinCampbell Jul 24, 2024
10c1cd4
Use VsLspFactory tuple overloads in various places
DustinCampbell Jul 24, 2024
708f1e0
Rename GetZeroWidthSpan to ToZeroWidthSpan
DustinCampbell Jul 24, 2024
00a09d6
Remove ILogger from various extension methods
DustinCampbell Jul 24, 2024
f1d00c0
Clean up TryGetAbsoluteIndex and friends
DustinCampbell Jul 24, 2024
5887060
Remove arg checking and rework a few methods
DustinCampbell Jul 24, 2024
84393fc
Remove RazorCodeDocument.GetSourceText() extension method
DustinCampbell Jul 24, 2024
4c1e923
Move RazorCodeDocumentExtensions to MS.ANC.Razor.Language namespace
DustinCampbell Jul 24, 2024
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 @@ -11,7 +11,6 @@
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand Down Expand Up @@ -123,7 +122,7 @@ public async Task RazorCSharpFormattingAsync()

#if DEBUG
// For debugging purposes only.
var changedText = DocumentText.WithChanges(edits.Select(e => e.ToTextChange(DocumentText)));
var changedText = DocumentText.WithChanges(edits.Select(DocumentText.GetTextChange));
_ = changedText.ToString();
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.CodeAnalysis.Text;
using Microsoft.CommonLanguageServerProtocol.Framework;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer;

Expand Down Expand Up @@ -78,9 +79,9 @@ public async Task SetupAsync()
DocumentSnapshot = await GetDocumentSnapshotAsync(projectFilePath, _filePath, targetPath);
DocumentText = await DocumentSnapshot.GetTextAsync();

RazorCodeActionRange = ToRange(razorCodeActionIndex);
CSharpCodeActionRange = ToRange(csharpCodeActionIndex);
HtmlCodeActionRange = ToRange(htmlCodeActionIndex);
RazorCodeActionRange = DocumentText.GetZeroWidthRange(razorCodeActionIndex);
CSharpCodeActionRange = DocumentText.GetZeroWidthRange(csharpCodeActionIndex);
HtmlCodeActionRange = DocumentText.GetZeroWidthRange(htmlCodeActionIndex);

var documentContext = new VersionedDocumentContext(DocumentUri, DocumentSnapshot, projectContext: null, 1);

Expand All @@ -89,16 +90,6 @@ public async Task SetupAsync()
codeDocument.SetCodeGenerationOptions(RazorCodeGenerationOptions.Create(c => c.RootNamespace = "Root.Namespace"));

RazorRequestContext = new RazorRequestContext(documentContext, RazorLanguageServerHost.GetRequiredService<ILspServices>(), "lsp/method", uri: null);

Range ToRange(int index)
{
DocumentText.GetLineAndOffset(index, out var line, out var offset);
return new Range
{
Start = new Position(line, offset),
End = new Position(line, offset)
};
}
}

private string GetFileContents(FileTypes fileType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using Microsoft.CodeAnalysis.Razor.DocumentMapping;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CommonLanguageServerProtocol.Framework;
using Microsoft.VisualStudio.LanguageServer.Protocol;
Expand Down Expand Up @@ -49,7 +48,7 @@ public async Task SetupAsync()
var configurationService = new DefaultRazorConfigurationService(clientConnection, loggerFactory);
var optionsMonitor = new RazorLSPOptionsMonitor(configurationService, RazorLSPOptions.Default);

CompletionEndpoint = new RazorCompletionEndpoint(completionListProvider, telemetryReporter: null, optionsMonitor, loggerFactory);
CompletionEndpoint = new RazorCompletionEndpoint(completionListProvider, telemetryReporter: null, optionsMonitor);

var clientCapabilities = new VSInternalClientCapabilities
{
Expand Down Expand Up @@ -78,16 +77,10 @@ public async Task SetupAsync()
DocumentSnapshot = await GetDocumentSnapshotAsync(projectFilePath, _filePath, targetPath);
DocumentText = await DocumentSnapshot.GetTextAsync();

RazorPosition = ToPosition(razorCodeActionIndex);
RazorPosition = DocumentText.GetPosition(razorCodeActionIndex);

var documentContext = new VersionedDocumentContext(DocumentUri, DocumentSnapshot, projectContext: null, 1);
RazorRequestContext = new RazorRequestContext(documentContext, RazorLanguageServerHost.GetRequiredService<ILspServices>(), "lsp/method", uri: null);

Position ToPosition(int index)
{
DocumentText.GetLineAndOffset(index, out var line, out var offset);
return new Position(line, offset);
}
}

private static string GetFileContents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Moq;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer;

Expand Down Expand Up @@ -206,10 +207,10 @@ public Task<TResponse> SendRequestAsync<TParams, TResponse>(string method, TPara
}
}

private Range InRange { get; set; } = new Range { Start = new Position(85, 8), End = new Position(85, 16) };
private Range OutRange { get; set; } = new Range { Start = new Position(6, 8), End = new Position(6, 16) };
private Range InRange { get; set; } = VsLspFactory.CreateSingleLineRange(line: 85, character: 8, length: 8);
private Range OutRange { get; set; } = VsLspFactory.CreateSingleLineRange(line: 6, character: 8, length: 8);

private Diagnostic[] GetDiagnostics(int N) => Enumerable.Range(1, N).Select(_ => new Diagnostic()
private Diagnostic[] GetDiagnostics(int n) => Enumerable.Range(1, n).Select(_ => new Diagnostic()
{
Range = InRange,
Code = "CS0103",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private bool TryMapToHostDocumentPosition(IRazorGeneratedDocument generatedDocum
// Found the generated span that contains the generated absolute index

hostDocumentIndex = mapping.OriginalSpan.AbsoluteIndex + distanceIntoGeneratedSpan;
hostDocumentPosition = codeDocument.Source.Text.Lines.GetLinePosition(hostDocumentIndex);
hostDocumentPosition = codeDocument.Source.Text.GetLinePosition(hostDocumentIndex);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
Expand All @@ -21,6 +20,7 @@
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer;

Expand Down Expand Up @@ -66,19 +66,9 @@ public async Task InitializeRazorSemanticAsync()
DocumentContext = new VersionedDocumentContext(documentUri, documentSnapshot, projectContext: null, version);

var text = await DocumentContext.GetSourceTextAsync(CancellationToken.None).ConfigureAwait(false);
Range = new Range
{
Start = new Position
{
Line = 0,
Character = 0
},
End = new Position
{
Line = text.Lines.Count - 1,
Character = text.Lines.Last().Span.Length - 1
}
};
Range = VsLspFactory.CreateRange(
start: (0, 0),
end: (text.Lines.Count - 1, text.Lines[^1].Span.Length - 1));
}

[Benchmark(Description = "Razor Semantic Tokens Range Handling")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
Expand All @@ -23,6 +22,7 @@
using Microsoft.CommonLanguageServerProtocol.Framework;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer;

Expand Down Expand Up @@ -62,8 +62,6 @@ public async Task InitializeRazorSemanticAsync()
{
EnsureServicesInitialized();

var loggerFactory = RazorLanguageServerHost.GetRequiredService<ILoggerFactory>();

var projectRoot = Path.Combine(RepoRoot, "src", "Razor", "test", "testapps", "ComponentApp");
ProjectFilePath = Path.Combine(projectRoot, "ComponentApp.csproj");
PagesDirectory = Path.Combine(projectRoot, "Components", "Pages");
Expand All @@ -81,11 +79,9 @@ public async Task InitializeRazorSemanticAsync()
SemanticTokensRangeEndpoint = new SemanticTokensRangeEndpoint(RazorSemanticTokenService, razorSemanticTokensLegendService, razorOptionsMonitor, telemetryReporter: null);

var text = await DocumentContext.GetSourceTextAsync(CancellationToken.None).ConfigureAwait(false);
Range = new Range
{
Start = new Position { Line = 0, Character = 0 },
End = new Position { Line = text.Lines.Count - 1, Character = text.Lines.Last().Span.Length - 1 }
};
Range = VsLspFactory.CreateRange(
start: (0, 0),
end: (text.Lines.Count - 1, text.Lines[^1].Span.Length - 1));

var documentVersion = 1;
VersionCache.TrackDocumentVersion(DocumentSnapshot, documentVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using static Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer.RazorSemanticTokensBenchmark;
using Range = Microsoft.VisualStudio.LanguageServer.Protocol.Range;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.LanguageServer;

Expand Down Expand Up @@ -54,19 +55,9 @@ public async Task InitializeRazorSemanticAsync()
DocumentContext = new VersionedDocumentContext(documentUri, documentSnapshot, projectContext: null, version: 1);

var text = await DocumentSnapshot.GetTextAsync().ConfigureAwait(false);
Range = new Range
{
Start = new Position
{
Line = 0,
Character = 0
},
End = new Position
{
Line = text.Lines.Count - 1,
Character = 0
}
};
Range = VsLspFactory.CreateRange(
start: (0, 0),
end: (text.Lines.Count - 1, 0));
}

private const int WindowSize = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
<IsShipping>false</IsShipping>
</PropertyGroup>

<ItemGroup>
<Using Alias="Range" Include="Microsoft.VisualStudio.LanguageServer.Protocol.Range" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Compiler\Microsoft.CodeAnalysis.Razor.Compiler\src\Microsoft.CodeAnalysis.Razor.Compiler.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.LanguageServer\Microsoft.AspNetCore.Razor.LanguageServer.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
using System.Text.Json;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.LanguageServer;
using Microsoft.AspNetCore.Razor.LanguageServer.Completion;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis.Razor.Completion;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.Microbenchmarks.Serialization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected virtual Task<TResponse> HandleDelegatedResponseAsync(TResponse delegat
return default;
}

var positionInfo = await DocumentPositionInfoStrategy.TryGetPositionInfoAsync(_documentMappingService, documentContext, request.Position, Logger, cancellationToken).ConfigureAwait(false);
var positionInfo = await DocumentPositionInfoStrategy.TryGetPositionInfoAsync(_documentMappingService, documentContext, request.Position, cancellationToken).ConfigureAwait(false);
if (positionInfo is null)
{
return default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.AutoInsert;

internal sealed class AutoClosingTagOnAutoInsertProvider : IOnAutoInsertProvider
internal sealed class AutoClosingTagOnAutoInsertProvider(RazorLSPOptionsMonitor optionsMonitor) : IOnAutoInsertProvider
{
// From http://dev.w3.org/html5/spec/Overview.html#elements-0
private static readonly ImmutableHashSet<string> s_voidElements = ImmutableHashSet.Create(StringComparer.OrdinalIgnoreCase,
Expand All @@ -36,26 +35,10 @@ internal sealed class AutoClosingTagOnAutoInsertProvider : IOnAutoInsertProvider
"track",
"wbr"
);
private static readonly ImmutableHashSet<string> s_voidElementsCaseSensitive = s_voidElements.WithComparer(StringComparer.Ordinal);

private readonly RazorLSPOptionsMonitor _optionsMonitor;
private readonly ILogger _logger;

public AutoClosingTagOnAutoInsertProvider(RazorLSPOptionsMonitor optionsMonitor, ILoggerFactory loggerFactory)
{
if (optionsMonitor is null)
{
throw new ArgumentNullException(nameof(optionsMonitor));
}

if (loggerFactory is null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
private static readonly ImmutableHashSet<string> s_voidElementsCaseSensitive = s_voidElements.WithComparer(StringComparer.Ordinal);

_optionsMonitor = optionsMonitor;
_logger = loggerFactory.GetOrCreateLogger<IOnAutoInsertProvider>();
}
private readonly RazorLSPOptionsMonitor _optionsMonitor = optionsMonitor;

public string TriggerCharacter => ">";

Expand All @@ -68,7 +51,7 @@ public bool TryResolveInsertion(Position position, FormattingContext context, [N
return false;
}

if (!position.TryGetAbsoluteIndex(context.SourceText, _logger, out var afterCloseAngleIndex))
if (!context.SourceText.TryGetAbsoluteIndex(position, out var afterCloseAngleIndex))
{
format = default;
edit = default;
Expand All @@ -85,11 +68,7 @@ public bool TryResolveInsertion(Position position, FormattingContext context, [N
if (autoClosingBehavior == AutoClosingBehavior.EndTag)
{
format = InsertTextFormat.Snippet;
edit = new TextEdit()
{
NewText = $"$0</{tagName}>",
Range = new Range { Start = position, End = position },
};
edit = VsLspFactory.CreateTextEdit(position, $"$0</{tagName}>");

return true;
}
Expand All @@ -100,16 +79,7 @@ public bool TryResolveInsertion(Position position, FormattingContext context, [N

// Need to replace the `>` with ' />$0' or '/>$0' depending on if there's prefixed whitespace.
var insertionText = char.IsWhiteSpace(context.SourceText[afterCloseAngleIndex - 2]) ? "/" : " /";
var insertionPosition = new Position(position.Line, position.Character - 1);
edit = new TextEdit()
{
NewText = insertionText,
Range = new Range
{
Start = insertionPosition,
End = insertionPosition
}
};
edit = VsLspFactory.CreateTextEdit(position.Line, position.Character - 1, insertionText);

return true;
}
Expand Down
Loading