Skip to content

Commit 371633f

Browse files
committed
Merge pull request #5 from smashwilson/decorate-frame
Decorate lines within Editors that correspond to the active Stacktrace.
2 parents 73df0f6 + 774497a commit 371633f

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed

lib/editor-decorator.coffee

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Decorate any lines within an {Editor} that correspond to an active {Stacktrace}.
2+
3+
{Stacktrace} = require './stacktrace'
4+
5+
markers = []
6+
7+
module.exports = (editor) ->
8+
m.destroy() for m in markers
9+
markers = []
10+
11+
active = Stacktrace.getActivated()
12+
return unless active?
13+
14+
for frame in active.frames
15+
if frame.realPath is editor.getPath()
16+
range = editor.getBuffer().rangeForRow frame.bufferLineNumber()
17+
marker = editor.markBufferRange range
18+
editor.decorateMarker marker, type: 'line', class: 'line-stackframe'
19+
editor.decorateMarker marker, type: 'gutter', class: 'gutter-stackframe'
20+
markers.push marker

lib/main.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
EnterDialog = require './enter-dialog'
22
{Stacktrace} = require './stacktrace'
33
{StacktraceView} = require './stacktrace-view'
4+
editorDecorator = require './editor-decorator'
45

56
module.exports =
67

@@ -13,6 +14,10 @@ module.exports =
1314
text = (s.getText() for s in (selections or [])).join ''
1415
atom.emit 'stacktrace:accept-trace', trace: text
1516

17+
atom.workspace.eachEditor editorDecorator
18+
Stacktrace.on 'active-changed', ->
19+
editorDecorator(e) for e in atom.workspace.getEditors()
20+
1621
StacktraceView.registerIn(atom.workspace)
1722

1823
atom.on 'stacktrace:accept-trace', ({trace}) =>
@@ -21,6 +26,7 @@ module.exports =
2126
atom.workspace.open trace.getUrl()
2227

2328
deactivate: ->
29+
Stacktrace.off 'active-changed'
2430
atom.off 'stacktrace:accept-trace'
2531

2632
serialize: ->

lib/stacktrace.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class Frame
9292
constructor: (@rawLine, @rawPath, @lineNumber, @functionName) ->
9393
@realPath = @rawPath
9494

95+
# Public: Return the zero-indexed line number.
96+
#
97+
bufferLineNumber: -> @lineNumber - 1
98+
9599
# Public: Asynchronously collect n lines of context around the specified line number in this
96100
# frame's source file.
97101
#

spec/editor-marker-spec.coffee

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
path = require 'path'
2+
{Editor, WorkspaceView} = require 'atom'
3+
4+
{Stacktrace, Frame} = require '../lib/stacktrace'
5+
editorDecorator = require '../lib/editor-decorator'
6+
7+
framePath = (fname) -> path.join __dirname, 'fixtures', fname
8+
9+
frames = [
10+
new Frame('raw0', framePath('bottom.rb'), 12, 'botfunc')
11+
new Frame('raw1', framePath('middle.rb'), 42, 'midfunc')
12+
new Frame('raw2', framePath('top.rb'), 37, 'topfunc')
13+
new Frame('raw3', framePath('middle.rb'), 5, 'otherfunc')
14+
]
15+
trace = new Stacktrace(frames, 'Boom')
16+
17+
describe 'editorDecorator', ->
18+
[editor, editorView] = []
19+
20+
beforeEach ->
21+
atom.workspaceView = new WorkspaceView
22+
23+
afterEach ->
24+
Stacktrace.getActivated()?.deactivate()
25+
26+
withEditorOn = (fname, callback) ->
27+
waitsForPromise ->
28+
atom.workspace.open(framePath fname)
29+
30+
runs ->
31+
atom.workspaceView.attachToDom()
32+
editorView = atom.workspaceView.getActiveView()
33+
editor = editorView.getEditor()
34+
callback()
35+
36+
it 'does nothing if there is no active trace', ->
37+
expect(Stacktrace.getActivated()).toBeNull()
38+
39+
withEditorOn 'bottom.rb', ->
40+
editorDecorator(editor)
41+
expect(editorView.find '.line.line-stackframe').toHaveLength 0
42+
43+
describe 'with an active trace', ->
44+
45+
beforeEach -> trace.activate()
46+
47+
it "does nothing if the file doesn't appear in the active trace", ->
48+
withEditorOn 'context.txt', ->
49+
editorDecorator(editor)
50+
expect(editorView.find '.line.line-stackframe').toHaveLength 0
51+
52+
it 'decorates stackframe lines in applicable editors', ->
53+
withEditorOn 'bottom.rb', ->
54+
editorDecorator(editor)
55+
decorated = editorView.find '.line.line-stackframe'
56+
expect(decorated).toHaveLength 1
57+
expect(decorated.text()).toEqual(" puts 'this is the stack line'")
58+
59+
it 'removes prior decorations when deactivated', ->
60+
withEditorOn 'bottom.rb', ->
61+
editorDecorator(editor)
62+
trace.deactivate()
63+
editorDecorator(editor)
64+
expect(editorView.find '.line.line-stackframe').toHaveLength 0

spec/fixtures/bottom.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# This isn't a real Ruby file. It's a test fixture I can reference in other places.
2+
3+
4+
5+
6+
7+
8+
9+
10+
def botfunc
11+
before = true
12+
puts 'this is the stack line'
13+
after = false
14+
end

spec/fixtures/middle.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This isn't a real Ruby file. It's a test fixture I can reference in other places.
2+
# This one has two lines in the "stack trace".
3+
4+
def otherfunc
5+
puts 'this is the line'
6+
# and more stuff
7+
end
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
def botfunc
41+
# more things
42+
puts 'this is the line'
43+
end

spec/fixtures/top.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This isn't a real Ruby file. It's a test fixture I can reference in other places.
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
def topfunc
36+
# things
37+
puts 'this is the line'
38+
end

stylesheets/stacktrace.less

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// See https://github.com/atom/atom-dark-ui/blob/master/stylesheets/ui-variables.less
44
// for a full listing of what's available.
55
@import "ui-variables";
6+
@import "syntax-variables";
7+
8+
@stackframe-background: mix(@background-color-info, @syntax-background-color, 40%);
9+
@stackframe-gutter-background: mix(@background-color-info, @syntax-gutter-background-color, 40%);
610

711
.stacktrace {
812
&.enter-dialog .editor {
@@ -45,3 +49,13 @@
4549
}
4650

4751
}
52+
53+
.editor {
54+
.line.line-stackframe {
55+
background: @stackframe-background;
56+
}
57+
58+
.gutter .gutter-stackframe {
59+
background: @stackframe-gutter-background;
60+
}
61+
}

0 commit comments

Comments
 (0)