Skip to content

Commit 033659e

Browse files
committed
Calculate the actual frame line.
1 parent 5ca3aff commit 033659e

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

lib/stacktrace.coffee

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ class Frame
9595
# Public: Asynchronously collect n lines of context around the specified line number in this
9696
# frame's source file.
9797
#
98-
# n - The number of lines of context to collect on *each* side of the error line. The error
99-
# line will always be `lines[n]` and `lines.length` will be `n * 2 + 1`.
100-
# callback - Invoked with any errors or an Array containing the relevant lines.
98+
# n - The number of lines of context to collect on *each* side of the error line.
99+
# callback - Invoked with any errors, or an Array containing the relevant lines and the index of
100+
# the line in the Array that corresponds to the actual frame.
101101
#
102102
getContext: (n, callback) ->
103103
# Notice that @lineNumber is one-indexed, not zero-indexed.
@@ -106,7 +106,18 @@ class Frame
106106
toLine: @lineNumber + n
107107
trim: false
108108
keepLastEmptyLine: true
109-
chomp fs.createReadStream(@realPath), range, callback
109+
chomp fs.createReadStream(@realPath), range, (err, lines) =>
110+
if err?
111+
callback(err)
112+
else
113+
# Determine which line is the actual trace line.
114+
if lines.length < (2 * n + 1) and range.fromLine < 0
115+
# The front is cut off.
116+
traceLine = @lineNumber - 1
117+
else
118+
traceLine = n
119+
120+
callback(null, lines, traceLine)
110121

111122
navigateTo: ->
112123
position = [@lineNumber - 1, 0]

spec/stacktrace-spec.coffee

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,19 @@ describe 'Stacktrace', ->
103103
expect(event.newTrace).toBe(trace)
104104

105105
describe 'Frame', ->
106-
[frame] = []
106+
[fixturePath] = []
107107

108108
beforeEach ->
109109
fixturePath = path.join __dirname, 'fixtures', 'context.txt'
110-
frame = new Frame('five', fixturePath, 5, 'something')
111110

112111
it 'acquires n lines of context asynchronously', ->
113-
lines = null
112+
[lines, traceLine] = []
113+
frame = new Frame('five', fixturePath, 5, 'something')
114114

115-
frame.getContext 2, (err, ls) ->
115+
frame.getContext 2, (err, ls, lnum) ->
116116
throw err if err?
117117
lines = ls
118+
traceLine = lnum
118119

119120
waitsFor -> lines?
120121

@@ -125,3 +126,40 @@ describe 'Frame', ->
125126
expect(lines[2]).toEqual('five')
126127
expect(lines[3]).toEqual('six')
127128
expect(lines[4]).toEqual('')
129+
expect(traceLine).toBe(2)
130+
131+
it 'identifies the trace line if the beginning is cut off', ->
132+
[lines, traceLine] = []
133+
frame = new Frame('two', fixturePath, 2, 'something')
134+
135+
frame.getContext 3, (err, ls, lnum) ->
136+
throw err if err?
137+
lines = ls
138+
traceLine = lnum
139+
140+
waitsFor -> lines?
141+
142+
runs ->
143+
expect(lines.length).toBe(5)
144+
expect(lines[0]).toEqual('one')
145+
expect(lines[4]).toEqual('five')
146+
expect(traceLine).toBe(1)
147+
expect(lines[1]).toEqual('two')
148+
149+
it 'identifies the trace line if the end is cut off', ->
150+
[lines, traceLine] = []
151+
frame = new Frame('nine', fixturePath, 9, 'something')
152+
153+
frame.getContext 3, (err, ls, lnum) ->
154+
throw err if err?
155+
lines = ls
156+
traceLine = lnum
157+
158+
waitsFor -> lines?
159+
160+
runs ->
161+
expect(lines.length).toBe(6)
162+
expect(lines[0]).toEqual('six')
163+
expect(lines[5]).toEqual('')
164+
expect(traceLine).toBe(3)
165+
expect(lines[3]).toEqual('nine')

0 commit comments

Comments
 (0)