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

rubocop:disable Metrics/ClassLength – because of the nested Annotation

class

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.

Parameters:

  • tag (String, nil) (defaults to: nil)

    the annotation tags to use

  • options (Hash{Symbol => Mixed}) (defaults to: {})

    additional options



36
37
38
39
40
41
42
# File 'lib/countless/annotations.rb', line 36

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.



17
18
19
# File 'lib/countless/annotations.rb', line 17

def annotations
  @annotations
end

#dirsObject (readonly)

Returns the value of attribute dirs.



17
18
19
# File 'lib/countless/annotations.rb', line 17

def dirs
  @dirs
end

#filesObject (readonly)

Returns the value of attribute files.



17
18
19
# File 'lib/countless/annotations.rb', line 17

def files
  @files
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/countless/annotations.rb', line 17

def options
  @options
end

#tagObject (readonly)

Returns the value of attribute tag.



17
18
19
# File 'lib/countless/annotations.rb', line 17

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.

Parameters:

  • file (String)

    the file to use

Returns:

  • (Hash{String => Array<Annotation>})

    the found annotations per file



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/countless/annotations.rb', line 87

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.

Parameters:

  • file (String)

    the file path to extract annotations from

  • pattern (RegExp)

    the matching pattern to use

Returns:

  • (Hash{String => Annotation})

    the found annotation of the file



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/countless/annotations.rb', line 110

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.

Parameters:

  • files (Array<String>) (defaults to: [])

    the files to use

  • dirs (Array<String>) (defaults to: [])

    the directories to use

Returns:

  • (Hash{String => Array<Annotation>})

    the found annotations per file



50
51
52
53
54
55
# File 'lib/countless/annotations.rb', line 50

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.

Parameters:

  • dir (String)

    the directory to use

Returns:

  • (Hash{String => Array<Annotation>})

    the found annotations per file



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/countless/annotations.rb', line 64

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.

rubocop:disable Metrics/AbcSize – because of the indentation logic

Returns:

  • (String)

    the formatted annotations



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/countless/annotations.rb', line 129

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