Skip to content

Commit

Permalink
fix(julia): add support on attach to activate configured julia enviro…
Browse files Browse the repository at this point in the history
…nment
  • Loading branch information
mehalter committed Sep 24, 2024
1 parent 0bf6660 commit 795d194
Showing 1 changed file with 99 additions and 78 deletions.
177 changes: 99 additions & 78 deletions lua/astrocommunity/pack/julia/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
-- Julia language server may be setup manually to use local system images. We should ignore installing the package
-- from Mason if that's the case because it completely changes how the Julia Language Server works
local cached_check
local function julials_manually_setup()
return vim.tbl_contains(vim.tbl_get(require("astrocore").plugin_opts "astrolsp", "servers") or {}, "julials")
if cached_check == nil then
cached_check =
vim.tbl_contains(vim.tbl_get(require("astrocore").plugin_opts "astrolsp", "servers") or {}, "julials")
end
return cached_check
end

return {
Expand Down Expand Up @@ -36,95 +41,111 @@ return {
},
{
"AstroNvim/astrolsp",
opts = {
commands = {
JuliaActivateEnv = {
cond = function(client) return client.name == "julials" end,
function(args)
local bufnr = vim.api.nvim_get_current_buf()
local julials_clients = (vim.lsp.get_clients or vim.lsp.get_active_clients) {
bufnr = bufnr,
name = "julials",
}
if #julials_clients == 0 then
vim.notify(
"method julia/activateenvironment is not supported by any servers active on the current buffer",
vim.log.levels.WARN
)
return
end
local julia_project_files = { "Project.toml", "JuliaProject.toml" }
local function _activate_env(environment)
if environment then
for _, julials_client in ipairs(julials_clients) do
julials_client.notify("julia/activateenvironment", { envPath = environment })
end
vim.notify("Julia environment activated: \n`" .. environment .. "`", vim.log.levels.INFO)
opts = function(_, opts)
local astrocore = require "astrocore"
opts = astrocore.extend_tbl(opts, {
commands = {
JuliaActivateEnv = {
cond = function(client) return client.name == "julials" end,
function(args)
local bufnr = vim.api.nvim_get_current_buf()
local julials_clients = (vim.lsp.get_clients or vim.lsp.get_active_clients) {
bufnr = bufnr,
name = "julials",
}
if #julials_clients == 0 then
vim.notify(
"method julia/activateenvironment is not supported by any servers active on the current buffer",
vim.log.levels.WARN
)
return
end
end
if args.args ~= "" then
local path = vim.fs.normalize(require("plenary.path"):new(args.args):expand())
local found_env = false
for _, project_file in ipairs(julia_project_files) do
local file = (vim.uv or vim.loop).fs_stat(vim.fs.joinpath(path, project_file))
if file and file.type then
found_env = true
break
local julia_project_files = { "Project.toml", "JuliaProject.toml" }
local function _activate_env(environment)
if environment then
for _, julials_client in ipairs(julials_clients) do
julials_client.notify("julia/activateenvironment", { envPath = environment })
end
vim.notify("Julia environment activated: \n`" .. environment .. "`", vim.log.levels.INFO)
end
end
if not found_env then
vim.notify("Path is not a julia environment: \n`" .. path .. "`", vim.log.levels.WARN)
return
end
_activate_env(path)
else
local depot_paths = vim.env.JULIA_DEPOT_PATH
and vim.split(vim.env.JULIA_DEPOT_PATH, vim.fn.has "win32" == 1 and ";" or ":")
or { vim.fn.expand "~/.julia" }
local environments = {}
vim.list_extend(
environments,
vim.fs.find(julia_project_files, { type = "file", upward = true, limit = math.huge })
)
for _, depot_path in ipairs(depot_paths) do
local depot_env = vim.fs.joinpath(vim.fs.normalize(depot_path), "environments")
if args.args ~= "" then
local path = vim.fs.normalize(require("plenary.path"):new(args.args):expand())
local found_env = false
for _, project_file in ipairs(julia_project_files) do
local file = (vim.uv or vim.loop).fs_stat(vim.fs.joinpath(path, project_file))
if file and file.type then
found_env = true
break
end
end
if not found_env then
vim.notify("Path is not a julia environment: \n`" .. path .. "`", vim.log.levels.WARN)
return
end
_activate_env(path)
else
local depot_paths = vim.env.JULIA_DEPOT_PATH
and vim.split(vim.env.JULIA_DEPOT_PATH, vim.fn.has "win32" == 1 and ";" or ":")
or { vim.fn.expand "~/.julia" }
local environments = {}
vim.list_extend(
environments,
vim.fs.find(
function(name, env_path)
return vim.tbl_contains(julia_project_files, name)
and string.sub(env_path, #depot_env + 1):match "^/[^/]*$"
end,
{ path = depot_env, type = "file", limit = math.huge }
)
vim.fs.find(julia_project_files, { type = "file", upward = true, limit = math.huge })
)
for _, depot_path in ipairs(depot_paths) do
local depot_env = vim.fs.joinpath(vim.fs.normalize(depot_path), "environments")
vim.list_extend(
environments,
vim.fs.find(
function(name, env_path)
return vim.tbl_contains(julia_project_files, name)
and string.sub(env_path, #depot_env + 1):match "^/[^/]*$"
end,
{ path = depot_env, type = "file", limit = math.huge }
)
)
end
environments = vim.tbl_map(vim.fs.dirname, environments)
vim.ui.select(environments, { prompt = "Select a Julia environment" }, _activate_env)
end
environments = vim.tbl_map(vim.fs.dirname, environments)
vim.ui.select(environments, { prompt = "Select a Julia environment" }, _activate_env)
end
end,
desc = "Activate a julia environment",
nargs = "?",
complete = "file",
end,
desc = "Activate a julia environment",
nargs = "?",
complete = "file",
},
},
},
config = {
julials = {
settings = {
-- use the same default settings as the Julia VS Code extension
julia = {
completionmode = "qualify",
lint = { missingrefs = "none" },
inlayHints = {
static = {
enabled = false,
variableTypes = { enabled = true },
config = {
julials = {
settings = {
-- use the same default settings as the Julia VS Code extension
julia = {
completionmode = "qualify",
lint = { missingrefs = "none" },
inlayHints = {
static = {
enabled = false,
variableTypes = { enabled = true },
},
},
},
},
},
},
},
},
})

if julials_manually_setup() then
opts.config.julials.on_attach = astrocore.patch_func(
opts.config.julials.on_attach,
function(orig, client, bufnr)
local environment = vim.tbl_get(client, "settings", "julia", "environmentPath")
if environment then client.notify("julia/activateenvironment", { envPath = environment }) end
orig(client, bufnr)
end
)
end

return opts
end,
},
}

0 comments on commit 795d194

Please sign in to comment.