Module: Ctags::Exuberant

Extended by:
Exuberant
Includes:
POSIX::Spawn
Included in:
Exuberant
Defined in:
lib/ctags/exuberant.rb

Constant Summary collapse

BIN =
File.expand_path("../../../ext/dst/bin/ctags", __FILE__)
CONFIG =
File.expand_path("../../ctags.cnf", __FILE__)

Instance Method Summary collapse

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/ctags/exuberant.rb', line 38

def alive?
  return true if @pid && Process.kill(0, @pid)
  false
rescue Errno::ENOENT, Errno::ESRCH
  false
rescue Errno::EPERM
  raise "EPERM checking if child process is alive."
end

#execute(command, data = nil) ⇒ Object



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
77
78
79
# File 'lib/ctags/exuberant.rb', line 47

def execute(command, data=nil)
  start unless alive?

  json = Yajl.dump(command)
  @in.puts json
  @in.write data if data

  warnings = []
  tags = []

  while line = @out.gets
    obj = Yajl.load(line, :symbolize_keys => true)
    if obj[:error] && obj[:fatal]
      raise obj[:error]
    elsif obj[:error]
      warnings << obj
    elsif obj[:_type] == 'tag'
      obj.delete :_type
      obj[:pattern].strip!
      tags << obj
    elsif obj[:completed]
      break
    else
      raise "unknown obj: #{obj.inspect}"
    end
  end

  if tags.empty? and warnings.any?
    raise Error, warnings[0][:error]
  end

  tags
end

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ctags/exuberant.rb', line 13

def start
  args = [
    '--options=NONE',
    "--options=#{CONFIG}",
    '--fields=+KlnzsStimfa',
    '--sort=no',
    '--json'
  ]

  @pid, @in, @out, @err = popen4(BIN, *args)
  at_exit{ stop }
  @info = Yajl.load(@out.gets, :symbolize_keys => true)
end

#stopObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/ctags/exuberant.rb', line 27

def stop
  if @pid
    begin
      Process.kill('KILL', @pid)
      Process.waitpid(@pid)
    rescue Errno::ESRCH, Errno::ECHILD
    end
  end
  @pid = nil
end

#tags_for_file(filename, code = nil) ⇒ Object



81
82
83
84
85
# File 'lib/ctags/exuberant.rb', line 81

def tags_for_file(filename, code=nil)
  cmd = {'command'=>'generate-tags','filename'=>filename}
  cmd['size'] = code.bytesize if code
  execute(cmd, code)
end