提交 3fac3705 编写于 作者: D dustincampbell

[de minimis] Core IDE Support for VB string interpolation

The vast majority of this came from ADGreen (thanks Anthony!), which in turn had a great deal ported from the work I'd done in C#.  At this point, I'm not sure *who* wrote this code. It's shelvesets all the way down.

* Automatic brace completion
* Formatting
* Completion list
* Brace matching
* Classification (changeset 1390884)
上级 f5a4e033
......@@ -17,12 +17,12 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Classification
Public Function GetClassification(token As SyntaxToken) As String
If SyntaxFacts.IsKeywordKind(token.Kind) Then
Return ClassificationTypeNames.Keyword
ElseIf IsStringToken(token) Then
Return ClassificationTypeNames.StringLiteral
ElseIf SyntaxFacts.IsPunctuation(token.Kind) Then
Return ClassifyPunctuation(token)
ElseIf token.Kind = SyntaxKind.IdentifierToken Then
Return ClassifyIdentifierSyntax(token)
ElseIf token.IsKind(SyntaxKind.StringLiteralToken, SyntaxKind.CharacterLiteralToken) Then
Return ClassificationTypeNames.StringLiteral
ElseIf token.IsNumericLiteral() Then
Return ClassificationTypeNames.NumericLiteral
ElseIf token.Kind = SyntaxKind.XmlNameToken Then
......@@ -45,7 +45,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Classification
ElseIf token.IsKind(SyntaxKind.None, SyntaxKind.BadToken) Then
Return Nothing
Else
Return Contract.FailWithReturn(Of String)("Unhandled token kind: " & token.Kind)
Return Contract.FailWithReturn(Of String)("Unhandled token kind: " & token.Kind().ToString())
End If
End Function
......@@ -88,6 +88,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Classification
End If
End Function
Private Function IsStringToken(token As SyntaxToken) As Boolean
If token.IsKind(SyntaxKind.StringLiteralToken, SyntaxKind.CharacterLiteralToken, SyntaxKind.InterpolatedStringTextToken) Then
Return True
End If
Return token.IsKind(SyntaxKind.DollarSignDoubleQuoteToken, SyntaxKind.DoubleQuoteToken) AndAlso
token.Parent.IsKind(SyntaxKind.InterpolatedStringExpression)
End Function
Private Function ClassifyTypeDeclarationIdentifier(identifier As SyntaxToken) As String
Select Case identifier.Parent.Kind
Case SyntaxKind.ClassStatement
......
' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Runtime.CompilerServices
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.Extensions
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports Microsoft.CodeAnalysis.VisualBasic.Utilities
Imports System.Threading
......@@ -309,6 +305,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Extensions.ContextQuery
targetToken.IsChildToken(Of IfStatementSyntax)(Function(ifStatement) ifStatement.IfKeyword) OrElse
targetToken.IsChildToken(Of ElseIfStatementSyntax)(Function(elseIfStatement) elseIfStatement.ElseIfKeyword) OrElse
targetToken.IsChildToken(Of InferredFieldInitializerSyntax)(Function(inferredField) inferredField.KeyKeyword) OrElse
targetToken.IsChildToken(Of InterpolationSyntax)(Function(interpolation) interpolation.OpenBraceToken) OrElse
targetToken.IsChildToken(Of EqualsValueSyntax)(Function(initializer) initializer.EqualsToken) OrElse
targetToken.IsChildToken(Of JoinClauseSyntax)(Function(joinQuery) joinQuery.OnKeyword) OrElse
targetToken.IsChildSeparatorToken(Of JoinClauseSyntax, JoinConditionSyntax)(Function(joinQuery) joinQuery.JoinConditions) OrElse
......
......@@ -258,6 +258,40 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Formatting
Return CreateAdjustSpacesOperation(1, AdjustSpacesOption.ForceSpacesIfOnSingleLine)
End If
' No space after $" at the start of an interpolated string
If previousToken.Kind = SyntaxKind.DollarSignDoubleQuoteToken AndAlso previousToken.Parent.IsKind(SyntaxKind.InterpolatedStringExpression) Then
Return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpaces)
End If
' No space before " at the end of an interpolated string
If currentToken.Kind = SyntaxKind.DoubleQuoteToken AndAlso currentToken.Parent.IsKind(SyntaxKind.InterpolatedStringExpression) Then
Return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpaces)
End If
' No space before { Or after } in interpolations
If (currentToken.Kind = SyntaxKind.OpenBraceToken AndAlso currentToken.Parent.IsKind(SyntaxKind.Interpolation)) OrElse
(previousToken.Kind = SyntaxKind.CloseBraceToken AndAlso previousToken.Parent.IsKind(SyntaxKind.Interpolation)) Then
Return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpaces)
End If
' Preserve space after { Or before } in interpolations (i.e. between the braces And the expression)
If (previousToken.Kind = SyntaxKind.OpenBraceToken AndAlso previousToken.Parent.IsKind(SyntaxKind.Interpolation)) OrElse
(currentToken.Kind = SyntaxKind.CloseBraceToken AndAlso currentToken.Parent.IsKind(SyntaxKind.Interpolation)) Then
Return CreateAdjustSpacesOperation(0, AdjustSpacesOption.PreserveSpaces)
End If
' No space before Or after , in interpolation alignment clause
If (previousToken.Kind = SyntaxKind.CommaToken AndAlso previousToken.Parent.IsKind(SyntaxKind.InterpolationAlignmentClause)) OrElse
(currentToken.Kind = SyntaxKind.CommaToken AndAlso currentToken.Parent.IsKind(SyntaxKind.InterpolationAlignmentClause)) Then
Return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpaces)
End If
' No space before Or after : in interpolation format clause
If (previousToken.Kind = SyntaxKind.ColonToken AndAlso previousToken.Parent.IsKind(SyntaxKind.InterpolationFormatClause)) OrElse
(currentToken.Kind = SyntaxKind.ColonToken AndAlso currentToken.Parent.IsKind(SyntaxKind.InterpolationFormatClause)) Then
Return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpaces)
End If
' * }
' * )
' * ,
......@@ -353,7 +387,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Formatting
Return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine)
End If
Return nextFunc.Invoke()
End Function
End Class
......
......@@ -4018,5 +4018,123 @@ End Module
AssertFormatLf2CrLf(text.Value, expected.Value)
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Formatting)>
Public Sub InterpolatedString1()
Dim text = <Code>
Class C
Sub M()
Dim a = "World"
Dim b =$"Hello, {a}"
End Sub
End Class
</Code>
Dim expected = <Code>
Class C
Sub M()
Dim a = "World"
Dim b = $"Hello, {a}"
End Sub
End Class
</Code>
AssertFormatLf2CrLf(text.Value, expected.Value)
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Formatting)>
Public Sub InterpolatedString2()
Dim text = <Code>
Class C
Sub M()
Dim a = "Hello"
Dim b = "World"
Dim c = $"{a}, {b}"
End Sub
End Class
</Code>
Dim expected = <Code>
Class C
Sub M()
Dim a = "Hello"
Dim b = "World"
Dim c = $"{a}, {b}"
End Sub
End Class
</Code>
AssertFormatLf2CrLf(text.Value, expected.Value)
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Formatting)>
Public Sub InterpolatedString3()
Dim text = <Code>
Class C
Sub M()
Dim a = "World"
Dim b = $"Hello, { a }"
End Sub
End Class
</Code>
Dim expected = <Code>
Class C
Sub M()
Dim a = "World"
Dim b = $"Hello, { a }"
End Sub
End Class
</Code>
AssertFormatLf2CrLf(text.Value, expected.Value)
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Formatting)>
Public Sub InterpolatedString4()
Dim text = <Code>
Class C
Sub M()
Dim a = "Hello"
Dim b = "World"
Dim c = $"{ a }, { b }"
End Sub
End Class
</Code>
Dim expected = <Code>
Class C
Sub M()
Dim a = "Hello"
Dim b = "World"
Dim c = $"{ a }, { b }"
End Sub
End Class
</Code>
AssertFormatLf2CrLf(text.Value, expected.Value)
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Formatting)>
Public Sub InterpolatedString5()
Dim text = <Code>
Class C
Sub M()
Dim s = $"{42 , -4 :x}"
End Sub
End Class
</Code>
Dim expected = <Code>
Class C
Sub M()
Dim s = $"{42,-4:x}"
End Sub
End Class
</Code>
AssertFormatLf2CrLf(text.Value, expected.Value)
End Sub
End Class
End Namespace
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册