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

Commit

Permalink
feat: do not reorder imports if not necessary (#102)
Browse files Browse the repository at this point in the history
* Do not reorder imports if not necessary

* add documentation for the new option

* Fix typo

* Change variable name
academo authored Jan 11, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 99e126d commit fb2f9d8
Showing 3 changed files with 52 additions and 15 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -61,6 +61,17 @@ built-in LSP client.
LSP `omnifunc`. Enable by setting `enable_import_on_completion` to `true`
inside `setup` (see below).

- Avoid organizing imports

By default `always_organize_imports` is set to `true`, every call to
`:TSLspImportAll` or `:TSLspImportCurrent` will also run `:TSLspOrganize`
to fix possible duplicated imports.

If `always_organize_imports` is set to `false`, it will only run
`:TSLspOrganize` in situations where is necessary: i.e. two new imports
from the same module.


- Fix invalid ranges

`tsserver` uses non-compliant ranges in some code actions (most notably "Move
@@ -172,6 +183,8 @@ lspconfig.tsserver.setup({
},
import_all_scan_buffers = 100,
import_all_select_source = false,
-- if false will avoid organizing imports
always_organize_imports = true,

-- filter diagnostics
filter_out_diagnostics_by_severity = {},
53 changes: 38 additions & 15 deletions lua/nvim-lsp-ts-utils/import-all.lua
Original file line number Diff line number Diff line change
@@ -177,30 +177,53 @@ local response_handler_factory = function(callback)
end
end

local should_reorder = function(edits)
local modules = {}
for _, edit in ipairs(edits) do
if edit.newText then
local module = string.match(edit.newText, "import.*from (.+)[;\n]?")

if not module then
return
end

local source = vim.trim(module)
if modules[source] then
return true
end

modules[source] = true
end
end
return false
end

local apply_edits = function(edits, bufnr)
if vim.tbl_count(edits) == 0 then
return
end

lsp.util.apply_text_edits(edits, bufnr)

-- organize imports to merge separate import statements from the same file
require("nvim-lsp-ts-utils.organize-imports").async(bufnr, function()
-- remove empty lines created by merge
local empty_start, empty_end
for i, line in ipairs(api.nvim_buf_get_lines(bufnr, 0, -1, false)) do
if not empty_end and empty_start and line ~= "" then
empty_end = i - 1
break
if o.get().always_organize_imports or should_reorder(edits) then
-- organize imports to merge separate import statements from the same file
require("nvim-lsp-ts-utils.organize-imports").async(bufnr, function()
-- remove empty lines created by merge
local empty_start, empty_end
for i, line in ipairs(api.nvim_buf_get_lines(bufnr, 0, -1, false)) do
if not empty_end and empty_start and line ~= "" then
empty_end = i - 1
break
end
if not empty_start and line == "" then
empty_start = i
end
end
if not empty_start and line == "" then
empty_start = i
if empty_start and empty_end and empty_start < empty_end then
api.nvim_buf_set_lines(bufnr, empty_start, empty_end, false, {})
end
end
if empty_start and empty_end and empty_start < empty_end then
api.nvim_buf_set_lines(bufnr, empty_start, empty_end, false, {})
end
end)
end)
end
end

return function(bufnr, diagnostics)
1 change: 1 addition & 0 deletions lua/nvim-lsp-ts-utils/options.lua
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ local defaults = {
},
import_all_select_source = false,
import_all_scan_buffers = 100,
always_organize_imports = true,

-- completion
enable_import_on_completion = false,

0 comments on commit fb2f9d8

Please sign in to comment.