summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/lsp/tsserver-ls.lua
blob: c687dfcd0f676f220b1e313210fcb47f6f4448ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
vim.cmd "let proj = FindRootDirectory()"
local root_dir = vim.api.nvim_get_var "proj"

-- use the global prettier if you didn't find the local one
local prettier_instance = root_dir .. "/node_modules/.bin/prettier"
if vim.fn.executable(prettier_instance) ~= 1 then
  prettier_instance = O.lang.tsserver.formatter.exe
end

O.formatters.filetype["javascriptreact"] = {
  function()
    local args = { "--stdin-filepath", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)) }
    local extend_args = O.lang.tsserver.formatter.args

    if extend_args then
      for i = 1, #extend_args do
        table.insert(args, extend_args[i])
      end
    end

    return {
      exe = prettier_instance,
      args = args,
      stdin = true,
    }
  end,
}
O.formatters.filetype["javascript"] = O.formatters.filetype["javascriptreact"]
O.formatters.filetype["typescript"] = O.formatters.filetype["javascriptreact"]
O.formatters.filetype["typescriptreact"] = O.formatters.filetype["javascriptreact"]

require("formatter.config").set_defaults {
  logging = false,
  filetype = O.formatters.filetype,
}

if require("utils").check_lsp_client_active "tsserver" then
  return
end

-- npm install -g typescript typescript-language-server
-- require'snippets'.use_suggested_mappings()
-- local capabilities = vim.lsp.protocol.make_client_capabilities()
-- capabilities.textDocument.completion.completionItem.snippetSupport = true;
-- local on_attach_common = function(client)
-- print("LSP Initialized")
-- require'completion'.on_attach(client)
-- require'illuminate'.on_attach(client)
-- end

local on_attach = function(client, bufnr)
  local lsp = require "lsp"
  lsp.common_on_attach(client, bufnr)
  lsp.tsserver_on_attach(client, bufnr)
end

require("lspconfig").tsserver.setup {
  cmd = {
    DATA_PATH .. "/lspinstall/typescript/node_modules/.bin/typescript-language-server",
    "--stdio",
  },
  filetypes = {
    "javascript",
    "javascriptreact",
    "javascript.jsx",
    "typescript",
    "typescriptreact",
    "typescript.tsx",
  },
  on_attach = on_attach,
  -- This makes sure tsserver is not used for formatting (I prefer prettier)
  settings = { documentFormatting = false },
  handlers = {
    -- ["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
    --   virtual_text = O.lang.tsserver.diagnostics.virtual_text,
    --   signs = O.lang.tsserver.diagnostics.signs,
    --   underline = O.lang.tsserver.diagnostics.underline,
    --   update_in_insert = true,
    -- }),
  },
}

require("lsp.ts-fmt-lint").setup()