Spaces not tabs.

This commit is contained in:
Ashkan Kiani
2019-11-20 16:16:36 -08:00
parent c40f8600d2
commit 1e16b3cf28

View File

@@ -16,11 +16,11 @@ local function resolve_bufnr(bufnr)
end end
local function ok_or_nil(status, ...) local function ok_or_nil(status, ...)
if not status then return end if not status then return end
return ... return ...
end end
local function npcall(fn, ...) local function npcall(fn, ...)
return ok_or_nil(pcall(fn, ...)) return ok_or_nil(pcall(fn, ...))
end end
local function err_message(...) local function err_message(...)
@@ -29,78 +29,78 @@ local function err_message(...)
end end
local function find_window_by_var(name, value) local function find_window_by_var(name, value)
for _, win in ipairs(api.nvim_list_wins()) do for _, win in ipairs(api.nvim_list_wins()) do
if npcall(api.nvim_win_get_var, win, name) == value then if npcall(api.nvim_win_get_var, win, name) == value then
return win return win
end end
end end
end end
local function request(method, params, callback) local function request(method, params, callback)
-- TODO(ashkan) enable this. -- TODO(ashkan) enable this.
-- callback = vim.lsp.default_callbacks[method] or callback -- callback = vim.lsp.default_callbacks[method] or callback
validate { validate {
method = {method, 's'}; method = {method, 's'};
callback = {callback, 'f'}; callback = {callback, 'f'};
} }
return vim.lsp.buf_request(0, method, params, function(err, _, result, client_id) return vim.lsp.buf_request(0, method, params, function(err, _, result, client_id)
if err then error(tostring(err)) end if err then error(tostring(err)) end
return callback(err, method, result, client_id) return callback(err, method, result, client_id)
end) end)
end end
local function focusable_preview(method, params, fn) local function focusable_preview(method, params, fn)
if npcall(api.nvim_win_get_var, 0, method) then if npcall(api.nvim_win_get_var, 0, method) then
return api.nvim_command("wincmd p") return api.nvim_command("wincmd p")
end end
local bufnr = api.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
do do
local win = find_window_by_var(method, bufnr) local win = find_window_by_var(method, bufnr)
if win then if win then
return api.nvim_set_current_win(win) return api.nvim_set_current_win(win)
end end
end end
return request(method, params, function(_, _, result, _) return request(method, params, function(_, _, result, _)
-- TODO(ashkan) could show error in preview... -- TODO(ashkan) could show error in preview...
local lines, filetype, opts = fn(result) local lines, filetype, opts = fn(result)
if lines then if lines then
local _, winnr = util.open_floating_preview(lines, filetype, opts) local _, winnr = util.open_floating_preview(lines, filetype, opts)
api.nvim_win_set_var(winnr, method, bufnr) api.nvim_win_set_var(winnr, method, bufnr)
end end
end) end)
end end
function M.hover() function M.hover()
local params = protocol.make_text_document_position_params() local params = protocol.make_text_document_position_params()
focusable_preview('textDocument/hover', params, function(result) focusable_preview('textDocument/hover', params, function(result)
if not (result and result.contents) then return end if not (result and result.contents) then return end
local markdown_lines = util.convert_input_to_markdown_lines(result.contents) local markdown_lines = util.convert_input_to_markdown_lines(result.contents)
markdown_lines = util.trim_empty_lines(markdown_lines) markdown_lines = util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then if vim.tbl_isempty(markdown_lines) then
return { 'No information available' } return { 'No information available' }
end end
return markdown_lines, util.try_trim_markdown_code_blocks(markdown_lines) return markdown_lines, util.try_trim_markdown_code_blocks(markdown_lines)
end) end)
end end
function M.peek_definition() function M.peek_definition()
local params = protocol.make_text_document_position_params() local params = protocol.make_text_document_position_params()
request('textDocument/peekDefinition', params, function(_, _, result, _) request('textDocument/peekDefinition', params, function(_, _, result, _)
if not (result and result[1]) then return end if not (result and result[1]) then return end
local loc = result[1] local loc = result[1]
local bufnr = vim.uri_to_bufnr(loc.uri) or error("couldn't find file "..tostring(loc.uri)) local bufnr = vim.uri_to_bufnr(loc.uri) or error("couldn't find file "..tostring(loc.uri))
local start = loc.range.start local start = loc.range.start
local finish = loc.range["end"] local finish = loc.range["end"]
util.open_floating_peek_preview(bufnr, start, finish, { offset_x = 1 }) util.open_floating_peek_preview(bufnr, start, finish, { offset_x = 1 })
local headbuf = util.open_floating_preview({"Peek:"}, nil, { local headbuf = util.open_floating_preview({"Peek:"}, nil, {
offset_y = -(finish.line - start.line); offset_y = -(finish.line - start.line);
width = finish.character - start.character + 2; width = finish.character - start.character + 2;
}) })
-- TODO(ashkan) change highlight group? -- TODO(ashkan) change highlight group?
api.nvim_buf_add_highlight(headbuf, -1, 'Keyword', 0, -1) api.nvim_buf_add_highlight(headbuf, -1, 'Keyword', 0, -1)
end) end)
end end
@@ -158,23 +158,23 @@ local function location_callback(_, method, result)
end end
function M.declaration() function M.declaration()
local params = protocol.make_text_document_position_params() local params = protocol.make_text_document_position_params()
request('textDocument/declaration', params, location_callback) request('textDocument/declaration', params, location_callback)
end end
function M.definition() function M.definition()
local params = protocol.make_text_document_position_params() local params = protocol.make_text_document_position_params()
request('textDocument/definition', params, location_callback) request('textDocument/definition', params, location_callback)
end end
function M.type_definition() function M.type_definition()
local params = protocol.make_text_document_position_params() local params = protocol.make_text_document_position_params()
request('textDocument/typeDefinition', params, location_callback) request('textDocument/typeDefinition', params, location_callback)
end end
function M.implementation() function M.implementation()
local params = protocol.make_text_document_position_params() local params = protocol.make_text_document_position_params()
request('textDocument/implementation', params, location_callback) request('textDocument/implementation', params, location_callback)
end end
--- Convert SignatureHelp response to preview contents. --- Convert SignatureHelp response to preview contents.
@@ -237,49 +237,50 @@ local function signature_help_to_preview_contents(input)
end end
function M.signature_help() function M.signature_help()
local params = protocol.make_text_document_position_params() local params = protocol.make_text_document_position_params()
focusable_preview('textDocument/signatureHelp', params, function(result) focusable_preview('textDocument/signatureHelp', params, function(result)
if not (result and result.signatures and result.signatures[1]) then return end if not (result and result.signatures and result.signatures[1]) then return end
-- TODO show empty popup when signatures is empty? -- TODO show empty popup when signatures is empty?
local lines = signature_help_to_preview_contents(result) local lines = signature_help_to_preview_contents(result)
lines = util.trim_empty_lines(lines) lines = util.trim_empty_lines(lines)
if vim.tbl_isempty(lines) then if vim.tbl_isempty(lines) then
return { 'No signature available' } return { 'No signature available' }
end end
return lines, util.try_trim_markdown_code_blocks(lines) return lines, util.try_trim_markdown_code_blocks(lines)
end) end)
end end
-- TODO(ashkan) ? -- TODO(ashkan) ?
function M.completion(context) function M.completion(context)
local params = protocol.make_text_document_position_params() local params = protocol.make_text_document_position_params()
params.context = context params.context = context
return request('textDocument/completion', params, function(_, _, result) return request('textDocument/completion', params, function(_, _, result)
if vim.tbl_isempty(result or {}) then return end if vim.tbl_isempty(result or {}) then return end
local row, col = unpack(api.nvim_win_get_cursor(0)) local row, col = unpack(api.nvim_win_get_cursor(0))
local line = assert(api.nvim_buf_get_lines(0, row-1, row, false)[1]) local line = assert(api.nvim_buf_get_lines(0, row-1, row, false)[1])
local line_to_cursor = line:sub(col+1) local line_to_cursor = line:sub(col+1)
local matches = util.text_document_completion_list_to_complete_items(result, line_to_cursor) local matches = util.text_document_completion_list_to_complete_items(result, line_to_cursor)
vim.fn.complete(col, matches) vim.fn.complete(col, matches)
end) end)
end end
function M.range_formatting() function M.range_formatting()
end end
function M.rename(new_name) function M.rename(new_name)
-- TODO(ashkan) use prepareRename -- TODO(ashkan) use prepareRename
-- * result: [`Range`](#range) \| `{ range: Range, placeholder: string }` \| `null` describing the range of the string to rename and optionally a placeholder text of the string content to be renamed. If `null` is returned then it is deemed that a 'textDocument/rename' request is not valid at the given position. -- * result: [`Range`](#range) \| `{ range: Range, placeholder: string }` \| `null` describing the range of the string to rename and optionally a placeholder text of the string content to be renamed. If `null` is returned then it is deemed that a 'textDocument/rename' request is not valid at the given position.
local params = protocol.make_text_document_position_params() local params = protocol.make_text_document_position_params()
new_name = new_name or npcall(vfn.input, "New Name: ") new_name = new_name or npcall(vfn.input, "New Name: ")
if not (new_name and #new_name > 0) then return end if not (new_name and #new_name > 0) then return end
params.newName = new_name params.newName = new_name
request('textDocument/rename', params, function(_, _, result) request('textDocument/rename', params, function(_, _, result)
if not result then return end if not result then return end
util.workspace_apply_workspace_edit(result) util.workspace_apply_workspace_edit(result)
end) end)
end end
return M return M
-- vim:sw=2 ts=2 et