Skip to content

Commit

Permalink
🔧 Fix julials setup in single file mode
Browse files Browse the repository at this point in the history
This workaround is required due to the issue described in:

williamboman/mason-lspconfig.nvim#423
  • Loading branch information
ronisbr committed Jun 5, 2024
1 parent 3bbe3e5 commit b33925b
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions lua/plugins/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ local servers = {
-- Julia Language Server -----------------------------------------------------------------

julials = {
julia_env_path = "~/.julia/environments/v1.10/",

settings = {
julia = {
inlayHints = {
Expand All @@ -24,7 +26,66 @@ local servers = {
}
}
}
}
},

on_new_config = function(config, workspace_dir)
local _ = require("mason-core.functional")
local fs = require("mason-core.fs")
local path = require("mason-core.path")

-- The default configuration used by `mason-lspconfig`:
--
-- https://github.com/williamboman/mason-lspconfig.nvim/blob/main/lua/mason-lspconfig/server_configurations/julials/init.lua
--
-- has the following logic to obtain the current environment path:
--
-- 1. Check if `env_path` is defined.
-- 2. Check if we are in a Julia project.
-- 3. Call julia to return the current env path.
--
-- However, the third step causes a significant slow down when Julia is called in a
-- single file mode because it must wait loading Julia. Here, we will invert the
-- logic:
--
-- 1. Check if we are in a Julia project.
-- 2. Check if `env_path` is defined.
-- 3. Call julia to return the current env path.
--
-- Hence, if we define `env_path`, we can still use the project folder as root and
-- avoid the slowdown in the single file case.
local env_path = nil
local file_exists = _.compose(fs.sync.file_exists, path.concat, _.concat { workspace_dir })
if (file_exists { "Project.toml" } and file_exists { "Manifest.toml" }) or
(file_exists { "JuliaProject.toml" } and file_exists { "JuliaManifest.toml" }) then
env_path = workspace_dir
end

if not env_path then
env_path = config.julia_env_path and vim.fn.expand(config.julia_env_path)
end

if not env_path then
local ok, env = pcall(vim.fn.system, {
"julia",
"--startup-file=no",
"--history-file=no",
"-e",
"using Pkg; print(dirname(Pkg.Types.Context().env.project_file))",
})
if ok then
env_path = env
end
end

config.cmd = {
vim.fn.exepath "julia-lsp",
env_path,
}
config.cmd_env = vim.tbl_extend("keep", config.cmd_env or {}, {
SYMBOL_SERVER = config.symbol_server,
SYMBOL_CACHE_DOWNLOAD = (config.symbol_cache_download == false) and "0" or "1",
})
end,
},

-- Lua Language Server -------------------------------------------------------------------
Expand Down Expand Up @@ -171,7 +232,7 @@ return {

config = function()
require("mason").setup()
require('mason-lspconfig').setup()
require("mason-lspconfig").setup()
require("mason-lspconfig").setup_handlers({
function (server_name)
local server = servers[server_name] or {}
Expand Down

0 comments on commit b33925b

Please sign in to comment.