Skip to content

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

Open

Description

@chenle02

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.

Activity

  1. bostick commented on Aug 26, 2022

    @bostick

    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?

  2. hooger commented on Nov 5, 2022

    @hooger

    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. 🎉

  3. bostick commented on Nov 7, 2022

    @bostick

    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!

  4. even4void commented on Feb 21, 2023

    @even4void

    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>
    
  5. JuanG970 commented on Mar 22, 2023

    @JuanG970

    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 ?

  6. even4void commented on Mar 23, 2023

    @even4void

    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).

  7. JuanG970 commented on Feb 29, 2024

    @JuanG970

    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.

  8. Abastro commented on Jul 31, 2024

    @Abastro

    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.)
    
  9. ArttuLai commented on Jun 29, 2026

    @ArttuLai

    I had success with @JuanG970 ’s config, thanks! I think the "kernel" item in cmd table is not needed, but it also didn’t really do any harm either. In Sublime repo the kernel is just a variable that gets replaced with the path to the WolframKernel executable, which is already passed prior. Semantic tokens are enabled by default and that caused errors with sematicTokensProvider on every keypress:

    vim.schedule callback:
      ....12.3/share/nvim/runtime/lua/vim/lsp/semantic_tokens.lua:437:
      ....12.3/share/nvim/runtime/lua/vim/lsp/semantic_tokens.lua:85:
      attempt to index field 'semanticTokensProvider' (a userdata value)
    

    I solved it by disabling semantic tokens for Wolfram LS by adding this to the start command:

    local client = vim.lsp.start ({
      -- ... rest of the config ...
      on_init = function(client, _)
        client.server_capabilities.semanticTokensProvider = nil
      end
    })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions