visual studio 2010 – How to find the position equivalent in an iTextEdit of an EditPoint.AbsoluteCharOffset – Education Career Blog

I’m trying delete a class body in using the new Text Editor code model in a Visual Studio 2010 extension. That is, I’m trying to use the Delete method on ITextEdit to delete everything between curly brackets.

I can get access to the Absolute Character Position of the start and end of the class using the




methods on the CodeClass interface. If I then get an ITextEdit from the current IWpfTextViewHost’s ITextBuffer, and then try to delete:

iTextEdit.Delete(startCharOffset, endCharOffset - startCharOffset); 

the deletion is incorrect. It appears there isn’t a 1:1 mapping between AbsoluteCharOffset on the old model and position in the new text model.

How do I interact between the two models?

Thanks in advance for your help.


AbsoluteCharOffset returns a value that is actually not the absolute char offset, as it always counts line break characters as 1 character long, even though “\r\n” is probably what your file uses. The best way to do the translation is to get the line number and column offset, maybe a helper method like:

SnapshotPoint SnapshotPointFromEditPoint(EditPoint editPoint, ITextSnapshot snapshot)
    int lineNumber = editPoint.Line - 1;
    int offset = editPoint.LineCharOffset - 1;
    return snapshot.GetLineFromLineNumber(lineNumber).Start + offset;

(I just learned about this a few months ago, from a performance issue in Dev10 where computing the AbsoluteCharOffset is really expensive. I’d recommend avoiding it every place you can)

Leave a Comment