Class: RubyLens::Index::DeclarationCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/rubylens/index/declaration_collector.rb

Overview

Streams Rubydex's declarations once and splits them into the three populations the snapshot needs: workspace namespaces that become scene points, Ruby construct tallies per category, and dependency declaration rows aggregated per package.

It is a single pass because Rubydex materializes a new wrapper for every accessor call, so revisiting the stream costs as much as producing it.

Defined Under Namespace

Classes: Namespace, Result

Constant Summary collapse

CLASS =
0
MODULE =
1
METHOD =
2
CONSTANT =
3
OTHER_KIND =
2
MIXED_SCOPE =
2

Instance Method Summary collapse

Constructor Details

#initialize(manifest:, locations:) ⇒ DeclarationCollector

: (manifest: untyped, locations: LocationIndex) -> void



51
52
53
54
# File 'lib/rubylens/index/declaration_collector.rb', line 51

def initialize(manifest:, locations:)
  @manifest = manifest
  @locations = locations
end

Instance Method Details

#call(declarations) ⇒ Object

: (Enumerable) -> Result



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rubylens/index/declaration_collector.rb', line 57

def call(declarations)
  namespaces = []
  category_stats = { "core" => Array.new(4, 0), "tests" => Array.new(4, 0) }
  aggregation = Model::DependencyAggregation.new(package_count: @manifest.packages.length)
  definition_ranges = Hash.new { |ranges, uri| ranges[uri] = [] }
  dependency_positions = {}

  declarations.each do |declaration|
    name = declaration.name
    next unless eligible?(declaration, name)

    namespace = declaration.is_a?(Rubydex::Namespace)
    construct = construct_index(declaration)
    # Destructured immediately rather than wrapped: this runs once per
    # declaration in the largest loop RubyLens has, and the names below
    # are the only place these five values are ever read.
    workspace_count, tests_only, site_keys, canonical_scope, package_site_keys =
      summarize(declaration, namespace)

    if site_keys
      site_keys = site_keys.uniq if site_keys.length > 1
      ordinal = namespaces.length
      site_keys.each do |uri, start_line, start_column, end_line, end_column|
        definition_ranges[uri] << [start_line, start_column, end_line, end_column, ordinal]
      end
      namespaces << Namespace.new(
        declaration: declaration,
        name: name,
        definition_sites: site_keys.length,
        scope: canonical_scope,
      )
    end
    if construct && workspace_count.positive?
      category_stats.fetch(tests_only ? "tests" : "core")[construct] += 1
    end
    if package_site_keys
      collect_dependency(
        aggregation, dependency_positions, declaration, name, namespace, construct, package_site_keys
      )
    end
  end

  Result.new(
    namespaces: namespaces,
    definition_ranges: definition_ranges,
    category_stats: category_stats,
    dependency_packages: aggregation.packages,
    dependency_signal_maxima: aggregation.signal_maxima,
    dependency_ordinal_by_name: global_dependency_ordinals(aggregation.packages, dependency_positions),
  )
end

#construct_index(declaration) ⇒ Object

: (Rubydex::Declaration) -> Integer?



124
125
126
127
128
129
130
131
132
# File 'lib/rubylens/index/declaration_collector.rb', line 124

def construct_index(declaration)
  case declaration
  when Rubydex::SingletonClass then nil
  when Rubydex::Class then CLASS
  when Rubydex::Module then MODULE
  when Rubydex::Method then METHOD
  when Rubydex::Constant, Rubydex::ConstantAlias then CONSTANT
  end
end

#eligible?(declaration, name = declaration.name) ⇒ Boolean

Rubydex reports synthetic declarations RubyLens never draws: anonymous namespaces, Todo placeholders standing in for unresolved constants, and singleton classes attached to either.

: (Rubydex::Declaration, ?String?) -> bool

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
# File 'lib/rubylens/index/declaration_collector.rb', line 114

def eligible?(declaration, name = declaration.name)
  return false if name.nil? || name.empty? || name.include?("<anonymous>")
  return false if declaration.is_a?(Rubydex::Todo)
  return true unless declaration.is_a?(Rubydex::SingletonClass)

  attached = declaration.attached_class
  !attached.is_a?(Rubydex::SingletonClass) && !attached.is_a?(Rubydex::Todo)
end