Skip to content
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
25 changes: 25 additions & 0 deletions Source/DafnyLanguageServer.Test/Lookup/DocumentSymbolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@
namespace Microsoft.Dafny.LanguageServer.IntegrationTest.Lookup {
public class DocumentSymbolTest : ClientBasedLanguageServerTest {

[Fact]
public async Task BadReturnSyntax() {
var source = @"method FindZero(a: array<int>) returns (index) // note lack of type annotation";
var documentItem = CreateAndOpenTestDocument(source);
var symbols = (await RequestDocumentSymbol(documentItem)).ToList();
CheckValidSymbols(symbols);
}

private void CheckValidSymbols(IEnumerable<DocumentSymbol> symbols) {
foreach (var symbol in symbols) {
CheckValidRange(symbol.SelectionRange);
CheckValidRange(symbol.Range);
CheckValidSymbols(symbol.Children);
}
}

private void CheckValidRange(Range range) {
ValidPosition(range.Start);
ValidPosition(range.End);
}

private void ValidPosition(Position position) {
Assert.True(position.Line >= 0 && position.Character >= 0);
}

[Fact]
public async Task ExportImport() {
var source = @"
Expand Down
12 changes: 12 additions & 0 deletions Source/DafnyLanguageServer/Handlers/DafnyDocumentSymbolHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Dafny.LanguageServer.Language;
using Range = OmniSharp.Extensions.LanguageServer.Protocol.Models.Range;

namespace Microsoft.Dafny.LanguageServer.Handlers {
/// <summary>
Expand Down Expand Up @@ -60,6 +61,9 @@ private IEnumerable<DocumentSymbol> FromSymbol(ISymbol symbol) {
}

var range = symbol.ToLspRange();
if (!IsValidRange(range)) {
return [];
}
return new DocumentSymbol[] {
new() {
Children = children,
Expand All @@ -71,5 +75,13 @@ private IEnumerable<DocumentSymbol> FromSymbol(ISymbol symbol) {
}
};
}

private bool IsValidRange(Range range) {
return IsValidPosition(range.Start) && IsValidPosition(range.End);
}

private bool IsValidPosition(Position position) {
return position.Line >= 0 && position.Character >= 0;
}
}
}
1 change: 1 addition & 0 deletions docs/dev/news/6299.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix LSP bug that cause IDE exceptions during invalid parse states