Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let Locator support use directives #1235

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions test/rbs/locator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class RBS::LocatorTest < Test::Unit::TestCase
include TestHelper

def locator(src)
_, _, decls = Parser.parse_signature(src)
Locator.new(decls: decls)
buffer, dirs, decls = Parser.parse_signature(src)
Locator.new(buffer: buffer, dirs: dirs, decls: decls)
end

def test_find_class
Expand Down