diff --git a/lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb b/lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb index d78b1003f9..90e5610c66 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb @@ -60,6 +60,10 @@ def insert(key, value) node = node.children[char] ||= Node.new(char, value, node) end + # This line is to allow a value to be overridden. When we are indexing files, we want to be able to update entries + # for a given fully qualified name if we find more occurrences of it. Without being able to override, that would + # not be possible + node.value = value node.leaf = true end @@ -112,7 +116,7 @@ class Node attr_reader :key sig { returns(Value) } - attr_reader :value + attr_accessor :value sig { returns(T::Boolean) } attr_accessor :leaf diff --git a/lib/ruby_indexer/test/prefix_tree_test.rb b/lib/ruby_indexer/test/prefix_tree_test.rb index 34111986f4..d180bc9e46 100644 --- a/lib/ruby_indexer/test/prefix_tree_test.rb +++ b/lib/ruby_indexer/test/prefix_tree_test.rb @@ -115,5 +115,15 @@ def test_deleted_node_is_removed_from_the_tree root = tree.instance_variable_get(:@root) assert_empty(root.children) end + + def test_overriding_values + tree = PrefixTree[Integer].new + + tree.insert("foo/bar", 123) + assert_equal([123], tree.search("foo/bar")) + + tree.insert("foo/bar", 456) + assert_equal([456], tree.search("foo/bar")) + end end end