Skip to content

Commit 6ec51a9

Browse files
authored
Merge pull request #38 from disberd/fix-packagename-imports
2 parents 54affe9 + cfdfb2e commit 6ec51a9

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/frompackage/code_parsing.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,29 @@ function remove_pluto_exprs(ex)
2828
end
2929
remove_pluto_exprs(ex, args...) = remove_pluto_exprs(ex)
3030

31+
# This will substitute PackageName with the correct path pointed to the loaded module
32+
function modify_package_using!(ex::Expr, loc, package_dict::Dict, eval_module::Module)
33+
Meta.isexpr(ex, (:using, :import)) || return true
34+
package_name = Symbol(package_dict["name"])
35+
package_exprs = if length(ex.args) === 1
36+
# We are in the form import PkgName: vars...
37+
package_expr, _ = extract_import_args(ex)
38+
[package_expr]
39+
else
40+
# We are in the form import PkgA, PkgB
41+
ex.args
42+
end
43+
for package_expr in package_exprs
44+
package_expr_args = package_expr.args
45+
extracted_package_name = first(package_expr_args)
46+
if extracted_package_name === package_name
47+
prepend!(package_expr_args, modname_path(fromparent_module[]))
48+
end
49+
end
50+
return true
51+
end
52+
3153
# This will simply make the using/import statements of the calling package point to the parent module
32-
modify_extension_using!(x, args...) = true
3354
function modify_extension_using!(ex::Expr, loc, package_dict::Dict, eval_module::Module)
3455
Meta.isexpr(ex, (:using, :import)) || return true
3556
has_extensions(package_dict) || return true
@@ -46,10 +67,7 @@ function modify_extension_using!(ex::Expr, loc, package_dict::Dict, eval_module:
4667
package_expr, _ = extract_import_args(ex)
4768
package_expr_args = package_expr.args
4869
extracted_package_name = first(package_expr_args)
49-
if extracted_package_name === package_name
50-
# We just add .. to the name because the extension module was added to the toplevel of the parent
51-
prepend!(package_expr_args, (:., :.))
52-
elseif extracted_package_name === weakdep
70+
if extracted_package_name === weakdep
5371
# We first add :_LoadedModules_
5472
pushfirst!(package_expr_args, :_LoadedModules_)
5573
# We also add the module path of the fromparent_module which contains _LoadedModules_
@@ -77,7 +95,7 @@ function process_expr!(ex, loc, dict, eval_module)
7795
ex isa Expr || return true # Apart from Nothing, we keep everything that is not an expr
7896
_is_block(ex) && return process_block!(ex, loc, dict, eval_module)
7997
_is_include(ex) && error("A call to include not at toplevel was found around line $loc. This is not permitted")
80-
keep = all((remove_pluto_exprs, modify_extension_using!)) do f
98+
keep = all((remove_pluto_exprs, modify_package_using!, modify_extension_using!)) do f
8199
f(ex, loc, dict, eval_module)
82100
end
83101
end

test/TestPackage/out_notebook.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ this_file = relpath(split(@__FILE__,"#==#")[1], dirname(dirname(dirname(@__DIR__
2929
@fromparent begin
3030
import TestPackage
3131
@skiplines begin
32-
"20" # Skip line 13 in the main file TestPackage.jl, which defines module Inner
32+
"22" # Skip line in the main file TestPackage.jl which defines module Inner
3333
"test_macro2.jl" # This skips the whole file test_macro2.jl
34-
"31-32" # This skips from line 24 to 25 in the main file, including extrema. Which are the 2 include statements of the inner SpecificImport module
34+
"33-34" # This skips lines in the main file, which are the 2 include statements of the inner SpecificImport module
3535
"test_macro1.jl:::28-10000" # This skips parts of test_macro1.jl
3636
end
3737
end

test/TestPackage/src/TestPackage.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module SUBINIT
1515
end
1616
end
1717

18+
import TestPackage.SUBINIT: TEST_SUBINIT
19+
1820
include("notebook1.jl")
1921

2022
module Inner

0 commit comments

Comments
 (0)