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

Escape record keywords #74227

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -127,7 +127,7 @@ private static bool IsEscapable(SymbolDisplayPartKind kind)
private static string EscapeIdentifier(string identifier)
{
var kind = SyntaxFacts.GetKeywordKind(identifier);
return kind == SyntaxKind.None
return kind == SyntaxKind.None && !StringComparer.Ordinal.Equals(identifier, "record")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong again about this, but this seems the next most logical place to always escape record identifiers

Copy link
Member

@333fred 333fred Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost. I think we likely want this, but we also want to only escape some kinds. Let's add a parallel to IsEscapable specifically for records; we only want to escape type names, as that's all we warn on. We can then pass that flag into EscapeIdentifier and adjust this second half of the condition appropriately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost. I think we likely want this, but we also want to only escape some kinds. Let's add a parallel to IsEscapable specifically for records; we only want to escape type names, as that's all we warn on. We can then pass that flag into EscapeIdentifier and adjust this second half of the condition appropriately.

Hi Fred, when you say 'we only want to escape type names': I can't see that in the list of SymbolDisplayPartKind enum. What about TypeParameterName?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have the symbol itself, just use that.

? identifier
: $"@{identifier}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,47 @@ class @true {
SymbolDisplayPartKind.Punctuation);
}

[Fact]
public void TestEscapeRecordKeywordIdentifiers()
{
var text = @"
class @record {
@record @struct(@record @true, bool @bool = true) { return @record; } }
";

Func<NamespaceSymbol, Symbol> findSymbol = global =>
global.GetTypeMembers("record", 0).Single().
GetMembers("struct").Single();

var format = new SymbolDisplayFormat(
memberOptions: SymbolDisplayMemberOptions.IncludeType | SymbolDisplayMemberOptions.IncludeParameters,
parameterOptions: SymbolDisplayParameterOptions.IncludeType | SymbolDisplayParameterOptions.IncludeName | SymbolDisplayParameterOptions.IncludeDefaultValue,
miscellaneousOptions: SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers | SymbolDisplayMiscellaneousOptions.UseSpecialTypes);

TestSymbolDescription(
text,
findSymbol,
format,
"@record @struct(@record @true, bool @bool = true)",
SymbolDisplayPartKind.ClassName,
SymbolDisplayPartKind.Space,
SymbolDisplayPartKind.MethodName, //@struct
SymbolDisplayPartKind.Punctuation,
SymbolDisplayPartKind.ClassName,
SymbolDisplayPartKind.Space,
SymbolDisplayPartKind.ParameterName, //@record
SymbolDisplayPartKind.Punctuation,
SymbolDisplayPartKind.Space,
SymbolDisplayPartKind.Keyword,
SymbolDisplayPartKind.Space,
SymbolDisplayPartKind.ParameterName, //@bool
SymbolDisplayPartKind.Space,
SymbolDisplayPartKind.Punctuation,
SymbolDisplayPartKind.Space,
SymbolDisplayPartKind.Keyword,
SymbolDisplayPartKind.Punctuation);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/74117")]
public void TestRecordStructName()
{
Expand Down
Loading