Skip to content
New issue

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

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

Already on GitHub? # to your account

Expose breakable range handler to Razor #76629

Merged
merged 2 commits into from
Jan 6, 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 @@ -28,12 +28,13 @@ internal sealed class ValidateBreakableRangeHandler() : ILspServiceDocumentReque
public TextDocumentIdentifier GetTextDocumentIdentifier(LSP.VSInternalValidateBreakableRangeParams request)
=> request.TextDocument;

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

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

if (span.Length > 0)
Expand Down Expand Up @@ -62,7 +63,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(LSP.VSInternalValidateBr
if (tree.GetDiagnostics(cancellationToken).Any(d => d.Severity == DiagnosticSeverity.Error))
{
// Keep the span as is.
return request.Range;
return range;
}
}
}
Expand Down Expand Up @@ -93,7 +94,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(LSP.VSInternalValidateBr
// BP: int a = $$ GetData();
//
// If the user types "1;" we'd shrink the breakpoint, so stick to the end of the range.
if (!result.IsLineBreakpoint && BreakpointRangeIsSmaller(breakpointRange, request.Range))
if (!result.IsLineBreakpoint && BreakpointRangeIsSmaller(breakpointRange, range))
{
var secondResult = await breakpointService.ResolveBreakpointAsync(document, new TextSpan(span.End, length: 0), cancellationToken).ConfigureAwait(false);
if (secondResult is not null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CodeAnalysis.LanguageServer.Handler;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost.Handlers;

internal static class ValidateBreakableRange
{
public static async Task<LinePositionSpan?> GetBreakableRangeAsync(Document document, LinePositionSpan span, CancellationToken cancellationToken)
{
var range = ProtocolConversions.LinePositionToRange(span);
var result = await ValidateBreakableRangeHandler.GetBreakableRangeAsync(document, range, cancellationToken).ConfigureAwait(false);

if (result is null)
{
return null;
}

return ProtocolConversions.RangeToLinePositionSpan(result);
}
}
18 changes: 18 additions & 0 deletions src/Tools/ExternalAccess/Razor/ITextBufferExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;

namespace Microsoft.CodeAnalysis.ExternalAccess.Razor;

internal static class ITextBufferExtensions
{
public static bool TryGetTextDocument(this ITextBuffer textBuffer, [NotNullWhen(true)] out TextDocument? textDocument)
{
textDocument = textBuffer.CurrentSnapshot.AsText().GetOpenTextDocumentInCurrentContextWithChanges();
return textDocument is not null;
}
}
Loading