M
Modular•3w ago
Sarctiann

Current NEOVIM config

Let's use this post to share neovim setups for mojo!
No description
12 Replies
Sarctiann
SarctiannOP•3w ago
2024-12-30 ~/.congif/nvim/lua/plugins/mojo-conf.lua
return {
"neovim/nvim-lspconfig",
opts = {
servers = {
mojo = {
-- this is required if you use magic-cli instead of modular-cli
on_new_config = function()
local result = vim.fn.system("magic shell")
if vim.v.shell_error ~= 0 then
vim.notify("error running `magic shell`: " .. result, vim.log.levels.error)
end
end,
},
},
},
}
return {
"neovim/nvim-lspconfig",
opts = {
servers = {
mojo = {
-- this is required if you use magic-cli instead of modular-cli
on_new_config = function()
local result = vim.fn.system("magic shell")
if vim.v.shell_error ~= 0 then
vim.notify("error running `magic shell`: " .. result, vim.log.levels.error)
end
end,
},
},
},
}
I'm using LazyVim with neovim 0.10 (it works on MacOS, but not on Linux)
Ket
Ket•3w ago
i see
Manuel Saelices
Manuel Saelices•3w ago
This is my tree-sitter config for Mojo, using Lazy:
{
"nvim-treesitter/nvim-treesitter",
event = { "BufReadPost", "BufNewFile" },
cmd = { "TSInstall", "TSBufEnable", "TSBufDisable", "TSModuleInfo" },
build = ":TSUpdate",
dependencies = {
"nvim-treesitter/nvim-treesitter-textobjects",
},
opts = function()
return require "plugins.configs.treesitter"
end,
config = function(_, opts)
dofile(vim.g.base46_cache .. "syntax")
require("nvim-treesitter.configs").setup(opts)
-- use the python highlighter for .mojo files, which are recognized as conf ones
-- vim.treesitter.language.register('python', 'conf')

local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.mojo = {
install_info = {
url = "https://github.com/lsh/tree-sitter-mojo", -- local path or git repo
files = {"src/parser.c"}, -- note that some parsers also require src/scanner.c or src/scanner.cc
-- optional entries:
branch = "main", -- default branch in case of git repo if different from master
generate_requires_npm = false, -- if stand-alone parser without npm dependencies
requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c
},
filetype = "mojo", -- if filetype does not match the parser name
}

-- Set the comment string for Mojo files
vim.api.nvim_create_autocmd("FileType", {
pattern = "mojo",
callback = function()
vim.bo.commentstring = "# %s"
end,
})
end,
},
{
"nvim-treesitter/nvim-treesitter",
event = { "BufReadPost", "BufNewFile" },
cmd = { "TSInstall", "TSBufEnable", "TSBufDisable", "TSModuleInfo" },
build = ":TSUpdate",
dependencies = {
"nvim-treesitter/nvim-treesitter-textobjects",
},
opts = function()
return require "plugins.configs.treesitter"
end,
config = function(_, opts)
dofile(vim.g.base46_cache .. "syntax")
require("nvim-treesitter.configs").setup(opts)
-- use the python highlighter for .mojo files, which are recognized as conf ones
-- vim.treesitter.language.register('python', 'conf')

local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
parser_config.mojo = {
install_info = {
url = "https://github.com/lsh/tree-sitter-mojo", -- local path or git repo
files = {"src/parser.c"}, -- note that some parsers also require src/scanner.c or src/scanner.cc
-- optional entries:
branch = "main", -- default branch in case of git repo if different from master
generate_requires_npm = false, -- if stand-alone parser without npm dependencies
requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c
},
filetype = "mojo", -- if filetype does not match the parser name
}

-- Set the comment string for Mojo files
vim.api.nvim_create_autocmd("FileType", {
pattern = "mojo",
callback = function()
vim.bo.commentstring = "# %s"
end,
})
end,
},
Installed with :TSInstall mojo
Sarctiann
SarctiannOP•3w ago
2025-01-03 ~/.congif/nvim/lua/plugins/mojo-conf.lua
return {
"neovim/nvim-lspconfig",
opts = {
servers = {
mojo = {
on_new_config = function(config)
if vim.fn.executable("mojo-lsp-server") == 0 then
-- try to run `magic shell`
local result = vim.fn.system("magic shell")
if vim.v.shell_error ~= 0 then
vim.notify("LSP: " .. result, vim.log.levels.ERROR)
vim.notify("(Run `magic shell` before entering neovim)", vim.log.levels.INFO)
-- avoid trying to run mojo-lsp-server
config.cmd = { "echo", "" }
return
end
end
end,
},
},
},
}
return {
"neovim/nvim-lspconfig",
opts = {
servers = {
mojo = {
on_new_config = function(config)
if vim.fn.executable("mojo-lsp-server") == 0 then
-- try to run `magic shell`
local result = vim.fn.system("magic shell")
if vim.v.shell_error ~= 0 then
vim.notify("LSP: " .. result, vim.log.levels.ERROR)
vim.notify("(Run `magic shell` before entering neovim)", vim.log.levels.INFO)
-- avoid trying to run mojo-lsp-server
config.cmd = { "echo", "" }
return
end
end
end,
},
},
},
}
I have not yet managed to get magic shell to run automatically on linux. But with this configuration at least a solution is offered through a notification.
Melody Daniel
Melody Daniel•3w ago
With Astronvim, I only have to configure this in astrolsp.lua -
servers = {
"mojo",
},
servers = {
"mojo",
},
Sarctiann
SarctiannOP•3w ago
return {
"neovim/nvim-lspconfig",
opts = {
servers = {
mojo = {}
}
}
}
return {
"neovim/nvim-lspconfig",
opts = {
servers = {
mojo = {}
}
}
}
also worked with lazyvim before I removed the .modular folder to use magic only. @Kennit could you share your installation details?
Melody Daniel
Melody Daniel•3w ago
https://discord.com/channels/1087530497313357884/1268303557900238958 I have it here. But basically I start a Neovim instance inside a magic shell, that way mojo lsp is available to Neovim. You can do this with magic run nvim .
Sarctiann
SarctiannOP•3w ago
hmm I didn't get this working this script works well on macOS but on Linux, you need to run manually magic shell and then nvim 🫤
Melody Daniel
Melody Daniel•3w ago
Oh wow
Sarctiann
SarctiannOP•3w ago
oh oh... I'm testing on macos, and it doesn't work either... some how I get mojo-lsp-server installed inside ~/.modular/pkg/...
No description
Sarctiann
SarctiannOP•3w ago
This is the best I can do 😦
No description
Sarctiann
SarctiannOP•3w ago
2025-01-03
-- ~/.congif/nvim/lua/plugins/mojo-conf.lua

return {
"neovim/nvim-lspconfig",
opts = {
servers = {
mojo = {
on_new_config = function(config)
if vim.fn.executable("mojo-lsp-server") == 0 then
vim.notify(
"(Run `magic shell` before entering neovim)",
vim.log.levels.WARN,
{ title = "MOJO LSP NOT STARTED", icon = "🚨", timeout = 10000 }
)
-- avoid trying to run mojo-lsp-server
config.cmd = { "echo", "" }
return
end
end,
},
},
},
}
-- ~/.congif/nvim/lua/plugins/mojo-conf.lua

return {
"neovim/nvim-lspconfig",
opts = {
servers = {
mojo = {
on_new_config = function(config)
if vim.fn.executable("mojo-lsp-server") == 0 then
vim.notify(
"(Run `magic shell` before entering neovim)",
vim.log.levels.WARN,
{ title = "MOJO LSP NOT STARTED", icon = "🚨", timeout = 10000 }
)
-- avoid trying to run mojo-lsp-server
config.cmd = { "echo", "" }
return
end
end,
},
},
},
}
After some iterations, I realized it was impossible to run magic shell once nvim started (neither on macOS nor Linux). So this config requires you to run magic shell before starting nvim If so, it will run the LSP

Did you find this page helpful?