Skip to content

Commit

Permalink
Merge pull request #2148 from OmniSharp/bugfix/null-response
Browse files Browse the repository at this point in the history
Do not return null responses from BlockStructureService and CodeStructureService
  • Loading branch information
filipw committed May 5, 2021
2 parents d52c76d + b41a1bd commit 469b8aa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
Expand Up @@ -56,7 +56,7 @@ public async Task<BlockStructureResponse> Handle(BlockStructureRequest request)
var document = await _workspace.GetDocumentFromFullProjectModelAsync(request.FileName);
if (document == null)
{
return null;
return new BlockStructureResponse { Spans = Array.Empty<CodeFoldingBlock>() };
}

var text = await document.GetTextAsync();
Expand Down
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Threading.Tasks;
Expand Down Expand Up @@ -36,7 +37,7 @@ public async Task<CodeStructureResponse> Handle(CodeStructureRequest request)
var document = await _workspace.GetDocumentFromFullProjectModelAsync(request.FileName);
if (document == null)
{
return null;
return new CodeStructureResponse { Elements = Array.Empty<CodeElement>() };
}

var elements = await GetCodeElementsAsync(document);
Expand Down
16 changes: 16 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/BlockStructureFacts.cs
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using OmniSharp.Models.V2;
Expand Down Expand Up @@ -41,6 +42,21 @@ public async Task UsesRoslynBlockStructureService()
Assert.Equal(expected, lineSpans);
}

[Fact]
public async Task NonExistingFile()
{
var request = new BlockStructureRequest
{
FileName = $"{Guid.NewGuid().ToString("N")}.cs"
};

var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
var response = await requestHandler.Handle(request);

Assert.NotNull(response);
Assert.Empty(response.Spans);
}

[Fact]
public async Task SupportsRegionBlocks()
{
Expand Down
18 changes: 17 additions & 1 deletion tests/OmniSharp.Roslyn.CSharp.Tests/CodeStructureFacts.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using OmniSharp.Mef;
Expand Down Expand Up @@ -413,6 +414,21 @@ class C
AssertRange(elementC.Children[15], testFile.Content, "nameThis", "name");
}

[Fact]
public async Task NonExistingFile()
{
var request = new CodeStructureRequest
{
FileName = $"{Guid.NewGuid().ToString("N")}.cs"
};

var requestHandler = GetRequestHandler(SharedOmniSharpTestHost);
var response = await requestHandler.Handle(request);

Assert.NotNull(response);
Assert.Empty(response.Elements);
}

private static void AssertRange(CodeElement elementC, TestContent content, string contentSpanName, string elementRangeName)
{
var span = Assert.Single(content.GetSpans(contentSpanName));
Expand Down

0 comments on commit 469b8aa

Please sign in to comment.