mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
WIP: Start on CommandInterpreter and substitution
This commit is contained in:
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)
|
||||
|
||||
Reference in New Issue
Block a user