How to create a TextRange from a line number?

Given an IProjectFile instance and a line number in that file, what is the "right" way to create a TextRange for that line, suitable for passing to Navigator.NavigateTextRange()?  Right now I'm opening the file and reading it, counting the character length of all lines until my line number, and pass
ing that sum to the TextRange ctor.  But when I navigate to the location it's often short a couple lines.  I'm guessing that perhaps my count is off due to line ending characters, and I could code for that, but perhaps there exists a better way?

Here where I compute the TextRange parameter.


And here's how I use it.





Thanks
Josh

---
Original message URL: http://www.jetbrains.net/devnet/message/5230363#5230363

1 comment

You have to get the IDocument for that project file (using
DocumentManager.GetDocument method), and using document get the line
offsets. The create the TextRange using that offsets

--
Eugene Pasynkov
ReSharper Technical Lead
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


"jbuedel" <no_reply@jetbrains.com> wrote in message
news:32080952.75301232368730630.JavaMail.clearspace@app4.labs.intellij.net...

Given an IProjectFile instance and a line number in that file, what is the
"right" way to create a TextRange for that line, suitable for passing to
Navigator.NavigateTextRange()? Right now I'm opening the file and reading
it, counting the character length of all lines until my line number, and
pass
ing that sum to the TextRange ctor. But when I navigate to the location
it's often short a couple lines. I'm guessing that perhaps my count is off
due to line ending characters, and I could code for that, but perhaps
there exists a better way?

>

Here where I compute the TextRange parameter.

>
>

 public TextRange TextRange
> {
> get
> {
> using (TextReader tr = new StreamReader(ProjectFile.CreateReadStream()))
> {
> int position = 0;
> int char_count = 0;
> string line;
> while (position++ < StartLineNumber && (line = tr.ReadLine()) != null)
> {
> char_count += line.Length + 1;
> }
> textRange = new TextRange(char_count);
> }
>
> return textRange;
> }
> }
> ..
> }]]>

>

And here's how I use it.

>

 transferFocus)
> {
> if (node != null && node.DataValue is IBlock)
> {
> IBlock block = (IBlock)node.DataValue;
>
> // TODO! Navigation has some off by one errors or something.
> Navigator.NavigateTextRange(block.ProjectFile, block.TextRange, 
> transferFocus);
> return true;
> }
> return false;
> }]]>

>
>
>
>

Thanks
Josh

>

---
Original message URL:
http://www.jetbrains.net/devnet/message/5230363#5230363



0

Please sign in to leave a comment.