Class: SyntaxTree::CLI::CTags

Inherits:
Action
  • Object
show all
Defined in:
lib/syntax_tree/cli.rb

Overview

An action of the CLI that generates ctags for the given source.

Instance Attribute Summary collapse

Attributes inherited from Action

#options

Instance Method Summary collapse

Methods inherited from Action

#failure

Constructor Details

#initialize(options) ⇒ CTags

Returns a new instance of CTags.



161
162
163
164
# File 'lib/syntax_tree/cli.rb', line 161

def initialize(options)
  super(options)
  @entries = []
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



159
160
161
# File 'lib/syntax_tree/cli.rb', line 159

def entries
  @entries
end

Instance Method Details

#run(item) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/syntax_tree/cli.rb', line 166

def run(item)
  lines = item.source.lines(chomp: true)

  SyntaxTree
    .index(item.source)
    .each do |entry|
      line = lines[entry.location.line - 1]
      pattern = "/^#{line.gsub("\\", "\\\\\\\\").gsub("/", "\\/")}$/;\""

      entries << case entry
      when SyntaxTree::Index::ModuleDefinition
        parts = [entry.name, item.filepath, pattern, "m"]

        if entry.nesting != [[entry.name]]
          parts << "class:#{entry.nesting.flatten.tap(&:pop).join(".")}"
        end

        parts.join("\t")
      when SyntaxTree::Index::ClassDefinition
        parts = [entry.name, item.filepath, pattern, "c"]

        if entry.nesting != [[entry.name]]
          parts << "class:#{entry.nesting.flatten.tap(&:pop).join(".")}"
        end

        unless entry.superclass.empty?
          inherits = entry.superclass.join(".").delete_prefix(".")
          parts << "inherits:#{inherits}"
        end

        parts.join("\t")
      when SyntaxTree::Index::MethodDefinition
        parts = [entry.name, item.filepath, pattern, "f"]

        unless entry.nesting.empty?
          parts << "class:#{entry.nesting.flatten.join(".")}"
        end

        parts.join("\t")
      when SyntaxTree::Index::SingletonMethodDefinition
        parts = [entry.name, item.filepath, pattern, "F"]

        unless entry.nesting.empty?
          parts << "class:#{entry.nesting.flatten.join(".")}"
        end

        parts.join("\t")
      when SyntaxTree::Index::AliasMethodDefinition
        parts = [entry.name, item.filepath, pattern, "a"]

        unless entry.nesting.empty?
          parts << "class:#{entry.nesting.flatten.join(".")}"
        end

        parts.join("\t")
      when SyntaxTree::Index::ConstantDefinition
        parts = [entry.name, item.filepath, pattern, "C"]

        unless entry.nesting.empty?
          parts << "class:#{entry.nesting.flatten.join(".")}"
        end

        parts.join("\t")
      end
    end
end

#successObject



233
234
235
236
237
238
239
240
# File 'lib/syntax_tree/cli.rb', line 233

def success
  puts(<<~HEADER)
    !_TAG_FILE_FORMAT	2	/extended format; --format=1 will not append ;" to lines/
    !_TAG_FILE_SORTED	1	/0=unsorted, 1=sorted, 2=foldcase/
  HEADER

  entries.sort.each { |entry| puts(entry) }
end