WIP: Start on CommandInterpreter and substitution

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-03-21 14:59:49 -06:00
parent a91c8098a3
commit 909337bc1c
6 changed files with 5202 additions and 0 deletions

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", ->
fit "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

@ -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,22 @@
module.exports =
class Substitution
constructor: (@findText, @replaceText) ->
@findRegex = new RegExp(@findText)
perform: (editor) ->
{ buffer } = editor
selectedText = editor.getSelectedText()
# console.log editor.getSelection().getBufferRange()
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], @replace)

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