sswwqqaa
sswwqqaa

Reputation: 1616

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.

Upvotes: 3

Views: 553

Answers (1)

Sergey Vlasov
Sergey Vlasov

Reputation: 27930

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);

Upvotes: 4

Related Questions