From 70292566372d63fcc723263cb07e20cd7dcee20f Mon Sep 17 00:00:00 2001 From: Matthew Leong Date: Wed, 6 Mar 2024 07:18:24 -0800 Subject: [PATCH] feat: readd ts-tools commands --- lazy-lock.json | 41 +++++++------- lua/cosmic/lsp/commands.lua | 107 ++++++++++++++++++++++++++++++++++++ lua/cosmic/lsp/init.lua | 1 + 3 files changed, 129 insertions(+), 20 deletions(-) create mode 100644 lua/cosmic/lsp/commands.lua diff --git a/lazy-lock.json b/lazy-lock.json index c01218b..6fd9baa 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -1,7 +1,7 @@ { "Comment.nvim": { "branch": "master", "commit": "0236521ea582747b58869cb72f70ccfa967d2e89" }, - "LuaSnip": { "branch": "master", "commit": "c4b9c7c3b02826df74b93ae91009e05b758bfacf" }, - "SchemaStore.nvim": { "branch": "main", "commit": "91b56a811d87b9e7e0600c95f80ff2d08245bf61" }, + "LuaSnip": { "branch": "master", "commit": "a7a4b4682c4b3e2ba82b82a4e6e5f5a0e79dec32" }, + "SchemaStore.nvim": { "branch": "main", "commit": "88355559bb42cc8e4f6ca7b624dc4c3b654c5c08" }, "auto-session": { "branch": "main", "commit": "29a8c77a0579636d5520aebd38bdbc2e6079f2f5" }, "cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" }, "cmp-cmdline": { "branch": "main", "commit": "8ee981b4a91f536f52add291594e89fb6645e451" }, @@ -9,35 +9,36 @@ "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" }, "cosmic-ui": { "branch": "main", "commit": "c0b14531999f2bfef3d927c4dcd57a1a8fed5ee9" }, - "friendly-snippets": { "branch": "main", "commit": "5cc1f45c6aac699ad008fb85f6ae03236062667d" }, + "friendly-snippets": { "branch": "main", "commit": "dcd4a586439a1c81357d5b9d26319ae218cc9479" }, "gitsigns.nvim": { "branch": "main", "commit": "2c2463dbd82eddd7dbab881c3a62cfbfbe3c67ae" }, "lazy.nvim": { "branch": "main", "commit": "aedcd79811d491b60d0a6577a9c1701063c2a609" }, - "lualine.nvim": { "branch": "master", "commit": "7d131a8d3ba5016229e8a1d08bf8782acea98852" }, - "mason-lspconfig.nvim": { "branch": "main", "commit": "fe4cce44dec93c69be17dad79b21de867dde118a" }, - "mason.nvim": { "branch": "main", "commit": "c43eeb5614a09dc17c03a7fb49de2e05de203924" }, - "neodev.nvim": { "branch": "main", "commit": "365ef03dbf5b8579e6eb205b3500fc3bee17484a" }, + "lualine.nvim": { "branch": "master", "commit": "8b56462bfb746760465264de41b4907310f113ec" }, + "mason-lspconfig.nvim": { "branch": "main", "commit": "21d33d69a81f6351e5a5f49078b2e4f0075c8e73" }, + "mason.nvim": { "branch": "main", "commit": "3b5068f0fc565f337d67a2d315d935f574848ee7" }, + "neodev.nvim": { "branch": "main", "commit": "84e0290f5600e8b89c0dfcafc864f45496a53400" }, "noice.nvim": { "branch": "main", "commit": "bf67d70bd7265d075191e7812d8eb42b9791f737" }, - "none-ls.nvim": { "branch": "main", "commit": "34b1311bd07bd3741e60e06b34d0709d6e5a9f0f" }, - "nui.nvim": { "branch": "main", "commit": "af7dfee12fbf51d12cfc6ee386fa54f7a5a573c8" }, - "nvim-autopairs": { "branch": "master", "commit": "096d0baecc34f6c5d8a6dd25851e9d5ad338209b" }, + "none-ls.nvim": { "branch": "main", "commit": "ff3819c52ca04232fb70fbd6c1639de9abcbe122" }, + "nui.nvim": { "branch": "main", "commit": "756c59f46057cd2d43619cd3a6d4e01b2aa60295" }, + "nvim-autopairs": { "branch": "master", "commit": "c6139ca0d5ad7af129ea6c89cb4c56093f2c034a" }, "nvim-cmp": { "branch": "main", "commit": "04e0ca376d6abdbfc8b52180f8ea236cbfddf782" }, "nvim-colorizer.lua": { "branch": "master", "commit": "36c610a9717cc9ec426a07c8e6bf3b3abcb139d6" }, - "nvim-lspconfig": { "branch": "master", "commit": "114bf1875c4adef7c39c86ef538246478b4af87c" }, - "nvim-notify": { "branch": "master", "commit": "7138c86b28de61b6866c8cae60289136f0d539fa" }, - "nvim-tree.lua": { "branch": "master", "commit": "863cf832ceb0b2377c913b7696dd7d64f4978941" }, - "nvim-treesitter": { "branch": "master", "commit": "bdaa6b817aaef459e2d1968c50ce0061e51410e8" }, + "nvim-lspconfig": { "branch": "master", "commit": "e5e600232188ed9fb960f5df6bb06084ae8bf3ec" }, + "nvim-notify": { "branch": "master", "commit": "5371f4bfc1f6d3adf4fe9d62cd3a9d44356bfd15" }, + "nvim-tree.lua": { "branch": "master", "commit": "efafd73efa9bc8c26282aed563ba0f01c7465b06" }, + "nvim-treesitter": { "branch": "master", "commit": "c09932bd2de01dc9c01e870fe83060693c67de13" }, "nvim-treesitter-refactor": { "branch": "master", "commit": "65ad2eca822dfaec2a3603119ec3cc8826a7859e" }, "nvim-ts-autotag": { "branch": "main", "commit": "531f48334c422222aebc888fd36e7d109cb354cd" }, "nvim-ts-context-commentstring": { "branch": "main", "commit": "7ab799a9792f7cf3883cf28c6a00ad431f3d382a" }, - "nvim-web-devicons": { "branch": "master", "commit": "7f30f2da3c3641841ceb0e2c150281f624445e8f" }, + "nvim-web-devicons": { "branch": "master", "commit": "4adea17610d140a99c313e3f79a9dc01825d59ae" }, "nx.nvim": { "branch": "main", "commit": "f8a3a21b3d540889401a40d1f2803083794c0372" }, - "plenary.nvim": { "branch": "master", "commit": "4f71c0c4a196ceb656c824a70792f3df3ce6bb6d" }, + "plenary.nvim": { "branch": "master", "commit": "f7adfc4b3f4f91aab6caebf42b3682945fbc35be" }, "popup.nvim": { "branch": "master", "commit": "b7404d35d5d3548a82149238289fa71f7f6de4ac" }, - "telescope-fzf-native.nvim": { "branch": "main", "commit": "6c921ca12321edaa773e324ef64ea301a1d0da62" }, - "telescope.nvim": { "branch": "master", "commit": "45d61cc8da213e4052cf698653692b6a4c961760" }, + "telescope-fzf-native.nvim": { "branch": "main", "commit": "9ef21b2e6bb6ebeaf349a0781745549bbb870d27" }, + "telescope.nvim": { "branch": "master", "commit": "dc1ea28cc2a02cd1cebb9d80e967807d668f78f8" }, "todo-comments.nvim": { "branch": "main", "commit": "833d8dd8b07eeda37a09e99460f72a02616935cb" }, "tokyonight.nvim": { "branch": "main", "commit": "610179f7f12db3d08540b6cc61434db2eaecbcff" }, - "vim-floaterm": { "branch": "master", "commit": "97c085b2f69e931ed78bb1c260512d1e0492a754" }, - "vim-fugitive": { "branch": "master", "commit": "011cf4fcb93a9649ffc6dcdff56ef948f5d0f7cc" }, + "typescript-tools.nvim": { "branch": "master", "commit": "c43d9580c3ff5999a1eabca849f807ab33787ea7" }, + "vim-floaterm": { "branch": "master", "commit": "c0535d758004bcce016839e318cbbe802b92c469" }, + "vim-fugitive": { "branch": "master", "commit": "41beedabc7e948c787ea5696e04c3544c3674e23" }, "which-key.nvim": { "branch": "main", "commit": "4433e5ec9a507e5097571ed55c02ea9658fb268a" } } \ No newline at end of file diff --git a/lua/cosmic/lsp/commands.lua b/lua/cosmic/lsp/commands.lua new file mode 100644 index 0000000..1a1045e --- /dev/null +++ b/lua/cosmic/lsp/commands.lua @@ -0,0 +1,107 @@ +local user_config = require('cosmic.core.user') + +local found = false +for _, plugin in ipairs(user_config.add_plugins) do + if plugin[1] == 'pmizio/typescript-tools.nvim' then + found = true + break + end +end + +if found then + -- Major props to @failen here + -- https://github.com/pmizio/typescript-tools.nvim/issues/124 + vim.api.nvim_create_user_command('NxInit', function() + local now = tostring(os.time()) + local fileName = string.format('%s_graph.json', now) + -- @TODO check if we should use yarn or npx + local cmd = string.format('yarn -s nx graph --file=%s', fileName) + + local function handle_response(_, code) + -- read the file into memory + local projectGraphFile = io.open(fileName, 'r') + if projectGraphFile then + -- read project graph from file + local projectGraph = vim.json.decode(projectGraphFile:read('*a')) + projectGraphFile:close() + + -- remove the graph file + os.remove(fileName) + + -- get typescript-tools lsp client + local lspClients = vim.lsp.get_clients() + local tsclient + for _, client in ipairs(lspClients) do + if client.name == 'typescript-tools' then + tsclient = client + end + end + if not tsclient then + print('typescript-tools.nvim not active') + return + end + + -- get workspace root + -- always first? alternatives to get absolute workspace root? + local workspacePath = tsclient.config.workspace_folders[1].name + if not workspacePath then + print('Could not figure out workspace path') + return + end + + -- create external files for monodon + local externalFiles = {} + for _, project in pairs(projectGraph.graph.nodes) do + local sourceRoot = project.data.sourceRoot + + -- skip the root + if sourceRoot ~= '.' and sourceRoot then + -- localte the entry file. perhaps use tsconfig.[app|lib].json + local mainFile + if + project.data + and project.data.targets + and project.data.targets.build + and project.data.targets.build.options + and project.data.targets.build.options.main + then + mainFile = workspacePath .. '/' .. project.data.targets.build.options.main + else + mainFile = workspacePath .. '/' .. sourceRoot .. '/index.ts' + end + + -- insert to config + table.insert(externalFiles, { + mainFile = mainFile, -- this is not always index.ts! + directory = workspacePath .. '/' .. sourceRoot, + }) + end + end + + -- print(vim.inspect(externalFiles)) + -- send configuration request of monodon plugin to tsserver + local constants = require('typescript-tools.protocol.constants') + local method = constants.CustomMethods.ConfigurePlugin + local args = { + pluginName = '@monodon/typescript-nx-imports-plugin', + configuration = { + externalFiles = externalFiles, + }, + } + + ---@diagnostic disable-next-line: missing-parameter + tsclient.request(method, args, function() + print('tsserver handled configuration request', method) + end) + print('NX Plugin initialized!') + end + end + + -- call nx deamon with graph request, run it in the background with job + vim.fn.jobstart(cmd, { + on_exit = handle_response, + stdout_buffered = true, + stderr_buffered = true, + }) + end, {}) +end diff --git a/lua/cosmic/lsp/init.lua b/lua/cosmic/lsp/init.lua index 92f7ba0..0666bb2 100644 --- a/lua/cosmic/lsp/init.lua +++ b/lua/cosmic/lsp/init.lua @@ -1,3 +1,4 @@ require('cosmic.lsp.diagnostics') +require('cosmic.lsp.commands') -- LSP server initialization can be found in mason-lspconfig setup