Search code examples
visual-studiovisual-studio-extensionsvsix

Get text by line number


What is the easiest way to get the whole line of text having line number/where the mouse caret is? (In currently open document.)

I'm getting line number using:

 DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;
 int line = ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).ActivePoint.Line;

also using similar approach I can get selected text:

string line = ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).Text;

but I'm struggling to find anything that could be useful.


Solution

  • To get the whole line of text where the caret is:

    var activePoint = ((EnvDTE.TextSelection)dte.ActiveDocument.Selection).ActivePoint;
    string text = activePoint.CreateEditPoint().GetLines(activePoint.Line, activePoint.Line + 1);