Skip to content

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

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

Closed
carlos-quintero opened this issue Aug 3, 2017 · 2 comments

Comments

@carlos-quintero
Copy link
Contributor

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.

@jinujoseph jinujoseph modified the milestones: 15.5, Unknown Oct 14, 2017
@CyrusNajmabadi
Copy link
Member

Closing out. CodeModle is in maintenance mode and not being touched.

@CyrusNajmabadi CyrusNajmabadi closed this as not planned Won't fix, can't repro, duplicate, stale Oct 18, 2024
@dotnet dotnet locked as resolved and limited conversation to collaborators Oct 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants