Class: RipperTags::EmacsFormatter

Inherits:
DefaultFormatter show all
Defined in:
lib/ripper-tags/emacs_formatter.rb

Overview

Generates etags format as described in en.wikipedia.org/wiki/Ctags#Etags_2

The format is non-trivial since it requires section header for each source file to contain the size of tag data in bytes. This is accomplished by buffering tag definitions per-file and flushing them to target IO when a new source file is encountered or when ‘with_output` block finishes. This assumes that incoming tags are ordered by source file.

Instance Attribute Summary

Attributes inherited from DefaultFormatter

#options

Instance Method Summary collapse

Methods inherited from DefaultFormatter

#check_supported_flags, #constant?, #display_inheritance, #display_kind, #extra_flag?, #field?, #initialize, #relative_path, #stdout?, #supported_fields, #tag_file_dir

Constructor Details

This class inherits a constructor from RipperTags::DefaultFormatter

Instance Method Details

#flush_file_section(out) ⇒ Object



57
58
59
60
61
62
# File 'lib/ripper-tags/emacs_formatter.rb', line 57

def flush_file_section(out)
  if @section_io
    write_section(@current_file, @section_io.string, out)
    @section_io = nil
  end
end

#format(tag, name_field = :name) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/ripper-tags/emacs_formatter.rb', line 69

def format(tag, name_field = :name)
  "%s\x7F%s\x01%d,%d" % [
    tag.fetch(:pattern),
    tag.fetch(name_field),
    tag.fetch(:line),
    0,
  ]
end

#format_section_header(filename, data) ⇒ Object



64
65
66
67
# File 'lib/ripper-tags/emacs_formatter.rb', line 64

def format_section_header(filename, data)
  data_size = data.respond_to?(:bytesize) ? data.bytesize : data.size
  "\x0C\n%s,%d\n" % [ filename, data_size ]
end

#include_qualified_names?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/ripper-tags/emacs_formatter.rb', line 16

def include_qualified_names?
  return @include_qualified_names if defined? @include_qualified_names
  @include_qualified_names = extra_flag?('q')
end

#start_file_section(filename, io) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/ripper-tags/emacs_formatter.rb', line 48

def start_file_section(filename, io)
  if filename != @current_file
    flush_file_section(io)
    @current_file = filename
    @section_io = StringIO.new
  end
  @section_io
end

#supported_flagsObject



14
# File 'lib/ripper-tags/emacs_formatter.rb', line 14

def supported_flags() ['q'] end

#with_outputObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ripper-tags/emacs_formatter.rb', line 21

def with_output
  @current_file = nil
  @section_io = nil

  super do |io|
    begin
      yield io
    ensure
      flush_file_section(io)
    end
  end
end

#write(tag, io) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/ripper-tags/emacs_formatter.rb', line 34

def write(tag, io)
  filename = relative_path(tag)
  section_io = start_file_section(filename, io)
  section_io.puts format(tag)
  if include_qualified_names? && tag[:full_name] != tag[:name] && constant?(tag)
    section_io.puts format(tag, :full_name)
  end
end

#write_section(filename, data, io) ⇒ Object



43
44
45
46
# File 'lib/ripper-tags/emacs_formatter.rb', line 43

def write_section(filename, data, io)
  io.write format_section_header(filename, data)
  io.write data
end