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

Prevent LSP server from crashing on code actions #5543

Merged
merged 4 commits into from
Jun 7, 2024
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
13 changes: 13 additions & 0 deletions Source/DafnyLanguageServer.Test/CodeActions/CodeActionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ private async Task<CodeAction> RequestResolveCodeAction(CodeAction codeAction) {
return await client.ResolveCodeAction(codeAction, CancellationToken);
}

[Fact]
public async Task MethodKeywordCodeAction() {
await SetUp(o => o.Set(CommonOptionBag.RelaxDefiniteAssignment, true));

MarkupTestFile.GetPositionsAndAnnotatedRanges(@"
method><".TrimStart(), out var source, out var positions,
out var ranges);
var documentItem = await CreateOpenAndWaitForResolve(source);
var position = positions[0];
var completionList = await RequestCodeActionAsync(documentItem, new Range(position, position));
Assert.Empty(completionList);
}

[Fact]
public async Task GitIssue4401CorrectInsertionPlace() {
await TestCodeAction(@"
Expand Down
6 changes: 6 additions & 0 deletions Source/DafnyLanguageServer/Plugins/DafnyCodeActionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public override IEnumerable<DafnyCodeAction> GetDafnyCodeActions(IDafnyCodeActio
public RangeToken? FindTokenRangeFromLspRange(IDafnyCodeActionInput input, Range range) {
var start = range.Start;
var startNode = input.Program.FindNode<Node>(input.Uri.ToUri(), start.ToDafnyPosition());
if (startNode == null) {
// A program should have FileModuleDefinition nodes whose ranges span the entire contents of files,
// But currently those nodes are missing
return null;
}

var startToken = startNode.CoveredTokens.FirstOrDefault(t => t.line - 1 == start.Line && t.col - 1 == start.Character);
if (startToken == null) {
logger.LogError($"Could not find starting token for position {start} in node {startNode}");
Expand Down