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

Do not return null responses from BlockStructureService and CodeStructureService #2148

Merged
merged 3 commits into from May 5, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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