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



29
30
31
32
33
34
# File 'lib/CoffeeTags/formatter.rb', line 29

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
# File 'lib/CoffeeTags/formatter.rb', line 16

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

Instance Method Details

#line_to_string(entry) ⇒ Object



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

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],
    "line:#{entry[:line]}",
    namespace,
    "kind:"+Formatter::kinds()[entry[:kind]],
    "language:coffee"
  ].join("\t")
end

#linesObject



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

def lines
  @lines
end

#parse_treeObject



58
59
60
61
62
63
# File 'lib/CoffeeTags/formatter.rb', line 58

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



38
39
40
# File 'lib/CoffeeTags/formatter.rb', line 38

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

#tagsObject



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

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