Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set up this LSPServer in neovim or vim? #3

Open
chenle02 opened this issue Aug 26, 2022 · 8 comments
Open

How to set up this LSPServer in neovim or vim? #3

chenle02 opened this issue Aug 26, 2022 · 8 comments

Comments

@chenle02
Copy link

I am a neovim 0.7+ user. It would be great there is some support for the lsp setup for neovim.
Thanks a lot in advance for any suggestions.

@bostick
Copy link

bostick commented Aug 26, 2022

The LSPServer package provides a Wolfram Language LSP server for any LSP client, so if your vim or neovim has LSP client support, then it should definitely work just fine.

What have you tried so far?

@hooger
Copy link

hooger commented Nov 5, 2022

Maybe necro bump and not relevant, but I want to document it somewhere. With Kate I also hade some trouble setting up LSPServer, here is how I solved it.

First I ran the provided PacletInstall commands inside Mathematica, that is documented (mostly) clearly and worked without issue. However the documentation felt a bit ambiguous about the version information, as first I thougth it is also only available from 13+ based on the Setup while I was running 12.2. Based on my experience now I understand that from 13+ you don't have to run PacletInstall and it is needed (but works!) on earlier versions.

What I needed next was a command that Kate can run to fire up the server, however the oneliner with arguments did not work, so I created a script file (under Linux it was a bash script, but I think bat file under Windows, or the equivalent under Mac works the same) that contains the following:

#! /bin/bash

WolframKernel -noinit -noprompt -nopaclet -noicon -nostartuppaclets -run 'Needs["LSPServer`"];LSPServer`StartServer[]'

and provided the script to the lspclient. The oneliner does not look the same as in the "Using LSPServer" section in the readme. I copied it from the sublime extensions source code, here: https://github.com/WolframResearch/Sublime-WolframLanguage/blob/master/WolframLanguage.sublime-settings#L21-L36 I'm not sure why it differs from the readme.

If I may suggest, please update the 'Using LSPServer" section of the Readme with some hints around the used arguments and that it is not just a test, however this should be given also to the lspclient. It may be evident to someone with knowledge around the protocol, but it took me a couple of hours to figure out.

Also, to leave a positive comment as well, I really like the fact that Wolfram Mathematica contains everything essential to be used even without the notebook frontend. 🎉

@bostick
Copy link

bostick commented Nov 7, 2022

Thank you for the feedback.

A possible source of confusion about versions may be about the specific versions that appear in the paths for WolframKernel. When talking about the full path to WolframKernel on your machine, I like to give the default location for the latest version, so for example on Windows, it would be:
C:\Program Files\Wolfram Research\Wolfram Engine\13.1\WolframKernel.exe

And this will be updated when 13.2 is released.

But this in no way implies that 13.1 is required. In fact, if you built from sources, you would be to go back to using version 12.1.

I'd like to understand any suggestion for better documentation around version information.

I will work to update the "Using LSPServer" section to be more clear.

Thanks again!

@even4void
Copy link

I can confirm that @hooger's solution works in Neovim (nightly). Here is part of my config that shows how to setup Wolfram Language Server:

local configs = require("lspconfig.configs")
configs.lsp_wl = {
  default_config = {
    cmd = {
      "wolfram",
      "kernel",
      "-noinit",
      "-noprompt",
      "-nopaclet",
      "-noicon",
      "-nostartuppaclets",
      "-run",
      'Needs["LSPServer`"];LSPServer`StartServer[]',
    },
    filetypes = { "mma" },
    root_dir = nvim_lsp.util.path.dirname,
    autostart = false,
  },
}
nvim_lsp.lsp_wl.setup({
  on_attach = function(client, bufnr)
    vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.MiniCompletion.completefunc_lsp")
    vim.keymap.set("n", "[d", vim.diagnostic.goto_prev)
    vim.keymap.set("n", "]d", vim.diagnostic.goto_next)
    vim.keymap.set("n", "<leader>wd", vim.diagnostic.open_float)
    vim.keymap.set("n", "<leader>ww", vim.diagnostic.setqflist)
    vim.keymap.set("i", "<C-h>", vim.lsp.buf.signature_help, opts)
    vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
    vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
    vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
    vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
    vim.keymap.set("n", "gI", vim.lsp.buf.type_definition, opts)
    vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
    vim.keymap.set("n", "z=", vim.lsp.buf.code_action, opts)
    vim.keymap.set("n", "zr", vim.lsp.buf.rename, opts)
  end,
})

Note that:

  1. I use the old mma filetype, even for wls scripts.
  2. With these settings, you will need to start the server manually (or set autostart = true).
  3. You can enable autoformatting on save in on_attach in addition to LSP mappings, or use a custom mapping in your ftplugin directory, e.g.:
nmap <buffer> <silent> g= :lua vim.lsp.buf.format()<cr>

@JuanG970
Copy link

Hello @even4void I am having some problems connecting Wolfram LSP and neovim, could you provide some more details about your config file? It should be placed under init.vim ?

Do you think it is possible to provide wolfram language support using COC ?

@even4void
Copy link

could you provide some more details about your config file? It should be placed under init.vim ?

I put everything related to LSP in a dedicated file (http://ix.io/4rAH), then require it from my init fiile (http://ix.io/4rAI), or rather a separate file (http://ix.io/4rAJ) where I manage plugins. I don't know about your config, but the above config is tied to a proper setup of nvim-lspconfig. I don't use COC so I can't tell if these settings apply in this case (probably not, since COC uses its own setup).

@JuanG970
Copy link

I have this working using the following settings inside my init.vim file:

lua << EOF
local autocmd = vim.api.nvim_create_autocmd
autocmd("FileType", {
    pattern = "mma",
    callback = function()
        local root_dir = vim.fs.dirname(
            vim.fs.find({ '.git' }, { upward = true })[1]
        )

        local client = vim.lsp.start({
            name = 'wolfram-lsp',
            cmd = {
                "/Applications/Mathematica.app/Contents/MacOS/WolframKernel",
                "kernel",
                "-noinit",
                "-noprompt",
                "-nopaclet",
                "-noicon",
                "-nostartuppaclets",
                "-run",
                'Needs["LSPServer`"];LSPServer`StartServer[]',
            },
            root_dir = root_dir
        })


        vim.keymap.set("n", "[d", vim.diagnostic.goto_prev)
        vim.keymap.set("n", "]d", vim.diagnostic.goto_next)
        vim.keymap.set("n", "<leader>kd", vim.diagnostic.open_float)
        vim.keymap.set("n", "<leader>kw", vim.diagnostic.setqflist)
        vim.keymap.set("i", "<C-h>", vim.lsp.buf.signature_help)
        vim.keymap.set("n", "K", vim.lsp.buf.hover)
        vim.keymap.set("n", "gr", vim.lsp.buf.references)
        -- This requires Telescope to be installed.
        vim.keymap.set("n", "<leader>d", ":Telescope lsp_document_symbols<CR>", { noremap = true, silent = true })

        -- This line is optional
        print('Key bindings defined')

        vim.lsp.buf_attach_client(0, client)

    end
})
EOF

Essentially, starts the LSP each time a mathematica file is opened.

@Abastro
Copy link

Abastro commented Jul 31, 2024

Was anyone able to setup with CoC? While I set it up to start the server, it stops at

Starting server... (If this is the last line you see, then StartServer[] may have been called in an unexpected way and the server is hanging.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants