Class: Countless::Annotations

Inherits:
Object
  • Object
show all
Defined in:
lib/countless/annotations.rb

Overview

Annotation objects are triplets :line, :tag, :text that represent the line where the annotation lives, its tag, and its text. Note the filename is not stored.

Annotations are looked for in comments and modulus whitespace they have to start with the tag optionally followed by a colon. Everything up to the end of the line (or closing ERB comment tag) is considered to be their text.

Heavily stolen from: bit.ly/3nBS0aj

Defined Under Namespace

Classes: Annotation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag = nil, options = {}) ⇒ Countless::SourceAnnotationExtractor

Setup a new instance of the source annotation extractor.

If tag is nil, annotations with either default or registered tags are printed. Specific directories can be explicitly set using the :dirs key in options.

Countless::SourceAnnotationExtractor.enumerate(
  'TODO|FIXME', dirs: %w(app lib), tag: true
)

If options has a :tag flag, it will be passed to each annotation’s to_s. See #find_in for a list of file extensions that will be taken into account.



33
34
35
36
37
38
39
# File 'lib/countless/annotations.rb', line 33

def initialize(tag = nil, options = {})
  @tag = tag || Annotation.tags.join('|')
  @dirs = options.delete(:dirs) || Annotation.directories
  @files = options.delete(:files) || Annotation.files
  @options = options
  @annotations = find(dirs: dirs, files: files)
end

Instance Attribute Details

#annotationsObject (readonly)

Returns the value of attribute annotations.



14
15
16
# File 'lib/countless/annotations.rb', line 14

def annotations
  @annotations
end

#dirsObject (readonly)

Returns the value of attribute dirs.



14
15
16
# File 'lib/countless/annotations.rb', line 14

def dirs
  @dirs
end

#filesObject (readonly)

Returns the value of attribute files.



14
15
16
# File 'lib/countless/annotations.rb', line 14

def files
  @files
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/countless/annotations.rb', line 14

def options
  @options
end

#tagObject (readonly)

Returns the value of attribute tag.



14
15
16
# File 'lib/countless/annotations.rb', line 14

def tag
  @tag
end

Instance Method Details

#annotations_in(file) ⇒ Hash{String => Array<Annotation>}

Returns a hash that maps filenames under file (de-glob-bed) to arrays with their annotations. Files with extensions registered in Countless::SourceAnnotationExtractor::Annotation.extensions are taken into account. Only files with annotations are included.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/countless/annotations.rb', line 84

def annotations_in(file)
  results = {}

  Dir.glob(file) do |item|
    extension =
      Annotation.extensions.detect { |regexp, _block| regexp.match(item) }

    if extension
      pattern = extension.last.call(tag)
      results.update(extract_annotations_from(item, pattern)) if pattern
    end
  end

  results
end

#extract_annotations_from(file, pattern) ⇒ Hash{String => Annotation}

If file is the filename of a file that contains annotations this method returns a hash with a single entry that maps file to an array of its annotations. Otherwise it returns an empty hash.



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/countless/annotations.rb', line 107

def extract_annotations_from(file, pattern)
  lineno = 0
  result = File.readlines(
    file, encoding: Encoding::BINARY
  ).inject([]) do |list, line|
    lineno += 1
    next list unless line =~ pattern

    list << Annotation.new(lineno, Regexp.last_match(1),
                           Regexp.last_match(2))
  end
  result.empty? ? {} : { file => result }
end

#find(files: [], dirs: []) ⇒ Hash{String => Array<Annotation>}

Returns a hash that maps filenames under dirs (recursively) to arrays with their annotations.



47
48
49
50
51
52
# File 'lib/countless/annotations.rb', line 47

def find(files: [], dirs: [])
  results = {}
  files.inject(results) { |memo, file| memo.update(annotations_in(file)) }
  dirs.inject(results) { |memo, dir| memo.update(find_in(dir)) }
  results
end

#find_in(dir) ⇒ Hash{String => Array<Annotation>}

Returns a hash that maps filenames under dir (recursively) to arrays with their annotations. Files with extensions registered in Countless::SourceAnnotationExtractor::Annotation.extensions are taken into account. Only files with annotations are included.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/countless/annotations.rb', line 61

def find_in(dir)
  results = {}

  Dir.glob("#{dir}/*") do |item|
    next if File.basename(item)[0] == '.'

    if File.directory?(item)
      results.update(find_in(item))
    else
      results.update(annotations_in(item))
    end
  end

  results
end

#to_sString

Formats the found annotations.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/countless/annotations.rb', line 124

def to_s
  buf = []
  options[:indent] = annotations.flat_map do |_f, a|
    a.map(&:line)
  end.max.to_s.size
  annotations.keys.sort.each do |file|
    buf << "#{file}:"
    annotations[file].each { |note| buf << "  * #{note.to_s(options)}" }
    buf << ''
  end
  buf.join("\n")
end