Skip to content
Browse files

Fix "InvalidOperationException: Trying to build visual line from coll…

…apsed line":

When the last line in the document was collapsed, the method HeightTree.GetNodeByVisualPosition(position after end of document)
was returning the last line in the document, when it should have returned the last non-collapsed line.

Also fixed a minor bug in FoldingMargin (missing end marker when folding starts above viewport and ends on last line of document).
  • Loading branch information...
1 parent d071c68 commit 28c2f38f4fadd54b8d2c96f2eec3c3248d27ac3c @dgrunwald dgrunwald committed Dec 9, 2011
View
2 src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Folding/FoldingMargin.cs
@@ -222,7 +222,7 @@ void CalculateFoldLinesForFoldingsActiveAtStart(List<TextLine> allTextLines, Pen
int maxEndOffset = 0;
foreach (FoldingSection fs in foldings) {
int end = fs.EndOffset;
- if (end < viewEndOffset && !fs.IsFolded) {
+ if (end <= viewEndOffset && !fs.IsFolded) {
int textLineNr = GetTextLineIndexFromOffset(allTextLines, end);
if (textLineNr >= 0) {
endMarker[textLineNr] = foldingControlPen;
View
40 src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Rendering/HeightTree.cs
@@ -425,26 +425,34 @@ HeightTreeNode GetNodeByIndex(int index)
HeightTreeNode GetNodeByVisualPosition(double position)
{
- if (position <= 0) {
- return root.LeftMost;
- }
- if (position > root.totalHeight) {
- return root.RightMost;
- }
HeightTreeNode node = root;
while (true) {
- if (node.left != null && position < node.left.totalHeight) {
- node = node.left;
- } else {
- if (node.left != null) {
- position -= node.left.totalHeight;
+ double positionAfterLeft = position;
+ if (node.left != null) {
+ positionAfterLeft -= node.left.totalHeight;
+ if (positionAfterLeft < 0) {
+ // Descend into left
+ node = node.left;
+ continue;
}
- position -= node.lineNode.TotalHeight;
- if (position < 0 || node.right == null)
+ }
+ double positionBeforeRight = positionAfterLeft - node.lineNode.TotalHeight;
+ if (positionBeforeRight < 0) {
+ // Found the correct node
+ return node;
+ }
+ if (node.right == null || node.right.totalHeight == 0) {
+ // Can happen when position>node.totalHeight,
+ // i.e. at the end of the document, or due to rounding errors in previous loop iterations.
+
+ // If node.lineNode isn't collapsed, return that.
+ if (node.lineNode.TotalHeight > 0)
return node;
- // node.right==null can happen when totalHeight is incorrect due to rounding errors,
- // so position can be below the rounded totalHeight but larger than the sum
- // of all nodes
+ // Otherwise, descend into left (find the last non-collapsed node)
+ node = node.left;
+ } else {
+ // Descend into right
+ position = positionBeforeRight;
node = node.right;
}
}

0 comments on commit 28c2f38

Please sign in to comment.
Something went wrong with that request. Please try again.