aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua/xdiff.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-11-29 22:39:54 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-11-29 22:39:54 +0000
commit21cb7d04c387e4198ca8098a884c78b56ffcf4c2 (patch)
tree84fe5690df1551f0bb2bdfe1a13aacd29ebc1de7 /src/nvim/lua/xdiff.c
parentd9c904f85a23a496df4eb6be42aa43f007b22d50 (diff)
parent4a8bf24ac690004aedf5540fa440e788459e5e34 (diff)
downloadrneovim-colorcolchar.tar.gz
rneovim-colorcolchar.tar.bz2
rneovim-colorcolchar.zip
Merge remote-tracking branch 'upstream/master' into colorcolcharcolorcolchar
Diffstat (limited to 'src/nvim/lua/xdiff.c')
-rw-r--r--src/nvim/lua/xdiff.c76
1 files changed, 39 insertions, 37 deletions
diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c
index 857b159af5..16c3aa5e11 100644
--- a/src/nvim/lua/xdiff.c
+++ b/src/nvim/lua/xdiff.c
@@ -1,9 +1,7 @@
-// This is an open source non-commercial project. Dear PVS-Studio, please check
-// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
-
#include <lauxlib.h>
#include <lua.h>
#include <stdbool.h>
+#include <stdint.h>
#include <string.h>
#include "luaconf.h"
@@ -13,9 +11,9 @@
#include "nvim/lua/converter.h"
#include "nvim/lua/executor.h"
#include "nvim/lua/xdiff.h"
-#include "nvim/macros.h"
+#include "nvim/macros_defs.h"
#include "nvim/memory.h"
-#include "nvim/vim.h"
+#include "nvim/pos_defs.h"
#include "xdiff/xdiff.h"
#define COMPARED_BUFFER0 (1 << 0)
@@ -32,7 +30,7 @@ typedef struct {
Error *err;
mmfile_t *ma;
mmfile_t *mb;
- bool linematch;
+ int64_t linematch;
bool iwhite;
} hunkpriv_t;
@@ -63,25 +61,25 @@ static void lua_pushhunk(lua_State *lstate, long start_a, long count_a, long sta
lua_rawseti(lstate, -2, (signed)lua_objlen(lstate, -2) + 1);
}
-static void get_linematch_results(lua_State *lstate, mmfile_t *ma, mmfile_t *mb, long start_a,
- long count_a, long start_b, long count_b, bool iwhite)
+static void get_linematch_results(lua_State *lstate, mmfile_t *ma, mmfile_t *mb, int start_a,
+ int count_a, int start_b, int count_b, bool iwhite)
{
// get the pointer to char of the start of the diff to pass it to linematch algorithm
const char *diff_begin[2] = { ma->ptr, mb->ptr };
- int diff_length[2] = { (int)count_a, (int)count_b };
+ int diff_length[2] = { count_a, count_b };
- fastforward_buf_to_lnum(&diff_begin[0], start_a + 1);
- fastforward_buf_to_lnum(&diff_begin[1], start_b + 1);
+ fastforward_buf_to_lnum(&diff_begin[0], (linenr_T)start_a + 1);
+ fastforward_buf_to_lnum(&diff_begin[1], (linenr_T)start_b + 1);
int *decisions = NULL;
size_t decisions_length = linematch_nbuffers(diff_begin, diff_length, 2, &decisions, iwhite);
- long lnuma = start_a, lnumb = start_b;
+ int lnuma = start_a, lnumb = start_b;
- long hunkstarta = lnuma;
- long hunkstartb = lnumb;
- long hunkcounta = 0;
- long hunkcountb = 0;
+ int hunkstarta = lnuma;
+ int hunkstartb = lnumb;
+ int hunkcounta = 0;
+ int hunkcountb = 0;
for (size_t i = 0; i < decisions_length; i++) {
if (i && (decisions[i - 1] != decisions[i])) {
lua_pushhunk(lstate, hunkstarta, hunkcounta, hunkstartb, hunkcountb);
@@ -109,8 +107,8 @@ static int write_string(void *priv, mmbuffer_t *mb, int nbuf)
{
luaL_Buffer *buf = (luaL_Buffer *)priv;
for (int i = 0; i < nbuf; i++) {
- const long size = mb[i].size;
- for (long total = 0; total < size; total += LUAL_BUFFERSIZE) {
+ const int size = mb[i].size;
+ for (int total = 0; total < size; total += LUAL_BUFFERSIZE) {
const int tocopy = MIN((int)(size - total), LUAL_BUFFERSIZE);
char *p = luaL_prepbuffer(buf);
if (!p) {
@@ -124,11 +122,11 @@ static int write_string(void *priv, mmbuffer_t *mb, int nbuf)
}
// hunk_func callback used when opts.hunk_lines = true
-static int hunk_locations_cb(long start_a, long count_a, long start_b, long count_b, void *cb_data)
+static int hunk_locations_cb(int start_a, int count_a, int start_b, int count_b, void *cb_data)
{
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 {
@@ -139,7 +137,7 @@ static int hunk_locations_cb(long start_a, long count_a, long start_b, long coun
}
// hunk_func callback used when opts.on_hunk is given
-static int call_on_hunk_cb(long start_a, long count_a, long start_b, long count_b, void *cb_data)
+static int call_on_hunk_cb(int start_a, int count_a, int start_b, int count_b, void *cb_data)
{
// Mimic extra offsets done by xdiff, see:
// src/xdiff/xemit.c:284
@@ -193,11 +191,11 @@ static bool check_xdiff_opt(ObjectType actType, ObjectType expType, const char *
{
if (actType != expType) {
const char *type_str =
- expType == kObjectTypeString ? "string" :
- expType == kObjectTypeInteger ? "integer" :
- expType == kObjectTypeBoolean ? "boolean" :
- expType == kObjectTypeLuaRef ? "function" :
- "NA";
+ expType == kObjectTypeString
+ ? "string" : (expType == kObjectTypeInteger
+ ? "integer" : (expType == kObjectTypeBoolean
+ ? "boolean" : (expType == kObjectTypeLuaRef
+ ? "function" : "NA")));
api_set_error(err, kErrorTypeValidation, "%s is not a %s", name,
type_str);
@@ -208,7 +206,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);
@@ -257,16 +255,20 @@ static NluaXdiffMode process_xdl_diff_opts(lua_State *lstate, xdemitconf_t *cfg,
if (check_xdiff_opt(v->type, kObjectTypeInteger, "ctxlen", err)) {
goto exit_1;
}
- cfg->ctxlen = v->data.integer;
+ cfg->ctxlen = (long)v->data.integer;
} else if (strequal("interhunkctxlen", k.data)) {
if (check_xdiff_opt(v->type, kObjectTypeInteger, "interhunkctxlen",
err)) {
goto exit_1;
}
- cfg->interhunkctxlen = v->data.integer;
+ 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 +332,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);
@@ -362,18 +364,18 @@ int nlua_xdl_diff(lua_State *lstate)
cfg.hunk_func = call_on_hunk_cb;
priv = (hunkpriv_t) {
.lstate = lstate,
- .err = &err,
+ .err = &err,
};
ecb.priv = &priv;
break;
case kNluaXdiffModeLocations:
cfg.hunk_func = hunk_locations_cb;
priv = (hunkpriv_t) {
- .lstate = lstate,
- .ma = &ma,
- .mb = &mb,
+ .lstate = lstate,
+ .ma = &ma,
+ .mb = &mb,
.linematch = linematch,
- .iwhite = (params.flags & XDF_IGNORE_WHITESPACE) > 0
+ .iwhite = (params.flags & XDF_IGNORE_WHITESPACE) > 0
};
ecb.priv = &priv;
lua_createtable(lstate, 0, 0);