Skip to content

Commit

Permalink
Prevent LSP server from crashing on code actions (#5543)
Browse files Browse the repository at this point in the history
Fixes #5541

### Description
- Prevent LSP server from crashing on code actions

### How has this been tested?
- Added an LSP test

<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
  • Loading branch information
keyboardDrummer committed Jun 7, 2024
1 parent 430d485 commit fdf362f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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 @@ public class CodeActionTest : ClientBasedLanguageServerTest {
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 abstract class DiagnosticDafnyCodeActionProvider : DafnyCodeActionProvide
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

0 comments on commit fdf362f

Please sign in to comment.