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

LSP breakpoint placement #59428

Merged
merged 5 commits into from
Feb 25, 2022
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
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<VisualStudioEditorNewPackagesVersion>17.0.487</VisualStudioEditorNewPackagesVersion>
<ILAsmPackageVersion>5.0.0-alpha1.19409.1</ILAsmPackageVersion>
<ILDAsmPackageVersion>5.0.0-preview.1.20112.8</ILDAsmPackageVersion>
<MicrosoftVisualStudioLanguageServerProtocolPackagesVersion>17.1.11</MicrosoftVisualStudioLanguageServerProtocolPackagesVersion>
<MicrosoftVisualStudioLanguageServerProtocolPackagesVersion>17.2.3</MicrosoftVisualStudioLanguageServerProtocolPackagesVersion>
<MicrosoftVisualStudioShellPackagesVersion>17.0.31723.112</MicrosoftVisualStudioShellPackagesVersion>
<RefOnlyMicrosoftBuildPackagesVersion>16.5.0</RefOnlyMicrosoftBuildPackagesVersion>
<!-- The version of Roslyn we build Source Generators against that are built in this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private static VSServerCapabilities GetVSServerCapabilities()
vsServerCapabilities.OnAutoInsertProvider = new VSInternalDocumentOnAutoInsertOptions { TriggerCharacters = new[] { "'", "/", "\n" } };
vsServerCapabilities.DocumentHighlightProvider = true;
vsServerCapabilities.ProjectContextProvider = true;
vsServerCapabilities.BreakableRangeProvider = true;

// Diagnostic requests are only supported from PullDiagnosticsInProcLanguageClient.
vsServerCapabilities.SupportsDiagnosticRequests = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host.Mef;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.CodeAnalysis.Debugging;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.LanguageServer.Handler
{
[ExportRoslynLanguagesLspRequestHandlerProvider(typeof(ValidateBreakableRangeHandler)), Shared]
[Method(LSP.VSInternalMethods.TextDocumentValidateBreakableRangeName)]
internal sealed class ValidateBreakableRangeHandler : AbstractStatelessRequestHandler<LSP.VSInternalValidateBreakableRangeParams, LSP.Range?>
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public ValidateBreakableRangeHandler()
{
}

public override bool MutatesSolutionState => false;
public override bool RequiresLSPSolution => true;

public override LSP.TextDocumentIdentifier? GetTextDocumentIdentifier(LSP.VSInternalValidateBreakableRangeParams request)
=> request.TextDocument;

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

var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var span = ProtocolConversions.RangeToTextSpan(request.Range, text);
var breakpointService = document.Project.LanguageServices.GetRequiredService<IBreakpointResolutionService>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this break for non-c#/vb @dibarbet shoudl we make this GetService and then return null if it's not available?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think its necessary - we should just not advertise the capability for other scenarios


var result = await breakpointService.ResolveBreakpointAsync(document, span, cancellationToken).ConfigureAwait(false);
if (result == null)
{
return null;
}

// zero-width range means line breakpoint:
var breakpointSpan = result.IsLineBreakpoint ? new TextSpan(span.Start, length: 0) : result.TextSpan;

return ProtocolConversions.TextSpanToRange(breakpointSpan, text);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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.

#nullable enable

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Roslyn.Test.Utilities;
using Xunit;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.ValidateBreakableRange
{
public class ValidateBreakableRange : AbstractLanguageServerProtocolTests
{
[Fact]
public async Task SimpleStatement()
{
var markup =
@"class A
{
void M()
{
{|caret:|}M();
}
}";
using var testLspServer = await CreateTestLspServerAsync(markup);

var caret = testLspServer.GetLocations("caret").Single();

var expected = new LSP.Range()
{
Start = caret.Range.Start,
End = new LSP.Position(caret.Range.Start.Line, caret.Range.Start.Character + "M();".Length),
};

var result = await RunAsync(testLspServer, caret);
AssertJsonEquals(expected, result);
}

[Fact]
public async Task LineBreakpoint()
{
var markup =
@"class A
{
void M()
{
#if FALSE
{|caret:|}M();
#endif
}
}";
using var testLspServer = await CreateTestLspServerAsync(markup);

var caret = testLspServer.GetLocations("caret").Single();

var expected = new LSP.Range()
{
Start = caret.Range.Start,
End = caret.Range.Start,
};

var result = await RunAsync(testLspServer, caret);
AssertJsonEquals(expected, result);
}

[Fact]
public async Task NoBreakpointSpan()
{
var markup =
@"class A
{
void M()
{
{|caret:|}const int a = 1;
}
}";
using var testLspServer = await CreateTestLspServerAsync(markup);

var caret = testLspServer.GetLocations("caret").Single();

var result = await RunAsync(testLspServer, caret);
Assert.Null(result);
}

private static async Task<LSP.Range?> RunAsync(TestLspServer testLspServer, LSP.Location caret)
{
return await testLspServer.ExecuteRequestAsync<LSP.VSInternalValidateBreakableRangeParams, LSP.Range?>(
LSP.VSInternalMethods.TextDocumentValidateBreakableRangeName,
new LSP.VSInternalValidateBreakableRangeParams()
{
TextDocument = new LSP.TextDocumentIdentifier { Uri = caret.Uri },
Range = caret.Range
},
CancellationToken.None);
}
}
}