More consistent method names among Highlighter, LineWrapper,
LineFolder, and LineMap
This commit is contained in:
Nathan Sobo
2012-02-22 17:24:27 -07:00
parent 852d066378
commit 237c03be7b
9 changed files with 86 additions and 86 deletions

View File

@@ -11,8 +11,8 @@ describe "Highlighter", ->
describe "constructor", ->
it "tokenizes all the lines in the buffer", ->
expect(highlighter.screenLineForRow(0).tokens[0]).toEqual(type: 'keyword.definition', value: 'var')
expect(highlighter.screenLineForRow(11).tokens[1]).toEqual(type: 'keyword', value: 'return')
expect(highlighter.lineForScreenRow(0).tokens[0]).toEqual(type: 'keyword.definition', value: 'var')
expect(highlighter.lineForScreenRow(11).tokens[1]).toEqual(type: 'keyword', value: 'return')
describe "when the buffer changes", ->
changeHandler = null
@@ -26,11 +26,11 @@ describe "Highlighter", ->
range = new Range([0, 0], [2, 0])
buffer.change(range, "foo()\nbar()\n")
expect(highlighter.screenLineForRow(0).tokens[0]).toEqual(type: 'identifier', value: 'foo')
expect(highlighter.screenLineForRow(1).tokens[0]).toEqual(type: 'identifier', value: 'bar')
expect(highlighter.lineForScreenRow(0).tokens[0]).toEqual(type: 'identifier', value: 'foo')
expect(highlighter.lineForScreenRow(1).tokens[0]).toEqual(type: 'identifier', value: 'bar')
# line 2 is unchanged
expect(highlighter.screenLineForRow(2).tokens[1]).toEqual(type: 'keyword', value: 'if')
expect(highlighter.lineForScreenRow(2).tokens[1]).toEqual(type: 'keyword', value: 'if')
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -43,9 +43,9 @@ describe "Highlighter", ->
changeHandler.reset()
buffer.insert([2, 0], '/*')
expect(highlighter.screenLineForRow(3).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(4).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(5).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(3).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(4).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(5).tokens[0].type).toBe 'comment'
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -57,7 +57,7 @@ describe "Highlighter", ->
buffer.insert([5, 0], '*/')
buffer.insert([1, 0], 'var ')
expect(highlighter.screenLineForRow(1).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(1).tokens[0].type).toBe 'comment'
describe "when lines are both updated and removed", ->
it "updates tokens to reflect the removed lines", ->
@@ -65,16 +65,16 @@ describe "Highlighter", ->
buffer.change(range, "foo()")
# previous line 0 remains
expect(highlighter.screenLineForRow(0).tokens[0]).toEqual(type: 'keyword.definition', value: 'var')
expect(highlighter.lineForScreenRow(0).tokens[0]).toEqual(type: 'keyword.definition', value: 'var')
# previous line 3 should be combined with input to form line 1
expect(highlighter.screenLineForRow(1).tokens[0]).toEqual(type: 'identifier', value: 'foo')
expect(highlighter.screenLineForRow(1).tokens[6]).toEqual(type: 'identifier', value: 'pivot')
expect(highlighter.lineForScreenRow(1).tokens[0]).toEqual(type: 'identifier', value: 'foo')
expect(highlighter.lineForScreenRow(1).tokens[6]).toEqual(type: 'identifier', value: 'pivot')
# lines below deleted regions should be shifted upward
expect(highlighter.screenLineForRow(2).tokens[1]).toEqual(type: 'keyword', value: 'while')
expect(highlighter.screenLineForRow(3).tokens[1]).toEqual(type: 'identifier', value: 'current')
expect(highlighter.screenLineForRow(4).tokens[3]).toEqual(type: 'keyword.operator', value: '<')
expect(highlighter.lineForScreenRow(2).tokens[1]).toEqual(type: 'keyword', value: 'while')
expect(highlighter.lineForScreenRow(3).tokens[1]).toEqual(type: 'identifier', value: 'current')
expect(highlighter.lineForScreenRow(4).tokens[3]).toEqual(type: 'keyword.operator', value: '<')
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -86,9 +86,9 @@ describe "Highlighter", ->
changeHandler.reset()
buffer.change(new Range([2, 0], [3, 0]), '/*')
expect(highlighter.screenLineForRow(2).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(3).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(4).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(2).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(3).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(4).tokens[0].type).toBe 'comment'
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -101,19 +101,19 @@ describe "Highlighter", ->
buffer.change(range, "foo()\nbar()\nbaz()\nquux()")
# previous line 0 remains
expect(highlighter.screenLineForRow(0).tokens[0]).toEqual(type: 'keyword.definition', value: 'var')
expect(highlighter.lineForScreenRow(0).tokens[0]).toEqual(type: 'keyword.definition', value: 'var')
# 3 new lines inserted
expect(highlighter.screenLineForRow(1).tokens[0]).toEqual(type: 'identifier', value: 'foo')
expect(highlighter.screenLineForRow(2).tokens[0]).toEqual(type: 'identifier', value: 'bar')
expect(highlighter.screenLineForRow(3).tokens[0]).toEqual(type: 'identifier', value: 'baz')
expect(highlighter.lineForScreenRow(1).tokens[0]).toEqual(type: 'identifier', value: 'foo')
expect(highlighter.lineForScreenRow(2).tokens[0]).toEqual(type: 'identifier', value: 'bar')
expect(highlighter.lineForScreenRow(3).tokens[0]).toEqual(type: 'identifier', value: 'baz')
# previous line 2 is joined with quux() on line 4
expect(highlighter.screenLineForRow(4).tokens[0]).toEqual(type: 'identifier', value: 'quux')
expect(highlighter.screenLineForRow(4).tokens[4]).toEqual(type: 'keyword', value: 'if')
expect(highlighter.lineForScreenRow(4).tokens[0]).toEqual(type: 'identifier', value: 'quux')
expect(highlighter.lineForScreenRow(4).tokens[4]).toEqual(type: 'keyword', value: 'if')
# previous line 3 is pushed down to become line 5
expect(highlighter.screenLineForRow(5).tokens[3]).toEqual(type: 'identifier', value: 'pivot')
expect(highlighter.lineForScreenRow(5).tokens[3]).toEqual(type: 'identifier', value: 'pivot')
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -125,13 +125,13 @@ describe "Highlighter", ->
changeHandler.reset()
buffer.insert([2, 0], '/*\nabcde\nabcder')
expect(highlighter.screenLineForRow(2).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(3).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(4).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(5).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(6).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(7).tokens[0].type).toBe 'comment'
expect(highlighter.screenLineForRow(8).tokens[0].type).not.toBe 'comment'
expect(highlighter.lineForScreenRow(2).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(3).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(4).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(5).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(6).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(7).tokens[0].type).toBe 'comment'
expect(highlighter.lineForScreenRow(8).tokens[0].type).not.toBe 'comment'
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]

