Class: Holistic::Ruby::Scope::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/holistic/ruby/scope/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database:) ⇒ Repository

Returns a new instance of Repository.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/holistic/ruby/scope/repository.rb', line 7

def initialize(database:)
  @root = database.store(
    "root_scope", 
    Record.new("root_scope", {
      fully_qualified_name: "::",
      name: "::",
      kind: Kind::ROOT 
    })
  )

  @database = database
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



5
6
7
# File 'lib/holistic/ruby/scope/repository.rb', line 5

def database
  @database
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/holistic/ruby/scope/repository.rb', line 5

def root
  @root
end

Instance Method Details

#find(fully_qualified_name) ⇒ Object



20
21
22
# File 'lib/holistic/ruby/scope/repository.rb', line 20

def find(fully_qualified_name)
  database.find(fully_qualified_name)
end

#find_by_cursor(cursor) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/holistic/ruby/scope/repository.rb', line 24

def find_by_cursor(cursor)
  database.find(cursor.file_path)&.then do |file|
    file.defines_scopes.find do |scope|
      scope.locations.any? { _1.declaration.contains?(cursor) }
    end
  end
end

#find_inner_most_scope_by_cursor(cursor) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/holistic/ruby/scope/repository.rb', line 32

def find_inner_most_scope_by_cursor(cursor)
  file = database.find(cursor.file_path)

  return nil if file.nil?

  matching_scopes = file.defines_scopes.filter_map do |scope|
    scope.locations.find { |location| location.body.contains?(cursor) }&.then do |location|
      { location:, scope: }
    end
  end

  inner_most_matching_scope = matching_scopes.sort_by { |match| match[:location].declaration.start_line }.last

  inner_most_matching_scope&.then { _1[:scope] }
end

#list_scopes_in_file(file_path) ⇒ Object



48
49
50
# File 'lib/holistic/ruby/scope/repository.rb', line 48

def list_scopes_in_file(file_path)
  database.find(file_path)&.defines_scopes || []
end