aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorJesse Hoyos <jesse@mojotech.com>2019-11-15 17:28:56 -0500
committerAshkan Kiani <ashkan.k.kiani@gmail.com>2019-11-15 14:28:56 -0800
commit242c6aae1dfa7fdc7e4fd1f7b76768e6b0688f7a (patch)
tree9a1eba68173c3aeae0251a2ad4365d6dcc6834b2 /lua
parentTexlab settings (#14) (diff)
downloadnvim-lspconfig-242c6aae1dfa7fdc7e4fd1f7b76768e6b0688f7a.tar
nvim-lspconfig-242c6aae1dfa7fdc7e4fd1f7b76768e6b0688f7a.tar.gz
nvim-lspconfig-242c6aae1dfa7fdc7e4fd1f7b76768e6b0688f7a.tar.bz2
nvim-lspconfig-242c6aae1dfa7fdc7e4fd1f7b76768e6b0688f7a.tar.lz
nvim-lspconfig-242c6aae1dfa7fdc7e4fd1f7b76768e6b0688f7a.tar.xz
nvim-lspconfig-242c6aae1dfa7fdc7e4fd1f7b76768e6b0688f7a.tar.zst
nvim-lspconfig-242c6aae1dfa7fdc7e4fd1f7b76768e6b0688f7a.zip
Initial typescript support (#8)
Diffstat (limited to 'lua')
-rw-r--r--lua/nvim_lsp.lua1
-rw-r--r--lua/nvim_lsp/tsserver.lua109
2 files changed, 110 insertions, 0 deletions
diff --git a/lua/nvim_lsp.lua b/lua/nvim_lsp.lua
index 4a6518f2..a2305eee 100644
--- a/lua/nvim_lsp.lua
+++ b/lua/nvim_lsp.lua
@@ -4,6 +4,7 @@ require 'nvim_lsp/elmls'
require 'nvim_lsp/gopls'
require 'nvim_lsp/pyls'
require 'nvim_lsp/texlab'
+require 'nvim_lsp/tsserver'
local M = {
util = require 'nvim_lsp/util';
diff --git a/lua/nvim_lsp/tsserver.lua b/lua/nvim_lsp/tsserver.lua
new file mode 100644
index 00000000..e9f5031f
--- /dev/null
+++ b/lua/nvim_lsp/tsserver.lua
@@ -0,0 +1,109 @@
+local skeleton = require 'nvim_lsp/skeleton'
+local util = require 'nvim_lsp/util'
+local lsp = vim.lsp
+local fn = vim.fn
+local api = vim.api
+local need_bins = util.need_bins
+
+local default_capabilities = lsp.protocol.make_client_capabilities()
+default_capabilities.offsetEncoding = {"utf-8", "utf-16"}
+
+local bin_name = "typescript-language-server"
+
+local install_dir = util.path.join(fn.stdpath("cache"), "nvim_lsp", "typescript-language-server")
+
+local function get_install_info()
+ local bin_dir = util.path.join(install_dir, "node_modules", ".bin")
+ local bins = { bin_dir = bin_dir; install_dir = install_dir; }
+ bins.tsserver = util.path.join(bin_dir, bin_name)
+ bins.is_installed = need_bins(bins.tsserver)
+ return bins
+end
+
+local global_commands = {
+ TsServerInstall = {
+ function()
+ if get_install_info().is_installed then
+ return print('typescript-language-server is already installed')
+ end
+ skeleton.tsserver.install()
+ end;
+ description = 'Install typescript-language-server and its dependencies to stdpath("cache")/nvim_lsp/typescript-language-server';
+ };
+ TsServerInstallInfo = {
+ function()
+ local install_info = get_install_info()
+ if not install_info.is_installed then
+ return print('typescript-language-server is not installed')
+ end
+ print(vim.inspect(install_info))
+ end;
+ description = 'Print installation infor for `typescript-language-server`'
+ }
+};
+
+util.create_module_commands("typescript-language-server", global_commands)
+
+
+skeleton.tsserver = {
+ default_config = {
+ cmd = {bin_name};
+ filetypes = {"typescript", "typescriptreact", "typescript.tsx"};
+ root_dir = util.root_pattern("package.json");
+ log_level = lsp.protocol.MessageType.Warning;
+ settings = {};
+ capabilities = default_capabilities;
+ on_init = function(client, result)
+ if result.offsetEncoding then
+ client.offset_encoding = result.offsetEncoding
+ end
+ end
+ };
+ commands = global_commands;
+ on_new_config = function(new_config)
+ local install_info = get_install_info()
+ if install_info.is_installed then
+ new_config.cmd = {install_info.tsserver}
+ end
+ end;
+ docs = {
+ description = [[
+https://github.com/theia-ide/typescript-language-server
+
+typescript-language-server can be installed via `:TsServerInstall` or by yourself with `npm`:
+```sh
+npm install -g typescript-language-server
+```
+]];
+ default_config = {
+ root_dir = [[root_pattern("package.json")]];
+ on_init = [[function to handle changing offsetEncoding]];
+ capabilities = [[default capabilities, with offsetEncoding utf-8]];
+ };
+ };
+}
+
+function skeleton.tsserver.install()
+ if not need_bins(bin_name) then return end
+ if not need_bins("sh", "npm", "mkdir", "cd") then
+ vim.api.nvim_err_writeln('Installation requires "sh", "npm", "mkdir", "cd"')
+ return
+ end
+ local install_info = get_install_info()
+ if install_info.is_installed then
+ return
+ end
+ local cmd = io.popen("sh", "w")
+ local install_script = ([[
+set -eo pipefail
+mkdir -p "{{install_dir}}"
+cd "{{install_dir}}"
+npm install typescript-language-server
+]]):gsub("{{(%S+)}}", install_info)
+ print(install_script)
+ cmd:write(install_script)
+ cmd:close()
+end
+
+skeleton.tsserver.get_install_info = get_install_info
+-- vim:et ts=2 sw=2