Skip to content

Commit

Permalink
refactor and export api
Browse files Browse the repository at this point in the history
options argument added to all functions.
db path is put inside options argument.
  • Loading branch information
kalyazin committed Apr 23, 2014
1 parent 3f63e3b commit 434e225
Showing 1 changed file with 26 additions and 64 deletions.
90 changes: 26 additions & 64 deletions src/cscope.coffee
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
# TODO: rename methods in a consistent way
# TODO: Gruntfile.coffee (refer to github.com/atom/node-ctags)
# TODO: add tests
# TODO: move all this to github issues

nexpect = require 'nexpect'

findSymbolCmd = "1"
funcsCalledByCmd = "2"
funcsCallingCmd = "3"
# findTextStringCmd = "4" # FIXME: only same dir
# changeTextStringCmd = "5" # TODO: do i need this at all?
# findEgrepPatternCmd = "6" # FIXME: only same dir
findFileCmd = "7"
findFilesIncludingCmd = "8"
findAssignCmd = "9"

symbolToSearch = process.argv[2]
dbPath = process.argv[3]
if !symbolToSearch || !dbPath
console.log "Usage: coffee cscope.coffee <symbol> <db>"
process.exit 1
cmd =
findSymbol: '1'
findCallees: '2'
findCallers: '3'
findFile: '7'
findIncluders: '8'
findAssignments: '9'

genericFind = (cmd, symbol, db, callback) ->
genericFind = (cmd, symbol, options, callback) ->
cscopeRegex = /cscope: (\d+) lines/
outputRegex = /([\w.\/]+)\s+([a-zA-Z_$][\w$]+|<unknown>)\s+(\d+)\s+(.+)/
prompt = ">> "
quitCmd = "q"

nexpect.spawn "cscope"
, ["-d", "-k", "-l", "-f" + db]
opts = ['-l', '-k', '-d']
opts.push '-f' + options.refFile if options.refFile

nexpect.spawn "cscope", opts
.expect prompt
.sendline cmd + symbolToSearch
.sendline cmd + symbol
.wait cscopeRegex
.sendline quitCmd
.run (err, stdout, exitcode) ->
Expand Down Expand Up @@ -63,43 +52,16 @@ genericFind = (cmd, symbol, db, callback) ->
ctx: m[4]
callback res

findSymbol = (symbol, db, callback) ->
genericFind findSymbolCmd, symbol, db, callback

funcsCalledBy = (symbol, db, callback) ->
genericFind funcsCalledByCmd, symbol, db, callback

funcsCalling = (symbol, db, callback) ->
genericFind funcsCallingCmd, symbol, db, callback

# findTextString = (symbol, db, callback) ->
# genericFind findTextStringCmd, symbol, db, callback

# changeTextString = (symbol, db, callback) ->
# genericFind changeTextStringCmd, symbol, db, callback

# findEgrepPattern = (symbol, db, callback) ->
# genericFind findEgrepPatternCmd, symbol, db, callback

findFile = (symbol, db, callback) ->
genericFind findFileCmd, symbol, db, callback

findFilesIncluding = (symbol, db, callback) ->
genericFind findFilesIncludingCmd, symbol, db, callback

findAssign = (symbol, db, callback) ->
genericFind findAssignCmd, symbol, db, callback

printResult = (results) ->
for r in results
console.log "file: #{r.file}, sym: #{r.sym}, line: #{r.line}"

# findSymbol symbolToSearch, dbPath, printResult
funcsCalledBy symbolToSearch, dbPath, printResult
# funcsCalling symbolToSearch, dbPath, printResult
# findTextString symbolToSearch, dbPath, printResult
# changeTextString symbolToSearch, dbPath, printResult
# findEgrepPattern symbolToSearch, dbPath, printResult
# findFile symbolToSearch, dbPath, printResult
# findFilesIncluding symbolToSearch, dbPath, printResult
# findAssign symbolToSearch, dbPath, printResult
module.exports =
findSymbol: (symbol, options, callback) ->
genericFind cmd.findSymbolCmd, symbol, options, callback
findCallees: (symbol, options, callback) ->
genericFind cmd.findCallees, symbol, options, callback
findCallers: (symbol, options, callback) ->
genericFind cmd.findCallers, symbol, options, callback
findFile: (symbol, options, callback) ->
genericFind cmd.findFile, symbol, options, callback
findIncluders: (symbol, options, callback) ->
genericFind cmd.findIncluders, symbol, options, callback
findAssignments: (symbol, options, callback) ->
genericFind cmd.findAssignments, symbol, options, callback

0 comments on commit 434e225

Please sign in to comment.