Skip to content

Commit

Permalink
Clean up Find References with record/delegate instead of tuple/action
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutch committed Apr 17, 2024
1 parent 2c199f6 commit 834ae72
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ async Task FindTargetDefinitions (string targetName, ITextBuffer buffer)
await searchCtx.OnCompletedAsync ();
}

delegate MSBuildReferenceCollector ReferenceCollectorFactory (MSBuildDocument doc, ITextSource textSource, ILogger logger, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult);
delegate MSBuildReferenceCollector ReferenceCollectorFactory (MSBuildDocument doc, ITextSource textSource, ILogger logger, FindReferencesReporter reportResult);

/// <remarks>
/// this does not need a cancellation token because it creates UI that handles cancellation
Expand Down Expand Up @@ -312,7 +312,7 @@ await ParallelAsync.ForEach (jobs, Environment.ProcessorCount, async (job, token
var progress = Interlocked.Increment (ref jobsCompleted);
await searchCtx.ReportProgressAsync (progress, jobs.Count);
void ReportResult ((int Offset, int Length, ReferenceUsage Usage) result)
void ReportResult (FindReferencesResult result)
{
var line = job.TextSource.Snapshot.GetLineFromPosition (result.Offset);
var col = result.Offset - line.Start.Position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace MonoDevelop.MSBuild.Tests
[TestFixture]
class MSBuildFindReferencesTests : MSBuildDocumentTest
{
List<(int Offset, int Length, ReferenceUsage Usage)> FindReferences (string docString, MSBuildReferenceKind kind, object reference, MSBuildSchema schema = null, [CallerMemberName] string testMethodName = null)
List<FindReferencesResult> FindReferences (string docString, MSBuildReferenceKind kind, object reference, MSBuildSchema schema = null, [CallerMemberName] string testMethodName = null)
{
var textDoc = new StringTextSource (docString);

Expand All @@ -40,7 +40,7 @@ class MSBuildFindReferencesTests : MSBuildDocumentTest

var functionTypeProvider = new RoslynFunctionTypeProvider (null, parseContext.Logger);

var results = new List<(int Offset, int Length, ReferenceUsage Usage)> ();
var results = new List<FindReferencesResult> ();
var collector = MSBuildReferenceCollector.Create (
doc, textDoc,logger,
new MSBuildResolver.MSBuildMutableResolveResult {
Expand All @@ -54,14 +54,14 @@ class MSBuildFindReferencesTests : MSBuildDocumentTest

void AssertLocations (
string doc, string expectedValue,
List<(int Offset, int Length, ReferenceUsage Usage)> actual,
List<FindReferencesResult> actual,
params (int Offset, int Length, ReferenceUsage Usage)[] expected
)
=> AssertLocations (doc, actual, expected.Select (e => (expectedValue, e.Offset, e.Length, e.Usage)).ToArray ());

void AssertLocations (
string doc,
List<(int Offset, int Length, ReferenceUsage Usage)> actual,
List<FindReferencesResult> actual,
params (string expectedValue, int Offset, int Length, ReferenceUsage Usage)[] expected)
{
if (actual.Count != expected.Length) {
Expand Down
35 changes: 19 additions & 16 deletions MonoDevelop.MSBuild/Language/MSBuildReferenceCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ public enum ReferenceUsage
Write = 1 << 2,
}

record struct FindReferencesResult (int Offset, int Length, ReferenceUsage Usage);

delegate void FindReferencesReporter (FindReferencesResult result);
abstract class MSBuildReferenceCollector : MSBuildDocumentVisitor
{
readonly Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult;
readonly FindReferencesReporter reportResult;

protected MSBuildReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string name, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
protected MSBuildReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string name, FindReferencesReporter reportResult)
: base (document, textSource, logger)
{
if (string.IsNullOrEmpty (name)) {
Expand Down Expand Up @@ -68,8 +71,8 @@ protected bool IsPureMatch (ExpressionText t, out int offset, out int length, bo
return false;
}

protected void AddResult (int offset, int length, ReferenceUsage usage) => reportResult ((offset, length, usage));
protected void AddResult (TextSpan span, ReferenceUsage usage) => reportResult ((span.Start, span.Length, usage));
protected void AddResult (int offset, int length, ReferenceUsage usage) => reportResult (new FindReferencesResult (offset, length, usage));
protected void AddResult (TextSpan span, ReferenceUsage usage) => reportResult (new FindReferencesResult (span.Start, span.Length, usage));

public static bool CanCreate (MSBuildResolveResult rr)
{
Expand All @@ -95,7 +98,7 @@ public static bool CanCreate (MSBuildResolveResult rr)
return false;
}

public static MSBuildReferenceCollector Create (MSBuildDocument document, ITextSource textSource, ILogger logger, MSBuildResolveResult rr, IFunctionTypeProvider functionTypeProvider, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public static MSBuildReferenceCollector Create (MSBuildDocument document, ITextSource textSource, ILogger logger, MSBuildResolveResult rr, IFunctionTypeProvider functionTypeProvider, FindReferencesReporter reportResult)
{
switch (rr.ReferenceKind) {
case MSBuildReferenceKind.Property:
Expand Down Expand Up @@ -131,7 +134,7 @@ public static MSBuildReferenceCollector Create (MSBuildDocument document, ITextS

class MSBuildItemReferenceCollector : MSBuildReferenceCollector
{
public MSBuildItemReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string itemName, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildItemReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string itemName, FindReferencesReporter reportResult)
: base (document, textSource, logger, itemName, reportResult) { }

protected override void VisitResolvedElement (XElement element, MSBuildElementSyntax elementSyntax, ITypedSymbol elementSymbol)
Expand Down Expand Up @@ -176,7 +179,7 @@ protected override void VisitValue (

class MSBuildPropertyReferenceCollector : MSBuildReferenceCollector
{
public MSBuildPropertyReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string propertyName, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildPropertyReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string propertyName, FindReferencesReporter reportResult)
: base (document, textSource, logger, propertyName, reportResult) { }


Expand Down Expand Up @@ -217,7 +220,7 @@ protected override void VisitValue (

class MSBuildTaskReferenceCollector : MSBuildReferenceCollector
{
public MSBuildTaskReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string taskName, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildTaskReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string taskName, FindReferencesReporter reportResult)
: base (document, textSource, logger, taskName, reportResult) { }

protected override void VisitResolvedElement (XElement element, MSBuildElementSyntax elementSyntax, ITypedSymbol elementSymbol)
Expand Down Expand Up @@ -247,7 +250,7 @@ class MSBuildMetadataReferenceCollector : MSBuildReferenceCollector
{
readonly string itemName;

public MSBuildMetadataReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string itemName, string metadataName, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildMetadataReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string itemName, string metadataName, FindReferencesReporter reportResult)
: base (document, textSource, logger, metadataName, reportResult)
{
this.itemName = itemName;
Expand Down Expand Up @@ -342,7 +345,7 @@ void CheckMatch (ExpressionText t)

class MSBuildTargetReferenceCollector : MSBuildReferenceCollector
{
public MSBuildTargetReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string targetName, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildTargetReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string targetName, FindReferencesReporter reportResult)
: base (document, textSource, logger, targetName, reportResult) { }

protected override void VisitValue (
Expand Down Expand Up @@ -382,7 +385,7 @@ void CheckMatch (ExpressionText node, bool isDeclaration)

class MSBuildTargetDefinitionCollector : MSBuildReferenceCollector
{
public MSBuildTargetDefinitionCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string targetName, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildTargetDefinitionCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string targetName, FindReferencesReporter reportResult)
: base (document, textSource, logger, targetName, reportResult) { }

protected override void VisitResolvedElement (XElement element, MSBuildElementSyntax elementSyntax, ITypedSymbol elementSymbol)
Expand All @@ -401,7 +404,7 @@ class MSBuildStaticPropertyFunctionReferenceCollector : MSBuildReferenceCollecto
{
readonly string className;

public MSBuildStaticPropertyFunctionReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string className, string functionName, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildStaticPropertyFunctionReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string className, string functionName, FindReferencesReporter reportResult)
: base (document, textSource, logger, MSBuildPropertyFunctionReferenceCollector.StripGetPrefix (functionName), reportResult)
{
this.className = className;
Expand Down Expand Up @@ -447,7 +450,7 @@ internal static string StripGetPrefix (string name)
public MSBuildPropertyFunctionReferenceCollector (
MSBuildDocument document, ITextSource textSource, ILogger logger,
MSBuildValueKind valueKind, string functionName, IFunctionTypeProvider functionTypeProvider,
Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
FindReferencesReporter reportResult)
: base (document, textSource, logger, StripGetPrefix(functionName), reportResult)
{
if (valueKind == MSBuildValueKind.Unknown) {
Expand Down Expand Up @@ -487,7 +490,7 @@ protected override void VisitValue (

class MSBuildItemFunctionReferenceCollector : MSBuildReferenceCollector
{
public MSBuildItemFunctionReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string functionName, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildItemFunctionReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string functionName, FindReferencesReporter reportResult)
: base (document, textSource, logger, functionName, reportResult) { }

protected override void VisitValue (
Expand All @@ -510,7 +513,7 @@ protected override void VisitValue (

class MSBuildClassReferenceCollector : MSBuildReferenceCollector
{
public MSBuildClassReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string className, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildClassReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string className, FindReferencesReporter reportResult)
: base (document, textSource, logger, className, reportResult) { }

protected override void VisitValue (
Expand All @@ -533,7 +536,7 @@ protected override void VisitValue (

class MSBuildEnumReferenceCollector : MSBuildReferenceCollector
{
public MSBuildEnumReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string enumName, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildEnumReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, string enumName, FindReferencesReporter reportResult)
: base (document, textSource, logger, enumName, reportResult) { }

protected override void VisitValue (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MSBuildKnownValueReferenceCollector : MSBuildReferenceCollector
readonly CustomTypeInfo? customType;
readonly bool isWarningCode;

public MSBuildKnownValueReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, ITypedSymbol knownValue, Action<(int Offset, int Length, ReferenceUsage Usage)> reportResult)
public MSBuildKnownValueReferenceCollector (MSBuildDocument document, ITextSource textSource, ILogger logger, ITypedSymbol knownValue, FindReferencesReporter reportResult)
: base (document, textSource, logger, knownValue.Name, reportResult)
{
kind = knownValue.ValueKind;
Expand Down

0 comments on commit 834ae72

Please sign in to comment.