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

Merged
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
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 << node.value if node.leaf
stack.concat(node.children.values)
end

result
Expand Down
8 changes: 4 additions & 4 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Foo
end
RUBY

assert_equal(["path/foo", "path/other_foo"], @index.search_require_paths("path").map(&:require_path))
assert_equal(["path/other_foo", "path/foo"], @index.search_require_paths("path").map(&:require_path))
end

def test_searching_for_entries_based_on_prefix
Expand All @@ -157,10 +157,10 @@ class Foo::Bizt
RUBY

results = @index.prefix_search("Foo", []).map { |entries| entries.map(&:name) }
assert_equal([["Foo::Bizw", "Foo::Bizw"], ["Foo::Bizt"]], results)
assert_equal([["Foo::Bizt"], ["Foo::Bizw", "Foo::Bizw"]], results)

results = @index.prefix_search("Biz", ["Foo"]).map { |entries| entries.map(&:name) }
assert_equal([["Foo::Bizw", "Foo::Bizw"], ["Foo::Bizt"]], results)
assert_equal([["Foo::Bizt"], ["Foo::Bizw", "Foo::Bizw"]], results)
end

def test_resolve_normalizes_top_level_names
Expand Down Expand Up @@ -2128,7 +2128,7 @@ def self.do
end
RUBY

abc, adf = @index.instance_variable_completion_candidates("@", "Child::<Class:Child>")
adf, abc = @index.instance_variable_completion_candidates("@", "Child::<Class:Child>")

refute_nil(abc)
refute_nil(adf)
Expand Down
26 changes: 13 additions & 13 deletions lib/ruby_indexer/test/prefix_tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def test_multiple_items
tree = PrefixTree[String].new
["foo", "bar", "baz"].each { |item| tree.insert(item, item) }

assert_equal(["foo", "bar", "baz"], tree.search(""))
assert_equal(["bar", "baz"], tree.search("b"))
assert_equal(["baz", "bar", "foo"], tree.search(""))
assert_equal(["baz", "bar"], tree.search("b"))
assert_equal(["foo"], tree.search("fo"))
assert_equal(["bar", "baz"], tree.search("ba"))
assert_equal(["baz", "bar"], tree.search("ba"))
assert_equal(["baz"], tree.search("baz"))
assert_empty(tree.search("qux"))
end
Expand Down Expand Up @@ -80,17 +80,17 @@ def test_multiple_prefixes_with_shuffled_order

assert_equal(
[
"foo/bar/support/selection",
"foo/bar/support/semantic",
"foo/bar/support/syntax",
"foo/bar/support/source",
"foo/bar/support/formatting",
"foo/bar/support/prefix",
"foo/bar/support/highlight",
"foo/bar/support/diagnostic",
"foo/bar/support/rails",
"foo/bar/support/runner",
"foo/bar/support/runner2",
"foo/bar/support/rails",
"foo/bar/support/diagnostic",
"foo/bar/support/highlight",
"foo/bar/support/prefix",
"foo/bar/support/formatting",
"foo/bar/support/source",
"foo/bar/support/syntax",
"foo/bar/support/semantic",
"foo/bar/support/selection",
],
tree.search("foo/bar/support"),
)
Expand All @@ -99,7 +99,7 @@ def test_multiple_prefixes_with_shuffled_order
def test_deletion
tree = PrefixTree[String].new
["foo/bar", "foo/baz"].each { |item| tree.insert(item, item) }
assert_equal(["foo/bar", "foo/baz"], tree.search("foo"))
assert_equal(["foo/baz", "foo/bar"], tree.search("foo"))

tree.delete("foo/bar")
assert_empty(tree.search("foo/bar"))
Expand Down
34 changes: 17 additions & 17 deletions test/requests/completion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,9 @@ def process
})