View File

@@ -12,7 +12,7 @@ describe "LineMap", ->
buffer = new Buffer(require.resolve 'fixtures/sample.js')
highlighter = new Highlighter(buffer)
map = new LineMap
[line0, line1, line2, line3, line4] = highlighter.screenLinesForRows(0, 4)
[line0, line1, line2, line3, line4] = highlighter.linesForScreenRows(0, 4)
describe ".insertAtBufferRow(row, screenLine(s))", ->
describe "when passed a single, line fragment", ->
@@ -192,7 +192,7 @@ describe "LineMap", ->
it "wraps buffer positions at the end of a screen line to the end end of the next screen line", ->
expect(map.screenPositionForBufferPosition([4, 20], true)).toEqual [3, 0]
describe ".screenLineCount()", ->
describe ".lineCount()", ->
it "returns the total of all inserted screen row deltas", ->
[line1a, line1b] = line1.splitAt(10)
[line3a, line3b] = line3.splitAt(10)
@@ -201,6 +201,6 @@ describe "LineMap", ->
map.insertAtBufferRow(0, [line0, line1a, line1b, line2])
expect(map.screenLineCount()).toBe 4
expect(map.lineCount()).toBe 4

View File

@@ -16,22 +16,22 @@ describe "LineWrapper", ->
describe ".tokensForScreenRow(row)", ->
it "returns tokens for the line fragment corresponding to the given screen row", ->
expect(tokensText wrapper.screenLineForRow(3).tokens).toEqual(' var pivot = items.shift(), current, left = [], ')
expect(tokensText wrapper.screenLineForRow(4).tokens).toEqual('right = [];')
expect(tokensText wrapper.screenLineForRow(5).tokens).toEqual(' while(items.length > 0) {')
expect(tokensText wrapper.lineForScreenRow(3).tokens).toEqual(' var pivot = items.shift(), current, left = [], ')
expect(tokensText wrapper.lineForScreenRow(4).tokens).toEqual('right = [];')
expect(tokensText wrapper.lineForScreenRow(5).tokens).toEqual(' while(items.length > 0) {')
describe ".screenLineCount()", ->
describe ".lineCount()", ->
it "returns the total number of screen lines", ->
expect(wrapper.screenLineCount()).toBe 16
expect(wrapper.lineCount()).toBe 16
describe "when the buffer changes", ->
describe "when a buffer line is updated", ->
describe "when the number of screen lines remains the same for the changed buffer line", ->
it "re-wraps the existing lines and emits a change event for all its screen lines", ->
buffer.insert([6, 28], '1234567')
expect(wrapper.screenLineForRow(7).text).toBe ' current < pivot ? left1234567.push(current) '
expect(wrapper.screenLineForRow(8).text).toBe ': right.push(current);'
expect(wrapper.screenLineForRow(9).text).toBe ' }'
expect(wrapper.lineForScreenRow(7).text).toBe ' current < pivot ? left1234567.push(current) '
expect(wrapper.lineForScreenRow(8).text).toBe ': right.push(current);'
expect(wrapper.lineForScreenRow(9).text).toBe ' }'
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -41,10 +41,10 @@ describe "LineWrapper", ->
describe "when the number of screen lines increases for the changed buffer line", ->
it "re-wraps and adds an additional screen line and emits a change event for all screen lines", ->
buffer.insert([6, 28], '1234567890')
expect(wrapper.screenLineForRow(7).text).toBe ' current < pivot ? '
expect(wrapper.screenLineForRow(8).text).toBe 'left1234567890.push(current) : '
expect(wrapper.screenLineForRow(9).text).toBe 'right.push(current);'
expect(wrapper.screenLineForRow(10).text).toBe ' }'
expect(wrapper.lineForScreenRow(7).text).toBe ' current < pivot ? '
expect(wrapper.lineForScreenRow(8).text).toBe 'left1234567890.push(current) : '
expect(wrapper.lineForScreenRow(9).text).toBe 'right.push(current);'
expect(wrapper.lineForScreenRow(10).text).toBe ' }'
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -54,8 +54,8 @@ describe "LineWrapper", ->
describe "when the number of screen lines decreases for the changed buffer line", ->
it "re-wraps and removes a screen line and emits a change event for all screen lines", ->
buffer.change(new Range([6, 24], [6, 42]), '')
expect(wrapper.screenLineForRow(7).text).toBe ' current < pivot ? : right.push(current);'
expect(wrapper.screenLineForRow(8).text).toBe ' }'
expect(wrapper.lineForScreenRow(7).text).toBe ' current < pivot ? : right.push(current);'
expect(wrapper.lineForScreenRow(8).text).toBe ' }'
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -65,10 +65,10 @@ describe "LineWrapper", ->
describe "when buffer lines are inserted", ->
it "re-wraps existing and new screen lines and emits a change event", ->
buffer.insert([6, 21], '1234567890 abcdefghij 1234567890\nabcdefghij')
expect(wrapper.screenLineForRow(7).text).toBe ' current < pivot1234567890 abcdefghij '
expect(wrapper.screenLineForRow(8).text).toBe '1234567890'
expect(wrapper.screenLineForRow(9).text).toBe 'abcdefghij ? left.push(current) : '
expect(wrapper.screenLineForRow(10).text).toBe 'right.push(current);'
expect(wrapper.lineForScreenRow(7).text).toBe ' current < pivot1234567890 abcdefghij '
expect(wrapper.lineForScreenRow(8).text).toBe '1234567890'
expect(wrapper.lineForScreenRow(9).text).toBe 'abcdefghij ? left.push(current) : '
expect(wrapper.lineForScreenRow(10).text).toBe 'right.push(current);'
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -78,10 +78,10 @@ describe "LineWrapper", ->
describe "when buffer lines are removed", ->
it "removes screen lines and emits a change event", ->
buffer.change(new Range([3, 21], [7, 5]), ';')
expect(wrapper.screenLineForRow(3).text).toBe ' var pivot = items;'
expect(wrapper.screenLineForRow(4).text).toBe ' return '
expect(wrapper.screenLineForRow(5).text).toBe 'sort(left).concat(pivot).concat(sort(right));'
expect(wrapper.screenLineForRow(6).text).toBe ' };'
expect(wrapper.lineForScreenRow(3).text).toBe ' var pivot = items;'
expect(wrapper.lineForScreenRow(4).text).toBe ' return '
expect(wrapper.lineForScreenRow(5).text).toBe 'sort(left).concat(pivot).concat(sort(right));'
expect(wrapper.lineForScreenRow(6).text).toBe ' };'
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]
@@ -91,9 +91,9 @@ describe "LineWrapper", ->
describe ".setMaxLength(length)", ->
it "changes the length at which lines are wrapped and emits a change event for all screen lines", ->
wrapper.setMaxLength(40)
expect(tokensText wrapper.screenLineForRow(4).tokens).toBe 'left = [], right = [];'
expect(tokensText wrapper.screenLineForRow(5).tokens).toBe ' while(items.length > 0) {'
expect(tokensText wrapper.screenLineForRow(12).tokens).toBe 'sort(left).concat(pivot).concat(sort(rig'
expect(tokensText wrapper.lineForScreenRow(4).tokens).toBe 'left = [], right = [];'
expect(tokensText wrapper.lineForScreenRow(5).tokens).toBe ' while(items.length > 0) {'
expect(tokensText wrapper.lineForScreenRow(12).tokens).toBe 'sort(left).concat(pivot).concat(sort(rig'
expect(changeHandler).toHaveBeenCalled()
[event] = changeHandler.argsForCall[0]

