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

Add binding.GetParametersGroups API [WIP] #406

Open
wants to merge 5 commits into
base: net223
Choose a base branch
from
Open
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
44 changes: 33 additions & 11 deletions ReSharper.FSharp/src/FSharp.Psi/src/Resolve/FSharpParameterUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static IDeclaredElement GetFieldDeclaredElement([NotNull] IReference ref
return field?.GetDeclaredElement(referenceOwner.GetPsiModule(), referenceOwner);
}

public static IReadOnlyList<IReadOnlyList<string>> GetParametersGroupNames(ITreeNode node) =>
public static IReadOnlyList<IReadOnlyList<(string name, ITreeNode node)>> GetParametersGroupNames(ITreeNode node) =>
(node switch
{
IBinding binding => binding.Expression is ILambdaExpr lambda
Expand All @@ -90,12 +90,12 @@ public static IReadOnlyList<IReadOnlyList<string>> GetParametersGroupNames(ITree
IMemberSignature memberSignature => GetParameterNames(((IMemberSignatureOrDeclaration)memberSignature)
.ReturnTypeInfo.ReturnType),

IUnionCaseDeclaration ucDecl => new[] { ucDecl.Fields.Select(t => t.SourceName) },
IUnionCaseDeclaration ucDecl => new[] { ucDecl.Fields.Select(t => (t.SourceName, (ITreeNode)t)) },

IFSharpTypeDeclaration { TypeRepresentation: IDelegateRepresentation repr } =>
GetParameterNames(repr.TypeUsage),

_ => EmptyList<string[]>.Enumerable
_ => EmptyList<IEnumerable<(string, ITreeNode)>>.Enumerable
})
.Select(t => t.ToIReadOnlyList())
.ToIReadOnlyList();
Expand All @@ -108,7 +108,7 @@ public static IReadOnlyList<IReadOnlyList<string>> GetParametersGroupNames(ITree
_ => false
};

private static IEnumerable<IEnumerable<string>> GetLambdaArgs(ILambdaExpr expr)
private static IEnumerable<IEnumerable<(string, ITreeNode)>> GetLambdaArgs(ILambdaExpr expr)
{
var lambdaParams = expr.Patterns;
var parameters = lambdaParams.Select(GetParameterNames);
Expand All @@ -117,38 +117,60 @@ private static IEnumerable<IEnumerable<string>> GetLambdaArgs(ILambdaExpr expr)
return parameters;
}

public static IEnumerable<string> GetParameterNames(this IFSharpPattern pattern)
public static IEnumerable<(string, ITreeNode)> GetParameterNames(this IFSharpPattern pattern)
{
IEnumerable<string> GetParameterNamesInternal(IFSharpPattern pat, bool isTopLevelParameter)
IEnumerable<(string, ITreeNode)> GetParameterNamesInternal(IFSharpPattern pat, bool isTopLevelParameter)
{
pat = pat.IgnoreInnerParens();
return pat switch
{
ILocalReferencePat local => new[] { local.SourceName },
ILocalReferencePat local => new[] { (local.SourceName, (ITreeNode)local) },
IOptionalValPat opt => GetParameterNamesInternal(opt.Pattern, isTopLevelParameter),
ITypedPat typed => GetParameterNamesInternal(typed.Pattern, false),
IAttribPat attrib => GetParameterNamesInternal(attrib.Pattern, false),
IAsPat asPat => GetParameterNamesInternal(asPat.RightPattern, false),
ITuplePat tuplePat when isTopLevelParameter =>
tuplePat.PatternsEnumerable.SelectMany(t => GetParameterNamesInternal(t, false)),
_ => new[] { SharedImplUtil.MISSING_DECLARATION_NAME }
var x => new[] { (SharedImplUtil.MISSING_DECLARATION_NAME, (ITreeNode)x) }
};
}

return GetParameterNamesInternal(pattern, true);
}

public static IEnumerable<IEnumerable<string>> GetParameterNames(this ITypeUsage pattern) =>
public static IEnumerable<IEnumerable<(string, ITreeNode)>> GetParameterNames(this ITypeUsage pattern) =>
pattern switch
{
IParenTypeUsage parenUsage => GetParameterNames(parenUsage.InnerTypeUsage),
IConstrainedTypeUsage constrained => GetParameterNames(constrained.TypeUsage),
IParameterSignatureTypeUsage local =>
new[] { new[] { local.Identifier?.Name ?? SharedImplUtil.MISSING_DECLARATION_NAME } },
new[]
{
new[]
{
local.Identifier is { } identifier
? (identifier.Name, (ITreeNode)local)
: (SharedImplUtil.MISSING_DECLARATION_NAME, null)
}
},
IFunctionTypeUsage funPat =>
GetParameterNames(funPat.ArgumentTypeUsage).Union(GetParameterNames(funPat.ReturnTypeUsage)),
ITupleTypeUsage tuplePat => tuplePat.Items.SelectMany(GetParameterNames),
_ => EmptyList<IEnumerable<string>>.Enumerable
_ => EmptyList<IEnumerable<(string, ITreeNode)>>.Enumerable
};

public static IReadOnlyList<IReadOnlyList<(string name, ITreeNode node)>> GetParametersGroups(this IBinding binding)
{
var parameters = binding.ParameterPatterns.Select(GetParameterNames);
var bodyExpr = binding.Expression;

while (bodyExpr.IgnoreInnerParens() is ILambdaExpr lambdaExpr)
{
parameters = parameters.Union(lambdaExpr.Patterns.Select(GetParameterNames));
bodyExpr = lambdaExpr.Expression;
}

return parameters.Select(t => t.ToIReadOnlyList()).ToIReadOnlyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static (string, int caretOffset) GetDocTemplate(IDocCommentBlock docComme

foreach (var parameter in FSharpParameterUtil.GetParametersGroupNames(docCommentBlock.Parent)
.SelectMany(t => t)
.Select(t => t.name)
.Where(t => t != SharedImplUtil.MISSING_DECLARATION_NAME))
text.Append($"{lineEnding}{linePrefix}<param name=\"{parameter}\"></param>");

Expand Down