Skip to content

Commit 3dc2790

Browse files
committed
fix(julia): add support on attach to activate configured julia environment
1 parent 0bf6660 commit 3dc2790

File tree

1 file changed

+88
-77
lines changed

1 file changed

+88
-77
lines changed

lua/astrocommunity/pack/julia/init.lua

Lines changed: 88 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -36,95 +36,106 @@ return {
3636
},
3737
{
3838
"AstroNvim/astrolsp",
39-
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)
39+
opts = function(_, opts)
40+
local astrocore = require "astrocore"
41+
opts = astrocore.extend_tbl(opts, {
42+
commands = {
43+
JuliaActivateEnv = {
44+
cond = function(client) return client.name == "julials" end,
45+
function(args)
46+
local bufnr = vim.api.nvim_get_current_buf()
47+
local julials_clients = (vim.lsp.get_clients or vim.lsp.get_active_clients) {
48+
bufnr = bufnr,
49+
name = "julials",
50+
}
51+
if #julials_clients == 0 then
52+
vim.notify(
53+
"method julia/activateenvironment is not supported by any servers active on the current buffer",
54+
vim.log.levels.WARN
55+
)
56+
return
6357
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
58+
local julia_project_files = { "Project.toml", "JuliaProject.toml" }
59+
local function _activate_env(environment)
60+
if environment then
61+
for _, julials_client in ipairs(julials_clients) do
62+
julials_client.notify("julia/activateenvironment", { envPath = environment })
63+
end
64+
vim.notify("Julia environment activated: \n`" .. environment .. "`", vim.log.levels.INFO)
7365
end
7466
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")
67+
if args.args ~= "" then
68+
local path = vim.fs.normalize(require("plenary.path"):new(args.args):expand())
69+
local found_env = false
70+
for _, project_file in ipairs(julia_project_files) do
71+
local file = (vim.uv or vim.loop).fs_stat(vim.fs.joinpath(path, project_file))
72+
if file and file.type then
73+
found_env = true
74+
break
75+
end
76+
end
77+
if not found_env then
78+
vim.notify("Path is not a julia environment: \n`" .. path .. "`", vim.log.levels.WARN)
79+
return
80+
end
81+
_activate_env(path)
82+
else
83+
local depot_paths = vim.env.JULIA_DEPOT_PATH
84+
and vim.split(vim.env.JULIA_DEPOT_PATH, vim.fn.has "win32" == 1 and ";" or ":")
85+
or { vim.fn.expand "~/.julia" }
86+
local environments = {}
9187
vim.list_extend(
9288
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-
)
89+
vim.fs.find(julia_project_files, { type = "file", upward = true, limit = math.huge })
10090
)
91+
for _, depot_path in ipairs(depot_paths) do
92+
local depot_env = vim.fs.joinpath(vim.fs.normalize(depot_path), "environments")
93+
vim.list_extend(
94+
environments,
95+
vim.fs.find(
96+
function(name, env_path)
97+
return vim.tbl_contains(julia_project_files, name)
98+
and string.sub(env_path, #depot_env + 1):match "^/[^/]*$"
99+
end,
100+
{ path = depot_env, type = "file", limit = math.huge }
101+
)
102+
)
103+
end
104+
environments = vim.tbl_map(vim.fs.dirname, environments)
105+
vim.ui.select(environments, { prompt = "Select a Julia environment" }, _activate_env)
101106
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",
107+
end,
108+
desc = "Activate a julia environment",
109+
nargs = "?",
110+
complete = "file",
111+
},
109112
},
110-
},
111-
config = {
112-
julials = {
113-
settings = {
114-
-- use the same default settings as the Julia VS Code extension
115-
julia = {
116-
completionmode = "qualify",
117-
lint = { missingrefs = "none" },
118-
inlayHints = {
119-
static = {
120-
enabled = false,
121-
variableTypes = { enabled = true },
113+
config = {
114+
julials = {
115+
settings = {
116+
-- use the same default settings as the Julia VS Code extension
117+
julia = {
118+
completionmode = "qualify",
119+
lint = { missingrefs = "none" },
120+
inlayHints = {
121+
static = {
122+
enabled = false,
123+
variableTypes = { enabled = true },
124+
},
122125
},
123126
},
124127
},
125128
},
126129
},
127-
},
128-
},
130+
})
131+
132+
opts.config.julials.on_attach = astrocore.patch_func(opts.config.julials.on_attach, function(orig, client, bufnr)
133+
local environment = vim.tbl_get(client, "settings", "julia", "environmentPath")
134+
if environment then client.notify("julia/activateenvironment", { envPath = environment }) end
135+
orig(client, bufnr)
136+
end)
137+
138+
return opts
139+
end,
129140
},
130141
}

0 commit comments

Comments
 (0)