class SyntaxTree::CLI::CTags

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

def initialize(options)

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

def run(item)

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

def success

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