-
Notifications
You must be signed in to change notification settings - Fork 17
/
runner.coffee
76 lines (66 loc) · 2.45 KB
/
runner.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{BufferedProcess} = require 'atom'
module.exports =
class Runner
commandString: null
process: null
useGitGrep: false
columnArg: false
env: process.env
constructor: ()->
atom.config.observe 'atom-fuzzy-grep.grepCommandString', =>
@commandString = atom.config.get 'atom-fuzzy-grep.grepCommandString'
@columnArg = @detectColumnFlag()
atom.config.observe 'atom-fuzzy-grep.detectGitProjectAndUseGitGrep', =>
@useGitGrep = atom.config.get 'atom-fuzzy-grep.detectGitProjectAndUseGitGrep'
run: (@search, @rootPath, callback)->
listItems = []
if @useGitGrep and @isGitRepo()
@commandString = atom.config.get 'atom-fuzzy-grep.gitGrepCommandString'
@columnArg = false
[command, args...] = @commandString.split(/\s/)
args.push @search
options = cwd: @rootPath, stdio: ['ignore', 'pipe', 'pipe'], env: @env
stdout = (output)=>
if listItems.length > atom.config.get('atom-fuzzy-grep.maxCandidates')
@destroy()
return
listItems = listItems.concat(@parseOutput(output))
callback(listItems)
stderr = (error)->
callback([error: error])
exit = (code)->
callback([]) if code == 1
@process = new BufferedProcess({command, exit, args, stdout, stderr, options})
@process
parseOutput: (output, callback)->
items = []
contentRegexp = if @columnArg then /^(\d+):\s*/ else /^\s+/
for item in output.split(/\n/)
break unless item.length
[path, line, content...] = item.split(':')
content = content.join ':'
items.push
filePath: path
fullPath: @rootPath + '/' + path
line: line-1
column: @getColumn content
content: content.replace(contentRegexp, '')
items
getColumn: (content)->
if @columnArg
return content.match(/^(\d+):/)?[1] - 1
# escaped characters in regexp can cause error
# skip it for a while
try
match = content.match(new RegExp(@search, 'gi'))?[0]
catch error
match = false
if match then content.indexOf(match) else 0
destroy: ->
@process?.kill()
isGitRepo: ->
atom.project.getRepositories().some (repo)=>
@rootPath?.startsWith(repo.getWorkingDirectory()) if repo
detectColumnFlag: ->
/(ag|pt|ack|rg)$/.test(@commandString.split(/\s/)[0]) and ~@commandString.indexOf('--column')
setEnv: (@env)->