From 825630a5d28634fbb3663d1e605ff0a82b843d81 Mon Sep 17 00:00:00 2001 From: Jose Alvarez Date: Tue, 16 Nov 2021 14:24:51 -0500 Subject: [PATCH] feat: add :TSLspFixCurrent --- README.md | 5 +++++ lua/nvim-lsp-ts-utils/define-commands.lua | 1 + lua/nvim-lsp-ts-utils/import-all.lua | 4 ++-- lua/nvim-lsp-ts-utils/init.lua | 3 +++ test/spec/define-commands_spec.lua | 2 ++ test/spec/e2e_spec.lua | 3 +-- 6 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3d2f414..894f722 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,11 @@ built-in LSP client. which will prompt you to choose from the available options when there's a conflict. +- Import missing import under cursor (exposed as `:TSLspImportCurrent`) + + Adds the missing import under the cursor. Affected by the same options as + `:TSLspImportAll`. + - Import on completion Adds missing imports on completion confirm (``) when using the built-in diff --git a/lua/nvim-lsp-ts-utils/define-commands.lua b/lua/nvim-lsp-ts-utils/define-commands.lua index 1bed503..eb6580c 100644 --- a/lua/nvim-lsp-ts-utils/define-commands.lua +++ b/lua/nvim-lsp-ts-utils/define-commands.lua @@ -10,6 +10,7 @@ local define_commands = function() buf_command("TSLspOrganize", "organize_imports()") buf_command("TSLspOrganizeSync", "organize_imports_sync()") buf_command("TSLspImportAll", "import_all()") + buf_command("TSLspImportCurrent", "import_current()") -- Inlay hints buf_command("TSLspInlayHints", "inlay_hints()") buf_command("TSLspDisableInlayHints", "disable_inlay_hints()") diff --git a/lua/nvim-lsp-ts-utils/import-all.lua b/lua/nvim-lsp-ts-utils/import-all.lua index 84d6889..9bf30f3 100644 --- a/lua/nvim-lsp-ts-utils/import-all.lua +++ b/lua/nvim-lsp-ts-utils/import-all.lua @@ -213,11 +213,11 @@ local apply_edits = function(edits, bufnr) end) end -return function(bufnr) +return function(bufnr, diagnostics) local a = require("plenary.async") local runner = function() - local diagnostics = get_diagnostics(bufnr) + diagnostics = diagnostics or get_diagnostics(bufnr) if not diagnostics then return end diff --git a/lua/nvim-lsp-ts-utils/init.lua b/lua/nvim-lsp-ts-utils/init.lua index ad8cdb5..6fb41d4 100644 --- a/lua/nvim-lsp-ts-utils/init.lua +++ b/lua/nvim-lsp-ts-utils/init.lua @@ -25,6 +25,9 @@ M.setup_client = client.setup M.import_on_completion = import_on_completion.handle M.import_all = import_all +M.import_current = function() + import_all(vim.api.nvim_get_current_buf(), vim.lsp.diagnostic.get_line_diagnostics()) +end M.inlay_hints = inlay_hints.inlay_hints M.disable_inlay_hints = inlay_hints.disable_inlay_hints diff --git a/test/spec/define-commands_spec.lua b/test/spec/define-commands_spec.lua index dfd03a3..da12c68 100644 --- a/test/spec/define-commands_spec.lua +++ b/test/spec/define-commands_spec.lua @@ -27,6 +27,7 @@ describe("define_commands", function() assert.equals(exists("TSLspOrganizeSync"), false) assert.equals(exists("TSLspRenameFile"), false) assert.equals(exists("TSLspImportAll"), false) + assert.equals(exists("TSLspImportCurrent"), false) end) it("should define commands if option is not disabled", function() @@ -39,5 +40,6 @@ describe("define_commands", function() assert.equals(exists("TSLspOrganizeSync"), true) assert.equals(exists("TSLspRenameFile"), true) assert.equals(exists("TSLspImportAll"), true) + assert.equals(exists("TSLspImportCurrent"), true) end) end) diff --git a/test/spec/e2e_spec.lua b/test/spec/e2e_spec.lua index 4b06d52..06c1c51 100644 --- a/test/spec/e2e_spec.lua +++ b/test/spec/e2e_spec.lua @@ -2,7 +2,6 @@ local null_ls = require("null-ls") local lspconfig = require("lspconfig") local ts_utils = require("nvim-lsp-ts-utils") -local import_all = require("nvim-lsp-ts-utils.import-all") local tu = require("test.utils") local api = vim.api @@ -57,7 +56,7 @@ describe("e2e", function() end) it("should import both missing types", function() - import_all() + ts_utils.import_all() lsp_wait() assert.equals(get_buf_content(1), [[import { User, UserNotification } from "./test-types";]])