Skip to content
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

Handle enhanced #line directive in DirectiveTriviaSyntax.DirectiveNameToken #54636

Merged
merged 1 commit into from
Jul 7, 2021
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 @@ -44,6 +44,8 @@ public SyntaxToken DirectiveNameToken
return ((UndefDirectiveTriviaSyntax)this).UndefKeyword;
case SyntaxKind.LineDirectiveTrivia:
return ((LineDirectiveTriviaSyntax)this).LineKeyword;
case SyntaxKind.LineSpanDirectiveTrivia:
return ((LineSpanDirectiveTriviaSyntax)this).LineKeyword;
Copy link
Member

@jcouv jcouv Jul 6, 2021

Choose a reason for hiding this comment

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

Consider adding a direct compiler API test, since public API
Never mind, it's there :-)

case SyntaxKind.PragmaWarningDirectiveTrivia:
return ((PragmaWarningDirectiveTriviaSyntax)this).PragmaKeyword;
case SyntaxKind.PragmaChecksumDirectiveTrivia:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ private static ImmutableArray<SyntaxNode> GetStatementsAndExpressionBodies(Synta

private static ImmutableArray<string> GetLineMappings(SyntaxTree tree)
{
var directives = tree.GetRoot().DescendantNodesAndSelf(descendIntoTrivia: true).OfType<DirectiveTriviaSyntax>();
foreach (var directive in directives)
{
Assert.NotEqual(SyntaxKind.None, directive.DirectiveNameToken.Kind());
}
return tree.GetLineMappings().Select(mapping => mapping.ToString()!).ToImmutableArray();
}

Expand Down Expand Up @@ -545,15 +550,18 @@ static void Main()
B();
#line (1, 1) - (1, 100) ""b.txt""
C();
#line (2, 3) - (2, 4) 9 ""a.txt""
D();
}
static void A() { }
static void B() { }
static void C() { }
static void D() { }
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this cause the test to exhibit the bug fixed in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, this test change is unrelated to the bug fix, and is only here to test a more interesting case than the line above.

}".NormalizeLineEndings();
var verifier = CompileAndVerify(source, options: TestOptions.DebugDll);
verifier.VerifyIL("Program.Main", sequencePoints: "Program.Main", expectedIL:
@"{
// Code size 20 (0x14)
// Code size 26 (0x1a)
.maxstack 0
-IL_0000: nop
-IL_0001: call ""void Program.A()""
Expand All @@ -562,7 +570,9 @@ .maxstack 0
IL_000c: nop
-IL_000d: call ""void Program.C()""
IL_0012: nop
-IL_0013: ret
-IL_0013: call ""void Program.D()""
IL_0018: nop
-IL_0019: ret
}");
verifier.VerifyPdb("Program.Main", expectedPdb:
@"<symbols>
Expand All @@ -583,11 +593,13 @@ .maxstack 0
<entry offset=""0x1"" startLine=""3"" startColumn=""4"" endLine=""3"" endColumn=""8"" document=""2"" />
<entry offset=""0x7"" startLine=""8"" startColumn=""9"" endLine=""8"" endColumn=""13"" document=""1"" />
<entry offset=""0xd"" startLine=""1"" startColumn=""9"" endLine=""1"" endColumn=""13"" document=""3"" />
<entry offset=""0x13"" startLine=""2"" startColumn=""5"" endLine=""2"" endColumn=""6"" document=""3"" />
<entry offset=""0x13"" startLine=""2"" startColumn=""3"" endLine=""2"" endColumn=""5"" document=""2"" />
<entry offset=""0x19"" startLine=""3"" startColumn=""5"" endLine=""3"" endColumn=""6"" document=""2"" />
</sequencePoints>
</method>
</methods>
</symbols>");
</symbols>
");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ public void UnexpectedToken_09()
}

[Fact]
public void UnexpectedToken_11()
public void UnexpectedToken_10()
{
string source = @"#line (1, 2) - (3, 4) file.cs";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public async Task SnippetsNotInPreProcessorContextDirectiveNameAlreadyTyped()
public async Task ShowRegionSnippetWithHashRTyped()
=> await VerifyItemExistsAsync(@"#r$$", MockSnippetInfoService.PreProcessorSnippetShortcut.Substring(1), sourceCodeKind: SourceCodeKind.Regular);

[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task SnippetsInLineSpanDirective()
=> await VerifyItemIsAbsentAsync(@"#line (1, 2) - (3, 4) $$", MockSnippetInfoService.PreProcessorSnippetShortcut, sourceCodeKind: SourceCodeKind.Regular);

[WorkItem(968256, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/968256")]
[Fact, Trait(Traits.Feature, Traits.Features.Completion)]
public async Task ShowSnippetsFromOtherContext()
Expand Down
21 changes: 21 additions & 0 deletions src/Workspaces/CSharpTest/Formatting/FormattingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10078,5 +10078,26 @@ await AssertFormatAsync(
object F = Func((A,B)((A,B)t)=>t);
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task LineSpanDirective()
{
var optionSet = new OptionsCollection(LanguageNames.CSharp) { { FormattingOptions2.UseTabs, true } };
await AssertFormatAsync(
@"class Program
{
static void Main()
{
#line (1, 1) - (1, 100) 5 ""a.razor""
}
}",
@"class Program
{
static void Main()
{
#line (1,1)-(1,100) 5 ""a.razor""
}
}", changedOptionSet: optionSet);
}
}
}