extmark: move id to dict in nvim_buf_set_extmark

This commit is contained in:
Björn Linse
2020-01-21 14:01:40 +01:00
committed by Thomas Vigouroux
parent 54ce1010e8
commit d3302573ba
2 changed files with 30 additions and 19 deletions

View File

@@ -1274,7 +1274,7 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start,
/// @param opts Optional parameters. Currently not used. /// @param opts Optional parameters. Currently not used.
/// @param[out] err Error details, if any /// @param[out] err Error details, if any
/// @return Id of the created/updated extmark /// @return Id of the created/updated extmark
Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer id, Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id,
Integer line, Integer col, Integer line, Integer col,
Dictionary opts, Error *err) Dictionary opts, Error *err)
FUNC_API_SINCE(7) FUNC_API_SINCE(7)
@@ -1289,11 +1289,6 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer id,
return 0; return 0;
} }
if (opts.size > 0) {
api_set_error(err, kErrorTypeValidation, "opts dict isn't empty");
return 0;
}
size_t len = 0; size_t len = 0;
if (line < 0 || line > buf->b_ml.ml_line_count) { if (line < 0 || line > buf->b_ml.ml_line_count) {
api_set_error(err, kErrorTypeValidation, "line value outside range"); api_set_error(err, kErrorTypeValidation, "line value outside range");
@@ -1309,19 +1304,32 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer id,
return 0; return 0;
} }
uint64_t id_num; uint64_t id = 0;
if (id >= 0) { for (size_t i = 0; i < opts.size; i++) {
id_num = (uint64_t)id; String k = opts.items[i].key;
} else { Object *v = &opts.items[i].value;
api_set_error(err, kErrorTypeValidation, "Invalid mark id"); if (strequal("id", k.data)) {
return 0; if (v->type != kObjectTypeInteger || v->data.integer <= 0) {
api_set_error(err, kErrorTypeValidation,
"id is not a positive integer");
goto error;
} }
id_num = extmark_set(buf, (uint64_t)ns_id, id_num, id = (uint64_t)v->data.integer;
(int)line, (colnr_T)col, } else {
-1, -1, NULL, kExtmarkUndo); api_set_error(err, kErrorTypeValidation, "unexpected key: %s", k.data);
goto error;
}
}
return (Integer)id_num;
id = extmark_set(buf, (uint64_t)ns_id, id,
(int)line, (colnr_T)col, -1, -1, NULL, kExtmarkUndo);
return (Integer)id;
error:
return 0;
} }
/// Removes an extmark. /// Removes an extmark.

View File

@@ -32,7 +32,10 @@ local function set_extmark(ns_id, id, line, col, opts)
if opts == nil then if opts == nil then
opts = {} opts = {}
end end
return curbufmeths.set_extmark(ns_id, id, line, col, opts) if id ~= nil and id ~= 0 then
opts.id = id
end
return curbufmeths.set_extmark(ns_id, line, col, opts)
end end
local function get_extmarks(ns_id, start, end_, opts) local function get_extmarks(ns_id, start, end_, opts)
@@ -1357,14 +1360,14 @@ describe('API/extmarks', function()
it('can set a mark to other buffer', function() it('can set a mark to other buffer', function()
local buf = request('nvim_create_buf', 0, 1) local buf = request('nvim_create_buf', 0, 1)
request('nvim_buf_set_lines', buf, 0, -1, 1, {"", ""}) request('nvim_buf_set_lines', buf, 0, -1, 1, {"", ""})
local id = bufmeths.set_extmark(buf, ns, 0, 1, 0, {}) local id = bufmeths.set_extmark(buf, ns, 1, 0, {})
eq({{id, 1, 0}}, bufmeths.get_extmarks(buf, ns, 0, -1, {})) eq({{id, 1, 0}}, bufmeths.get_extmarks(buf, ns, 0, -1, {}))
end) end)
it('does not crash with append/delete/undo seqence', function() it('does not crash with append/delete/undo seqence', function()
meths.exec([[ meths.exec([[
let ns = nvim_create_namespace('myplugin') let ns = nvim_create_namespace('myplugin')
call nvim_buf_set_extmark(0, ns, 0, 0, 0, {}) call nvim_buf_set_extmark(0, ns, 0, 0, {})
call append(0, '') call append(0, '')
%delete %delete
undo]],false) undo]],false)