From bdfc862dac5905989f2c5b71fe8744476d540f17 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 29 Dec 2014 11:43:17 -0600 Subject: [PATCH] Move Project::replace to Workspace --- spec/project-spec.coffee | 100 ------------------------------------ spec/workspace-spec.coffee | 102 ++++++++++++++++++++++++++++++++++++- src/project.coffee | 39 +------------- src/workspace.coffee | 41 +++++++++++++++ 4 files changed, 144 insertions(+), 138 deletions(-) diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index d31164ae3..0583a0749 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -194,106 +194,6 @@ describe "Project", -> expect(atom.project.getPaths()[0]).toEqual path.dirname(require.resolve('./fixtures/dir/a')) expect(atom.project.getDirectories()[0].path).toEqual path.dirname(require.resolve('./fixtures/dir/a')) - describe ".replace()", -> - [filePath, commentFilePath, sampleContent, sampleCommentContent] = [] - - beforeEach -> - atom.project.setPaths([atom.project.resolve('../')]) - - filePath = atom.project.resolve('sample.js') - commentFilePath = atom.project.resolve('sample-with-comments.js') - sampleContent = fs.readFileSync(filePath).toString() - sampleCommentContent = fs.readFileSync(commentFilePath).toString() - - afterEach -> - fs.writeFileSync(filePath, sampleContent) - fs.writeFileSync(commentFilePath, sampleCommentContent) - - describe "when a file doesn't exist", -> - it "calls back with an error", -> - errors = [] - missingPath = path.resolve('/not-a-file.js') - expect(fs.existsSync(missingPath)).toBeFalsy() - - waitsForPromise -> - atom.project.replace /items/gi, 'items', [missingPath], (result, error) -> - errors.push(error) - - runs -> - expect(errors).toHaveLength 1 - expect(errors[0].path).toBe missingPath - - describe "when called with unopened files", -> - it "replaces properly", -> - results = [] - waitsForPromise -> - atom.project.replace /items/gi, 'items', [filePath], (result) -> - results.push(result) - - runs -> - expect(results).toHaveLength 1 - expect(results[0].filePath).toBe filePath - expect(results[0].replacements).toBe 6 - - describe "when a buffer is already open", -> - it "replaces properly and saves when not modified", -> - editor = null - results = [] - - waitsForPromise -> - atom.project.open('sample.js').then (o) -> editor = o - - runs -> - expect(editor.isModified()).toBeFalsy() - - waitsForPromise -> - atom.project.replace /items/gi, 'items', [filePath], (result) -> - results.push(result) - - runs -> - expect(results).toHaveLength 1 - expect(results[0].filePath).toBe filePath - expect(results[0].replacements).toBe 6 - - expect(editor.isModified()).toBeFalsy() - - it "does not replace when the path is not specified", -> - editor = null - results = [] - - waitsForPromise -> - atom.project.open('sample-with-comments.js').then (o) -> editor = o - - waitsForPromise -> - atom.project.replace /items/gi, 'items', [commentFilePath], (result) -> - results.push(result) - - runs -> - expect(results).toHaveLength 1 - expect(results[0].filePath).toBe commentFilePath - - it "does NOT save when modified", -> - editor = null - results = [] - - waitsForPromise -> - atom.project.open('sample.js').then (o) -> editor = o - - runs -> - editor.buffer.setTextInRange([[0,0],[0,0]], 'omg') - expect(editor.isModified()).toBeTruthy() - - waitsForPromise -> - atom.project.replace /items/gi, 'okthen', [filePath], (result) -> - results.push(result) - - runs -> - expect(results).toHaveLength 1 - expect(results[0].filePath).toBe filePath - expect(results[0].replacements).toBe 6 - - expect(editor.isModified()).toBeTruthy() - describe ".eachBuffer(callback)", -> beforeEach -> atom.project.bufferForPathSync('a') diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index e16f756cc..b96bc79af 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -558,7 +558,7 @@ describe "Workspace", -> expect(atom.workspace.panelForItem(item)).toBe panel expect(atom.workspace.panelForItem(itemWithNoPanel)).toBe null - describe "::scan(options, callback)", -> + describe "::scan(regex, options, callback)", -> describe "when called with a regex", -> it "calls the callback with all regex results in all files in the project", -> results = [] @@ -741,3 +741,103 @@ describe "Workspace", -> runs -> expect(results).toHaveLength 0 + + describe "::replace(regex, replacementText, paths, iterator)", -> + [filePath, commentFilePath, sampleContent, sampleCommentContent] = [] + + beforeEach -> + atom.project.setPaths([atom.project.resolve('../')]) + + filePath = atom.project.resolve('sample.js') + commentFilePath = atom.project.resolve('sample-with-comments.js') + sampleContent = fs.readFileSync(filePath).toString() + sampleCommentContent = fs.readFileSync(commentFilePath).toString() + + afterEach -> + fs.writeFileSync(filePath, sampleContent) + fs.writeFileSync(commentFilePath, sampleCommentContent) + + describe "when a file doesn't exist", -> + it "calls back with an error", -> + errors = [] + missingPath = path.resolve('/not-a-file.js') + expect(fs.existsSync(missingPath)).toBeFalsy() + + waitsForPromise -> + atom.workspace.replace /items/gi, 'items', [missingPath], (result, error) -> + errors.push(error) + + runs -> + expect(errors).toHaveLength 1 + expect(errors[0].path).toBe missingPath + + describe "when called with unopened files", -> + it "replaces properly", -> + results = [] + waitsForPromise -> + atom.workspace.replace /items/gi, 'items', [filePath], (result) -> + results.push(result) + + runs -> + expect(results).toHaveLength 1 + expect(results[0].filePath).toBe filePath + expect(results[0].replacements).toBe 6 + + describe "when a buffer is already open", -> + it "replaces properly and saves when not modified", -> + editor = null + results = [] + + waitsForPromise -> + atom.project.open('sample.js').then (o) -> editor = o + + runs -> + expect(editor.isModified()).toBeFalsy() + + waitsForPromise -> + atom.workspace.replace /items/gi, 'items', [filePath], (result) -> + results.push(result) + + runs -> + expect(results).toHaveLength 1 + expect(results[0].filePath).toBe filePath + expect(results[0].replacements).toBe 6 + + expect(editor.isModified()).toBeFalsy() + + it "does not replace when the path is not specified", -> + editor = null + results = [] + + waitsForPromise -> + atom.project.open('sample-with-comments.js').then (o) -> editor = o + + waitsForPromise -> + atom.workspace.replace /items/gi, 'items', [commentFilePath], (result) -> + results.push(result) + + runs -> + expect(results).toHaveLength 1 + expect(results[0].filePath).toBe commentFilePath + + it "does NOT save when modified", -> + editor = null + results = [] + + waitsForPromise -> + atom.project.open('sample.js').then (o) -> editor = o + + runs -> + editor.buffer.setTextInRange([[0,0],[0,0]], 'omg') + expect(editor.isModified()).toBeTruthy() + + waitsForPromise -> + atom.workspace.replace /items/gi, 'okthen', [filePath], (result) -> + results.push(result) + + runs -> + expect(results).toHaveLength 1 + expect(results[0].filePath).toBe filePath + expect(results[0].replacements).toBe 6 + + expect(editor.isModified()).toBeTruthy() diff --git a/src/project.coffee b/src/project.coffee index d8483b844..005c23165 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -180,44 +180,9 @@ class Project extends Model Grim.deprecate("Use atom.workspace.scan instead of atom.project.scan") atom.workspace.scan(regex, options, iterator) - # Public: Performs a replace across all the specified files in the project. - # - # * `regex` A {RegExp} to search with. - # * `replacementText` Text to replace all matches of regex with - # * `filePaths` List of file path strings to run the replace on. - # * `iterator` A {Function} callback on each file with replacements: - # * `options` {Object} with keys `filePath` and `replacements` replace: (regex, replacementText, filePaths, iterator) -> - deferred = Q.defer() - - openPaths = (buffer.getPath() for buffer in @getBuffers()) - outOfProcessPaths = _.difference(filePaths, openPaths) - - inProcessFinished = !openPaths.length - outOfProcessFinished = !outOfProcessPaths.length - checkFinished = -> - deferred.resolve() if outOfProcessFinished and inProcessFinished - - unless outOfProcessFinished.length - flags = 'g' - flags += 'i' if regex.ignoreCase - - task = Task.once require.resolve('./replace-handler'), outOfProcessPaths, regex.source, flags, replacementText, -> - outOfProcessFinished = true - checkFinished() - - task.on 'replace:path-replaced', iterator - task.on 'replace:file-error', (error) -> iterator(null, error) - - for buffer in @getBuffers() - continue unless buffer.getPath() in filePaths - replacements = buffer.replace(regex, replacementText, iterator) - iterator({filePath: buffer.getPath(), replacements}) if replacements - - inProcessFinished = true - checkFinished() - - deferred.promise + Grim.deprecate("Use atom.workspace.replace instead of atom.project.replace") + atom.workspace.replace(regex, replacementText, filePaths, iterator) ### Section: Private diff --git a/src/workspace.coffee b/src/workspace.coffee index 8761310dc..aaca0bf02 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -835,3 +835,44 @@ class Workspace extends Model task.terminate() deferred.resolve('cancelled') promise + + # Public: Performs a replace across all the specified files in the project. + # + # * `regex` A {RegExp} to search with. + # * `replacementText` Text to replace all matches of regex with + # * `filePaths` List of file path strings to run the replace on. + # * `iterator` A {Function} callback on each file with replacements: + # * `options` {Object} with keys `filePath` and `replacements` + # + # Returns a `Promise`. + replace: (regex, replacementText, filePaths, iterator) -> + deferred = Q.defer() + + openPaths = (buffer.getPath() for buffer in atom.project.getBuffers()) + outOfProcessPaths = _.difference(filePaths, openPaths) + + inProcessFinished = !openPaths.length + outOfProcessFinished = !outOfProcessPaths.length + checkFinished = -> + deferred.resolve() if outOfProcessFinished and inProcessFinished + + unless outOfProcessFinished.length + flags = 'g' + flags += 'i' if regex.ignoreCase + + task = Task.once require.resolve('./replace-handler'), outOfProcessPaths, regex.source, flags, replacementText, -> + outOfProcessFinished = true + checkFinished() + + task.on 'replace:path-replaced', iterator + task.on 'replace:file-error', (error) -> iterator(null, error) + + for buffer in atom.project.getBuffers() + continue unless buffer.getPath() in filePaths + replacements = buffer.replace(regex, replacementText, iterator) + iterator({filePath: buffer.getPath(), replacements}) if replacements + + inProcessFinished = true + checkFinished() + + deferred.promise