Chris Wanstrath 2011-11-26 00:30:11 -08:00
parent bbc4779f4d
commit c0cf81f319
2 changed files with 54 additions and 14 deletions

View File

@ -1,26 +1,64 @@
_ = require 'underscore'
$ = require 'jquery'
fs = require 'fs'
ChildProcess = require 'child-process'
Browser = require 'browser'
Extension = require 'extension'
ModalSelector = require 'modal-selector'
module.exports =
class FindInProject extends Extension
constructor: ->
atom.on 'project:open', @startup
class FindInProject extends Browser
window.resourceTypes.push this
startup: (@project) =>
running: true
query: ->
return if not @project
@findInProject (prompt "Find in project:"), (results) =>
@pane = new ModalSelector -> results
@pane.show()
# Array of { name, url } objects
results: []
# String search term
term: ''
open: (url) ->
return if not url
if match = url.match /^findinproject:\/\/(.+)/
@term = match[1]
@url = url
@title = "Find #{@term}"
@findInProject @term, (@results) => @show()
true
findInProject: (term, callback) ->
root = @project.url
ChildProcess.exec "ack --ignore-dir=Cocoa/build --ignore-dir=vendor #{term} #{@project.url}", (error, stdout, stderr) ->
callback _.map (stdout.split "\n"), (line) ->
name: line.replace root, ''
return callback [] if not url = window.url
ChildProcess.exec "ack --ignore-dir=Cocoa/build --ignore-dir=vendor #{term} #{url}", (error, stdout, stderr) ->
callback _.compact _.map (stdout.split "\n"), (line) ->
return if _.isEmpty line.trim()
name: line.replace url, ''
url: _.first line.split ":"
add: ->
super @innerHTML()
# gross!
iframe = $ $('iframe')[0].contentDocument.body
iframe.find('#find-in-project-results-view a').click ->
window.open this.href.replace 'file://', ''
false
innerHTML: ->
html = '''
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
<style>body { padding:10px; }</style>
'''
html += '<h1>Results</h1>'
html += '<ul id="find-in-project-results-view">'
for {name, url} in @results
line = _.escape name
[file, match...] = line.split ':'
match = (match.join ':').replace @term, "<code>#{@term}</code>"
html += "<li><a href='#{url}'>#{file}:#{match}</a></li>"
html += '</ul>'
html

View File

@ -1,2 +1,4 @@
findinproject:
'cmd-shift-f': (findinproject) => findinproject.query()
'cmd-shift-f': (findinproject) ->
if term = prompt "Find in project:"
window.open "findinproject://#{term}"