Automation vs2010: get the text value of an EnvDTE.CodeElement element

So, I am playing with the EnvDTE and EnvDTE.CodeModel API, and I was wondering if there is a way to get the text value represented by the CodeElement .

Let's say I have a CodeAttribute , is there a way to get a string of what CodeAttribute represents, i.e. [MyAttribute(value="myvalue")] .

I know that you can recover code using various CodeElement properties, at least in some scenarios, but for some things it seems that it would be easier to just get the text.

Thanks!

+7
c # visual-studio visual-studio-2010 code-generation
source share
3 answers

The CodeElement interface has StartPoint and EndPoint properties that represent the beginning and end of an element in the buffer. They contain a row / column number that can be passed to methods like IVsTextLines.GetLineText , and will return the value you are looking for.

To get IVsTextLines for a given CodeElement , you can do the following

 CodeElement ce = ...; TextDocument td = ce.StartPoint.Parent; IVsTextLines lines = td as IVsTextLines; 
+4
source share
  void WriteMapping(CodeProperty codeProperty) { WriteLine(""); WriteLine("///CodeProperty"); WriteLine("///<summary>"); WriteLine("///"+codeProperty.FullName); WriteLine("///</summary>"); if(codeProperty.Getter==null && codeProperty.Setter==null) return; if(codeProperty.Attributes!=null){ foreach(CodeAttribute a in codeProperty.Attributes) { Write("["+a.FullName); if(a.Children!=null && a.Children.Count>0) { var start=a.Children.Cast<CodeElement>().First().GetStartPoint(); var finish= a.GetEndPoint(); string allArguments=start.CreateEditPoint().GetText(finish); Write("("+allArguments); } WriteLine("]"); } } Write("public "+GetFullName(codeProperty.Type) +" "+codeProperty.Prototype); Write(" {"); //if(codeProperty.Getter!=null && codeProperty.Getter.Access!=vsCMAccess.vsCMAccessPrivate) Write("get;"); //if(codeProperty.Setter!=null) Write("set;"); WriteLine("}"); } 
+3
source share

In addition to @JaredPar's answer, an alternative approach:

 public string GetText(CodeAttribute attribute) { return attribute.StartPoint.CreateEditPoint().GetText(attribute.EndPoint); } 

What is it! (Thanks @JaredPar for the pointers)

Source: http://msdn.microsoft.com/en-us/library/envdte.editpoint.gettext.aspx

0
source share

All Articles