Skip to content

EnvDTE.CodeElement.GetStartPoint(vsCMPartBody)/GetEndPoint(vsCMPartBody) throw COMException for expression-bodied methods and properties #21306

Description

@carlos-quintero

Version Used: Visual Studio 2017 15.2 (26430.16 Release)

Steps to Reproduce:

  1. Create a VS package extension with a command that executes this code:
      private void MenuItemCallback(object sender, EventArgs e)
      {
         EnvDTE.DTE dte;

         dte = this.ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;

         ShowCodeElements(dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElements);
      }

      private void ShowCodeElements(EnvDTE.CodeElements codeElements)
      {
         foreach (EnvDTE.CodeElement codeElement in codeElements)
         {
            switch (codeElement)
            {
               case EnvDTE.CodeNamespace codeNamespace:

                  ShowCodeElements(codeNamespace.Members);
                  break;

               case EnvDTE.CodeType codeType:

                  ShowCodeElements(codeType.Members);
                  break;

               case EnvDTE.CodeProperty codeProperty:

                  GetPoints(codeElement);
                  break;

               case EnvDTE.CodeFunction codeFunction:

                  GetPoints(codeElement);
                  break;
            }
         }
      }

      private void GetPoints(EnvDTE.CodeElement codeElement)
      {
         System.Text.StringBuilder sb = new System.Text.StringBuilder();

         sb.AppendLine(codeElement.Name);
         sb.AppendLine();

         GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartWholeWithAttributes);
         GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartWhole);
         GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartHeaderWithAttributes);
         GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartHeader);
         GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartName);
         GetPoints(sb, codeElement, EnvDTE.vsCMPart.vsCMPartBody);

         VsShellUtilities.ShowMessageBox(
             this.ServiceProvider,
             sb.ToString(),
             "",
             OLEMSGICON.OLEMSGICON_INFO,
             OLEMSGBUTTON.OLEMSGBUTTON_OK,
             OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
      }

      private void GetPoints(System.Text.StringBuilder sb, EnvDTE.CodeElement codeElement, EnvDTE.vsCMPart part)
      {
         GetStartPoint(sb, codeElement, part);
         GetEndPoint(sb, codeElement, part);
         sb.AppendLine();
      }

      private void GetStartPoint(System.Text.StringBuilder sb, EnvDTE.CodeElement codeElement, EnvDTE.vsCMPart part)
      {
         EnvDTE.TextPoint textPoint;

         sb.Append(part.ToString() + " Start: ");

         try
         {
            textPoint = codeElement.GetStartPoint(part);
            sb.AppendLine(textPoint.Line.ToString() + ", " + textPoint.DisplayColumn.ToString());
         }
         catch (Exception ex)
         {
            sb.AppendLine(ex.GetType().Name);
         }
      }

      private void GetEndPoint(System.Text.StringBuilder sb, EnvDTE.CodeElement codeElement, EnvDTE.vsCMPart part)
      {
         EnvDTE.TextPoint textPoint;

         sb.Append(part.ToString() + " End: ");

         try
         {
            textPoint = codeElement.GetEndPoint(part);
            sb.AppendLine(textPoint.Line.ToString() + ", " + textPoint.DisplayColumn.ToString());
         }
         catch (Exception ex)
         {
            sb.AppendLine(ex.GetType().Name);
         }
      }

  1. In the Experimental instance, create a Class Library with this code:
namespace ClassLibrary1
{
   public class Class1
   {
      public int f1()
      {
         return 0;
      }

      public int p1
      {
         get
         {
            return 0;
         }
      }

      public int f2() => 0;

      public int p2 => 0;
   }
}

  1. Ensure that the code file is the active window and click the command of the extension

Expected Behavior: it is already bad that so many points throw NotImplementedException, but for expression-bodied members a COMException is returned when the part is vsCMPartBody.

Actual Behavior: at the very least, NotImplementedException should be returned instead of COMException. And even better, return actual information since there is a body and for regular members the body points are returned.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions