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

Readonly members symbol display #34876

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public override void VisitProperty(IPropertySymbol symbol)
AddAccessibilityIfRequired(symbol);
AddMemberModifiersIfRequired(symbol);

if (symbol.ShouldDisplayReadOnly())
{
AddReadOnlyIfRequired();
}

if (format.MemberOptions.IncludesOption(SymbolDisplayMemberOptions.IncludeType))
{
if (symbol.ReturnsByRef)
Expand Down Expand Up @@ -168,6 +173,12 @@ public override void VisitEvent(IEventSymbol symbol)
AddAccessibilityIfRequired(symbol);
AddMemberModifiersIfRequired(symbol);

var accessor = (symbol.AddMethod ?? symbol.RemoveMethod) as MethodSymbol;
if (accessor?.IsDeclaredReadOnly == true)
{
AddReadOnlyIfRequired();
}

if (format.KindOptions.IncludesOption(SymbolDisplayKindOptions.IncludeMemberKeyword))
{
AddKeyword(SyntaxKind.EventKeyword);
Expand Down Expand Up @@ -257,6 +268,11 @@ public override void VisitMethod(IMethodSymbol symbol)
AddAccessibilityIfRequired(symbol);
AddMemberModifiersIfRequired(symbol);

if ((symbol as MethodSymbol).IsDeclaredReadOnly)
{
AddReadOnlyIfRequired();
}

if (format.MemberOptions.IncludesOption(SymbolDisplayMemberOptions.IncludeType))
{
switch (symbol.MethodKind)
Expand Down Expand Up @@ -753,7 +769,7 @@ private void AddParametersIfRequired(bool hasThisParameter, bool isVarargs, Immu
}
}

private void AddAccessor(ISymbol property, IMethodSymbol method, SyntaxKind keyword)
private void AddAccessor(IPropertySymbol property, IMethodSymbol method, SyntaxKind keyword)
{
if (method != null)
{
Expand All @@ -763,6 +779,11 @@ private void AddAccessor(ISymbol property, IMethodSymbol method, SyntaxKind keyw
AddAccessibility(method);
}

if (!property.ShouldDisplayReadOnly() && (method as MethodSymbol)?.IsDeclaredReadOnly == true)
{
AddReadOnlyIfRequired();
}

AddKeyword(keyword);
AddPunctuation(SyntaxKind.SemicolonToken);
}
Expand Down Expand Up @@ -829,6 +850,17 @@ private void AddRefReadonlyIfRequired()
}
}

private void AddReadOnlyIfRequired()
{
// 'readonly' in this context is effectively a 'ref' modifier
// because it affects whether the 'this' parameter is 'ref' or 'in'.
if (format.MemberOptions.IncludesOption(SymbolDisplayMemberOptions.IncludeRef))
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
{
AddKeyword(SyntaxKind.ReadOnlyKeyword);
AddSpace();
}
}

private void AddParameterRefKindIfRequired(RefKind refKind)
{
if (format.ParameterOptions.IncludesOption(SymbolDisplayParameterOptions.IncludeParamsRefOut))
Expand Down
11 changes: 11 additions & 0 deletions src/Compilers/CSharp/Portable/Symbols/PropertySymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,16 @@ public static bool HasRefOrOutParameter(this PropertySymbol property)
}
return false;
}

public static bool ShouldDisplayReadOnly(this IPropertySymbol property)
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
{
var getMethod = property.GetMethod as MethodSymbol;
var setMethod = property.SetMethod as MethodSymbol;

// If at least one accessor is present and all present accessors are readonly, the property should be marked readonly.
return (getMethod != null || setMethod != null) &&
RikkiGibson marked this conversation as resolved.
Show resolved Hide resolved
getMethod?.IsDeclaredReadOnly != false &&
setMethod?.IsDeclaredReadOnly != false;
}
}
}
Loading