mattn / efm-langserver

General purpose Language Server
MIT License
1.36k stars 61 forks source link

Filetypes empty with "nim" files and formatting doesn't work #240

Open mogita opened 1 year ago

mogita commented 1 year ago

Hi, my efm config in neovim works for all my other languages except for nim. The :LspInfo panel shows the filetypes as empty, and the format on save would not work if I opened a nim file in the neovim editor. I have nimpretty installed and it formats nim source files correctly when I ran it from the command line.

How should I fix this so nim files can also be formated on save? Thanks.

Here's the screenshot of :LspInfo:

SCR-20230220-ub3

Here are my relevant configs:

------------------------------------------------------------
-- formatters
------------------------------------------------------------
local prettierFmt = { formatCommand = 'prettier --config-precedence prefer-file --stdin-filepath ${INPUT}', formatStdin = true }
local luaFmt = { formatCommand = "lua-format -i", formatStdin = true }
local nimFmt = { formatCommand = "nimpretty", formatStdin = true }

------------------------------------------------------------
-- efm setup
------------------------------------------------------------
nvim_lsp.efm.setup {
  init_options = {
    documentFormatting = true
  },
  settings = {
    rootMarkers = {'.git', 'package.json', 'packages.json'},
    languages = {
      lua = {luaFmt},
      typescript = {prettierFmt},
      javascript = {prettierFmt},
      typescriptreact = {prettierFmt},
      javascriptreact = {prettierFmt},
      ["javascript.jsx"] = {prettierFmt},
      ["typescript.tsx"] = {prettierFmt},
      json = {prettierFmt},
      html = {prettierFmt},
      less = {prettierFmt},
      scss = {prettierFmt},
      css = {prettierFmt},
      markdown = {prettierFmt},
      nim = {nimFmt}
    },
    filetypes = {
      "javascript",
      "javascriptreact",
      "javascript.jsx",
      "typescript",
      "typescriptreact",
      "typescript.tsx",
      "html",
      "json",
      "less",
      "scss",
      "css",
      "markdown",
      "lua",
      "nim",
    }
  }
}

------------------------------------------------------------
-- nimlsp setup
------------------------------------------------------------
nvim_lsp.nimls.setup {
  filetypes = { "nim" },
  cmd = { '/Users/mogita/.nimble/bin/nimlsp' },
  on_attach = on_attach
}

------------------------------------------------------------
-- on_attach definition
------------------------------------------------------------
local on_attach = function(client, bufnr)
  local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
  local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end

  --Enable completion triggered by <c-x><c-o>
  buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')

  -- Mappings
  local opts = { noremap = true, silent = true }

  buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)

  if client.name == 'tsserver' then
    client.server_capabilities.documentFormattingProvider = false
  end

  if client.server_capabilities.documentFormattingProvider then
    vim.api.nvim_command [[augroup Format]]
    vim.api.nvim_command [[autocmd! * <buffer>]]
    vim.api.nvim_command [[autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_seq_sync()]]
    vim.api.nvim_command [[augroup END]]
  end

  if client.name == 'gopls' then
    vim.api.nvim_command [[autocmd BufWritePre <buffer> :silent! lua org_imports(1000)]]
  end

  --protocol.SymbolKind = { }
  protocol.CompletionItemKind = {
    '', -- Text
    '', -- Method
    '', -- Function
    -- ... redacted for simplicity
  }

  -- highlight symbol under the cursor
  if client.server_capabilities.documentHighlightProvider then
    vim.cmd [[
      hi! LspReferenceRead cterm=bold ctermbg=35 guibg=#2c465c
      hi! LspReferenceText cterm=bold ctermbg=35 guibg=#2c465c
      hi! LspReferenceWrite cterm=bold ctermbg=35 guibg=#2c465c
    ]]
    vim.api.nvim_create_augroup('lsp_document_highlight', {})
    vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
      group = 'lsp_document_highlight',
      buffer = 0,
      callback = vim.lsp.buf.document_highlight,
    })
    vim.api.nvim_create_autocmd('CursorMoved', {
      group = 'lsp_document_highlight',
      buffer = 0,
      callback = vim.lsp.buf.clear_references,
    })
  end
end