New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
carlos-quintero opened this Issue Aug 3, 2017 · 1 comment

Comments

Projects
None yet
4 participants
@carlos-quintero

carlos-quintero commented Aug 3, 2017

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.

@Pilchie Pilchie added this to the 15.5 milestone Aug 8, 2017

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