Skip to content
This repository has been archived by the owner on Jul 17, 2022. It is now read-only.

Commit

Permalink
feat: add :TSLspFixCurrent
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Alvarez committed Nov 16, 2021
1 parent bd0140e commit 825630a
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`<C-y>`) when using the built-in
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-lsp-ts-utils/define-commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
Expand Down
4 changes: 2 additions & 2 deletions lua/nvim-lsp-ts-utils/import-all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lua/nvim-lsp-ts-utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/spec/define-commands_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
3 changes: 1 addition & 2 deletions test/spec/e2e_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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";]])
Expand Down

0 comments on commit 825630a

Please sign in to comment.