Roslyn: VB: Intellisense should NOT strip square brackets when autocompleting "Enum" type name.

Created on 22 Jul 2018  路  3Comments  路  Source: dotnet/roslyn

Version Used:

Visual Studio Community 2017 v15.5.6

Steps to Reproduce:

Let's say you want to write code similar to:

    Dim enumVal As AcceptRejectRule

    Dim foo = [Enum].TryParse(Of AcceptRejectRule)("foo", enumVal)

Notice that the type System.Enum uses a reserved keyword (Enum) in VB so you have to escape it by using square brackets to get [Enum]. The trouble is, you can't type it and use Intellisense because when you press Tab to auto-complete, the square brackets are stripped.

You start off like this by typing the square brackets:

vs_reserved_kwd_1

Next you start typing the letters of the keyword Enum and Intellisense kicks in offering to autocomplete (note the square brackets are still there):

vs_reserved_kwd_2

Now if you press Tab to accept the suggestion, it does complete the letters enu to the keyword Enum, but it also strips the square brackets:

vs_reserved_kwd_3

With the square brackets gone, you now have a syntax error, and attempts to press . so you can type TryParse, don't work (the dot just appears but Intellisense is NOT activated):

vs_reserved_kwd_4

If you manually type [Enum] complete with square brackets and ignore Intellisense completely, then you can press . to choose members like TryParse:

vs_reserved_kwd_5

Expected Behavior:

The square brackets should NOT be removed when you press tab to autocomplete if the resulting code would be a syntax error (as is the case when dealing with Enum). Interestingly enough, the editor is smart enough to handle cases where you have your own type named using a reserved keyword:

Public Module Module1
  Public Class [CDec]
    Public Shared Function DoSomething() As String
    End Function
  End Class

  Public Sub Main()
  End Sub
End Module

In the above code the class CDec uses the reserved keyword CDec for converting values to type Decimal. So if you want to call the shared method DoSomething which must be qualified with type name [CDec], you can type the square brackets, then begin typing cd to bring up Intellisense:

vs_reserved_kwd_6

Note the autocomplete list above shows CDec as a keyword and CDec as a type. If you choose to autocomplete with the keyword CDec it correctly strips the brackets as expected. If you choose to autocomplete with the type name CDec it correctly PRESERVES the brackets as expected. This bracket preservation should also work for the Enum type which like the CDec class we defined above just happens to be named after a reserved keyword.

Area-IDE Bug Language-VB Resolution-Fixed

All 3 comments

The bug for this is here:

https://github.com/dotnet/roslyn/blob/fcf5fd8a5ae8d9a47357d99522a3140cda265264/src/Workspaces/VisualBasic/Portable/Extensions/StringExtensions.vb#L93-L111

System_Enum should not be in that list. This is causing us to not properly escape this item in the completion list.

With that entry removed, we get the appropriate behavior:

enum

I'm actually surprised this has gone unnoticed for so long.

@CyrusNajmabadi it's really cool how you guys fix bugs even on Sundays! Many thanks for following up as always :+1: :+1:

Was this page helpful?
0 / 5 - 0 ratings