View File

@@ -8,7 +8,7 @@ describe "screenLineFragment", ->
beforeEach ->
buffer = new Buffer(require.resolve 'fixtures/sample.js')
highlighter = new Highlighter(buffer)
screenLine = highlighter.screenLineForRow(3)
screenLine = highlighter.lineForScreenRow(3)
describe ".splitAt(column)", ->
it "breaks the line fragment into two fragments", ->
@@ -65,7 +65,7 @@ describe "screenLineFragment", ->
[left, right] = screenLine.splitAt(14)
expect(left.concat(right)).toEqual screenLine
concatenated = screenLine.concat(highlighter.screenLineForRow(4))
concatenated = screenLine.concat(highlighter.lineForScreenRow(4))
expect(concatenated.text).toBe ' var pivot = items.shift(), current, left = [], right = []; while(items.length > 0) {'
expect(tokensText concatenated.tokens).toBe concatenated.text
expect(concatenated.screenDelta).toEqual [2, 0]

View File

@@ -129,7 +129,7 @@ class Editor extends View
renderLines: ->
@lines.empty()
for screenLine in @lineWrapper.screenLines()
for screenLine in @lineWrapper.lines()
@lines.append @buildLineElement(screenLine)
setBuffer: (@buffer) ->
@@ -144,7 +144,7 @@ class Editor extends View
@lineWrapper.on 'change', (e) =>
{ oldRange, newRange } = e
screenLines = @lineWrapper.screenLinesForRows(newRange.start.row, newRange.end.row)
screenLines = @lineWrapper.linesForScreenRows(newRange.start.row, newRange.end.row)
if newRange.end.row > oldRange.end.row
# update, then insert elements
for row in [newRange.start.row..newRange.end.row]

