WIP: Adding find all matches in project command

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-07-17 14:26:12 -06:00
parent c3fe9aa0b3
commit 34e96fb8d7
8 changed files with 65 additions and 15 deletions

View File

@@ -1,11 +1,13 @@
CommandInterpreter = require 'command-panel/command-interpreter'
Project = require 'project'
Buffer = require 'buffer'
EditSession = require 'edit-session'
describe "CommandInterpreter", ->
[interpreter, editSession, buffer, anchorCountBefore] = []
[project, interpreter, editSession, buffer, anchorCountBefore] = []
beforeEach ->
project = new Project(fixturesProject.resolve('dir/'))
interpreter = new CommandInterpreter(fixturesProject)
editSession = fixturesProject.open('sample.js')
buffer = editSession.buffer
@@ -303,3 +305,21 @@ describe "CommandInterpreter", ->
runs ->
expect(editSession.getSelectedBufferRanges()).toEqual [[[5, 0], [5, 16]], [[6, 0], [6, 36]]]
describe "X x/regex/", ->
it "returns selection operations for all regex matches in all the project's files", ->
editSession.destroy()
project = new Project(fixturesProject.resolve('dir/'))
interpreter = new CommandInterpreter(fixturesProject)
editSession = project.open('a')
operations = null
waitsForPromise ->
interpreter.eval("X x/a+/", editSession).done (ops) ->
operations = ops
runs ->
console.log operations
operation.destroy() for operation in operations

View File

@@ -0,0 +1 @@
bbb aaaa

View File

@@ -77,13 +77,7 @@ class Project
setSoftWrap: (@softWrap) ->
open: (filePath, editSessionOptions={}) ->
if filePath?
filePath = @resolve(filePath)
buffer = @bufferWithPath(filePath) ? @buildBuffer(filePath)
else
buffer = @buildBuffer()
@buildEditSession(buffer, editSessionOptions)
@buildEditSession(@bufferForPath(filePath), editSessionOptions)
buildEditSession: (buffer, editSessionOptions) ->
options = _.extend(@defaultEditSessionOptions(), editSessionOptions)
@@ -116,8 +110,13 @@ class Project
buffers
bufferWithPath: (path) ->
return editSession.buffer for editSession in @editSessions when editSession.buffer.getPath() == path
bufferForPath: (filePath) ->
if filePath?
filePath = @resolve(filePath)
return editSession.buffer for editSession in @editSessions when editSession.buffer.getPath() == filePath
@buildBuffer(filePath)
else
@buildBuffer()
buildBuffer: (filePath) ->
buffer = new Buffer(filePath)

View File

@@ -8,6 +8,7 @@
var CurrentSelectionAddress = require('command-panel/commands/current-selection-address')
var RegexAddress = require('command-panel/commands/regex-address')
var SelectAllMatches = require('command-panel/commands/select-all-matches')
var SelectAllMatchesInProject = require('command-panel/commands/select-all-matches-in-project')
}
start = expressions:(expression+) {
@@ -35,7 +36,7 @@ primitiveAddress
regexAddress
= reverse:'-'? '/' pattern:pattern '/'? { return new RegexAddress(pattern, reverse.length > 0)}
command = substitution / selectAllMatches
command = substitution / selectAllMatches / selectAllMatchesInProject
substitution
= "s" _ "/" find:pattern "/" replace:pattern "/" _ options:[g]* {
@@ -45,6 +46,9 @@ substitution
selectAllMatches
= 'x' _ '/' pattern:pattern '/'? { return new SelectAllMatches(pattern) }
selectAllMatchesInProject
= 'X' _ 'x' _ '/' pattern:pattern '/'? { return new SelectAllMatchesInProject(pattern) }
pattern
= pattern:[^/]* { return pattern.join('') }

View File

@@ -4,3 +4,4 @@ module.exports =
class Command
isAddress: -> false
preserveSelections: false
previewOperations: false

View File

@@ -22,10 +22,13 @@ class CompositeCommand
deferred.resolve()
else
editSession.clearAllSelections() unless currentCommand.preserveSelections
for operation in operations
operation.execute(editSession)
operation.destroy()
deferred.resolve()
if currentCommand.previewOperations
deferred.resolve(operations)
else
for operation in operations
operation.execute(editSession)
operation.destroy()
deferred.resolve()
deferred.promise()

View File

@@ -0,0 +1,20 @@
Command = require 'command-panel/commands/command'
Operation = require 'command-panel/operation'
$ = require 'jquery'
module.exports =
class SelectAllMatchesInProject extends Command
regex: null
previewOperations: true
constructor: (pattern) ->
@regex = new RegExp(pattern, 'g')
compile: (project, buffer, range) ->
deferred = $.Deferred()
operations = []
promise = project.scan @regex, ({path, range}) ->
operations.push(new Operation(buffer: project.bufferForPath(path), bufferRange: range))
promise.done -> deferred.resolve(operations)
deferred.promise()

View File

@@ -1,6 +1,7 @@
module.exports =
class Operation
constructor: ({@buffer, bufferRange, @newText, @preserveSelection}) ->
@buffer.retain()
@anchorRange = @buffer.addAnchorRange(bufferRange)
getBufferRange: ->
@@ -11,4 +12,5 @@ class Operation
editSession.addSelectionForBufferRange(@getBufferRange()) unless @preserveSelection
destroy: ->
@buffer.release()
@anchorRange.destroy()