class SyntaxTree::CLI::CTags

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

Attributes

entries[R]

Public Class Methods

new(options) click to toggle source
Calls superclass method SyntaxTree::CLI::Action::new
# File lib/syntax_tree/cli.rb, line 161
def initialize(options)
  super(options)
  @entries = []
end

Public Instance Methods

run(item) click to toggle source
# 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
success() click to toggle source
# 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