View File

@@ -10,7 +10,7 @@ class Highlighter
constructor: (@buffer) ->
@buildTokenizer()
@screenLines = @buildScreenLinesForRows('start', 0, @buffer.lastRow())
@screenLines = @buildLinesForScreenRows('start', 0, @buffer.lastRow())
@buffer.on 'change', (e) => @handleBufferChange(e)
buildTokenizer: ->
@@ -24,7 +24,7 @@ class Highlighter
startState = @screenLines[newRange.start.row - 1]?.state or 'start'
@screenLines[oldRange.start.row..oldRange.end.row] =
@buildScreenLinesForRows(startState, newRange.start.row, newRange.end.row)
@buildLinesForScreenRows(startState, newRange.start.row, newRange.end.row)
# spill detection
# compare scanner state of last re-highlighted line with its previous state.
@@ -35,7 +35,7 @@ class Highlighter
break if @screenLines[row].state == previousState
nextRow = row + 1
previousState = @screenLines[nextRow].state
@screenLines[nextRow] = @buildScreenLineForRow(@screenLines[row].state, nextRow)
@screenLines[nextRow] = @buildLineForScreenRow(@screenLines[row].state, nextRow)
# if highlighting spilled beyond the bounds of the textual change, update
# the pre and post range to reflect area of highlight changes
@@ -48,22 +48,22 @@ class Highlighter
@trigger("change", {oldRange, newRange})
buildScreenLinesForRows: (startState, startRow, endRow) ->
buildLinesForScreenRows: (startState, startRow, endRow) ->
state = startState
for row in [startRow..endRow]
screenLine = @buildScreenLineForRow(state, row)
screenLine = @buildLineForScreenRow(state, row)
state = screenLine.state
screenLine
buildScreenLineForRow: (state, row) ->
buildLineForScreenRow: (state, row) ->
line = @buffer.getLine(row)
{tokens, state} = @tokenizer.getLineTokens(line, state)
new ScreenLineFragment(tokens, line, [1, 0], [1, 0], { state })
screenLineForRow: (row) ->
lineForScreenRow: (row) ->
@screenLines[row]
screenLinesForRows: (startRow, endRow) ->
linesForScreenRows: (startRow, endRow) ->
@screenLines[startRow..endRow]
lastRow: ->

