Skip to content

Commit

Permalink
Classify function pointer punctuation (dotnet#47668)
Browse files Browse the repository at this point in the history
* Classify function pointers correctly

* Use a stable sort for classifications
  • Loading branch information
davidwengier authored Sep 15, 2020
1 parent 9737cff commit a2feb62
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1511,8 +1511,8 @@ await TestAsync(code,
testHost,
ParseOptions(Options.Regular),
Class("C"),
Static("M"),
Method("M"));
Method("M"),
Static("M"));
}

[Theory]
Expand Down Expand Up @@ -1811,8 +1811,8 @@ enum MyEnum
Namespace("MyNameSpace"),
Namespace("rabbit"),
Class("MyClass2"),
Static("method"),
Method("method"),
Static("method"),
Namespace("rabbit"),
Class("MyClass2"),
Event("myEvent"),
Expand All @@ -1822,12 +1822,12 @@ enum MyEnum
Struct("MyStruct"),
Namespace("rabbit"),
Class("MyClass2"),
Static("MyProp"),
Property("MyProp"),
Static("MyProp"),
Namespace("rabbit"),
Class("MyClass2"),
Static("myField"),
Field("myField"),
Static("myField"),
Namespace("rabbit"),
Class("MyClass2"),
Delegate("MyDelegate"),
Expand Down Expand Up @@ -4054,8 +4054,8 @@ void N()
}",
testHost,
Keyword("var"),
Static("Parse"),
Method("Parse"));
Method("Parse"),
Static("Parse"));
}

[Theory]
Expand Down Expand Up @@ -4087,8 +4087,8 @@ void N()
}",
testHost,
Keyword("_"),
Static("Parse"),
Method("Parse"));
Method("Parse"),
Static("Parse"));
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,8 @@ abstract class Goo : Bar
decimal goo4;
delegate void D();
delegate* managed<int, int> mgdfun;
delegate* unmanaged<int, int> unmgdfun;
double goo5;
Expand Down Expand Up @@ -2833,6 +2835,26 @@ interface Bar
Punctuation.OpenParen,
Punctuation.CloseParen,
Punctuation.Semicolon,
Keyword("delegate"),
Operators.Asterisk,
Keyword("managed"),
Punctuation.OpenAngle,
Keyword("int"),
Punctuation.Comma,
Keyword("int"),
Punctuation.CloseAngle,
Field("mgdfun"),
Punctuation.Semicolon,
Keyword("delegate"),
Operators.Asterisk,
Keyword("unmanaged"),
Punctuation.OpenAngle,
Keyword("int"),
Punctuation.Comma,
Keyword("int"),
Punctuation.CloseAngle,
Field("unmgdfun"),
Punctuation.Semicolon,
Keyword("double"),
Field("goo5"),
Punctuation.Semicolon,
Expand Down Expand Up @@ -5464,5 +5486,39 @@ void M<T>() where T : notnull { }
Punctuation.CloseCurly,
Punctuation.CloseCurly);
}

[Theory]
[CombinatorialData]
[WorkItem(45807, "https://github.com/dotnet/roslyn/issues/45807")]
public async Task FunctionPointer(TestHost testHost)
{
var code = @"
class C
{
delegate* unmanaged[Stdcall, SuppressGCTransition] <int, int> x;
}";

await TestAsync(code,
testHost,
Keyword("class"),
Class("C"),
Punctuation.OpenCurly,
Keyword("delegate"),
Operators.Asterisk,
Keyword("unmanaged"),
Punctuation.OpenBracket,
Identifier("Stdcall"),
Punctuation.Comma,
Identifier("SuppressGCTransition"),
Punctuation.CloseBracket,
Punctuation.OpenAngle,
Keyword("int"),
Punctuation.Comma,
Keyword("int"),
Punctuation.CloseAngle,
Field("x"),
Punctuation.Semicolon,
Punctuation.CloseCurly);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,8 @@ void Test()
ParseOptions(Options.Regular),
Keyword("static"),
Keyword("class"),
Static("ExtMethod"),
Class("ExtMethod"),
Static("ExtMethod"),
Punctuation.OpenCurly,
Keyword("public"),
Keyword("static"),
Expand All @@ -962,8 +962,8 @@ void Test()
Punctuation.OpenParen,
Punctuation.CloseParen,
Punctuation.OpenCurly,
Static("ExtMethod"),
Class("ExtMethod"),
Static("ExtMethod"),
Operators.Dot,
Method("TestMethod"),
Static("TestMethod"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ protected async Task TestAsync(
var span = new TextSpan(start, length);
var actual = await GetClassificationSpansAsync(allCode, span, parseOptions, testHost);

actual = actual.Sort((t1, t2) => t1.TextSpan.Start - t2.TextSpan.Start);
var actualOrdered = actual.OrderBy((t1, t2) => t1.TextSpan.Start - t2.TextSpan.Start);

var actualFormatted = actual.Select(a => new FormattedClassification(allCode.Substring(a.TextSpan.Start, a.TextSpan.Length), a.ClassificationType));
var actualFormatted = actualOrdered.Select(a => new FormattedClassification(allCode.Substring(a.TextSpan.Start, a.TextSpan.Length), a.ClassificationType));
AssertEx.Equal(expected, actualFormatted);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,13 @@ private static string GetClassificationForPunctuation(SyntaxToken token)
{
case SyntaxKind.LessThanToken:
case SyntaxKind.GreaterThanToken:
// the < and > tokens of a type parameter list should be classified as
// punctuation; otherwise, they're operators.
// the < and > tokens of a type parameter list or function pointer parameter
// list should be classified as punctuation; otherwise, they're operators.
if (token.Parent != null)
{
if (token.Parent.Kind() == SyntaxKind.TypeParameterList ||
token.Parent.Kind() == SyntaxKind.TypeArgumentList)
token.Parent.Kind() == SyntaxKind.TypeArgumentList ||
token.Parent.Kind() == SyntaxKind.FunctionPointerParameterList)
{
return ClassificationTypeNames.Punctuation;
}
Expand Down

0 comments on commit a2feb62

Please sign in to comment.