From 08fe81844c31899fc2de0963ace3d0b8a567accb Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 7 Jul 2015 19:33:21 -0500 Subject: [PATCH] Guard against IndexSizeError when measuring lines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #7464 #7465 #5997 This will ask the user for the content of the offending line, but only once so as not to be annoying. Hopefully this and the other data we’re collecting will help us solve the problem. Signed-off-by: Max Brunsfeld --- src/lines-tile-component.coffee | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/lines-tile-component.coffee b/src/lines-tile-component.coffee index 5fdadf212..8af408da5 100644 --- a/src/lines-tile-component.coffee +++ b/src/lines-tile-component.coffee @@ -346,17 +346,44 @@ class LinesTileComponent rangeForMeasurement ?= document.createRange() iterator = document.createNodeIterator(lineNode, NodeFilter.SHOW_TEXT, AcceptFilter) textNode = iterator.nextNode() + textNodeLength = textNode.length textNodeIndex = 0 nextTextNodeIndex = textNode.textContent.length while nextTextNodeIndex <= charIndex textNode = iterator.nextNode() + textNodeLength = textNode.length textNodeIndex = nextTextNodeIndex nextTextNodeIndex = textNodeIndex + textNode.textContent.length i = charIndex - textNodeIndex rangeForMeasurement.setStart(textNode, i) - rangeForMeasurement.setEnd(textNode, i + charLength) + + if i + charLength <= textNodeLength + rangeForMeasurement.setEnd(textNode, i + charLength) + else + rangeForMeasurement.setEnd(textNode, textNodeLength) + atom.assert false, "Expected index to be less than the length of text node while measuring", (error) => + editor = @presenter.model + screenRow = tokenizedLine.screenRow + bufferRow = editor.bufferRowForScreenRow(screenRow) + + error.metadata = { + grammarScopeName: editor.getGrammar().scopeName + screenRow: screenRow + bufferRow: bufferRow + softWrapped: editor.isSoftWrapped() + softTabs: editor.getSoftTabs() + i: i + charLength: charLength + textNodeLength: textNode.length + } + error.privateMetadataDescription = "The contents of line #{bufferRow + 1}." + error.privateMetadata = { + lineText: editor.lineTextForBufferRow(bufferRow) + } + error.privateMetadataRequestName = "measured-line-text" + charWidth = rangeForMeasurement.getBoundingClientRect().width @presenter.setScopedCharacterWidth(scopes, char, charWidth)