PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/VisualStudio/VisualBasic/Impl/CodeModel/VisualBasicCodeModelService_Prototype.vb

https://gitlab.com/sharadag/Roslyn
Visual Basic | 261 lines | 205 code | 50 blank | 6 comment | 0 complexity | 9590a297920d00284cad0845f98a4957 MD5 | raw file
  1. ' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. Imports System.Collections.Immutable
  3. Imports System.Text
  4. Imports Microsoft.CodeAnalysis
  5. Imports Microsoft.CodeAnalysis.VisualBasic
  6. Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
  7. Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
  8. Imports Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel
  9. Imports Microsoft.VisualStudio.LanguageServices.Implementation.Utilities
  10. Namespace Microsoft.VisualStudio.LanguageServices.VisualBasic.CodeModel
  11. Partial Friend Class VisualBasicCodeModelService
  12. Private Shared ReadOnly s_prototypeFullNameFormat As SymbolDisplayFormat =
  13. New SymbolDisplayFormat(
  14. memberOptions:=SymbolDisplayMemberOptions.IncludeContainingType,
  15. typeQualificationStyle:=SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
  16. genericsOptions:=SymbolDisplayGenericsOptions.IncludeTypeParameters,
  17. miscellaneousOptions:=SymbolDisplayMiscellaneousOptions.ExpandNullable Or SymbolDisplayMiscellaneousOptions.UseSpecialTypes)
  18. Private Shared ReadOnly s_prototypeTypeNameFormat As SymbolDisplayFormat =
  19. New SymbolDisplayFormat(
  20. memberOptions:=SymbolDisplayMemberOptions.IncludeContainingType,
  21. typeQualificationStyle:=SymbolDisplayTypeQualificationStyle.NameAndContainingTypes,
  22. genericsOptions:=SymbolDisplayGenericsOptions.IncludeTypeParameters,
  23. miscellaneousOptions:=SymbolDisplayMiscellaneousOptions.ExpandNullable Or SymbolDisplayMiscellaneousOptions.UseSpecialTypes)
  24. Public Overrides Function GetPrototype(node As SyntaxNode, symbol As ISymbol, flags As PrototypeFlags) As String
  25. Debug.Assert(symbol IsNot Nothing)
  26. Select Case symbol.Kind
  27. Case SymbolKind.Field
  28. Return GetVariablePrototype(DirectCast(symbol, IFieldSymbol), flags)
  29. Case SymbolKind.Property
  30. Dim propertySymbol = DirectCast(symbol, IPropertySymbol)
  31. Return GetFunctionPrototype(propertySymbol, propertySymbol.Parameters, flags)
  32. Case SymbolKind.Method
  33. Dim methodSymbol = DirectCast(symbol, IMethodSymbol)
  34. Return GetFunctionPrototype(methodSymbol, methodSymbol.Parameters, flags)
  35. Case SymbolKind.NamedType
  36. Dim namedTypeSymbol = DirectCast(symbol, INamedTypeSymbol)
  37. If namedTypeSymbol.TypeKind = TypeKind.Delegate Then
  38. Return GetFunctionPrototype(namedTypeSymbol, namedTypeSymbol.DelegateInvokeMethod.Parameters, flags)
  39. End If
  40. Case SymbolKind.Event
  41. Dim eventSymbol = DirectCast(symbol, IEventSymbol)
  42. Dim eventType = TryCast(eventSymbol.Type, INamedTypeSymbol)
  43. Dim parameters = If(eventType IsNot Nothing AndAlso eventType.DelegateInvokeMethod IsNot Nothing,
  44. eventType.DelegateInvokeMethod.Parameters,
  45. ImmutableArray.Create(Of IParameterSymbol)())
  46. Return GetEventPrototype(eventSymbol, parameters, flags)
  47. End Select
  48. Debug.Fail(String.Format("Invalid symbol kind: {0}", symbol.Kind))
  49. Throw Exceptions.ThrowEUnexpected()
  50. End Function
  51. Private Function GetEventPrototype(symbol As IEventSymbol, parameters As ImmutableArray(Of IParameterSymbol), flags As PrototypeFlags) As String
  52. If Not AreValidFunctionPrototypeFlags(flags) Then
  53. Throw Exceptions.ThrowEInvalidArg()
  54. End If
  55. If flags = PrototypeFlags.Signature Then
  56. Return symbol.GetDocumentationCommentId()
  57. End If
  58. Dim builder = New StringBuilder()
  59. AppendName(builder, symbol, flags)
  60. AppendParameters(builder, parameters, flags)
  61. AppendType(builder, symbol.Type, flags)
  62. Return builder.ToString()
  63. End Function
  64. Private Function GetFunctionPrototype(symbol As ISymbol, parameters As ImmutableArray(Of IParameterSymbol), flags As PrototypeFlags) As String
  65. If Not AreValidFunctionPrototypeFlags(flags) Then
  66. Throw Exceptions.ThrowEInvalidArg()
  67. End If
  68. If flags = PrototypeFlags.Signature Then
  69. Return symbol.GetDocumentationCommentId()
  70. End If
  71. Dim builder = New StringBuilder()
  72. AppendName(builder, symbol, flags)
  73. AppendParameters(builder, parameters, flags)
  74. If symbol.Kind = SymbolKind.Method Then
  75. Dim methodSymbol = DirectCast(symbol, IMethodSymbol)
  76. If methodSymbol.MethodKind = MethodKind.Ordinary AndAlso
  77. methodSymbol.ReturnType IsNot Nothing AndAlso
  78. Not methodSymbol.ReturnType.SpecialType = SpecialType.System_Void Then
  79. AppendType(builder, methodSymbol.ReturnType, flags)
  80. End If
  81. End If
  82. Return builder.ToString()
  83. End Function
  84. Private Function GetVariablePrototype(symbol As IFieldSymbol, flags As PrototypeFlags) As String
  85. If Not AreValidVariablePrototypeFlags(flags) Then
  86. Throw Exceptions.ThrowEInvalidArg()
  87. End If
  88. If flags = PrototypeFlags.Signature Then
  89. Return symbol.GetDocumentationCommentId()
  90. End If
  91. Dim builder = New StringBuilder()
  92. AppendAccessibility(builder, symbol.DeclaredAccessibility)
  93. builder.Append(" "c)
  94. AppendName(builder, symbol, flags)
  95. AppendType(builder, symbol.Type, flags)
  96. If (flags And PrototypeFlags.Initializer) <> 0 Then
  97. Dim modifiedIdentifier = TryCast(symbol.DeclaringSyntaxReferences.FirstOrDefault().GetSyntax(), ModifiedIdentifierSyntax)
  98. If modifiedIdentifier IsNot Nothing Then
  99. Dim variableDeclarator = TryCast(modifiedIdentifier.Parent, VariableDeclaratorSyntax)
  100. If variableDeclarator IsNot Nothing AndAlso
  101. variableDeclarator.Initializer IsNot Nothing AndAlso
  102. variableDeclarator.Initializer.Value IsNot Nothing AndAlso
  103. Not variableDeclarator.Initializer.Value.IsMissing Then
  104. builder.Append(" = ")
  105. builder.Append(variableDeclarator.Initializer.Value.ToString())
  106. End If
  107. End If
  108. End If
  109. Return builder.ToString()
  110. End Function
  111. Private Shared Sub AppendAccessibility(builder As StringBuilder, accessibility As Accessibility)
  112. Select accessibility
  113. Case Accessibility.Private
  114. builder.Append("Private")
  115. Case Accessibility.Protected
  116. builder.Append("Protected")
  117. Case Accessibility.Friend
  118. builder.Append("Friend")
  119. Case Accessibility.ProtectedOrFriend
  120. builder.Append("Protected Friend")
  121. Case Accessibility.Public
  122. builder.Append("Public")
  123. End Select
  124. End Sub
  125. Private Shared Sub AppendName(builder As StringBuilder, symbol As ISymbol, flags As PrototypeFlags)
  126. Select Case flags And PrototypeFlags.NameMask
  127. Case PrototypeFlags.FullName
  128. builder.Append(symbol.ToDisplayString(s_prototypeFullNameFormat))
  129. Case PrototypeFlags.TypeName
  130. builder.Append(symbol.ToDisplayString(s_prototypeTypeNameFormat))
  131. Case PrototypeFlags.BaseName
  132. builder.Append(symbol.Name)
  133. End Select
  134. End Sub
  135. Private Sub AppendParameters(builder As StringBuilder, parameters As ImmutableArray(Of IParameterSymbol), flags As PrototypeFlags)
  136. builder.Append("("c)
  137. If (flags And PrototypeFlags.ParametersMask) <> 0 Then
  138. Dim first = True
  139. For Each parameter In parameters
  140. If Not first Then
  141. builder.Append(", ")
  142. End If
  143. If (flags And PrototypeFlags.ParameterNames) <> 0 Then
  144. builder.Append(parameter.Name)
  145. builder.Append(" "c)
  146. If (flags And PrototypeFlags.ParameterTypes) <> 0 Then
  147. builder.Append("As ")
  148. End If
  149. End If
  150. If (flags And PrototypeFlags.ParameterTypes) <> 0 Then
  151. builder.Append(parameter.Type.ToDisplayString(s_prototypeFullNameFormat))
  152. End If
  153. If (flags And PrototypeFlags.ParameterDefaultValues) <> 0 Then
  154. Dim parameterNode = TryCast(parameter.DeclaringSyntaxReferences(0).GetSyntax(), ParameterSyntax)
  155. If parameterNode IsNot Nothing AndAlso
  156. parameterNode.Default IsNot Nothing AndAlso
  157. parameterNode.Default.Value IsNot Nothing AndAlso
  158. Not parameterNode.Default.Value.IsMissing Then
  159. builder.Append(" = ")
  160. builder.Append(parameterNode.Default.Value.ToString())
  161. End If
  162. End If
  163. Next
  164. End If
  165. builder.Append(")"c)
  166. End Sub
  167. Private Shared Sub AppendType(builder As StringBuilder, type As ITypeSymbol, flags As PrototypeFlags)
  168. If (flags And PrototypeFlags.Type) <> 0 Then
  169. builder.Append(" As ")
  170. builder.Append(type.ToDisplayString(s_prototypeFullNameFormat))
  171. End If
  172. End Sub
  173. Private Shared Function AreValidEventPrototypeFlags(flags As PrototypeFlags) As Boolean
  174. ' Unsupported flags for events
  175. If (flags And PrototypeFlags.Initializer) <> 0 Then
  176. Return False
  177. End If
  178. Return AreValidPrototypeFlags(flags)
  179. End Function
  180. Private Shared Function AreValidFunctionPrototypeFlags(flags As PrototypeFlags) As Boolean
  181. ' Unsupported flags for functions
  182. If (flags And PrototypeFlags.Initializer) <> 0 Then
  183. Return False
  184. End If
  185. Return AreValidPrototypeFlags(flags)
  186. End Function
  187. Private Shared Function AreValidVariablePrototypeFlags(flags As PrototypeFlags) As Boolean
  188. ' Unsupported flags for variables
  189. If (flags And PrototypeFlags.ParametersMask) <> 0 Then
  190. Return False
  191. End If
  192. Return AreValidPrototypeFlags(flags)
  193. End Function
  194. Private Shared Function AreValidPrototypeFlags(ByRef flags As PrototypeFlags) As Boolean
  195. ' Signature cannot be combined with anything else
  196. If (flags And PrototypeFlags.Signature) <> 0 AndAlso flags <> PrototypeFlags.Signature Then
  197. Return False
  198. End If
  199. ' Only one name flag can be specified
  200. Dim nameFlags = flags And PrototypeFlags.NameMask
  201. If nameFlags <> 0 Then
  202. If Not nameFlags = PrototypeFlags.FullName AndAlso
  203. Not nameFlags = PrototypeFlags.TypeName AndAlso
  204. Not nameFlags = PrototypeFlags.NoName Then
  205. Return False
  206. End If
  207. End If
  208. Return True
  209. End Function
  210. End Class
  211. End Namespace