Skip to content

Commit 83debc8

Browse files
authored
Expose breakable range handler to Razor (#76629)
Roslyn side of dotnet/razor#11337
2 parents e4242b9 + 04063fb commit 83debc8

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/LanguageServer/Protocol/Handler/Breakpoints/ValidateBreakableRangeHandler.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ internal sealed class ValidateBreakableRangeHandler() : ILspServiceDocumentReque
2828
public TextDocumentIdentifier GetTextDocumentIdentifier(LSP.VSInternalValidateBreakableRangeParams request)
2929
=> request.TextDocument;
3030

31-
public async Task<LSP.Range?> HandleRequestAsync(LSP.VSInternalValidateBreakableRangeParams request, RequestContext context, CancellationToken cancellationToken)
32-
{
33-
var document = context.GetRequiredDocument();
31+
public Task<LSP.Range?> HandleRequestAsync(LSP.VSInternalValidateBreakableRangeParams request, RequestContext context, CancellationToken cancellationToken)
32+
=> GetBreakableRangeAsync(context.GetRequiredDocument(), request.Range, cancellationToken);
3433

34+
public static async Task<LSP.Range?> GetBreakableRangeAsync(Document document, LSP.Range range, CancellationToken cancellationToken)
35+
{
3536
var text = await document.GetValueTextAsync(cancellationToken).ConfigureAwait(false);
36-
var span = ProtocolConversions.RangeToTextSpan(request.Range, text);
37+
var span = ProtocolConversions.RangeToTextSpan(range, text);
3738
var breakpointService = document.Project.Services.GetRequiredService<IBreakpointResolutionService>();
3839

3940
if (span.Length > 0)
@@ -62,7 +63,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(LSP.VSInternalValidateBr
6263
if (tree.GetDiagnostics(cancellationToken).Any(d => d.Severity == DiagnosticSeverity.Error))
6364
{
6465
// Keep the span as is.
65-
return request.Range;
66+
return range;
6667
}
6768
}
6869
}
@@ -93,7 +94,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(LSP.VSInternalValidateBr
9394
// BP: int a = $$ GetData();
9495
//
9596
// If the user types "1;" we'd shrink the breakpoint, so stick to the end of the range.
96-
if (!result.IsLineBreakpoint && BreakpointRangeIsSmaller(breakpointRange, request.Range))
97+
if (!result.IsLineBreakpoint && BreakpointRangeIsSmaller(breakpointRange, range))
9798
{
9899
var secondResult = await breakpointService.ResolveBreakpointAsync(document, new TextSpan(span.End, length: 0), cancellationToken).ConfigureAwait(false);
99100
if (secondResult is not null)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Microsoft.CodeAnalysis.LanguageServer;
8+
using Microsoft.CodeAnalysis.LanguageServer.Handler;
9+
using Microsoft.CodeAnalysis.Text;
10+
11+
namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers;
12+
13+
internal static class ValidateBreakableRange
14+
{
15+
public static async Task<LinePositionSpan?> GetBreakableRangeAsync(Document document, LinePositionSpan span, CancellationToken cancellationToken)
16+
{
17+
var range = ProtocolConversions.LinePositionToRange(span);
18+
var result = await ValidateBreakableRangeHandler.GetBreakableRangeAsync(document, range, cancellationToken).ConfigureAwait(false);
19+
20+
if (result is null)
21+
{
22+
return null;
23+
}
24+
25+
return ProtocolConversions.RangeToLinePositionSpan(result);
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.Text;
7+
using Microsoft.VisualStudio.Text;
8+
9+
namespace Microsoft.CodeAnalysis.ExternalAccess.Razor;
10+
11+
internal static class ITextBufferExtensions
12+
{
13+
public static bool TryGetTextDocument(this ITextBuffer textBuffer, [NotNullWhen(true)] out TextDocument? textDocument)
14+
{
15+
textDocument = textBuffer.CurrentSnapshot.AsText().GetOpenTextDocumentInCurrentContextWithChanges();
16+
return textDocument is not null;
17+
}
18+
}

0 commit comments

Comments
 (0)