Skip to content

Commit

Permalink
Merge pull request #1823 from juliuszint/feature-configurablesymbolfi…
Browse files Browse the repository at this point in the history
…lter

Allow client to specify symbol filter for FindSymbols Endpoint.
  • Loading branch information
filipw authored Jul 10, 2020
2 parents d1eca15 + d38d82a commit 05a63c0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public class FindSymbolsRequest : IRequest
public string Filter { get; set; }
public int? MinFilterLength { get; set; }
public int? MaxItemsToReturn { get; set; }
public OmniSharpSymbolFilter? SymbolFilter { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace OmniSharp.Models.FindSymbols
{
[Flags]
public enum OmniSharpSymbolFilter
{
None = 0,
Namespace = 1,
Type = 2,
Member = 4,
TypeAndMember = Type | Member,
All = Type | Member | Namespace,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OmniSharp.Models;
using OmniSharp.Models.FindSymbols;
using static OmniSharp.Cake.Constants;
using SymbolFilter = Microsoft.CodeAnalysis.SymbolFilter;

namespace OmniSharp.Cake.Services.RequestHandlers.Navigation
{
Expand All @@ -26,8 +27,9 @@ public override Task<QuickFixResponse> Handle(FindSymbolsRequest request)
return Task.FromResult(new QuickFixResponse { QuickFixes = Array.Empty<QuickFix>() });
}

var symbolFilter = (SymbolFilter)(request?.SymbolFilter ?? OmniSharpSymbolFilter.TypeAndMember);
int maxItemsToReturn = (request?.MaxItemsToReturn).GetValueOrDefault();
return Workspace.CurrentSolution.FindSymbols(request?.Filter, LanguageNames.Cake, maxItemsToReturn);
return Workspace.CurrentSolution.FindSymbols(request?.Filter, LanguageNames.Cake, maxItemsToReturn, symbolFilter);
}

protected override Task<QuickFixResponse> TranslateResponse(QuickFixResponse response, FindSymbolsRequest request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public async Task<QuickFixResponse> Handle(FindSymbolsRequest request = null)
return new QuickFixResponse { QuickFixes = Array.Empty<QuickFix>() };
}

var symbolFilter = (SymbolFilter)(request?.SymbolFilter ?? OmniSharpSymbolFilter.TypeAndMember);
int maxItemsToReturn = (request?.MaxItemsToReturn).GetValueOrDefault();
var csprojSymbols = await _workspace.CurrentSolution.FindSymbols(request?.Filter, ".csproj", maxItemsToReturn);
var csxSymbols = await _workspace.CurrentSolution.FindSymbols(request?.Filter, ".csx", maxItemsToReturn);
var csprojSymbols = await _workspace.CurrentSolution.FindSymbols(request?.Filter, ".csproj", maxItemsToReturn, symbolFilter);
var csxSymbols = await _workspace.CurrentSolution.FindSymbols(request?.Filter, ".csx", maxItemsToReturn, symbolFilter);
return new QuickFixResponse()
{
QuickFixes = csprojSymbols.QuickFixes.Concat(csxSymbols.QuickFixes)
Expand Down
7 changes: 4 additions & 3 deletions src/OmniSharp.Roslyn/Extensions/SolutionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public static class SolutionExtensions
public static async Task<QuickFixResponse> FindSymbols(this Solution solution,
string pattern,
string projectFileExtension,
int maxItemsToReturn)
int maxItemsToReturn,
SymbolFilter symbolFilter = SymbolFilter.TypeAndMember)
{
var projects = solution.Projects.Where(p =>
(p.FilePath?.EndsWith(projectFileExtension, StringComparison.OrdinalIgnoreCase) ?? false) ||
Expand All @@ -24,8 +25,8 @@ public static async Task<QuickFixResponse> FindSymbols(this Solution solution,
foreach (var project in projects)
{
var symbols = !string.IsNullOrEmpty(pattern) ?
await SymbolFinder.FindSourceDeclarationsWithPatternAsync(project, pattern, SymbolFilter.TypeAndMember) :
await SymbolFinder.FindSourceDeclarationsAsync(project, candidate => true, SymbolFilter.TypeAndMember);
await SymbolFinder.FindSourceDeclarationsWithPatternAsync(project, pattern, symbolFilter) :
await SymbolFinder.FindSourceDeclarationsAsync(project, candidate => true, symbolFilter);

foreach (var symbol in symbols)
{
Expand Down
43 changes: 36 additions & 7 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FindSymbolsFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private string NestedMethod() {}
}";

code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "meth", minFilterLength: null, maxItemsToReturn: null);
var usages = await FindSymbolsWithFilterAsync(code, filename, "meth", minFilterLength: null, maxItemsToReturn: null, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);

var expected = new[]
Expand All @@ -252,7 +252,7 @@ public interface IConfigurationOptions { }
public class ConfigurationOptions : IConfigurationOptions { }";

code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "opti", minFilterLength: 0, maxItemsToReturn: 0);
var usages = await FindSymbolsWithFilterAsync(code, filename, "opti", minFilterLength: 0, maxItemsToReturn: 0, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);

var expected = new[]
Expand All @@ -265,6 +265,34 @@ public class ConfigurationOptions : IConfigurationOptions { }";
Assert.Equal(expected, symbols);
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
public async Task Only_returns_symbols_matching_symbolfilter(string filename)
{
string code = @"
public class Options {
public bool Property { get; }
}
public interface IConfigurationOptions {
public bool InterfaceProperty { get; set; }
}
public enum WeekDays { Monday }";

code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, string.Empty, minFilterLength: 0, maxItemsToReturn: 0, OmniSharpSymbolFilter.Type);
var symbols = usages.QuickFixes.Select(q => q.Text);

var expected = new[]
{
"Options",
"IConfigurationOptions",
"WeekDays"
};

Assert.Equal(expected, symbols);
}

[Theory]
[InlineData("dummy.cs")]
[InlineData("dummy.csx")]
Expand All @@ -274,7 +302,7 @@ public async Task no_symbols_returned_when_filter_too_short(string filename)
public class Options {}";

code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "op", minFilterLength: 3, maxItemsToReturn: 0);
var usages = await FindSymbolsWithFilterAsync(code, filename, "op", minFilterLength: 3, maxItemsToReturn: 0, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);

Assert.Empty(symbols);
Expand All @@ -291,7 +319,7 @@ public class Options2 {}
public class Options3 {}";

code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "op", minFilterLength: 0, maxItemsToReturn: 2);
var usages = await FindSymbolsWithFilterAsync(code, filename, "op", minFilterLength: 0, maxItemsToReturn: 2, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);

Assert.Equal(2, symbols.Count());
Expand All @@ -308,7 +336,7 @@ public class CoolProjectManager {}
public class ProbabilityManager {}";

code = WrapInNamespaceIfNeeded(code, filename);
var usages = await FindSymbolsWithFilterAsync(code, filename, "ProjMana", minFilterLength: 0, maxItemsToReturn: 0);
var usages = await FindSymbolsWithFilterAsync(code, filename, "ProjMana", minFilterLength: 0, maxItemsToReturn: 0, symbolFilter: null);
var symbols = usages.QuickFixes.Select(q => q.Text);
Assert.Contains("ProjectManager", symbols);
Assert.Contains("CoolProjectManager", symbols);
Expand All @@ -324,7 +352,7 @@ private async Task<QuickFixResponse> FindSymbolsAsync(string code, string filena
return await requestHandler.Handle(null);
}

private async Task<QuickFixResponse> FindSymbolsWithFilterAsync(string code, string filename, string filter, int? minFilterLength, int? maxItemsToReturn)
private async Task<QuickFixResponse> FindSymbolsWithFilterAsync(string code, string filename, string filter, int? minFilterLength, int? maxItemsToReturn, OmniSharpSymbolFilter? symbolFilter)
{
var testFile = new TestFile(filename, code);
SharedOmniSharpTestHost.AddFilesToWorkspace(testFile);
Expand All @@ -334,7 +362,8 @@ private async Task<QuickFixResponse> FindSymbolsWithFilterAsync(string code, str
{
Filter = filter,
MinFilterLength = minFilterLength,
MaxItemsToReturn = maxItemsToReturn
MaxItemsToReturn = maxItemsToReturn,
SymbolFilter = symbolFilter
});
}

Expand Down

0 comments on commit 05a63c0

Please sign in to comment.