From 9e7426718b678e299f3fd03ef94f81b1e2d01ab0 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 4 Apr 2023 23:59:39 +0100 Subject: [PATCH] feat(vim.diff): allow passing an integer for linematch --- runtime/doc/lua.txt | 7 ++++--- src/nvim/lua/xdiff.c | 16 ++++++++++------ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index ba83b18d07..6084a625ba 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -685,9 +685,10 @@ vim.diff({a}, {b}, {opts}) *vim.diff()* • "unified": (default) String in unified format. • "indices": Array of hunk locations. Note: This option is ignored if `on_hunk` is used. - • `linematch` (boolean): Run linematch on the resulting hunks - from xdiff. Requires `result_type = indices`, ignored - otherwise. + • `linematch` (boolean|integer): Run linematch on the resulting hunks + from xdiff. When integer, only hunks upto this size in + lines are run through linematch. Requires `result_type = indices`, + ignored otherwise. • `algorithm` (string): Diff algorithm to use. Values: • "myers" the default algorithm diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 9a7ae5c146..e0bbdb8942 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -32,7 +32,7 @@ typedef struct { Error *err; mmfile_t *ma; mmfile_t *mb; - bool linematch; + int64_t linematch; bool iwhite; } hunkpriv_t; @@ -128,7 +128,7 @@ static int hunk_locations_cb(long start_a, long count_a, long start_b, long coun { hunkpriv_t *priv = (hunkpriv_t *)cb_data; lua_State *lstate = priv->lstate; - if (priv->linematch) { + if (priv->linematch > 0 && count_a + count_b <= priv->linematch) { get_linematch_results(lstate, priv->ma, priv->mb, start_a, count_a, start_b, count_b, priv->iwhite); } else { @@ -208,7 +208,7 @@ static bool check_xdiff_opt(ObjectType actType, ObjectType expType, const char * } static NluaXdiffMode process_xdl_diff_opts(lua_State *lstate, xdemitconf_t *cfg, xpparam_t *params, - bool *linematch, Error *err) + int64_t *linematch, Error *err) { const DictionaryOf(LuaRef) opts = nlua_pop_Dictionary(lstate, true, err); @@ -265,8 +265,12 @@ static NluaXdiffMode process_xdl_diff_opts(lua_State *lstate, xdemitconf_t *cfg, } cfg->interhunkctxlen = (long)v->data.integer; } else if (strequal("linematch", k.data)) { - *linematch = api_object_to_bool(*v, "linematch", false, err); - if (ERROR_SET(err)) { + if (v->type == kObjectTypeBoolean) { + *linematch = v->data.boolean ? INT64_MAX : 0; + } else if (v->type == kObjectTypeInteger) { + *linematch = v->data.integer; + } else { + api_set_error(err, kErrorTypeValidation, "linematch must be a boolean or integer"); goto exit_1; } } else { @@ -330,7 +334,7 @@ int nlua_xdl_diff(lua_State *lstate) xdemitconf_t cfg; xpparam_t params; xdemitcb_t ecb; - bool linematch = false; + int64_t linematch = 0; CLEAR_FIELD(cfg); CLEAR_FIELD(params);