|
| 1 | +from heapq import heapify |
1 | 2 | import logging
|
2 | 3 | import re
|
3 | 4 | from dataclasses import dataclass, field
|
4 | 5 | from pathlib import Path
|
5 | 6 | from threading import Lock
|
6 | 7 | from typing import ClassVar
|
7 | 8 |
|
8 |
| -from beet import Context, File, NamespaceFile |
| 9 | +from beet import Context, File, Function, NamespaceFile |
9 | 10 | from beet.core.utils import extra_field, required_field
|
| 11 | +from bolt import Module |
| 12 | +from mecha import Mecha |
10 | 13 | from tokenstream import SourceLocation
|
11 | 14 |
|
12 | 15 | __all__ = ["FilePointer", "ResourceIndex", "AegisProjectIndex"]
|
@@ -43,22 +46,27 @@ class ResourceIndex:
|
43 | 46 | _files: dict[str, ResourceIndice] = extra_field(default_factory=dict)
|
44 | 47 | _lock: Lock = extra_field(default_factory=Lock)
|
45 | 48 |
|
46 |
| - def remove_associated(self, path: str | File): |
| 49 | + def remove_associated(self, path: str | File) -> list[str]: |
47 | 50 | self._lock.acquire()
|
48 | 51 |
|
49 | 52 | if isinstance(path, File):
|
50 | 53 | path = str(Path(path.ensure_source_path()).absolute())
|
51 | 54 |
|
| 55 | + removed = [] |
| 56 | + |
52 | 57 | for file, indice in list(self._files.items()):
|
53 |
| - if path in indice.definitions: |
54 |
| - del indice.definitions[path] |
| 58 | + |
55 | 59 | if path in indice.references:
|
56 | 60 | del indice.references[path]
|
| 61 | + if path in indice.definitions: |
| 62 | + del indice.definitions[path] |
57 | 63 |
|
58 |
| - if len(indice.definitions) == 0: |
59 |
| - del self._files[file] |
| 64 | + if len(indice.definitions) == 0: |
| 65 | + del self._files[file] |
| 66 | + removed.append(file) |
60 | 67 |
|
61 | 68 | self._lock.release()
|
| 69 | + return removed |
62 | 70 |
|
63 | 71 | def add_definition(
|
64 | 72 | self,
|
@@ -158,8 +166,31 @@ def __getitem__(self, key: type[NamespaceFile]):
|
158 | 166 | return self._resources.setdefault(key, ResourceIndex())
|
159 | 167 |
|
160 | 168 | def remove_associated(self, path: str):
|
161 |
| - for resource in self._resources.values(): |
162 |
| - resource.remove_associated(path) |
| 169 | + for resource, index in self._resources.items(): |
| 170 | + removed_files = index.remove_associated(path) |
| 171 | + |
| 172 | + for removed in removed_files: |
| 173 | + for pack in self._ctx.packs: |
| 174 | + if not removed in pack[resource]: |
| 175 | + continue |
| 176 | + |
| 177 | + file = pack[resource][removed] |
| 178 | + |
| 179 | + mecha = self._ctx.inject(Mecha) |
| 180 | + if file in mecha.database: |
| 181 | + del mecha.database[file] |
| 182 | + self._remove_from_queue(file, mecha) |
| 183 | + |
| 184 | + def _remove_from_queue(self, file, mecha: Mecha): |
| 185 | + index = -1 |
| 186 | + for i, (_, _, _, _, queued_file) in enumerate(mecha.database.queue): |
| 187 | + if file == queued_file: |
| 188 | + index = i |
| 189 | + break |
| 190 | + |
| 191 | + if index != -1: |
| 192 | + mecha.database.queue.pop(i) |
| 193 | + heapify(mecha.database.queue) |
163 | 194 |
|
164 | 195 | def dump(self) -> str:
|
165 | 196 | dump = ""
|
|
0 commit comments