mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2025-01-08 16:19:17 +03:00
WIP: Start on CommandInterpreter and substitution
This commit is contained in:
parent
a91c8098a3
commit
909337bc1c
18
spec/atom/command-interpreter-spec.coffee
Normal file
18
spec/atom/command-interpreter-spec.coffee
Normal 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);'
|
||||
|
12
src/atom/command-interpreter.coffee
Normal file
12
src/atom/command-interpreter.coffee
Normal 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)
|
||||
|
22
src/atom/command-interpreter/substitution.coffee
Normal file
22
src/atom/command-interpreter/substitution.coffee
Normal 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
8
src/atom/commands.pegjs
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
var Substitution = require('command-interpreter/substitution');
|
||||
}
|
||||
|
||||
substitution
|
||||
= "s" _ "/" find:([^/]*) "/" replace:([^/]*) "/" { return new Substitution(find.join(''), replace.join('')) }
|
||||
|
||||
_ = " "*
|
@ -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
5141
vendor/pegjs.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user