From ef581a4bf1d0aa93237d2b31ad926a4949aaab75 Mon Sep 17 00:00:00 2001 From: Brett Forsgren Date: Mon, 23 Mar 2015 10:47:28 -0700 Subject: [PATCH] enable quick-info for interpolated strings --- .../QuickInfo/SemanticQuickInfoSourceTests.cs | 28 +++++++++++++++++++ .../QuickInfo/SemanticQuickInfoSourceTests.vb | 16 +++++++++++ .../CSharpSyntaxFactsService.cs | 12 ++++++++ .../VisualBasicSyntaxFactsService.vb | 26 ++++++++++++++++- 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/EditorFeatures/CSharpTest/QuickInfo/SemanticQuickInfoSourceTests.cs b/src/EditorFeatures/CSharpTest/QuickInfo/SemanticQuickInfoSourceTests.cs index 00eebd96fdf..422cfe82156 100644 --- a/src/EditorFeatures/CSharpTest/QuickInfo/SemanticQuickInfoSourceTests.cs +++ b/src/EditorFeatures/CSharpTest/QuickInfo/SemanticQuickInfoSourceTests.cs @@ -1267,6 +1267,34 @@ public void TestStringLiteral() MainDescription("class System.String")); } + [WorkItem(1280, "https://github.com/dotnet/roslyn/issues/1280")] + [Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)] + public void TestVerbatimStringLiteral() + { + TestInMethod(@"string f = @""cat""$$", + MainDescription("class System.String")); + } + + [WorkItem(1280, "https://github.com/dotnet/roslyn/issues/1280")] + [Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)] + public void TestInterpolatedStringLiteral() + { + TestInMethod(@"string f = $""cat""$$", MainDescription("class System.String")); + TestInMethod(@"string f = $""c$$at""", MainDescription("class System.String")); + TestInMethod(@"string f = $""$$cat""", MainDescription("class System.String")); + TestInMethod(@"string f = $""cat {1$$ + 2} dog""", MainDescription("struct System.Int32")); + } + + [WorkItem(1280, "https://github.com/dotnet/roslyn/issues/1280")] + [Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)] + public void TestVerbatimInterpolatedStringLiteral() + { + TestInMethod(@"string f = $@""cat""$$", MainDescription("class System.String")); + TestInMethod(@"string f = $@""c$$at""", MainDescription("class System.String")); + TestInMethod(@"string f = $@""$$cat""", MainDescription("class System.String")); + TestInMethod(@"string f = $@""cat {1$$ + 2} dog""", MainDescription("struct System.Int32")); + } + [Fact, Trait(Traits.Feature, Traits.Features.QuickInfo)] public void TestCharLiteral() { diff --git a/src/EditorFeatures/VisualBasicTest/QuickInfo/SemanticQuickInfoSourceTests.vb b/src/EditorFeatures/VisualBasicTest/QuickInfo/SemanticQuickInfoSourceTests.vb index e3db65d2501..d9d90299fd2 100644 --- a/src/EditorFeatures/VisualBasicTest/QuickInfo/SemanticQuickInfoSourceTests.vb +++ b/src/EditorFeatures/VisualBasicTest/QuickInfo/SemanticQuickInfoSourceTests.vb @@ -149,6 +149,22 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.QuickInfo MainDescription("Class System.String")) End Sub + + + Public Sub TestStringLiteral() + TestInClass("Dim i = ""cat""$$", + MainDescription("Class System.String")) + End Sub + + + + Public Sub TestInterpolatedStringLiteral() + TestInClass("Dim i = $""cat""$$", MainDescription("Class System.String")) + TestInClass("Dim i = $""c$$at""", MainDescription("Class System.String")) + TestInClass("Dim i = $""$$cat""", MainDescription("Class System.String")) + TestInClass("Dim i = $""cat {1$$ + 2} dog""", MainDescription("Structure System.Int32")) + End Sub + Public Sub TestListOfString() TestInClass("Dim l As $$List(Of String)", diff --git a/src/Workspaces/CSharp/Portable/LanguageServices/CSharpSyntaxFactsService.cs b/src/Workspaces/CSharp/Portable/LanguageServices/CSharpSyntaxFactsService.cs index ce38388699e..3f96a3d5519 100644 --- a/src/Workspaces/CSharp/Portable/LanguageServices/CSharpSyntaxFactsService.cs +++ b/src/Workspaces/CSharp/Portable/LanguageServices/CSharpSyntaxFactsService.cs @@ -450,6 +450,10 @@ public bool IsLiteral(SyntaxToken token) case SyntaxKind.NullKeyword: case SyntaxKind.TrueKeyword: case SyntaxKind.FalseKeyword: + case SyntaxKind.InterpolatedStringStartToken: + case SyntaxKind.InterpolatedStringEndToken: + case SyntaxKind.InterpolatedVerbatimStringStartToken: + case SyntaxKind.InterpolatedStringTextToken: return true; } @@ -1272,6 +1276,14 @@ public SyntaxNode GetBindableParent(SyntaxToken token) } } + // The inside of an interpolated string is treated as its own token so we + // need to force navigation to the parent expression syntax. + if (node is InterpolatedStringTextSyntax && parent is InterpolatedStringExpressionSyntax) + { + node = parent; + break; + } + // If this node is not parented by a name, we're done. var name = parent as NameSyntax; if (name == null) diff --git a/src/Workspaces/VisualBasic/Portable/LanguageServices/VisualBasicSyntaxFactsService.vb b/src/Workspaces/VisualBasic/Portable/LanguageServices/VisualBasicSyntaxFactsService.vb index f38245dfb89..f06d814864c 100644 --- a/src/Workspaces/VisualBasic/Portable/LanguageServices/VisualBasicSyntaxFactsService.vb +++ b/src/Workspaces/VisualBasic/Portable/LanguageServices/VisualBasicSyntaxFactsService.vb @@ -386,7 +386,24 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End Function Public Function IsLiteral(token As SyntaxToken) As Boolean Implements ISyntaxFactsService.IsLiteral - Return SyntaxFacts.IsLiteralExpressionToken(CType(token.Kind, SyntaxKind)) + Select Case token.Kind() + Case _ + SyntaxKind.IntegerLiteralToken, + SyntaxKind.CharacterLiteralToken, + SyntaxKind.DecimalLiteralToken, + SyntaxKind.FloatingLiteralToken, + SyntaxKind.DateLiteralToken, + SyntaxKind.StringLiteralToken, + SyntaxKind.DollarSignDoubleQuoteToken, + SyntaxKind.DoubleQuoteToken, + SyntaxKind.InterpolatedStringTextToken, + SyntaxKind.TrueKeyword, + SyntaxKind.FalseKeyword, + SyntaxKind.NothingKeyword + Return True + End Select + + Return False End Function Public Function IsStringLiteral(token As SyntaxToken) As Boolean Implements ISyntaxFactsService.IsStringLiteral @@ -1028,6 +1045,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End If End If + ' The inside of an interpolated string is treated as its own token so we + ' need to force navigation to the parent expression syntax. + If TypeOf node Is InterpolatedStringTextSyntax AndAlso TypeOf parent Is InterpolatedStringExpressionSyntax Then + node = parent + Exit While + End If + ' If this node is not parented by a name, we're done. Dim name = TryCast(parent, NameSyntax) If name Is Nothing Then -- GitLab