Skip to content

Commit

Permalink
fix: popup showing when no LSP client attached
Browse files Browse the repository at this point in the history
Prevent renaming when no LSP client is attached to the current buffer.
Overhaul tests to cover the renaming logic.

Closes: GH-96
Signed-off-by: Filip Dutescu <[email protected]>
  • Loading branch information
filipdutescu committed Nov 29, 2021
1 parent dd25890 commit 1b98bae
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 22 deletions.
1 change: 1 addition & 0 deletions lua/renamer/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ local strings = {
checking_setup_called = 'Chechking whether setup was made...',
setup_not_called = '"renamer.setup()" not called. Please make sure setup is done before using the plugin.',
invalid_type_err_template = 'Invalid type for "%s". Expected "%s", but found "%s".',
no_lsp_client_found_err = 'No LSP client found for the current file, but "renamer" requires one to work.',
}

return {
Expand Down
14 changes: 12 additions & 2 deletions lua/renamer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ end
--- @return table prompt_window_opts @Keys: opts, border_opts
function renamer.rename(opts)
opts = opts or { empty = false }

local lsp_clients = vim.lsp.buf_get_clients(0)
if lsp_clients == nil or #lsp_clients < 1 then
log.error(strings.no_lsp_client_found_err)
end

local cword = vim.fn.expand(strings.cword_keyword)
local win_height = vim.api.nvim_win_get_height(0)
local win_width = vim.api.nvim_win_get_width(0)
Expand Down Expand Up @@ -368,7 +374,7 @@ function renamer._lsp_rename(word, pos)
log.error(err)
return
end
if not resp then
if resp == nil then
log.warn(strings.nil_lsp_response_err)
return
end
Expand All @@ -380,7 +386,7 @@ function renamer._lsp_rename(word, pos)
end
end

lsp_utils.apply_workspace_edit(resp)
renamer._apply_workspace_edit(resp)

if renamer.with_qf_list then
utils.set_qf_list(changes)
Expand Down Expand Up @@ -457,4 +463,8 @@ function renamer._buf_request(buf_id, method, params, handler)
vim.lsp.buf_request(buf_id, method, params, handler)
end

function renamer._apply_workspace_edit(resp)
lsp_utils.apply_workspace_edit(resp)
end

return renamer
177 changes: 169 additions & 8 deletions lua/tests/renamer_close_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local strings = require('renamer.constants').strings
local renamer = require 'renamer'
local utils = require 'renamer.utils'

local mock = require 'luassert.mock'
local stub = require 'luassert.stub'
Expand Down Expand Up @@ -142,32 +143,192 @@ describe('renamer', function()
lsp_rename.revert(lsp_rename)
end)

it('should set cursor after the end of the new word', function()
it('should not apply changes if error is received', function()
local expected_win_id, expected_buf_id = 123, 321
local expected_content = 'test'
local api_mock = mock(vim.api, true)
api_mock.nvim_win_get_buf.returns(expected_buf_id)
api_mock.nvim_buf_get_lines.returns { expected_content }
api_mock.nvim_command.returns()
api_mock.nvim_win_set_cursor.returns()
local on_close = stub(renamer, 'on_close').returns()
local buf_request = stub(renamer, '_buf_request').invokes(function(_, _, _, handler)
handler({}, nil)
end)
local expected_pos = { word_start = 1, line = 1, col = 1 }
local expected_params = {}
local make_params = stub(renamer, '_make_position_params').returns(expected_params)
local buf_request = stub(renamer, '_buf_request').invokes(function()
api_mock.nvim_win_set_cursor(0, { expected_pos.line, expected_pos.col })
renamer._buffers[expected_win_id] = { opts = { initial_word = 'test', initial_pos = expected_pos } }
local apply_changes = stub(renamer, '_apply_workspace_edit')
local make_params = stub(renamer, '_make_position_params').returns {}

renamer.on_submit(expected_win_id)

assert.spy(apply_changes).called_less_than(1)
assert.spy(on_close).was_called_with(expected_win_id, false)
assert.spy(api_mock.nvim_win_get_buf).was_called_with(expected_win_id)
assert.spy(api_mock.nvim_buf_get_lines).was_called_with(expected_buf_id, -2, -1, false)
mock.revert(api_mock)
on_close.revert(on_close)
buf_request.revert(buf_request)
apply_changes.revert(apply_changes)
make_params.revert(make_params)
end)

it('should not apply changes if no response is received', function()
local expected_win_id, expected_buf_id = 123, 321
local expected_content = 'test'
local api_mock = mock(vim.api, true)
api_mock.nvim_win_get_buf.returns(expected_buf_id)
api_mock.nvim_buf_get_lines.returns { expected_content }
api_mock.nvim_command.returns()
local on_close = stub(renamer, 'on_close').returns()
local buf_request = stub(renamer, '_buf_request').invokes(function(_, _, _, handler)
handler(nil, nil)
end)
renamer._buffers[expected_win_id] = { opts = { initial_pos = expected_pos } }
local expected_pos = { word_start = 1, line = 1, col = 1 }
renamer._buffers[expected_win_id] = { opts = { initial_word = 'test', initial_pos = expected_pos } }
local apply_changes = stub(renamer, '_apply_workspace_edit')
local make_params = stub(renamer, '_make_position_params').returns {}

renamer.on_submit(expected_win_id)

assert.spy(buf_request).was_called()
assert.spy(apply_changes).called_less_than(1)
assert.spy(on_close).was_called_with(expected_win_id, false)
assert.spy(api_mock.nvim_win_get_buf).was_called_with(expected_win_id)
assert.spy(api_mock.nvim_buf_get_lines).was_called_with(expected_buf_id, -2, -1, false)
mock.revert(api_mock)
on_close.revert(on_close)
buf_request.revert(buf_request)
apply_changes.revert(apply_changes)
make_params.revert(make_params)
end)

it('should apply changes if no error is received', function()
renamer.setup { with_qf_list = false }
local expected_win_id, expected_buf_id = 123, 321
local expected_content = 'test'
local api_mock = mock(vim.api, true)
api_mock.nvim_win_get_buf.returns(expected_buf_id)
api_mock.nvim_buf_get_lines.returns { expected_content }
api_mock.nvim_command.returns()
api_mock.nvim_get_mode.returns(strings.insert_mode_short_string)
local on_close = stub(renamer, 'on_close').returns()
local buf_request = stub(renamer, '_buf_request').invokes(function(_, _, _, handler)
handler(nil, {})
end)
local expected_pos = { word_start = 1, line = 1, col = 1 }
renamer._buffers[expected_win_id] = { opts = { initial_word = 'test', initial_pos = expected_pos } }
local apply_changes = stub(renamer, '_apply_workspace_edit')
local make_params = stub(renamer, '_make_position_params').returns {}

renamer.on_submit(expected_win_id)

assert.spy(apply_changes).was_called()
assert.spy(on_close).was_called_with(expected_win_id, false)
assert.spy(api_mock.nvim_win_get_buf).was_called_with(expected_win_id)
assert.spy(api_mock.nvim_buf_get_lines).was_called_with(expected_buf_id, -2, -1, false)
mock.revert(api_mock)
on_close.revert(on_close)
buf_request.revert(buf_request)
apply_changes.revert(apply_changes)
make_params.revert(make_params)
end)

it('should call the custom handler if it is set', function()
local custom_handler_called = false
renamer.setup {
with_qf_list = false,
handler = function()
custom_handler_called = true
end,
}
local expected_win_id, expected_buf_id = 123, 321
local expected_content = 'test'
local api_mock = mock(vim.api, true)
api_mock.nvim_win_get_buf.returns(expected_buf_id)
api_mock.nvim_buf_get_lines.returns { expected_content }
api_mock.nvim_command.returns()
api_mock.nvim_get_mode.returns(strings.insert_mode_short_string)
local on_close = stub(renamer, 'on_close').returns()
local buf_request = stub(renamer, '_buf_request').invokes(function(_, _, _, handler)
handler(nil, {})
end)
local expected_pos = { word_start = 1, line = 1, col = 1 }
renamer._buffers[expected_win_id] = { opts = { initial_word = 'test', initial_pos = expected_pos } }
local apply_changes = stub(renamer, '_apply_workspace_edit')
local make_params = stub(renamer, '_make_position_params').returns {}

renamer.on_submit(expected_win_id)

eq(true, custom_handler_called)
assert.spy(on_close).was_called_with(expected_win_id, false)
assert.spy(api_mock.nvim_win_get_buf).was_called_with(expected_win_id)
assert.spy(api_mock.nvim_buf_get_lines).was_called_with(expected_buf_id, -2, -1, false)
mock.revert(api_mock)
on_close.revert(on_close)
buf_request.revert(buf_request)
apply_changes.revert(apply_changes)
make_params.revert(make_params)
end)

it('should set qf list if the option is turned on', function()
local expected_win_id, expected_buf_id = 123, 321
local expected_content = 'test'
local api_mock = mock(vim.api, true)
api_mock.nvim_win_get_buf.returns(expected_buf_id)
api_mock.nvim_buf_get_lines.returns { expected_content }
api_mock.nvim_command.returns()
local on_close = stub(renamer, 'on_close').returns()
local buf_request = stub(renamer, '_buf_request').invokes(function(_, _, _, handler)
handler({}, nil)
end)
local expected_pos = { word_start = 1, line = 1, col = 1 }
renamer._buffers[expected_win_id] = { opts = { initial_word = 'test', initial_pos = expected_pos } }
local apply_changes = stub(renamer, '_apply_workspace_edit')
local set_qf_list = stub(utils, 'set_qf_list')
local make_params = stub(renamer, '_make_position_params').returns {}

renamer.on_submit(expected_win_id)

assert.spy(apply_changes).called_less_than(1)
assert.spy(on_close).was_called_with(expected_win_id, false)
assert.spy(api_mock.nvim_win_get_buf).was_called_with(expected_win_id)
assert.spy(api_mock.nvim_buf_get_lines).was_called_with(expected_buf_id, -2, -1, false)
mock.revert(api_mock)
on_close.revert(on_close)
buf_request.revert(buf_request)
apply_changes.revert(apply_changes)
make_params.revert(make_params)
set_qf_list.revert(set_qf_list)
end)

it('should set cursor after the end of the new word', function()
renamer.setup { with_qf_list = false }
local expected_win_id, expected_buf_id = 123, 321
local expected_content = 'test'
local api_mock = mock(vim.api, true)
api_mock.nvim_win_get_buf.returns(expected_buf_id)
api_mock.nvim_buf_get_lines.returns { expected_content }
api_mock.nvim_command.returns()
api_mock.nvim_get_mode.returns(strings.insert_mode_short_string)
local on_close = stub(renamer, 'on_close').returns()
local buf_request = stub(renamer, '_buf_request').invokes(function(_, _, _, handler)
handler(nil, {})
end)
local expected_pos = { word_start = 1, line = 1, col = 1 }
local expected_col = expected_pos.word_start + #expected_content - 1
renamer._buffers[expected_win_id] = { opts = { initial_word = 'test', initial_pos = expected_pos } }
local apply_changes = stub(renamer, '_apply_workspace_edit')
local make_params = stub(renamer, '_make_position_params').returns {}

renamer.on_submit(expected_win_id)

assert.spy(api_mock.nvim_win_set_cursor).was_called_with(0, { expected_pos.line, expected_col })
assert.spy(on_close).was_called_with(expected_win_id, false)
assert.spy(api_mock.nvim_win_get_buf).was_called_with(expected_win_id)
assert.spy(api_mock.nvim_buf_get_lines).was_called_with(expected_buf_id, -2, -1, false)
mock.revert(api_mock)
on_close.revert(on_close)
buf_request.revert(buf_request)
apply_changes.revert(apply_changes)
make_params.revert(make_params)
end)
end)
Expand Down
Loading

0 comments on commit 1b98bae

Please sign in to comment.