From ebe243453c108ac315b9c5cdd7fa0e14c9b3e2fa Mon Sep 17 00:00:00 2001 From: Valentin Berlier Date: Sun, 15 Oct 2023 17:09:10 +0200 Subject: [PATCH] fix: import from overlay fall back to main pack --- bolt/module.py | 24 +++++++++++++++---- .../src/data/demo/functions/foo.mcfunction | 3 ++- .../src/data/demo/modules/vars.bolt | 1 + .../data/demo/functions/foo.mcfunction | 2 ++ .../examples__build_bolt_overlay__0.pack.md | 3 ++- 5 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 examples/bolt_overlay/src/data/demo/modules/vars.bolt diff --git a/bolt/module.py b/bolt/module.py index 10e559b..06d8318 100644 --- a/bolt/module.py +++ b/bolt/module.py @@ -174,6 +174,21 @@ def current_path(self) -> str: "Currently executing module has no associated resource location." ) + def lookup(self, path: str) -> TextFileBase[Any]: + """Lookup the associated source file in the compilation database. + + If we're currently in an overlay, fall back to the source file in the main + data pack. + """ + if current := self.database.index.get(path): + return current + + current_pack = self.database[self.database.current].pack + if current_pack is None or current_pack.overlay_parent is None: + raise KeyError(path) + + return self.database.indices[current_pack.overlay_parent][path] + def match_ast( self, node: AstRoot, @@ -183,7 +198,7 @@ def match_ast( if not current: current = self.database.current elif isinstance(current, str): - current = self.database.index[current] + current = self.lookup(current) compilation_unit = self.database[current] compilation_unit.ast = node @@ -192,7 +207,7 @@ def match_ast( def __getitem__(self, current: Union[TextFileBase[Any], str]) -> CompiledModule: if isinstance(current, str): - current = self.database.index[current] + current = self.lookup(current) compilation_unit = self.database[current] name = compilation_unit.resource_location or "" @@ -267,9 +282,10 @@ def __len__(self) -> int: def get(self, current: Union[TextFileBase[Any], str]) -> Optional[CompiledModule]: """Return executable module if exists.""" if isinstance(current, str): - if current not in self.database.index: + try: + current = self.lookup(current) + except KeyError: return None - current = self.database.index[current] if current not in self.database: return None diff --git a/examples/bolt_overlay/src/data/demo/functions/foo.mcfunction b/examples/bolt_overlay/src/data/demo/functions/foo.mcfunction index f9480c2..cae32b3 100644 --- a/examples/bolt_overlay/src/data/demo/functions/foo.mcfunction +++ b/examples/bolt_overlay/src/data/demo/functions/foo.mcfunction @@ -1 +1,2 @@ -say ("my" + "stuff") +from ./vars import stuff +say f"my {stuff}" diff --git a/examples/bolt_overlay/src/data/demo/modules/vars.bolt b/examples/bolt_overlay/src/data/demo/modules/vars.bolt new file mode 100644 index 0000000..7075681 --- /dev/null +++ b/examples/bolt_overlay/src/data/demo/modules/vars.bolt @@ -0,0 +1 @@ +stuff = 123 diff --git a/examples/bolt_overlay/src/dummy_overlay/data/demo/functions/foo.mcfunction b/examples/bolt_overlay/src/dummy_overlay/data/demo/functions/foo.mcfunction index 78f1ab4..b63e1f2 100644 --- a/examples/bolt_overlay/src/dummy_overlay/data/demo/functions/foo.mcfunction +++ b/examples/bolt_overlay/src/dummy_overlay/data/demo/functions/foo.mcfunction @@ -1 +1,3 @@ +from ./vars import stuff say (1 + 2) +say stuff diff --git a/tests/snapshots/examples__build_bolt_overlay__0.pack.md b/tests/snapshots/examples__build_bolt_overlay__0.pack.md index 5c3f9a9..a42caf2 100644 --- a/tests/snapshots/examples__build_bolt_overlay__0.pack.md +++ b/tests/snapshots/examples__build_bolt_overlay__0.pack.md @@ -18,7 +18,7 @@ `@function demo:foo` ```mcfunction -say mystuff +say my 123 ``` ## Overlay `dummy_overlay` @@ -31,6 +31,7 @@ say mystuff ```mcfunction say 3 +say 123 ``` `@endoverlay`