result = server.pop_response.response
assert_equal(["qux1", "qux2"], result.map(&:label))
assert_equal(["qux1", "qux2"], result.map(&:filter_text))
assert_equal(["qux1", "qux2"], result.map { |completion| completion.text_edit.new_text })
assert_equal(["qux2", "qux1"], result.map(&:label))
assert_equal(["qux2", "qux1"], result.map(&:filter_text))
assert_equal(["qux2", "qux1"], result.map { |completion| completion.text_edit.new_text })
assert_equal(["fake.rb", "fake.rb"], result.map { _1.label_details.description })
end
end
Expand All @@ -646,9 +646,9 @@ def process
})

result = server.pop_response.response
assert_equal(["bar", "baz"], result.map(&:label))
assert_equal(["bar", "baz"], result.map(&:filter_text))
assert_equal(["bar", "baz"], result.map { |completion| completion.text_edit.new_text })
assert_equal(["baz", "bar"], result.map(&:label))
assert_equal(["baz", "bar"], result.map(&:filter_text))
assert_equal(["baz", "bar"], result.map { |completion| completion.text_edit.new_text })
assert_equal([9, 9], result.map { |completion| completion.text_edit.range.start.character })
end
end
Expand Down Expand Up @@ -787,7 +787,7 @@ def do_it
})

result = server.pop_response.response
assert_equal(["module", "method1", "method2"], result.map(&:label))
assert_equal(["module", "method2", "method1"], result.map(&:label))
end
end
end
Expand Down Expand Up @@ -1053,15 +1053,15 @@ def test_completion_for_global_variables
})

result = server.pop_response.response
assert_equal(["$qar", "$qaz", "$qux", "$quux", "$qorge", "$qoo"], result.map(&:label))
assert_equal(["$qoo", "$qorge", "$quux", "$qux", "$qaz", "$qar"], result.map(&:label))

server.process_message(id: 1, method: "textDocument/completion", params: {
textDocument: { uri: uri },
position: { line: 7, character: 5 },
})

result = server.pop_response.response
assert_equal(["$LOADED_FEATURES", "$LOAD_PATH"], result.map(&:label))
assert_equal(["$LOAD_PATH", "$LOADED_FEATURES"], result.map(&:label))
assert_equal(["global_variables.rbs", "global_variables.rbs"], result.map { _1.label_details.description })

server.process_message(id: 1, method: "textDocument/completion", params: {
Expand Down Expand Up @@ -1181,7 +1181,7 @@ def do_something
})

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

Expand Down Expand Up @@ -1248,7 +1248,7 @@ def self.foobar
})

result = server.pop_response.response
assert_equal(["@@a", "@@b", "@@c", "@@d"], result.map(&:label))
assert_equal(["@@d", "@@c", "@@b", "@@a"], result.map(&:label))
end
end

Expand Down Expand Up @@ -1319,7 +1319,7 @@ def do_something
})

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

Expand Down Expand Up @@ -1372,15 +1372,15 @@ def bar
})

result = server.pop_response.response
assert_equal(["baz", "bar"], result.map(&:label))
assert_equal(["bar", "baz"], result.map(&:label))

server.process_message(id: 1, method: "textDocument/completion", params: {
textDocument: { uri: uri },
position: { line: 5, character: 7 },
})

result = server.pop_response.response
assert_equal(["begin", "break", "baz", "bar"], result.map(&:label))
assert_equal(["begin", "break", "bar", "baz"], result.map(&:label))
end
end

Expand Down Expand Up @@ -1416,15 +1416,15 @@ def self.yeah
})

result = server.pop_response.response
assert_equal(["@a", "@b", "@c"], result.map(&:label))
assert_equal(["@c", "@b", "@a"], result.map(&:label))

server.process_message(id: 1, method: "textDocument/completion", params: {
textDocument: { uri: uri },
position: { line: 18, character: 5 },
})

result = server.pop_response.response
assert_equal(["@a", "@b", "@c"], result.map(&:label))
assert_equal(["@c", "@b", "@a"], result.map(&:label))
end
end

Expand All @@ -1449,7 +1449,7 @@ def do_something
position: { line: 8, character: 5 },
})
result = server.pop_response.response
assert_equal(["begin", "break", "bar", "baz"], result.map(&:label))
assert_equal(["begin", "break", "baz", "bar"], result.map(&:label))
end
end

Expand Down