-
Notifications
You must be signed in to change notification settings - Fork 4k
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
LSP breakpoint placement #59428
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Features/LanguageServer/Protocol/Handler/Breakpoints/ValidateBreakableRangeHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
|
||
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); | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
...es/LanguageServer/ProtocolUnitTests/ValidateBreakableRange/ValidateBreakableRangeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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