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 4 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("Test 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,43 @@ 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'.
if (TryFindWhitespace(node.Content.ValueText, out var firstSpaceIndex))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (TryFindWhitespace(node.Content.ValueText, out var firstSpaceIndex))
var firstSpaceIndex = node.Content.Text.IndexOf(" ");

Don't use 'valueText'. it is interpreted characters, not the actual characters in the file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, there is so many of these - ValueText, Text, ToString, ToFullString, ...

Copy link
Member

Choose a reason for hiding this comment

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

yup. never use 'Value' if actually trying to get real positions infiles :)

{
var keywordSpan = new TextSpan(node.Content.SpanStart, firstSpaceIndex);
var stringLiteralSpan = new TextSpan(node.Content.SpanStart + firstSpaceIndex + 1, node.Content.Span.Length - firstSpaceIndex - 1);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var stringLiteralSpan = new TextSpan(node.Content.SpanStart + firstSpaceIndex + 1, node.Content.Span.Length - firstSpaceIndex - 1);
var stringLiteralSpan = TextSpan.FromBound(node.Content.SpanStart + firstSpaceIndex + 1, 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.

adding the +1 doesn't seem necessary. you're doing this to avoid classifying the spce. but you could have #:sdk whatever, and it would still classify the spaces after the first one. So just split on the space index and make the math easier :)


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

ClassifyDirectiveTrivia(node);

static bool TryFindWhitespace(string text, out int index)
{
for (var i = 0; i < text.Length; i++)
{
if (SyntaxFacts.IsWhitespace(text[i]))
{
index = i;
return true;
}
}

index = -1;
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
static bool TryFindWhitespace(string text, out int index)
{
for (var i = 0; i < text.Length; i++)
{
if (SyntaxFacts.IsWhitespace(text[i]))
{
index = i;
return true;
}
}
index = -1;
return false;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you suggesting to search just for space? Then having #:sdk\tTest wouldn't be colorized properly.

Copy link
Member

Choose a reason for hiding this comment

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

I'm totally ok with that.

Copy link
Member

Choose a reason for hiding this comment

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

or do IndexOfAny([' ', '\t']) if you do want to support that. i'm fine with both.

}

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