diff --git a/src/query.cc b/src/query.cc index 564d3c52..c8870fad 100644 --- a/src/query.cc +++ b/src/query.cc @@ -828,5 +828,34 @@ TEST_CASE("type usages") { REQUIRE(update.types_uses[0].to_add[0].range == Range(Position(2, 0))); } +TEST_CASE("apply delta") { + IndexFile previous("foo.cc"); + IndexFile current("foo.cc"); + + IndexFunc* pf = previous.Resolve(previous.ToFuncId("usr")); + IndexFunc* cf = current.Resolve(current.ToFuncId("usr")); + pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)))); + pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)))); + cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(4, 0)))); + cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(5, 0)))); + + QueryDatabase db; + IdMap previous_map(&db, previous.id_cache); + IdMap current_map(&db, current.id_cache); + REQUIRE(db.funcs.size() == 1); + + IndexUpdate import_update = IndexUpdate::CreateDelta(nullptr, &previous_map, nullptr, &previous); + IndexUpdate delta_update = IndexUpdate::CreateDelta(&previous_map, ¤t_map, &previous, ¤t); + + db.ApplyIndexUpdate(&import_update); + REQUIRE(db.funcs[0]->callers.size() == 2); + REQUIRE(db.funcs[0]->callers[0].loc.range == Range(Position(1, 0))); + REQUIRE(db.funcs[0]->callers[1].loc.range == Range(Position(2, 0))); + + db.ApplyIndexUpdate(&delta_update); + REQUIRE(db.funcs[0]->callers.size() == 2); + REQUIRE(db.funcs[0]->callers[0].loc.range == Range(Position(4, 0))); + REQUIRE(db.funcs[0]->callers[1].loc.range == Range(Position(5, 0))); +} TEST_SUITE_END(); \ No newline at end of file diff --git a/src/utils.h b/src/utils.h index d3389d2a..c276558b 100644 --- a/src/utils.h +++ b/src/utils.h @@ -75,12 +75,10 @@ void PushRange(std::queue* dest, const std::vector& to_add) { template void RemoveRange(std::vector* dest, const std::vector& to_remove) { - auto it = std::remove_if(dest->begin(), dest->end(), [&](const T& t) { + dest->erase(std::remove_if(dest->begin(), dest->end(), [&](const T& t) { // TODO: make to_remove a set? return std::find(to_remove.begin(), to_remove.end(), t) != to_remove.end(); - }); - if (it != dest->end()) - dest->erase(it); + }), dest->end()); } // http://stackoverflow.com/a/38140932