Logo

dev-resources.site

for different kinds of informations.

Avoid conflicts between denols and tsserver in Neovim

Published at
2/9/2024
Categories
neovim
typescript
lsp
deno
Author
davidecavaliere
Categories
4 categories in total
neovim
open
typescript
open
lsp
open
deno
open
Author
15 person written this
davidecavaliere
open
Avoid conflicts between denols and tsserver in Neovim

Recently I've been working on a project in a monorepo were I've got nodejs/typescript and deno projects. This is notoriously annoying because any ts file will start both tsserver and denols language services.

If you've got here you know what I'm talking about πŸ˜“

I'm setting up my lsp with lspconfig.

This is how denols is configured

lspconfig.denols.setup({
  root_dir = lspconfig.util.root_pattern("deno.json", "deno.jsonc"),
  init_options = {
    lint = true,
    unstable = true,
    suggest = {
      imports = {
        hosts = {
          ["https://deno.land"] = true,
          ["https://cdn.nest.land"] = true,
          ["https://crux.land"] = true,
        },
      },
    },
  },

  on_attach = on_attach,
})

Enter fullscreen mode Exit fullscreen mode

And this is the hack to avoid tsserver to start on a file that belongs to a deno project.

spconfig.tsserver.setup({
  on_attach = function (client, bufnr)
    on_attach(client, bufnr);
    vim.keymap.set('n', '<leader>ro', function()
      vim.lsp.buf.execute_command({
        command = "_typescript.organizeImports",
        arguments = { vim.fn.expand("%:p") }
      })
    end, { buffer = bufnr,  remap = false });
  end,
  root_dir = function (filename, bufnr)
    local denoRootDir = lspconfig.util.root_pattern("deno.json", "deno.json")(filename);
    if denoRootDir then
      -- print('this seems to be a deno project; returning nil so that tsserver does not attach');
      return nil;
    -- else
      -- print('this seems to be a ts project; return root dir based on package.json')
    end

    return lspconfig.util.root_pattern("package.json")(filename);
  end,
  single_file_support = false,
})
Enter fullscreen mode Exit fullscreen mode

This leverage the fact that if root_dir function returns nil then lspconfig wont's start a language service for that file. πŸŽ‰

lsp Article's
30 articles in total
Favicon
How to Setup Vim for Kotlin Development
Favicon
Managing LSPs in Neovim: Enable/Disable for the Entire Session
Favicon
Integrating the ruff language server
Favicon
Series Belajar Solid Principle - Liskov Substitution Principle (LSP)
Favicon
Avoid conflicts between denols and tsserver in Neovim
Favicon
Avoid conflicts between denols and tsserver in Neovim
Favicon
Implementing Payment Gateways with the Liskov Substitution Principle
Favicon
SOLID Principles: Liskov Substitution Principle (LSP)
Favicon
Configure Helix to use ElixirLS
Favicon
The Power of Liskov Substitution Principle in Improving Software Design
Favicon
Neovim config from scratch (Part II)
Favicon
Neovim config from scratch (Part I)
Favicon
Integrating a Language Server (LSP) in a VS Code Extension
Favicon
My Neovim setup for React, TypeScript, Tailwind CSS, etc
Favicon
How i setup neovim for speed up React, Js, Ts, etc
Favicon
Solve nvim lsp denols vs tsserver clash
Favicon
Perl Navigator - Language Server for Perl
Favicon
Neovim LSP Setup + Code Completion Engine
Favicon
NeoVim: init.lua, built in LSP e mais
Favicon
How to fix [deno-ts 2339] in the code for Deno Deploy
Favicon
SOLID e o princΓ­pio do 6 por meia dΓΊzia
Favicon
Java LSP in Sublime Text
Favicon
Neovim: Migrando do coc.vim para nvim-lsp
Favicon
Integrating Scala Metals with Doom Emacs using LSP on Pop!_OS/Ubuntu 20.04 LTS
Favicon
Builtin Solidity Language Server
Favicon
Liskov Substitution Principle
Favicon
Emacs as SQL client with LSP
Favicon
Vim Configuration from Minimal to Complete
Favicon
LSP and ISP: The LI of SOLID
Favicon
Go LSP in Sublime Text

Featured ones: