Skip to content

Commit 5afcec0

Browse files
authored
Merge pull request #103 from julia-vscode/workspacepackags
Allow `using` statements to access packages loaded within workspace
2 parents 7ad3ef3 + 01adc1e commit 5afcec0

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/imports.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ function _mark_import_arg(arg, par, state, usinged)
8484
end
8585
end
8686

87+
function has_workspace_package(server, name)
88+
haskey(server.workspacepackages, name) &&
89+
hasscope(getcst(server.workspacepackages[name])) &&
90+
haskey(scopeof(getcst(server.workspacepackages[name])).names, name) &&
91+
scopeof(getcst(server.workspacepackages[name])).names[name] isa Binding &&
92+
scopeof(getcst(server.workspacepackages[name])).names[name].val isa EXPR &&
93+
CSTParser.defines_module(scopeof(getcst(server.workspacepackages[name])).names[name].val)
94+
end
95+
8796
function add_to_imported_modules(scope::Scope, name::Symbol, val)
8897
if scope.modules isa Dict
8998
scope.modules[name] = val
@@ -112,6 +121,8 @@ function _get_field(par, arg, state)
112121
if par isa SymbolServer.EnvStore
113122
if (arg_scope = retrieve_scope(arg)) !== nothing && (tlm = get_named_toplevel_module(arg_scope, arg_str_rep)) !== nothing && hasbinding(tlm)
114123
return bindingof(tlm)
124+
elseif has_workspace_package(state.server, arg_str_rep)
125+
return scopeof(getcst(state.server.workspacepackages[arg_str_rep])).names[arg_str_rep]
115126
elseif haskey(par, Symbol(arg_str_rep))
116127
if isempty(state.env.project_deps) || Symbol(arg_str_rep) in state.env.project_deps
117128
return par[Symbol(arg_str_rep)]

src/server.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ end
1919
mutable struct FileServer <: AbstractServer
2020
files::Dict{String,File}
2121
roots::Set{File}
22+
workspacepackages::Dict{String,File} # list of files that may represent within-workspace packages
2223
external_env::ExternalEnv
2324
end
24-
FileServer() = FileServer(Dict{String,File}(), Set{File}(), ExternalEnv(Dict{Symbol,SymbolServer.ModuleStore}(:Base => SymbolServer.stdlibs[:Base], :Core => SymbolServer.stdlibs[:Core]), SymbolServer.collect_extended_methods(SymbolServer.stdlibs), Symbol[]))
25+
FileServer() = FileServer(Dict{String,File}(), Set{File}(), Dict{String,File}(), ExternalEnv(Dict{Symbol,SymbolServer.ModuleStore}(:Base => SymbolServer.stdlibs[:Base], :Core => SymbolServer.stdlibs[:Core]), SymbolServer.collect_extended_methods(SymbolServer.stdlibs), Symbol[]))
26+
2527

2628
hasfile(server::FileServer, path::String) = haskey(server.files, path)
2729
canloadfile(server, path) = isfile(path)

test/runtests.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,32 @@ end
16811681
@test length(StaticLint.loose_refs(bindingof(cst[1][3][3][1]))) == 2
16821682
end
16831683

1684+
@testset "test workspace packages" begin
1685+
empty!(server.files)
1686+
s1 = """
1687+
module WorkspaceMod
1688+
inner_sym = 1
1689+
exported_sym = 1
1690+
export exported_sym
1691+
end"""
1692+
f1 = StaticLint.File("workspacemod.jl", s1, CSTParser.parse(s1, true), nothing, server)
1693+
StaticLint.setroot(f1, f1)
1694+
StaticLint.setfile(server, f1.path, f1)
1695+
StaticLint.semantic_pass(f1)
1696+
server.workspacepackages["WorkspaceMod"] = f1
1697+
s2 = """
1698+
using WorkspaceMod
1699+
exported_sym
1700+
WorkspaceMod.inner_sym
1701+
"""
1702+
f2 = StaticLint.File("someotherfile.jl", s2, CSTParser.parse(s2, true), nothing, server)
1703+
StaticLint.setroot(f2, f2)
1704+
StaticLint.setfile(server, f2.path, f2)
1705+
StaticLint.semantic_pass(f2)
1706+
@test StaticLint.hasref(StaticLint.getcst(f2)[1][2][1])
1707+
@test StaticLint.hasref(StaticLint.getcst(f2)[2])
1708+
@test StaticLint.hasref(StaticLint.getcst(f2)[3][3][1])
1709+
end
16841710
@testset "#1218" begin
16851711
cst = parse_and_pass("""function foo(a; p) a+p end
16861712
foo(1, p = true)""")

0 commit comments

Comments
 (0)