Class: Coffeetags::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/CoffeeTags/formatter.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Formatter) initialize(file, tree = [])

New Formatter class



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/CoffeeTags/formatter.rb', line 20

def initialize  file, tree =[]
  @file = file
  @tree = tree


  @types = {
    'f' => 'type:function',
    'c' => 'type:class',
    'o' => 'type:object',
    'v' => 'type:var'
  }
  @header = Formatter.header
end

Class Method Details

+ (Object) header

Generates CTAGS header



5
6
7
8
9
10
11
12
13
14
# File 'lib/CoffeeTags/formatter.rb', line 5

def self.header
  return [
    "!_TAG_FILE_FORMAT	2	/extended format/",
    "!_TAG_FILE_SORTED	0	/0=unsorted, 1=sorted, 2=foldcase/",
    "!_TAG_PROGRAM_AUTHOR	#{::Coffeetags::AUTHOR}",
    "!_TAG_PROGRAM_NAME	#{::Coffeetags::NAME}	//",
    "!_TAG_PROGRAM_URL	#{::Coffeetags::URL}	/GitHub repository/",
    "!_TAG_PROGRAM_VERSION	#{::Coffeetags::VERSION}	//"
  ].map { |h| "#{h}\n"}.join ''
end

Instance Method Details

- (Object) line_to_string(entry)



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/CoffeeTags/formatter.rb', line 40

def line_to_string entry
  namespace = (entry[:parent].blank?) ? entry[:name]: entry[:parent]
  namespace =  namespace == entry[:name] ? '' : "object:#{namespace}"

  [
    entry[:name],
    @file,
    regex_line(entry[:source]),
    entry[:kind],
    "lineno:#{entry[:line]}",
    namespace,
    @types[entry[:kind]]
  ].join("\t")
end

- (Object) lines



67
68
69
# File 'lib/CoffeeTags/formatter.rb', line 67

def lines
  @lines
end

- (Object) parse_tree



55
56
57
58
59
60
# File 'lib/CoffeeTags/formatter.rb', line 55

def parse_tree
  @lines = @tree.map do | content|
    line_to_string content  unless content[:line].nil? or content[:name].blank?
  end
  @lines.reject!{|l| l.nil? }
end

- (Object) regex_line(line)

Helper function for formatting a source line into regex



36
37
38
# File 'lib/CoffeeTags/formatter.rb', line 36

def regex_line line
  "/^#{line.gsub(/([\\^$])/, '\\\\\1')}$/;\""
end

- (Object) tags



63
64
65
# File 'lib/CoffeeTags/formatter.rb', line 63

def tags
  @lines.map { |l| "#{l}\n"}.join ''
end