Class: RipperTags::VimFormatter

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

Instance Attribute Summary

Attributes inherited from DefaultFormatter

#options

Instance Method Summary collapse

Methods inherited from DefaultFormatter

#check_supported_flags, #constant?, #extra_flag?, #field?, #initialize, #relative_path, #stdout?, #tag_file_dir

Constructor Details

This class inherits a constructor from RipperTags::DefaultFormatter

Instance Method Details

#display_class(tag) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/ripper-tags/vim_formatter.rb', line 63

def display_class(tag)
  if tag[:class]
    "\tclass:%s" % display_constant(tag[:class])
  else
    ""
  end
end

#display_constant(const) ⇒ Object



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

def display_constant(const)
  const.to_s.gsub('::', '.')
end

#display_excmd_info(tag) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ripper-tags/vim_formatter.rb', line 47

def display_excmd_info(tag)
  case options.excmd
  when 'n', 'number'
    "%d;\"" % tag.fetch(:line)
  when nil, 'p', 'pattern', 'm', 'mixed'
    "/^%s$/;\"" % tag.fetch(:pattern).to_s.gsub('\\','\\\\\\\\').gsub('/','\\/')
  when 'c', 'combined'
    "%d;/^%s$/;\"" % [
      tag.fetch(:line) - 1,
      tag.fetch(:pattern).to_s.gsub('\\','\\\\\\\\').gsub('/','\\/')
    ]
  else
    raise 'invalid excmd value: %p', options.excmd
  end
end

#display_inheritance(tag) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/ripper-tags/vim_formatter.rb', line 71

def display_inheritance(tag)
  if tag[:inherits] && 'class' == tag[:kind]
    "\tinherits:%s" % display_constant(tag[:inherits])
  else
    ""
  end
end

#display_kind(tag) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/ripper-tags/vim_formatter.rb', line 91

def display_kind(tag)
  case tag.fetch(:kind)
  when 'method' then 'f'
  when 'singleton method' then 'F'
  when 'constant' then 'C'
  when 'scope', 'belongs_to', 'has_one', 'has_many', 'has_and_belongs_to_many'
    'F'
  else tag[:kind].slice(0,1)
  end
end

#display_languageObject



87
88
89
# File 'lib/ripper-tags/vim_formatter.rb', line 87

def display_language
  "\tlanguage:Ruby" if field?('l')
end

#display_line_number(tag) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/ripper-tags/vim_formatter.rb', line 79

def display_line_number(tag)
  if field?('n') && tag[:line]
    "\tline:%s" % tag[:line]
  else
    ""
  end
end

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



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ripper-tags/vim_formatter.rb', line 102

def format(tag, name_field = :name)
  "%s\t%s\t%s\t%s%s%s%s%s" % [
    tag.fetch(name_field),
    relative_path(tag),
    display_excmd_info(tag),
    display_kind(tag),
    display_line_number(tag),
    name_field == :full_name ? nil : display_class(tag),
    display_language,
    display_inheritance(tag),
  ]
end

#headerObject



13
14
15
16
17
18
# File 'lib/ripper-tags/vim_formatter.rb', line 13

def header
  <<-EOC
!_TAG_FILE_FORMAT\t2\t/extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED\t1\t/0=unsorted, 1=sorted, 2=foldcase/
  EOC
end

#include_qualified_names?Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/ripper-tags/vim_formatter.rb', line 8

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

#supported_fieldsObject



6
# File 'lib/ripper-tags/vim_formatter.rb', line 6

def supported_fields() ['l', 'n'] end

#supported_flagsObject



5
# File 'lib/ripper-tags/vim_formatter.rb', line 5

def supported_flags() ['q'] end

#with_outputObject

prepend header and sort lines before closing output



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

def with_output
  super do |out|
    out.puts header
    @queued_write = []
    yield out
    @queued_write.sort.each do |line|
      out.puts(line)
    end
  end
end

#write(tag, out) ⇒ Object



32
33
34
35
36
37
# File 'lib/ripper-tags/vim_formatter.rb', line 32

def write(tag, out)
  write_line(format(tag))
  if include_qualified_names? && tag[:full_name] != tag[:name] && constant?(tag)
    write_line(format(tag, :full_name))
  end
end

#write_line(line) ⇒ Object



39
40
41
# File 'lib/ripper-tags/vim_formatter.rb', line 39

def write_line(line)
  @queued_write << line
end