Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Ctags #334

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

Merged
merged 9 commits into from
Mar 6, 2023
Merged

Ctags #334

Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
attr_writer and attr_accessor
  • Loading branch information
kddnewton committed Mar 5, 2023
commit ee2db3ff99a68756d10fc7eb522a11a7c7dfe5bf
76 changes: 47 additions & 29 deletions lib/syntax_tree/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ def find_constant_path(insns, index)
end
end

def find_attr_arguments(insns, index)
orig_argc = insns[index][1][:orig_argc]
names = []

current = index - 1
while current >= 0 && names.length < orig_argc
if insns[current].is_a?(Array) && insns[current][0] == :putobject
names.unshift(insns[current][1])
end

current -= 1
end

names if insns[current] == [:putself] && names.length == orig_argc
end

def index_iseq(iseq, file_comments)
results = []
queue = [[iseq, []]]
Expand Down Expand Up @@ -324,31 +340,29 @@ def index_iseq(iseq, file_comments)
)
when :opt_send_without_block, :send
case insn[1][:mid]
when :attr_reader
# We're going to scan backward finding symbols until we hit a
# different instruction. We'll then use that to determine the
# receiver. It needs to be self if we're going to understand it.
names = []
current = index - 1

while current >= 0 && names.length < insn[1][:orig_argc]
if insns[current].is_a?(Array) && insns[current][0] == :putobject
names.unshift(insns[current][1])
end

current -= 1
end

next if insns[current] != [:putself]
when :attr_reader, :attr_writer, :attr_accessor
names = find_attr_arguments(insns, index)
next unless names

location = Location.new(line, 0)
names.each do |name|
results << MethodDefinition.new(
current_nesting,
name,
location,
EntryComments.new(file_comments, location)
)
if insn[1][:mid] != :attr_writer
results << MethodDefinition.new(
current_nesting,
name,
location,
EntryComments.new(file_comments, location)
)
end

if insn[1][:mid] != :attr_reader
results << MethodDefinition.new(
current_nesting,
:"#{name}=",
location,
EntryComments.new(file_comments, location)
)
end
end
when :"core#set_method_alias"
# Now we have to validate that the alias is happening with a
Expand Down Expand Up @@ -452,19 +466,23 @@ def visit_class(node)
end

def visit_command(node)
if node.message.value == "attr_reader"
case node.message.value
when "attr_reader", "attr_writer", "attr_accessor"
comments = comments_for(node)
location =
Location.new(node.location.start_line, node.location.start_column)

node.arguments.parts.each do |argument|
next unless argument.is_a?(SymbolLiteral)
name = argument.value.value.to_sym

results << MethodDefinition.new(
nesting.dup,
argument.value.value.to_sym,
location,
comments_for(node)
)
if node.message.value != "attr_writer"
results << MethodDefinition.new(nesting.dup, name, location, comments)
end

if node.message.value != "attr_reader"
results << MethodDefinition.new(nesting.dup, :"#{name}=", location, comments)
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ def test_attr_reader
end
end

def test_attr_writer
index_each("attr_writer :foo") do |entry|
assert_equal :foo=, entry.name
assert_empty entry.nesting
end
end

def test_attr_accessor
index_each("attr_accessor :foo") do |entry|
assert_equal :foo=, entry.name
assert_empty entry.nesting
end
end

def test_this_file
entries = Index.index_file(__FILE__, backend: Index::ParserBackend.new)

Expand Down