Skip to content

Commit 0bf6660

Browse files
committed
feat(julia): add JuliaActivateEnv command for activating an environment
1 parent 2367b0e commit 0bf6660

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

lua/astrocommunity/pack/julia/init.lua

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,77 @@ return {
3737
{
3838
"AstroNvim/astrolsp",
3939
opts = {
40+
commands = {
41+
JuliaActivateEnv = {
42+
cond = function(client) return client.name == "julials" end,
43+
function(args)
44+
local bufnr = vim.api.nvim_get_current_buf()
45+
local julials_clients = (vim.lsp.get_clients or vim.lsp.get_active_clients) {
46+
bufnr = bufnr,
47+
name = "julials",
48+
}
49+
if #julials_clients == 0 then
50+
vim.notify(
51+
"method julia/activateenvironment is not supported by any servers active on the current buffer",
52+
vim.log.levels.WARN
53+
)
54+
return
55+
end
56+
local julia_project_files = { "Project.toml", "JuliaProject.toml" }
57+
local function _activate_env(environment)
58+
if environment then
59+
for _, julials_client in ipairs(julials_clients) do
60+
julials_client.notify("julia/activateenvironment", { envPath = environment })
61+
end
62+
vim.notify("Julia environment activated: \n`" .. environment .. "`", vim.log.levels.INFO)
63+
end
64+
end
65+
if args.args ~= "" then
66+
local path = vim.fs.normalize(require("plenary.path"):new(args.args):expand())
67+
local found_env = false
68+
for _, project_file in ipairs(julia_project_files) do
69+
local file = (vim.uv or vim.loop).fs_stat(vim.fs.joinpath(path, project_file))
70+
if file and file.type then
71+
found_env = true
72+
break
73+
end
74+
end
75+
if not found_env then
76+
vim.notify("Path is not a julia environment: \n`" .. path .. "`", vim.log.levels.WARN)
77+
return
78+
end
79+
_activate_env(path)
80+
else
81+
local depot_paths = vim.env.JULIA_DEPOT_PATH
82+
and vim.split(vim.env.JULIA_DEPOT_PATH, vim.fn.has "win32" == 1 and ";" or ":")
83+
or { vim.fn.expand "~/.julia" }
84+
local environments = {}
85+
vim.list_extend(
86+
environments,
87+
vim.fs.find(julia_project_files, { type = "file", upward = true, limit = math.huge })
88+
)
89+
for _, depot_path in ipairs(depot_paths) do
90+
local depot_env = vim.fs.joinpath(vim.fs.normalize(depot_path), "environments")
91+
vim.list_extend(
92+
environments,
93+
vim.fs.find(
94+
function(name, env_path)
95+
return vim.tbl_contains(julia_project_files, name)
96+
and string.sub(env_path, #depot_env + 1):match "^/[^/]*$"
97+
end,
98+
{ path = depot_env, type = "file", limit = math.huge }
99+
)
100+
)
101+
end
102+
environments = vim.tbl_map(vim.fs.dirname, environments)
103+
vim.ui.select(environments, { prompt = "Select a Julia environment" }, _activate_env)
104+
end
105+
end,
106+
desc = "Activate a julia environment",
107+
nargs = "?",
108+
complete = "file",
109+
},
110+
},
40111
config = {
41112
julials = {
42113
settings = {

0 commit comments

Comments
 (0)