From fae507d70d57be7ea3c753f78e506b0a099ede66 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 20 Sep 2016 15:03:41 +0200 Subject: [PATCH] Don't remeasure the first character of a line Previously we were ignoring the measurement cache for characters located at `left: 0px` because `0` is evaluated as falsy in Javascript, causing those character to be constantly re-measured. This commit fixes it so that we explicitly check for null values when consulting the cache. --- src/lines-yardstick.coffee | 50 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/lines-yardstick.coffee b/src/lines-yardstick.coffee index 13da96fa1..098fc2288 100644 --- a/src/lines-yardstick.coffee +++ b/src/lines-yardstick.coffee @@ -91,34 +91,34 @@ class LinesYardstick lineNode = @lineNodesProvider.lineNodeForScreenRow(row) lineId = @lineNodesProvider.lineIdForScreenRow(row) - return 0 unless lineNode? - - if cachedPosition = @leftPixelPositionCache[lineId]?[column] - return cachedPosition - - textNodes = @lineNodesProvider.textNodesForScreenRow(row) - textNodeStartColumn = 0 - - for textNode in textNodes - textNodeEndColumn = textNodeStartColumn + textNode.textContent.length - if textNodeEndColumn > column - indexInTextNode = column - textNodeStartColumn - break + if lineNode? + if @leftPixelPositionCache[lineId]?[column]? + @leftPixelPositionCache[lineId][column] else - textNodeStartColumn = textNodeEndColumn + textNodes = @lineNodesProvider.textNodesForScreenRow(row) + textNodeStartColumn = 0 + for textNode in textNodes + textNodeEndColumn = textNodeStartColumn + textNode.textContent.length + if textNodeEndColumn > column + indexInTextNode = column - textNodeStartColumn + break + else + textNodeStartColumn = textNodeEndColumn - if textNode? - indexInTextNode ?= textNode.textContent.length - lineOffset = lineNode.getBoundingClientRect().left - if indexInTextNode is 0 - leftPixelPosition = @clientRectForRange(textNode, 0, 1).left - else - leftPixelPosition = @clientRectForRange(textNode, 0, indexInTextNode).right - leftPixelPosition -= lineOffset + if textNode? + indexInTextNode ?= textNode.textContent.length + lineOffset = lineNode.getBoundingClientRect().left + if indexInTextNode is 0 + leftPixelPosition = @clientRectForRange(textNode, 0, 1).left + else + leftPixelPosition = @clientRectForRange(textNode, 0, indexInTextNode).right + leftPixelPosition -= lineOffset - @leftPixelPositionCache[lineId] ?= {} - @leftPixelPositionCache[lineId][column] = leftPixelPosition - leftPixelPosition + @leftPixelPositionCache[lineId] ?= {} + @leftPixelPositionCache[lineId][column] = leftPixelPosition + leftPixelPosition + else + 0 else 0