| 288 | } |
| 289 | |
| 290 | void CodeModel::removeItem(const IndexedString& file, const IndexedQualifiedIdentifier& id) |
| 291 | { |
| 292 | if (!id.isValid()) |
| 293 | return; |
| 294 | |
| 295 | ifDebug(qCDebug(LANGUAGE) << "removeItem" << file.str() << id.identifier().toString(); ) |
| 296 | CodeModelRepositoryItem item; |
| 297 | item.file = file; |
| 298 | CodeModelRequestItem request(item); |
| 299 | |
| 300 | LockedItemRepository::write<CodeModel>([&](CodeModelRepo& repo) { |
| 301 | uint index = repo.findIndex(item); |
| 302 | |
| 303 | if (index) { |
| 304 | CodeModelItem searchItem; |
| 305 | searchItem.id = id; |
| 306 | |
| 307 | DynamicItem<CodeModelRepositoryItem, true> oldItem = repo.dynamicItemFromIndex(index); |
| 308 | |
| 309 | EmbeddedTreeAlgorithms<CodeModelItem, CodeModelItemHandler> alg(oldItem->items(), oldItem->itemsSize(), |
| 310 | oldItem->centralFreeItem); |
| 311 | |
| 312 | int listIndex = alg.indexOf(searchItem); |
| 313 | if (listIndex == -1) |
| 314 | return; |
| 315 | |
| 316 | auto* items = const_cast<CodeModelItem*>(oldItem->items()); |
| 317 | |
| 318 | --items[listIndex].referenceCount; |
| 319 | |
| 320 | if (oldItem->items()[listIndex].referenceCount) |
| 321 | return; // Nothing to remove, there's still a reference-count left |
| 322 | |
| 323 | // We have reduced the reference-count to zero, so remove the item from the list |
| 324 | |
| 325 | EmbeddedTreeRemoveItem<CodeModelItem, CodeModelItemHandler> remove(items, oldItem->itemsSize(), |
| 326 | oldItem->centralFreeItem, searchItem); |
| 327 | |
| 328 | uint newItemCount = remove.newItemCount(); |
| 329 | if (newItemCount != oldItem->itemsSize()) { |
| 330 | if (newItemCount == 0) { |
| 331 | // Has become empty, delete the item |
| 332 | repo.deleteItem(index); |
| 333 | |
| 334 | return; |
| 335 | } else { |
| 336 | // Make smaller |
| 337 | item.itemsList().resize(newItemCount); |
| 338 | remove.transferData(item.itemsList().data(), item.itemsSize(), &item.centralFreeItem); |
| 339 | |
| 340 | // Delete the old list |
| 341 | repo.deleteItem(index); |
| 342 | // Add the new list |
| 343 | repo.index(request); |
| 344 | return; |
| 345 | } |
| 346 | } |
| 347 | } |
no test coverage detected