View File

@@ -62,7 +62,7 @@ class LineFolder
@renderScreenLineForBufferRow(@bufferRowForScreenRow(screenRow))
renderScreenLineForBufferRow: (bufferRow, startColumn=0) ->
screenLine = @highlighter.screenLineForRow(bufferRow).splitAt(startColumn)[1]
screenLine = @highlighter.lineForScreenRow(bufferRow).splitAt(startColumn)[1]
for fold in @foldsForBufferRow(bufferRow)
{ start, end } = fold.range
if start.column > startColumn

View File

@@ -106,7 +106,7 @@ class LineMap
delta = delta.add(screenLine.bufferDelta)
delta.rows
screenLineCount: ->
lineCount: ->
delta = new Delta
for screenLine in @screenLines
delta = delta.add(screenLine.screenDelta)

View File

@@ -12,9 +12,9 @@ class LineWrapper
@highlighter.on 'change', (e) => @handleChange(e)
setMaxLength: (@maxLength) ->
oldRange = @rangeForAllScreenLines()
oldRange = @rangeForAllLines()
@buildLineMap()
newRange = @rangeForAllScreenLines()
newRange = @rangeForAllLines()
@trigger 'change', { oldRange, newRange }
buildLineMap: ->
@@ -36,14 +36,14 @@ class LineWrapper
{ start, end } = bufferRange
new Range([start.row, 0], [end.row, @lineMap.lineForBufferRow(end.row).text.length])
rangeForAllScreenLines: ->
endRow = @screenLineCount() - 1
rangeForAllLines: ->
endRow = @lineCount() - 1
endColumn = @lineMap.lineForScreenRow(endRow).text.length
new Range([0, 0], [endRow, endColumn])
buildScreenLinesForBufferRows: (start, end) ->
_(@highlighter
.screenLinesForRows(start, end)
.linesForScreenRows(start, end)
.map((screenLine) => @wrapScreenLine(screenLine))).flatten()
wrapScreenLine: (screenLine, startColumn=0) ->
@@ -86,16 +86,16 @@ class LineWrapper
bufferPositionForScreenPosition: (screenPosition) ->
@lineMap.bufferPositionForScreenPosition(screenPosition)
screenLineForRow: (screenRow) ->
@screenLinesForRows(screenRow, screenRow)[0]
lineForScreenRow: (screenRow) ->
@linesForScreenRows(screenRow, screenRow)[0]
screenLinesForRows: (startRow, endRow) ->
linesForScreenRows: (startRow, endRow) ->
@lineMap.linesForScreenRows(startRow, endRow)
screenLines: ->
@screenLinesForRows(0, @screenLineCount() - 1)
lines: ->
@linesForScreenRows(0, @lineCount() - 1)
screenLineCount: ->
@lineMap.screenLineCount()
lineCount: ->
@lineMap.lineCount()
_.extend(LineWrapper.prototype, EventEmitter)