Sha256: 32427745e116ae64e6cf7095bebb36bcb12ecc7fc8e675799a5cfffbafdb1a10

Contents?: true

Size: 1.69 KB

Versions: 10

Compression:

Stored size: 1.69 KB

Contents

require_relative 'c_file'

module Pry::CInternals
  class ETagParser
    SourceLocation = Struct.new(:file, :line, :symbol_type)

    attr_reader :tags_path
    attr_reader :ruby_source_folder

    def self.symbol_map_for(tags_path, ruby_source_folder)
      new(tags_path, ruby_source_folder).symbol_map
    end

    def initialize(tags_path, ruby_source_folder)
      @tags_path = tags_path
      @ruby_source_folder = ruby_source_folder
    end

    def symbol_map
      parse_tagfile.each_with_object({}) do |c_file, hash|
        # Append all the SourceLocations for a symbol to the same array
        # e.g
        # { "foo" => [SourceLocation_1] }
        # { "foo" => [SourceLocation_2] }
        # => { "foo" => [SourceLocation_1, SourceLocation_2] }
        hash.merge!(c_file.symbol_map) { |key, old_val, new_val| old_val + new_val }
      end
    end

    private

    # \f\n  indicates a new C file boundary in the etags file.
    # The first line is the name of the C file, e.g foo.c
    # The successive lines contain information about the symbols for that file.
    def parse_tagfile
      tagfile_sections = tagfile.split("\f\n")
      tagfile_sections.shift # first section is blank

      tagfile_sections.map do |c_file_section|
        file_name, content = file_name_and_content_for(c_file_section)
        CFile.new(file_name: file_name, content: content, ruby_source_folder: ruby_source_folder)
      end
    end

    def file_name_and_content_for(c_file_section)
      file_name, *content = c_file_section.lines
      [clean_file_name(file_name), content]
    end

    def tagfile
      File.read(tags_path)
    end

    def clean_file_name(file_name)
      file_name.split(",").first
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pry-doc-1.5.0 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb
pry-doc-1.4.0 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb
pry-doc-1.3.0 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb
pry-doc-1.2.0 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb
pry-doc-1.1.0 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb
pry-doc-1.0.0 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb
pry-doc-0.13.5 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb
pry-doc-0.13.4 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb
pry-doc-0.13.3 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb
pry-doc-0.13.2 lib/pry-doc/pry_ext/show_source_with_c_internals/etag_parser.rb