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

Conversation

jjonescz
Copy link
Member

@jjonescz jjonescz commented May 6, 2025

No description provided.

@jjonescz jjonescz added the Feature - Run File #: and #! directives and file-based C# programs label May 6, 2025
@jjonescz jjonescz requested a review from a team May 6, 2025 10:59
@ghost ghost added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels May 6, 2025
@jjonescz jjonescz marked this pull request as ready for review May 6, 2025 13:52
@jjonescz jjonescz requested a review from a team as a code owner May 6, 2025 13:52
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 :)

Comment on lines 351 to 365

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.

if (TryFindWhitespace(node.Content.ValueText, out var firstSpaceIndex))
{
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 :)

@jjonescz jjonescz requested a review from CyrusNajmabadi May 7, 2025 14:23
@jjonescz jjonescz merged commit d7903f9 into dotnet:main May 8, 2025
25 checks passed
@jjonescz jjonescz deleted the ignored-directives-color branch May 8, 2025 10:15
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone May 8, 2025
333fred added a commit to 333fred/roslyn that referenced this pull request May 8, 2025
* upstream/main: (415 commits)
  Use lazy initialization for members in CodeFixService (dotnet#78484)
  Targeted perf changes to CommandLineParser (dotnet#78446)
  Exclude VS.ExternalAPIs.Roslyn.Package from source-build
  Add syntax highlighting of ignored directives (dotnet#78458)
  Fix unexpected conditional state in nullable analysis of conditional access (dotnet#78439)
  Update RoslynAnalyzers to use Roslyns version
  Fix source-build by renaming Assets folder to assets
  Unlist M.CA.AnalyzerUtilities as an expected DevDivInsertion dependency
  Use a project reference for M.CA.AnalyzerUtilities
  Rectify status of dictionary expressions (dotnet#78497)
  Remove unused field
  Remove unused field
  Remove using
  Fix check
  Update eng/config/PublishData.json
  Make internal
  Remove now unused methods from IWorkspaceProjectContext (dotnet#78479)
  Reduce allocations during SourceGeneration (dotnet#78403)
  Update Roslyn.sln
  Merge EditorFeatures.Wpf entirely into EditorFeatures
  ...
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Feature - Run File #: and #! directives and file-based C# programs untriaged Issues and PRs which have not yet been triaged by a lead VSCode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants