From 54549f916a27b5cb956f8277131db203b6563f0e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 7 Jul 2015 19:29:23 -0500 Subject: [PATCH 1/3] Change assertion API to pass error to callback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don’t assume anything about metadata. Just give assertion callers access to the error object. Signed-off-by: Max Brunsfeld --- spec/atom-spec.coffee | 18 ++++++------------ src/atom.coffee | 9 ++------- src/tokenized-buffer.coffee | 6 ++++-- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/spec/atom-spec.coffee b/spec/atom-spec.coffee index d2d1d68ae21..13b62c8e3b2 100644 --- a/spec/atom-spec.coffee +++ b/spec/atom-spec.coffee @@ -127,7 +127,7 @@ describe "the `atom` global", -> column: 3 originalError: error - describe ".assert(condition, message, metadata)", -> + describe ".assert(condition, message, callback)", -> errors = null beforeEach -> @@ -142,17 +142,11 @@ describe "the `atom` global", -> expect(errors[0].message).toBe "Assertion failed: a == b" expect(errors[0].stack).toContain('atom-spec') - describe "if metadata is an object", -> - it "assigns the object on the error as `metadata`", -> - metadata = {foo: 'bar'} - atom.assert(false, "a == b", metadata) - expect(errors[0].metadata).toBe metadata - - describe "if metadata is a function", -> - it "assigns the function's return value on the error as `metadata`", -> - metadata = {foo: 'bar'} - atom.assert(false, "a == b", -> metadata) - expect(errors[0].metadata).toBe metadata + describe "if passed a callback function", -> + it "calls the callback with the assertion failure's error object", -> + error = null + atom.assert(false, "a == b", (e) -> error = e) + expect(error).toBe errors[0] describe "if the condition is true", -> it "does nothing", -> diff --git a/src/atom.coffee b/src/atom.coffee index 03525df687b..d50c49a30e2 100644 --- a/src/atom.coffee +++ b/src/atom.coffee @@ -721,17 +721,12 @@ class Atom extends Model Section: Private ### - assert: (condition, message, metadata) -> + assert: (condition, message, callback) -> return true if condition error = new Error("Assertion failed: #{message}") Error.captureStackTrace(error, @assert) - - if metadata? - if typeof metadata is 'function' - error.metadata = metadata() - else - error.metadata = metadata + callback?(error) @emitter.emit 'did-fail-assertion', error diff --git a/src/tokenized-buffer.coffee b/src/tokenized-buffer.coffee index b662de08ade..9c25e9cb386 100644 --- a/src/tokenized-buffer.coffee +++ b/src/tokenized-buffer.coffee @@ -296,10 +296,12 @@ class TokenizedBuffer extends Model # undefined. This should paper over the problem but we want to figure out # what is happening: tokenizedLine = @tokenizedLineForRow(row) - atom.assert tokenizedLine?, "TokenizedLine is defined", => - metadata: + atom.assert tokenizedLine?, "TokenizedLine is defined", (error) => + error.metadata = { row: row rowCount: @tokenizedLines.length + } + return false unless tokenizedLine? return false if @buffer.isRowBlank(row) or tokenizedLine.isComment() From 08fe81844c31899fc2de0963ace3d0b8a567accb Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 7 Jul 2015 19:33:21 -0500 Subject: [PATCH 2/3] 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 5fdadf21230..8af408da509 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) From 7b95ba0a08d79a9d295f9916ba5d7a9542909292 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 7 Jul 2015 19:34:37 -0500 Subject: [PATCH 3/3] :arrow_up: exception-reporting Signed-off-by: Max Brunsfeld --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7eaff274a06..bd384013800 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,7 @@ "deprecation-cop": "0.53.0", "dev-live-reload": "0.46.0", "encoding-selector": "0.21.0", - "exception-reporting": "0.34.0", + "exception-reporting": "0.35.0", "find-and-replace": "0.174.2", "fuzzy-finder": "0.87.0", "git-diff": "0.55.0",