@@ -3983,7 +3983,6 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
39833983 _hasActiveFoldsCache = false ;
39843984
39853985 final lineCount = controller.lineCount;
3986- // Build sorted list directly from fold ranges instead of per-line Set
39873986 final sortedLines = < int > [];
39883987 for (final fold in _foldRanges.values) {
39893988 if (fold != null && fold.isFolded) {
@@ -6248,19 +6247,26 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
62486247 visibleHeight += _getWrappedLineHeight (i);
62496248 }
62506249 } else {
6251- double cachedHeight = 0 ;
6252- int cachedCount = 0 ;
6253- for (final entry in _lineHeightCache.entries) {
6254- if (entry.key < lineCount) {
6255- cachedHeight += entry.value;
6256- cachedCount++ ;
6250+ const int exactWrapHeightThreshold = 512 ;
6251+ if (lineCount <= exactWrapHeightThreshold) {
6252+ for (int i = 0 ; i < lineCount; i++ ) {
6253+ visibleHeight += _getWrappedLineHeight (i);
62576254 }
6255+ } else {
6256+ double cachedHeight = 0 ;
6257+ int cachedCount = 0 ;
6258+ for (final entry in _lineHeightCache.entries) {
6259+ if (entry.key < lineCount) {
6260+ cachedHeight += entry.value;
6261+ cachedCount++ ;
6262+ }
6263+ }
6264+ final uncachedCount = lineCount - cachedCount;
6265+ final avgHeight = cachedCount > 0
6266+ ? cachedHeight / cachedCount
6267+ : _lineHeight;
6268+ visibleHeight = cachedHeight + (uncachedCount * avgHeight);
62586269 }
6259- final uncachedCount = lineCount - cachedCount;
6260- final avgHeight = cachedCount > 0
6261- ? cachedHeight / cachedCount
6262- : _lineHeight;
6263- visibleHeight = cachedHeight + (uncachedCount * avgHeight);
62646270 }
62656271 } else {
62666272 _wrapWidth = double .infinity;
@@ -7224,38 +7230,10 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
72247230 final errorColor = gutterStyle.errorLineNumberColor;
72257231 final warningColor = gutterStyle.warningLineNumberColor;
72267232
7227- int firstVisibleLine;
7228- double firstVisibleLineY;
7229-
7230- if (! lineWrap && ! hasActiveFolds && _virtualRemovedTotalLineCount == 0 ) {
7231- firstVisibleLine = (viewTop / _lineHeight).floor ().clamp (
7232- 0 ,
7233- lineCount - 1 ,
7234- );
7235- firstVisibleLineY = firstVisibleLine * _lineHeight;
7236- } else {
7237- double currentY = 0 ;
7238- firstVisibleLine = 0 ;
7239- firstVisibleLineY = 0 ;
7240- final blocks = controller.virtualRemovedBlocks;
7241- int blockIdx = 0 ;
7242-
7243- for (int i = 0 ; i < lineCount; i++ ) {
7244- if (hasActiveFolds && _isLineFolded (i)) continue ;
7245- while (blockIdx < blocks.length &&
7246- blocks[blockIdx].afterLine == i - 1 ) {
7247- currentY += blocks[blockIdx].lineCount * _lineHeight;
7248- blockIdx++ ;
7249- }
7250- final lineHeight = lineWrap ? _getWrappedLineHeight (i) : _lineHeight;
7251- if (currentY + lineHeight > viewTop) {
7252- firstVisibleLine = i;
7253- firstVisibleLineY = currentY;
7254- break ;
7255- }
7256- currentY += lineHeight;
7257- }
7258- }
7233+ final firstVisibleLine = _findVisibleLineByYPosition (
7234+ viewTop,
7235+ ).clamp (0 , lineCount - 1 );
7236+ final firstVisibleLineY = _getLineYOffset (firstVisibleLine, hasActiveFolds);
72597237
72607238 _actionBulbRects.clear ();
72617239
@@ -8280,6 +8258,7 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
82808258 }
82818259
82828260 final lineY = _getLineYOffset (lineIndex, hasActiveFolds);
8261+ final visualYOffset = _getTotalVirtualOffset (lineIndex);
82838262
82848263 final colorBoxOffsetStart = _getColorBoxOffsetForLine (
82858264 lineIndex,
@@ -8311,6 +8290,7 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
83118290 offset.dy +
83128291 (innerPadding? .top ?? 0 ) +
83138292 lineY +
8293+ visualYOffset +
83148294 box.top -
83158295 vscrollController.offset;
83168296
@@ -8332,6 +8312,7 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
83328312 offset.dy +
83338313 (innerPadding? .top ?? 0 ) +
83348314 lineY -
8315+ visualYOffset -
83358316 vscrollController.offset;
83368317
83378318 canvas.drawRect (
@@ -8479,6 +8460,7 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
84798460 final startCol = start - startLineOffset;
84808461
84818462 final startY = _getLineYOffset (startLine, hasActiveFolds);
8463+ final startVisualYOffset = _getTotalVirtualOffset (startLine);
84828464
84838465 double startX;
84848466 double startYInLine = 0 ;
@@ -8511,6 +8493,7 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
85118493 offset.dy +
85128494 (innerPadding? .top ?? 0 ) +
85138495 startY +
8496+ startVisualYOffset +
85148497 startYInLine -
85158498 vscrollController.offset;
85168499
@@ -8531,6 +8514,7 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
85318514 final endCol = end - endLineOffset;
85328515
85338516 final endY = _getLineYOffset (endLine, hasActiveFolds);
8517+ final endVisualYOffset = _getTotalVirtualOffset (endLine);
85348518
85358519 double endX;
85368520 double endYInLine = 0 ;
@@ -8563,6 +8547,7 @@ class _CodeFieldRenderer extends RenderBox implements MouseTrackerAnnotation {
85638547 offset.dy +
85648548 (innerPadding? .top ?? 0 ) +
85658549 endY +
8550+ endVisualYOffset +
85668551 endYInLine -
85678552 vscrollController.offset;
85688553
0 commit comments