Skip to content

Avoid recursion when collecting prefix tree node values #3401

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ def initialize(key, value, parent = nil)
#: -> Array[Value]
def collect
result = []
result << @value if @leaf
stack = [self]

@children.each_value do |node|
result.concat(node.collect)
while (node = stack.pop)
result.prepend(node.value) if node.leaf
stack.concat(node.children.values)
end

result
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ class Foo
assert_equal("==", method.name)

candidates = @index.method_completion_candidates("=", "Foo")
assert_equal(["==", "==="], candidates.map(&:name))
assert_equal(["===", "=="], candidates.map(&:name))
end

def test_entries_for
Expand Down
8 changes: 4 additions & 4 deletions lib/ruby_indexer/test/prefix_tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def test_multiple_prefixes
tree = PrefixTree[String].new
["fo", "foo"].each { |item| tree.insert(item, item) }

assert_equal(["fo", "foo"], tree.search(""))
assert_equal(["fo", "foo"], tree.search("f"))
assert_equal(["fo", "foo"], tree.search("fo"))
assert_equal(["foo", "fo"], tree.search(""))
assert_equal(["foo", "fo"], tree.search("f"))
assert_equal(["foo", "fo"], tree.search("fo"))
assert_equal(["foo"], tree.search("foo"))
assert_empty(tree.search("fooo"))
end
Expand Down Expand Up @@ -84,8 +84,8 @@ def test_multiple_prefixes_with_shuffled_order
"foo/bar/support/semantic",
"foo/bar/support/syntax",
"foo/bar/support/source",
"foo/bar/support/runner",
"foo/bar/support/runner2",
"foo/bar/support/runner",
"foo/bar/support/rails",
"foo/bar/support/diagnostic",
"foo/bar/support/highlight",
Expand Down
16 changes: 8 additions & 8 deletions test/requests/completion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class Baz
position: { line: 5, character: 9 },
})
result = server.pop_response.response
assert_equal(["Foo::Bar", "Foo::Bar::Baz"], result.map(&:label))
assert_equal(["Foo::Bar::Baz", "Foo::Bar"], result.map(&:label))
end
end
end
Expand Down Expand Up @@ -695,9 +695,9 @@ def bar
})

result = server.pop_response.response
assert_equal(["qux", "qux="], result.map(&:label))
assert_equal(["qux", "qux="], result.map(&:filter_text))
assert_equal(["qux", "qux="], result.map { |completion| completion.text_edit.new_text })
assert_equal(["qux=", "qux"], result.map(&:label))
assert_equal(["qux=", "qux"], result.map(&:filter_text))
assert_equal(["qux=", "qux"], result.map { |completion| completion.text_edit.new_text })
end
end
end
Expand Down Expand Up @@ -1112,7 +1112,7 @@ def baz
})

result = server.pop_response.response
assert_equal(["@@foo", "@@foobar"], result.map(&:label))
assert_equal(["@@foobar", "@@foo"], result.map(&:label))
assert_equal(["fake.rb", "fake.rb"], result.map { _1.label_details.description })
end
end
Expand All @@ -1138,7 +1138,7 @@ def bar
})

result = server.pop_response.response
assert_equal(["@@foo", "@@foobar"], result.map(&:label))
assert_equal(["@@foobar", "@@foo"], result.map(&:label))
end
end

Expand Down Expand Up @@ -1267,14 +1267,14 @@ def baz
position: { line: 7, character: 5 },
})
result = server.pop_response.response
assert_equal(["@foo", "@foobar"], result.map(&:label))
assert_equal(["@foobar", "@foo"], result.map(&:label))

server.process_message(id: 1, method: "textDocument/completion", params: {
textDocument: { uri: uri },
position: { line: 11, character: 5 },
})
result = server.pop_response.response
assert_equal(["@foo", "@foobar"], result.map(&:label))
assert_equal(["@foobar", "@foo"], result.map(&:label))
assert_equal(["fake.rb", "fake.rb"], result.map { _1.label_details.description })
end
end
Expand Down
Loading