Skip to content

Add syntax highlighting of ignored directives #78458

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

Merged
merged 6 commits into from
May 8, 2025
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 @@ -1542,6 +1542,85 @@ public async Task ShebangNotAsFirstCommentInScript(TestHost testHost)
await TestAsync(code, code, testHost, Options.Script, expected);
}

[Theory, CombinatorialData]
public async Task IgnoredDirective_01(TestHost testHost)
{
await TestAsync("""
#:unknown // comment
Console.Write();
""",
testHost,
PPKeyword("#"),
PPKeyword(":"),
PPKeyword("unknown"),
String(" // comment"),
Identifier("Console"),
Operators.Dot,
Identifier("Write"),
Punctuation.OpenParen,
Punctuation.CloseParen,
Punctuation.Semicolon);
}

[Theory, CombinatorialData]
public async Task IgnoredDirective_02(TestHost testHost)
{
await TestAsync("""
#:sdk Test 2.1.0
Console.Write();
""",
testHost,
PPKeyword("#"),
PPKeyword(":"),
PPKeyword("sdk"),
String(" Test 2.1.0"),
Identifier("Console"),
Operators.Dot,
Identifier("Write"),
Punctuation.OpenParen,
Punctuation.CloseParen,
Punctuation.Semicolon);
}

[Theory, CombinatorialData]
public async Task IgnoredDirective_03(TestHost testHost)
{
await TestAsync("""
#:no-space
Console.Write();
""",
testHost,
PPKeyword("#"),
PPKeyword(":"),
PPKeyword("no-space"),
Identifier("Console"),
Operators.Dot,
Identifier("Write"),
Punctuation.OpenParen,
Punctuation.CloseParen,
Punctuation.Semicolon);
}

[Theory, CombinatorialData]
public async Task IgnoredDirective_04(TestHost testHost)
{
await TestAsync($"""
#:sdk{'\t'}Test 2.1.0
Console.Write();
""",
testHost,
PPKeyword("#"),
PPKeyword(":"),
PPKeyword("sdk"),
String("\tTest 2.1.0"),
Identifier("Console"),
Operators.Dot,
Identifier("Write"),
Punctuation.OpenParen,
Punctuation.CloseParen,
Punctuation.Semicolon);
}

[Theory, CombinatorialData]
public async Task CommentAsMethodBodyContent(TestHost testHost)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ public override string ToString()
case "string - escape character":
return $"Escape(\"{Text}\")";

case "preprocessor keyword":
return $"""{nameof(FormattedClassifications.PPKeyword)}("{Text}")""";

default:
var trimmedClassification = ClassificationName;
if (trimmedClassification.EndsWith(" name"))
Expand Down
1 change: 1 addition & 0 deletions src/Workspaces/CSharp/Portable/Classification/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ private void ClassifyTrivia(SyntaxTrivia trivia, SyntaxTriviaList triviaList)
case SyntaxKind.PragmaChecksumDirectiveTrivia:
case SyntaxKind.ReferenceDirectiveTrivia:
case SyntaxKind.LoadDirectiveTrivia:
case SyntaxKind.IgnoredDirectiveTrivia:
case SyntaxKind.NullableDirectiveTrivia:
case SyntaxKind.BadDirectiveTrivia:
ClassifyPreprocessorDirective((DirectiveTriviaSyntax)trivia.GetStructure()!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Microsoft.CodeAnalysis.Classification;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.CodeAnalysis.CSharp.Classification;

Expand Down Expand Up @@ -69,6 +70,9 @@ private void ClassifyPreprocessorDirective(DirectiveTriviaSyntax node)
case SyntaxKind.LoadDirectiveTrivia:
ClassifyLoadDirective((LoadDirectiveTriviaSyntax)node);
break;
case SyntaxKind.IgnoredDirectiveTrivia:
ClassifyIgnoredDirective((IgnoredDirectiveTriviaSyntax)node);
break;
case SyntaxKind.NullableDirectiveTrivia:
ClassifyNullableDirective((NullableDirectiveTriviaSyntax)node);
break;
Expand Down Expand Up @@ -324,6 +328,29 @@ private void ClassifyLoadDirective(LoadDirectiveTriviaSyntax node)
ClassifyDirectiveTrivia(node);
}

private void ClassifyIgnoredDirective(IgnoredDirectiveTriviaSyntax node)
{
AddClassification(node.HashToken, ClassificationTypeNames.PreprocessorKeyword);
AddClassification(node.ColonToken, ClassificationTypeNames.PreprocessorKeyword);

// The first part (separated by whitespace) of content is a "keyword", e.g., 'sdk' in '#:sdk Test'.
// We only recognize some whitespace characters here for simplicity and performance.
if (node.Content.Text.IndexOfAny([' ', '\t']) is > 0 and var firstSpaceIndex)
{
var keywordSpan = new TextSpan(node.Content.SpanStart, firstSpaceIndex);
var stringLiteralSpan = TextSpan.FromBounds(node.Content.SpanStart + firstSpaceIndex, node.Content.FullSpan.End);
Copy link
Member

Choose a reason for hiding this comment

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

Was there a reason we're classifying the whitespace character?

Copy link
Member

Choose a reason for hiding this comment

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

Primarily simplicity. We didn't see any harm in it. The entire line is free form anyways. So there might be many spaces. Easiest to just classify these chunks.

Thoughts?


AddClassification(keywordSpan, ClassificationTypeNames.PreprocessorKeyword);
AddClassification(stringLiteralSpan, ClassificationTypeNames.StringLiteral);
}
else
{
AddClassification(node.Content, ClassificationTypeNames.PreprocessorKeyword);
}

ClassifyDirectiveTrivia(node);
}

private void ClassifyNullableDirective(NullableDirectiveTriviaSyntax node)
{
AddClassification(node.HashToken, ClassificationTypeNames.PreprocessorKeyword);
Expand Down
Loading