Skip to content

Commit

Permalink
Let Locator support use directives
Browse files Browse the repository at this point in the history
  • Loading branch information
soutaro committed Feb 14, 2023
1 parent 517d518 commit 5879a7b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
33 changes: 26 additions & 7 deletions lib/rbs/locator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

module RBS
class Locator
attr_reader :decls
attr_reader :decls, :dirs, :buffer

def initialize(decls:)
def initialize(buffer:, dirs:, decls:)
@buffer = buffer
@dirs = dirs
@decls = decls
end

def buffer
decls[0].location&.buffer or raise
end

def find(line:, column:)
pos = buffer.loc_to_pos([line, column])

dirs.each do |dir|
array = [] #: Array[component]
find_in_directive(pos, dir, array) and return array
end

decls.each do |decl|
array = []
array = [] #: Array[component]
find_in_decl(pos, decl: decl, array: array) and return array
end

Expand All @@ -36,6 +39,22 @@ def find2(line:, column:)
end
end

def find_in_directive(pos, dir, array)
if test_loc(pos, location: dir.location)
array.unshift(dir)

dir.clauses.each do |clause|
if test_loc(pos, location: clause.location)
array.unshift(clause)
find_in_loc(pos, location: clause.location, array: array)
return true
end
end
end

false
end

def find_in_decl(pos, decl:, array:)
if test_loc(pos, location: decl.location)
array.unshift(decl)
Expand Down
16 changes: 14 additions & 2 deletions sig/locator.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ module RBS
| AST::TypeParam
| AST::Declarations::Class::Super
| AST::Declarations::Module::Self
| AST::Directives::t
| AST::Directives::Use::clause

# The buffer the location points to
#
attr_reader buffer: Buffer

# Array of _top-level_ declarations.
#
attr_reader decls: Array[AST::Declarations::t]

def initialize: (decls: Array[AST::Declarations::t]) -> void
# Array of directives.
#
attr_reader dirs: Array[AST::Directives::t]

def buffer: () -> Buffer
def initialize: (buffer: Buffer, decls: Array[AST::Declarations::t], dirs: Array[AST::Directives::t]) -> void

# Returns list of components.
# Inner component comes first.
Expand All @@ -29,6 +37,10 @@ module RBS
#
def find2: (line: Integer, column: Integer) -> [Symbol?, Array[component]]?

private

def find_in_directive: (Integer pos, AST::Directives::t, Array[component]) -> bool

def find_in_decl: (Integer pos, decl: AST::Declarations::t, array: Array[component]) -> bool

def find_in_member: (Integer pos, member: AST::Members::t, array: Array[component]) -> bool
Expand Down

0 comments on commit 5879a7b

Please sign in to comment.