Module: Ctags::Universal

Extended by:
Universal
Includes:
POSIX::Spawn
Included in:
Universal
Defined in:
lib/ctags/universal.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)


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

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



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

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
# File 'lib/ctags/universal.rb', line 13

def start
  args = [
    '--options=NONE',
    "--options=#{CONFIG}",
    '--fields=*',
    '--interactive'
  ]

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

#stopObject



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

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



80
81
82
83
84
# File 'lib/ctags/universal.rb', line 80

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