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

F1 help for #if and #else refers to preprocessor symbols, not if_CSharpKeyword and else_CSharpkeyword #69192

Merged
merged 5 commits into from
Aug 1, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public override async Task<string> GetHelpTermAsync(Document document, TextSpan
if (TryGetTextForSpecialCharacters(token, out var text) ||
TryGetTextForContextualKeyword(token, out text) ||
TryGetTextForCombinationKeyword(token, out text) ||
TryGetTextForKeyword(token, out text) ||
TryGetTextForPreProcessor(token, out text) ||
TryGetTextForKeyword(token, out text) ||
Copy link
Member

Choose a reason for hiding this comment

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

have pp checks come first so we defer to the pp interepretation if inside a directive.

TryGetTextForOperator(token, document, out text) ||
TryGetTextForSymbol(token, semanticModel, document, cancellationToken, out text))
{
Expand Down Expand Up @@ -339,16 +339,28 @@ private static bool TryGetTextForPreProcessor(SyntaxToken token, [NotNullWhen(tr
{
var syntaxFacts = CSharpSyntaxFacts.Instance;

if (syntaxFacts.IsPreprocessorKeyword(token))
// Several keywords are both normal keywords and preprocessor keywords. So only consider this token a
// pp-keyword if we're actually in a directive.
var directive = token.GetAncestor<DirectiveTriviaSyntax>();
if (directive != null)
{
text = "#" + token.Text;
return true;
}
if (token.IsKind(SyntaxKind.DefaultKeyword) && token.Parent is LineDirectiveTriviaSyntax)
{
text = Keyword("defaultline");
return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

moved from the keyword handling case to the directive handling case.


if (token.IsKind(SyntaxKind.EndOfDirectiveToken) && token.GetAncestor<RegionDirectiveTriviaSyntax>() != null)
Copy link
Member

Choose a reason for hiding this comment

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

unified the behavior here so it works for any directive, not just regions.

{
text = "#region";
return true;
if (syntaxFacts.IsPreprocessorKeyword(token))
{
text = $"#{token.Text}";
return true;
}

if (token.IsKind(SyntaxKind.EndOfDirectiveToken))
{
text = $"#{directive.HashToken.GetNextToken(includeDirectives: true).Text}";
return true;
}
}

text = null;
Expand Down Expand Up @@ -472,12 +484,6 @@ private static bool TryGetTextForKeyword(SyntaxToken token, [NotNullWhen(true)]
text = Keyword("defaultcase");
return true;
}

if (token.Parent is LineDirectiveTriviaSyntax)
{
text = Keyword("defaultline");
return true;
}
}

if (token.IsKind(SyntaxKind.ClassKeyword) && token.Parent is ClassOrStructConstraintSyntax)
Expand Down
81 changes: 81 additions & 0 deletions src/VisualStudio/CSharp/Test/F1Help/F1HelpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ await TestAsync(
#endregion", "#region");
}

[Fact]
public async Task TestPreprocessor2()
{
await TestAsync(
@"#region[||]
#endregion", "#region");
}

[Fact]
public async Task TestConstructor()
{
Expand Down Expand Up @@ -1943,5 +1951,78 @@ await Test_KeywordAsync("""
1 >>[||]>= 2;
""", expectedText: ">>>=");
}

[Fact]
public async Task TestPreprocessorIf()
{
await TestAsync(
@"
#i[||]f ANY
#endif
", "#if");
}

[Fact]
public async Task TestPreprocessorIf2()
{
await TestAsync(
@"
#if ANY[||]
#endif
", "#if");
}

[Fact]
public async Task TestPreprocessorEndIf()
{
await TestAsync(
@"
#if ANY
#en[||]dif
", "#endif");
}

[Fact]
public async Task TestPreprocessorEndIf2()
{
await TestAsync(
@"
#if ANY
#endif[||]
", "#endif");
}

[Fact]
public async Task TestPreprocessorElse()
{
await TestAsync(
@"
#if ANY
#el[||]se
#endif
", "#else");
}

[Fact]
public async Task TestPreprocessorElse2()
{
await TestAsync(
@"
#if ANY
#else[||]
#endif
", "#else");
}

[Fact]
public async Task TestPreprocessorElIf()
{
await TestAsync(
@"
#if ANY
#el[||]if SOME
#endif
", "#elif");
}
}
}