Merge branch 'sam'

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-03-21 14:45:36 -07:00
8 changed files with 5234 additions and 0 deletions

View File

@@ -203,3 +203,21 @@ describe 'Buffer', ->
lineLength = buffer.lineForRow(2).length
range = [[2,10], [4,10]]
expect(buffer.getTextInRange(range)).toBe "ems.length <= 1) return items;\n var pivot = items.shift(), current, left = [], right = [];\n while("
describe ".characterIndexForPosition(position)", ->
it "returns the total number of charachters that precede the given position", ->
expect(buffer.characterIndexForPosition([0, 0])).toBe 0
expect(buffer.characterIndexForPosition([0, 1])).toBe 1
expect(buffer.characterIndexForPosition([0, 29])).toBe 29
expect(buffer.characterIndexForPosition([1, 0])).toBe 30
expect(buffer.characterIndexForPosition([2, 0])).toBe 61
expect(buffer.characterIndexForPosition([12, 2])).toBe 408
describe ".positionForCharacterIndex(position)", ->
it "returns the position based on charachter index", ->
expect(buffer.positionForCharacterIndex(0)).toEqual [0, 0]
expect(buffer.positionForCharacterIndex(1)).toEqual [0, 1]
expect(buffer.positionForCharacterIndex(29)).toEqual [0, 29]
expect(buffer.positionForCharacterIndex(30)).toEqual [1, 0]
expect(buffer.positionForCharacterIndex(61)).toEqual [2, 0]
expect(buffer.positionForCharacterIndex(408)).toEqual [12, 2]

View File

@@ -0,0 +1,18 @@
CommandInterpreter = require 'command-interpreter'
Buffer = require 'buffer'
Editor = require 'editor'
describe "CommandInterpreter", ->
[interpreter, editor, buffer] = []
beforeEach ->
buffer = new Buffer(require.resolve 'fixtures/sample.js')
editor = new Editor({buffer})
interpreter = new CommandInterpreter(editor)
describe "substitution", ->
it "performs the substitution within the current dot", ->
editor.selection.setBufferRange([[6, 0], [6, 44]])
interpreter.eval('s/current/foo/')
expect(buffer.lineForRow(6)).toBe ' foo < pivot ? left.push(current) : right.push(current);'

View File

@@ -1,5 +1,6 @@
_ = require 'underscore'
fs = require 'fs'
Point = require 'point'
Range = require 'range'
EventEmitter = require 'event-emitter'
UndoManager = require 'undo-manager'
@@ -59,6 +60,21 @@ class Buffer
lastLine: ->
@lineForRow(@lastRow())
characterIndexForPosition: (position) ->
position = Point.fromObject(position)
index = 0
index += @getLineLength(row) + 1 for row in [0...position.row]
index + position.column
positionForCharacterIndex: (index) ->
row = 0
while index >= (lineLength = @getLineLength(row) + 1)
index -= lineLength
row++
new Point(row, index)
deleteRow: (row) ->
range = null
if row == @lastRow()

View File

@@ -0,0 +1,12 @@
fs = require 'fs'
PEG = require 'pegjs'
module.exports =
class CommandInterpreter
constructor: (@editor) ->
@parser = PEG.buildParser(fs.read(require.resolve 'commands.pegjs'))
eval: (command) ->
operation = @parser.parse(command)
operation.perform(@editor)

View File

@@ -0,0 +1,20 @@
module.exports =
class Substitution
constructor: (@findText, @replaceText) ->
@findRegex = new RegExp(@findText)
perform: (editor) ->
{ buffer } = editor
selectedText = editor.getSelectedText()
selectionStartIndex = buffer.characterIndexForPosition(editor.getSelection().getBufferRange().start)
match = @findRegex.exec(selectedText)
matchStartIndex = selectionStartIndex + match.index
matchEndIndex = matchStartIndex + match[0].length
startPosition = buffer.positionForCharacterIndex(matchStartIndex)
endPosition = buffer.positionForCharacterIndex(matchEndIndex)
buffer.change([startPosition, endPosition], @replaceText)

8
src/atom/commands.pegjs Normal file
View File

@@ -0,0 +1,8 @@
{
var Substitution = require('command-interpreter/substitution');
}
substitution
= "s" _ "/" find:([^/]*) "/" replace:([^/]*) "/" { return new Substitution(find.join(''), replace.join('')) }
_ = " "*

View File

@@ -61,6 +61,7 @@ class LineMap
@translatePosition('screenDelta', 'bufferDelta', screenPosition)
screenRangeForBufferRange: (bufferRange) ->
bufferRange = Range.fromObject(bufferRange)
start = @screenPositionForBufferPosition(bufferRange.start)
end = @screenPositionForBufferPosition(bufferRange.end)
new Range(start, end)

5141
vendor/pegjs.js vendored Normal file

File diff suppressed because it is too large Load Diff