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

Sample custom LSP setup in lua? #313

Closed
Chocrates opened this issue Sep 5, 2023 · 2 comments
Closed

Sample custom LSP setup in lua? #313

Chocrates opened this issue Sep 5, 2023 · 2 comments

Comments

@Chocrates
Copy link

Does anyone know how to setup a language server in a non default way?

I am trying to use elmls and it is failing to find the language server

I think I basically need to set the elmPath setting during server setup, but I can't seem to get it to work.
This is what I have in my lsp.lua file that does my lsp config:

Relevant bit is:

require('mason-lspconfig').setup_handlers({
    require('lspconfig')['elmls'].setup({
        elmPath = "notreal"
    })
})

And the full file

local lsp = require("lsp-zero")

lsp.preset("recommended")

lsp.ensure_installed({
    'tsserver',
    'eslint',
    'rust_analyzer',
    'elmls'
})

-- Fix Undefined global 'vim'
lsp.nvim_workspace()


require('mason-lspconfig').setup_handlers({
    require('lspconfig')['elmls'].setup({
        elmPath = "notreal"
    })
})



local cmp = require('cmp')
local cmp_select = { behavior = cmp.SelectBehavior.Select }
local cmp_mappings = lsp.defaults.cmp_mappings({
    ['<C-p>'] = cmp.mapping.select_prev_item(cmp_select),
    ['<C-n>'] = cmp.mapping.select_next_item(cmp_select),
    ['<C-y>'] = cmp.mapping.confirm({ select = true }),
    ["<C-Space>"] = cmp.mapping.complete(),
})

-- cmp_mappings['<Tab>'] = nil -- Might be breaking copilot
cmp_mappings['<S-Tab>'] = nil

lsp.setup_nvim_cmp({
    mapping = cmp_mappings
})

lsp.set_preferences({
    -- suggest_lsp_servers = false,
    sign_icons = {
        error = 'E',
        warn = 'W',
        hint = 'H',
        info = 'I'
    }
})

lsp.on_attach(function(client, bufnr)
    local opts = { buffer = bufnr, remap = false }

    vim.keymap.set("n", "gd", function() vim.lsp.buf.definition() end, opts)
    vim.keymap.set("n", "K", function() vim.lsp.buf.hover() end, opts)
    vim.keymap.set("n", "<leader>vws", function() vim.lsp.buf.workspace_symbol() end, opts)
    vim.keymap.set("n", "<leader>vd", function() vim.diagnostic.open_float() end, opts)
    vim.keymap.set("n", "[d", function() vim.diagnostic.goto_next() end, opts)
    vim.keymap.set("n", "]d", function() vim.diagnostic.goto_prev() end, opts)
    vim.keymap.set("n", "<leader>vca", function() vim.lsp.buf.code_action() end, opts)
    vim.keymap.set("n", "<leader>vrr", function() vim.lsp.buf.references() end, opts)
    vim.keymap.set("n", "<leader>vrn", function() vim.lsp.buf.rename() end, opts)
    vim.keymap.set("i", "<C-h>", function() vim.lsp.buf.signature_help() end, opts)
end)

lsp.setup()

vim.diagnostic.config({
    virtual_text = true
})
@VonHeikemen
Copy link
Owner

I think that option belongs in the "settings" property.

And there is no need to add mason-lspconfig in the config. Just call lspconfig.

require('lspconfig')['elmls'].setup({
  settings = {
    elmLS = {
      elmPath = "notreal"
    }
  }
})

So this settings property is where you add the options that are specific to the language server.

@Chocrates
Copy link
Author

thanks! This is super helpful.

Didn't fix my issue but I think it is a bug with the lsp implementation and not something I can fix in config.

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

2 participants