Extend list_extend to take start/finish.

This commit is contained in:
Ashkan Kiani
2019-11-20 17:09:21 -08:00
parent 9b7f956715
commit b984f613c1
4 changed files with 31 additions and 27 deletions

View File

@@ -5,12 +5,17 @@ local api = vim.api
local M = {} local M = {}
local function err_message(...)
api.nvim_err_writeln(table.concat(vim.tbl_flatten{...}))
api.nvim_command("redraw")
end
M['textDocument/publishDiagnostics'] = function(_, _, result) M['textDocument/publishDiagnostics'] = function(_, _, result)
if not result then return end if not result then return end
local uri = result.uri local uri = result.uri
local bufnr = vim.uri_to_bufnr(uri) local bufnr = vim.uri_to_bufnr(uri)
if not bufnr then if not bufnr then
api.nvim_err_writeln(string.format("LSP.publishDiagnostics: Couldn't find buffer for %s", uri)) err_message("LSP.publishDiagnostics: Couldn't find buffer for ", uri)
return return
end end
util.buf_clear_diagnostics(bufnr) util.buf_clear_diagnostics(bufnr)
@@ -20,11 +25,6 @@ M['textDocument/publishDiagnostics'] = function(_, _, result)
-- util.buf_loclist(bufnr, result.diagnostics) -- util.buf_loclist(bufnr, result.diagnostics)
end end
local function err_message(...)
api.nvim_err_writeln(table.concat(vim.tbl_flatten{...}))
api.nvim_command("redraw")
end
local function log_message(_, _, result, client_id) local function log_message(_, _, result, client_id)
local message_type = result.type local message_type = result.type
local message = result.message local message = result.message
@@ -34,8 +34,6 @@ local function log_message(_, _, result, client_id)
err_message("LSP[", client_name, "] client has shut down after sending the message") err_message("LSP[", client_name, "] client has shut down after sending the message")
end end
if message_type == protocol.MessageType.Error then if message_type == protocol.MessageType.Error then
-- Might want to not use err_writeln,
-- but displaying a message with red highlights or something
err_message("LSP[", client_name, "] ", message) err_message("LSP[", client_name, "] ", message)
else else
local message_type_name = protocol.MessageType[message_type] local message_type_name = protocol.MessageType[message_type]

View File

@@ -546,7 +546,6 @@ end
-- Remove empty lines from the beginning and end. -- Remove empty lines from the beginning and end.
function M.trim_empty_lines(lines) function M.trim_empty_lines(lines)
local result = {}
local start = 1 local start = 1
for i = 1, #lines do for i = 1, #lines do
if #lines[i] > 0 then if #lines[i] > 0 then
@@ -561,11 +560,7 @@ function M.trim_empty_lines(lines)
break break
end end
end end
-- TODO(ashkan) use tbl_slice. return vim.list_extend({}, lines, start, finish)
for i = start, finish do
table.insert(result, lines[i])
end
return result
end end
-- Accepts markdown lines and tries to reduce it to a filetype if it is -- Accepts markdown lines and tries to reduce it to a filetype if it is

View File

@@ -226,18 +226,25 @@ function vim.tbl_add_reverse_lookup(o)
return o return o
end end
--- Extends a list-like table with the values of another list-like table. -- Extends a list-like table with the values of another list-like table.
--- --
-- NOTE: This *mutates* dst! -- NOTE: This *mutates* dst!
-- @see |extend()| -- @see |extend()|
--- --
--@param dst The list which will be modified and appended to. -- @param dst list which will be modified and appended to.
--@param src The list from which values will be inserted. -- @param src list from which values will be inserted.
function vim.list_extend(dst, src) -- @param start Start index on src. defaults to 1
assert(type(dst) == 'table', "dst must be a table") -- @param finish Final index on src. defaults to #src
assert(type(src) == 'table', "src must be a table") -- @returns dst
for _, v in ipairs(src) do function vim.list_extend(dst, src, start, finish)
table.insert(dst, v) vim.validate {
dst = {dst, 't'};
src = {src, 't'};
start = {start, 'n', true};
finish = {finish, 'n', true};
}
for i = start or 1, finish or #src do
table.insert(dst, src[i])
end end
return dst return dst
end end

View File

@@ -353,10 +353,14 @@ describe('lua stdlib', function()
it('vim.list_extend', function() it('vim.list_extend', function()
eq({1,2,3}, exec_lua [[ return vim.list_extend({1}, {2,3}) ]]) eq({1,2,3}, exec_lua [[ return vim.list_extend({1}, {2,3}) ]])
eq('Error executing lua: .../shared.lua: src must be a table', eq('Error executing lua: .../shared.lua: src: expected table, got nil',
pcall_err(exec_lua, [[ return vim.list_extend({1}, nil) ]])) pcall_err(exec_lua, [[ return vim.list_extend({1}, nil) ]]))
eq({1,2}, exec_lua [[ return vim.list_extend({1}, {2;a=1}) ]]) eq({1,2}, exec_lua [[ return vim.list_extend({1}, {2;a=1}) ]])
eq(true, exec_lua [[ local a = {1} return vim.list_extend(a, {2;a=1}) == a ]]) eq(true, exec_lua [[ local a = {1} return vim.list_extend(a, {2;a=1}) == a ]])
eq({2}, exec_lua [[ return vim.list_extend({}, {2;a=1}, 1) ]])
eq({}, exec_lua [[ return vim.list_extend({}, {2;a=1}, 2) ]])
eq({}, exec_lua [[ return vim.list_extend({}, {2;a=1}, 1, -1) ]])
eq({2}, exec_lua [[ return vim.list_extend({}, {2;a=1}, -1, 2) ]])
end) end)
it('vim.tbl_add_reverse_lookup', function() it('vim.tbl_add_reverse_lookup', function()