Class: Coffeetags::Formatter

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, tree = []) ⇒ Formatter

New Formatter class

Parameters:

  • file (String)

    file name

  • tree (Array) (defaults to: [])

    parse tree for given file generated by Coffeetags::Parser



31
32
33
34
35
36
# File 'lib/CoffeeTags/formatter.rb', line 31

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

  @header = Formatter.header
end

Class Method Details

.headerObject

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

.kindsObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/CoffeeTags/formatter.rb', line 16

def self.kinds
  return {
    'f' => 'function',
    'c' => 'class',
    'o' => 'object',
    'v' => 'var',
    'p' => 'proto',
    'b' => 'block'
  }
end

Instance Method Details

#line_to_string(entry) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/CoffeeTags/formatter.rb', line 44

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

  output = [
    entry[:name],
    @file,
    regex_line(entry[:source]),
    entry[:kind],
    "line:#{entry[:line]}",
    "type:"+Formatter::kinds()[entry[:kind]],
    "language:coffee"
  ].join("\t")
  unless namespace.empty? then output << "\t#{namespace}" end
  output
end

#linesObject



73
74
75
# File 'lib/CoffeeTags/formatter.rb', line 73

def lines
  @lines
end

#parse_treeObject



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

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

#regex_line(line) ⇒ Object

Helper function for formatting a source line into regex



40
41
42
# File 'lib/CoffeeTags/formatter.rb', line 40

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

#tagsObject



69
70
71
# File 'lib/CoffeeTags/formatter.rb', line 69

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