mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Use a legitimate replace method in AceAdaptor.
Fixes comment toggling in coffeescript, and requires a better implementation of findMatchingBracket in AceAdaptor for outdent to work.
This commit is contained in:
@@ -9,24 +9,45 @@ describe "TokenizedBuffer", ->
|
||||
buffer = new Buffer(require.resolve('fixtures/sample.js'))
|
||||
tokenizedBuffer = new TokenizedBuffer(buffer, ' ')
|
||||
|
||||
describe ".findClosingBracket(startBracketPosition)", ->
|
||||
describe "when called with a bracket type of '{'", ->
|
||||
it "returns the position of the matching bracket, skipping any nested brackets", ->
|
||||
expect(tokenizedBuffer.findClosingBracket([1, 29])).toEqual [9, 2]
|
||||
describe ".findClosingBracket(startBufferPosition)", ->
|
||||
it "returns the position of the matching bracket, skipping any nested brackets", ->
|
||||
expect(tokenizedBuffer.findClosingBracket([1, 29])).toEqual [9, 2]
|
||||
|
||||
describe ".findOpeningBracket(closingBufferPosition)", ->
|
||||
it "returns the position of the matching bracket, skipping any nested brackets", ->
|
||||
expect(tokenizedBuffer.findOpeningBracket([9, 2])).toEqual [1, 29]
|
||||
|
||||
describe ".toggleLineCommentsInRange(range)", ->
|
||||
it "comments/uncomments lines in the given range", ->
|
||||
tokenizedBuffer.toggleLineCommentsInRange([[4, 5], [7, 8]])
|
||||
expect(buffer.lineForRow(4)).toBe "// while(items.length > 0) {"
|
||||
expect(buffer.lineForRow(5)).toBe "// current = items.shift();"
|
||||
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
|
||||
expect(buffer.lineForRow(7)).toBe "// }"
|
||||
describe "javascript", ->
|
||||
it "comments/uncomments lines in the given range", ->
|
||||
tokenizedBuffer.toggleLineCommentsInRange([[4, 5], [7, 8]])
|
||||
expect(buffer.lineForRow(4)).toBe "// while(items.length > 0) {"
|
||||
expect(buffer.lineForRow(5)).toBe "// current = items.shift();"
|
||||
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
|
||||
expect(buffer.lineForRow(7)).toBe "// }"
|
||||
|
||||
tokenizedBuffer.toggleLineCommentsInRange([[4, 5], [5, 8]])
|
||||
expect(buffer.lineForRow(4)).toBe " while(items.length > 0) {"
|
||||
expect(buffer.lineForRow(5)).toBe " current = items.shift();"
|
||||
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
|
||||
expect(buffer.lineForRow(7)).toBe "// }"
|
||||
tokenizedBuffer.toggleLineCommentsInRange([[4, 5], [5, 8]])
|
||||
expect(buffer.lineForRow(4)).toBe " while(items.length > 0) {"
|
||||
expect(buffer.lineForRow(5)).toBe " current = items.shift();"
|
||||
expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);"
|
||||
expect(buffer.lineForRow(7)).toBe "// }"
|
||||
|
||||
describe "coffeescript", ->
|
||||
it "comments/uncomments lines in the given range", ->
|
||||
buffer = new Buffer(require.resolve('fixtures/coffee.coffee'))
|
||||
tokenizedBuffer = new TokenizedBuffer(buffer, ' ')
|
||||
|
||||
tokenizedBuffer.toggleLineCommentsInRange([[4, 5], [7, 8]])
|
||||
expect(buffer.lineForRow(4)).toBe " #pivot = items.shift()"
|
||||
expect(buffer.lineForRow(5)).toBe " #left = []"
|
||||
expect(buffer.lineForRow(6)).toBe " #right = []"
|
||||
expect(buffer.lineForRow(7)).toBe "#"
|
||||
|
||||
tokenizedBuffer.toggleLineCommentsInRange([[4, 5], [5, 8]])
|
||||
expect(buffer.lineForRow(4)).toBe " pivot = items.shift()"
|
||||
expect(buffer.lineForRow(5)).toBe " left = []"
|
||||
expect(buffer.lineForRow(6)).toBe " #right = []"
|
||||
expect(buffer.lineForRow(7)).toBe "#"
|
||||
|
||||
describe "fold suggestion", ->
|
||||
describe "javascript", ->
|
||||
@@ -216,4 +237,3 @@ describe "TokenizedBuffer", ->
|
||||
expect(tokens[1].isAtomic).toBeTruthy()
|
||||
|
||||
expect(tokenizedBuffer.lineForScreenRow(2).text).toBe "#{tabText} buy()#{tabText}while supply > demand"
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
Range = require 'range'
|
||||
|
||||
module.exports =
|
||||
class AceAdaptor
|
||||
foldWidgets: {}
|
||||
@@ -22,12 +24,5 @@ class AceAdaptor
|
||||
range = Range.fromObject(range)
|
||||
@buffer.change(range, text)
|
||||
|
||||
# We don't care where the bracket is; we always outdent one level
|
||||
findMatchingBracket: ({row, column}) ->
|
||||
{row: 0, column: 0}
|
||||
|
||||
# Does not actually replace text; always outdents one level
|
||||
replace: (range, text) ->
|
||||
start = range.start
|
||||
end = {row: range.start.row, column: range.start.column + @tokenizedBuffer.tabText.length}
|
||||
@buffer.change([start, end], "")
|
||||
@tokenizedBuffer.findOpeningBracket([row, column])
|
||||
@@ -141,6 +141,36 @@ class TokenizedBuffer
|
||||
return unless keepLooping
|
||||
bufferColumn += token.bufferDelta
|
||||
|
||||
backwardsIterateTokensInBufferRange: (bufferRange, iterator) ->
|
||||
bufferRange = Range.fromObject(bufferRange)
|
||||
{ start, end } = bufferRange
|
||||
|
||||
keepLooping = true
|
||||
stop = -> keepLooping = false
|
||||
|
||||
for bufferRow in [end.row..start.row]
|
||||
bufferColumn = @buffer.lineLengthForRow(bufferRow)
|
||||
for token in new Array(@screenLines[bufferRow].tokens...).reverse()
|
||||
bufferColumn -= token.bufferDelta
|
||||
startOfToken = new Point(bufferRow, bufferColumn)
|
||||
iterator(token, startOfToken, { stop }) if bufferRange.containsPoint(startOfToken)
|
||||
return unless keepLooping
|
||||
|
||||
findOpeningBracket: (startBufferPosition) ->
|
||||
range = [[0,0], startBufferPosition]
|
||||
position = null
|
||||
depth = 0
|
||||
@backwardsIterateTokensInBufferRange range, (token, startPosition, { stop }) ->
|
||||
if token.type.match /lparen|rparen/
|
||||
if token.value == '}'
|
||||
depth++
|
||||
else if token.value == '{'
|
||||
depth--
|
||||
if depth == 0
|
||||
position = startPosition
|
||||
stop()
|
||||
position
|
||||
|
||||
findClosingBracket: (startBufferPosition) ->
|
||||
range = [startBufferPosition, @buffer.getEofPosition()]
|
||||
position = null
|
||||
|
||||
Reference in New Issue
Block a user