Skip to content

Decorate the stack frame lines within each mini-Editor #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/stacktrace-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,18 @@ class FrameView extends View
grammar = atom.syntax.selectGrammar @frame.realPath, lines.join("\n")
@source.getEditor().setGrammar grammar

@frame.getContext 3, (err, lines) =>
@frame.getContext 3, (err, lines, traceLine) =>
if err?
console.error err
else
@source.getEditor().setText lines.join("\n")
editor = @source.getEditor()
editor.setText lines.join("\n")
range = editor.getBuffer().rangeForRow traceLine
@marker = editor.markBufferRange range
console.log editor.decorateMarker @marker, type: 'line', class: 'line-stackframe'

beforeRemove: ->
@marker.destroy() if @marker?

navigate: ->
@navCallback()
Expand Down
19 changes: 15 additions & 4 deletions lib/stacktrace.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ class Frame
# Public: Asynchronously collect n lines of context around the specified line number in this
# frame's source file.
#
# n - The number of lines of context to collect on *each* side of the error line. The error
# line will always be `lines[n]` and `lines.length` will be `n * 2 + 1`.
# callback - Invoked with any errors or an Array containing the relevant lines.
# n - The number of lines of context to collect on *each* side of the error line.
# callback - Invoked with any errors, or an Array containing the relevant lines and the index of
# the line in the Array that corresponds to the actual frame.
#
getContext: (n, callback) ->
# Notice that @lineNumber is one-indexed, not zero-indexed.
Expand All @@ -155,7 +155,18 @@ class Frame
toLine: @lineNumber + n
trim: false
keepLastEmptyLine: true
chomp fs.createReadStream(@realPath), range, callback
chomp fs.createReadStream(@realPath), range, (err, lines) =>
if err?
callback(err)
else
# Determine which line is the actual trace line.
if lines.length < (2 * n + 1) and range.fromLine < 0
# The front is cut off.
traceLine = @lineNumber - 1
else
traceLine = n

callback(null, lines, traceLine)

navigateTo: ->
position = [@lineNumber - 1, 0]
Expand Down
42 changes: 40 additions & 2 deletions spec/stacktrace-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ describe 'Frame', ->
frame = new Frame('five', fixturePath, 5, 'something')

it 'acquires n lines of context asynchronously', ->
lines = null
[lines, traceLine] = []

frame.getContext 2, (err, ls) ->
frame.getContext 2, (err, ls, lnum) ->
throw err if err?
lines = ls
traceLine = lnum

waitsFor -> lines?

Expand All @@ -170,6 +171,7 @@ describe 'Frame', ->
expect(lines[2]).toEqual('five')
expect(lines[3]).toEqual('six')
expect(lines[4]).toEqual('')
expect(traceLine).toBe(2)

describe 'recognizes itself in an Editor', ->
it 'is on a cursor', ->
Expand All @@ -180,3 +182,39 @@ describe 'Frame', ->

it 'is on a different file', ->
expect(frame.isOn(position: Point.fromObject([4, 0]), path: 'some/other/path.rb')).toBeFalsy()

it 'identifies the trace line if the beginning is cut off', ->
[lines, traceLine] = []
frame = new Frame('two', fixturePath, 2, 'something')

frame.getContext 3, (err, ls, lnum) ->
throw err if err?
lines = ls
traceLine = lnum

waitsFor -> lines?

runs ->
expect(lines.length).toBe(5)
expect(lines[0]).toEqual('one')
expect(lines[4]).toEqual('five')
expect(traceLine).toBe(1)
expect(lines[1]).toEqual('two')

it 'identifies the trace line if the end is cut off', ->
[lines, traceLine] = []
frame = new Frame('nine', fixturePath, 9, 'something')

frame.getContext 3, (err, ls, lnum) ->
throw err if err?
lines = ls
traceLine = lnum

waitsFor -> lines?

runs ->
expect(lines.length).toBe(6)
expect(lines[0]).toEqual('six')
expect(lines[5]).toEqual('')
expect(traceLine).toBe(3)
expect(lines[3]).toEqual('nine')
3 changes: 3 additions & 0 deletions spec/stacktrace-view-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ describe 'FrameView', ->
it 'shows the function name', ->
text = view.find('.function-name').text()
expect(text).toEqual('midfunc')

it 'decorates the active line', ->
expect(view.find('.editor .line-stacktrace')).toHaveLength(1)