-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from 4 commits
9e5ebf0
81ff498
a250fb0
0d911c9
6be720a
b43fd2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ | |||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
using Microsoft.CodeAnalysis.Classification; | ||||||||||||||||||||||||||||||||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||||||||||||||||||||||||||||||||
using Microsoft.CodeAnalysis.Text; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
namespace Microsoft.CodeAnalysis.CSharp.Classification; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
@@ -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; | ||||||||||||||||||||||||||||||||
|
@@ -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)) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
var keywordSpan = new TextSpan(node.Content.SpanStart, firstSpaceIndex); | ||||||||||||||||||||||||||||||||
var stringLiteralSpan = new TextSpan(node.Content.SpanStart + firstSpaceIndex + 1, node.Content.Span.Length - firstSpaceIndex - 1); | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding the |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
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; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you suggesting to search just for space? Then having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm totally ok with that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or do |
||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
private void ClassifyNullableDirective(NullableDirectiveTriviaSyntax node) | ||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||
AddClassification(node.HashToken, ClassificationTypeNames.PreprocessorKeyword); | ||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use 'valueText'. it is interpreted characters, not the actual characters in the file.
There was a problem hiding this comment.
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, ...
There was a problem hiding this comment.
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 :)