mirror of
https://github.com/atom/atom.git
synced 2026-01-23 05:48:10 -05:00
WIP: Adding find all matches in project command
This commit is contained in:
@@ -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
|
||||
|
||||
1
spec/fixtures/dir/a-dir/oh-git
vendored
1
spec/fixtures/dir/a-dir/oh-git
vendored
@@ -0,0 +1 @@
|
||||
bbb aaaa
|
||||
@@ -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)
|
||||
|
||||
@@ -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('') }
|
||||
|
||||
|
||||
@@ -4,3 +4,4 @@ module.exports =
|
||||
class Command
|
||||
isAddress: -> false
|
||||
preserveSelections: false
|
||||
previewOperations: false
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user