robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#include <lauxlib.h> 2#include <limits.h> 3#include <lua.h> 4#include <stdbool.h> 5#include <stdint.h> 6#include <string.h> 7 8#include "luaconf.h" 9#include "nvim/api/keysets_defs.h" 10#include "nvim/api/private/defs.h" 11#include "nvim/api/private/dispatch.h" 12#include "nvim/api/private/helpers.h" 13#include "nvim/linematch.h" 14#include "nvim/lua/converter.h" 15#include "nvim/lua/executor.h" 16#include "nvim/lua/xdiff.h" 17#include "nvim/macros_defs.h" 18#include "nvim/memory.h" 19#include "nvim/pos_defs.h" 20#include "xdiff/xdiff.h" 21 22#define COMPARED_BUFFER0 (1 << 0) 23#define COMPARED_BUFFER1 (1 << 1) 24 25typedef enum { 26kNluaXdiffModeUnified = 0 , 27kNluaXdiffModeOnHunkCB , 28kNluaXdiffModeLocations , 29}NluaXdiffMode ; 30 31typedef struct { 32lua_State * lstate ; 33Error * err ; 34mmfile_t * ma ; 35mmfile_t * mb ; 36int64_t linematch ; 37bool iwhite ; 38}hunkpriv_t ; 39 40#include "lua/xdiff.c.generated.h" 41 42static void lua_pushhunk (lua_State * lstate ,long start_a ,long count_a ,long start_b ,long count_b ) 43{ 44// Mimic extra offsets done by xdiff, see: 45// src/xdiff/xemit.c:284 46// src/xdiff/xutils.c:(356,368) 47if (count_a > 0 ) { 48start_a += 1 ; 49 } 50if (count_b > 0 ) { 51start_b += 1 ; 52 } 53lua_createtable (lstate ,0 ,0 ); 54lua_pushinteger (lstate ,start_a ); 55lua_rawseti (lstate ,-2 ,1 ); 56lua_pushinteger (lstate ,count_a ); 57lua_rawseti (lstate ,-2 ,2 ); 58lua_pushinteger (lstate ,start_b ); 59lua_rawseti (lstate ,-2 ,3 ); 60lua_pushinteger (lstate ,count_b ); 61lua_rawseti (lstate ,-2 ,4 ); 62lua_rawseti (lstate ,-2 , (signed )lua_objlen (lstate ,-2 )+ 1 ); 63} 64 65static void get_linematch_results (lua_State * lstate ,mmfile_t * ma ,mmfile_t * mb ,int start_a , 66int count_a ,int start_b ,int count_b ,bool iwhite ) 67{ 68// get the pointer to char of the start of the diff to pass it to linematch algorithm 69mmfile_t ma0 = fastforward_buf_to_lnum (* ma , (linenr_T )start_a + 1 ); 70mmfile_t mb0 = fastforward_buf_to_lnum (* mb , (linenr_T )start_b + 1 ); 71 72const mmfile_t * diff_begin [2 ]= {& ma0 ,& mb0 }; 73int diff_length [2 ]= {count_a ,count_b }; 74 75int * decisions = NULL ; 76size_t decisions_length = linematch_nbuffers (diff_begin ,diff_length ,2 ,& decisions ,iwhite ); 77 78int lnuma = start_a ; 79int lnumb = start_b ; 80 81int hunkstarta = lnuma ; 82int hunkstartb = lnumb ; 83int hunkcounta = 0 ; 84int hunkcountb = 0 ; 85for (size_t i = 0 ;i < decisions_length ;i ++ ) { 86if (i && (decisions [i - 1 ]!= decisions [i ])) { 87lua_pushhunk (lstate ,hunkstarta ,hunkcounta ,hunkstartb ,hunkcountb ); 88 89hunkstarta = lnuma ; 90hunkstartb = lnumb ; 91hunkcounta = 0 ; 92hunkcountb = 0 ; 93// create a new hunk 94 } 95if (decisions [i ]& COMPARED_BUFFER0 ) { 96lnuma ++ ; 97hunkcounta ++ ; 98 } 99if (decisions [i ]& COMPARED_BUFFER1 ) { 100lnumb ++ ; 101hunkcountb ++ ; 102 } 103 } 104lua_pushhunk (lstate ,hunkstarta ,hunkcounta ,hunkstartb ,hunkcountb ); 105xfree (decisions ); 106} 107 108static int write_string (void * priv ,mmbuffer_t * mb ,int nbuf ) 109{ 110luaL_Buffer * buf = (luaL_Buffer * )priv ; 111for (int i = 0 ;i < nbuf ;i ++ ) { 112const int size = mb [i ].size ; 113for (int total = 0 ;total < size ;total += LUAL_BUFFERSIZE ) { 114const int tocopy = MIN ((int )(size - total ),LUAL_BUFFERSIZE ); 115char * p = luaL_prepbuffer (buf ); 116if (!p ) { 117return -1 ; 118 } 119memcpy (p ,mb [i ].ptr + total , (unsigned )tocopy ); 120luaL_addsize (buf , (unsigned )tocopy ); 121 } 122 } 123return 0 ; 124} 125 126// hunk_func callback used when opts.hunk_lines = true 127static int hunk_locations_cb (int start_a ,int count_a ,int start_b ,int count_b ,void * cb_data ) 128{ 129hunkpriv_t * priv = (hunkpriv_t * )cb_data ; 130lua_State * lstate = priv -> lstate ; 131if (priv -> linematch > 0 && count_a + count_b <=priv -> linematch ) { 132get_linematch_results (lstate ,priv -> ma ,priv -> mb ,start_a ,count_a ,start_b ,count_b , 133priv -> iwhite ); 134 }else { 135lua_pushhunk (lstate ,start_a ,count_a ,start_b ,count_b ); 136 } 137 138return 0 ; 139} 140 141// hunk_func callback used when opts.on_hunk is given 142static int call_on_hunk_cb (int start_a ,int count_a ,int start_b ,int count_b ,void * cb_data ) 143{ 144// Mimic extra offsets done by xdiff, see: 145// src/xdiff/xemit.c:284 146// src/xdiff/xutils.c:(356,368) 147if (count_a > 0 ) { 148start_a += 1 ; 149 } 150if (count_b > 0 ) { 151start_b += 1 ; 152 } 153 154hunkpriv_t * priv = (hunkpriv_t * )cb_data ; 155lua_State * lstate = priv -> lstate ; 156Error * err = priv -> err ; 157const int fidx = lua_gettop (lstate ); 158lua_pushvalue (lstate ,fidx ); 159lua_pushinteger (lstate ,start_a ); 160lua_pushinteger (lstate ,count_a ); 161lua_pushinteger (lstate ,start_b ); 162lua_pushinteger (lstate ,count_b ); 163 164if (lua_pcall (lstate ,4 ,1 ,0 )!= 0 ) { 165api_set_error (err ,kErrorTypeException ,"on_hunk: %s" ,lua_tostring (lstate ,-1 )); 166return -1 ; 167 } 168 169int r = 0 ; 170if (lua_isnumber (lstate ,-1 )) { 171r = (int )lua_tonumber (lstate ,-1 ); 172 } 173 174lua_pop (lstate ,1 ); 175lua_settop (lstate ,fidx ); 176return r ; 177} 178 179static mmfile_t get_string_arg (lua_State * lstate ,int idx ) 180{ 181if (lua_type (lstate ,idx )!= LUA_TSTRING ) { 182luaL_argerror (lstate ,idx ,"expected string" ); 183 } 184mmfile_t mf ; 185size_t size ; 186mf .ptr = (char * )lua_tolstring (lstate ,idx ,& size ); 187if (size > INT_MAX ) { 188luaL_argerror (lstate ,idx ,"string too long" ); 189 } 190mf .size = (int )size ; 191return mf ; 192} 193 194static NluaXdiffMode process_xdl_diff_opts (lua_State * lstate ,xdemitconf_t * cfg ,xpparam_t * params , 195int64_t * linematch ,Error * err ) 196{ 197Dict (xdl_diff )opts = KEYDICT_INIT ; 198char * err_param = NULL ; 199nlua_pop_keydict (lstate ,& opts ,KeyDict_xdl_diff_get_field ,& err_param ,NULL ,err ); 200 201NluaXdiffMode mode = kNluaXdiffModeUnified ; 202 203bool had_result_type_indices = false; 204 205if (HAS_KEY (& opts ,xdl_diff ,result_type )) { 206if (strequal ("unified" ,opts .result_type .data )) { 207// the default 208 }else if (strequal ("indices" ,opts .result_type .data )) { 209had_result_type_indices = true; 210 }else { 211api_set_error (err ,kErrorTypeValidation ,"not a valid result_type" ); 212 gotoexit_1 ; 213 } 214 } 215 216if (HAS_KEY (& opts ,xdl_diff ,algorithm )) { 217if (strequal ("myers" ,opts .algorithm .data )) { 218// default 219 }else if (strequal ("minimal" ,opts .algorithm .data )) { 220params -> flags |=XDF_NEED_MINIMAL ; 221 }else if (strequal ("patience" ,opts .algorithm .data )) { 222params -> flags |=XDF_PATIENCE_DIFF ; 223 }else if (strequal ("histogram" ,opts .algorithm .data )) { 224params -> flags |=XDF_HISTOGRAM_DIFF ; 225 }else { 226api_set_error (err ,kErrorTypeValidation ,"not a valid algorithm" ); 227 gotoexit_1 ; 228 } 229 } 230 231if (HAS_KEY (& opts ,xdl_diff ,ctxlen )) { 232cfg -> ctxlen = (long )opts .ctxlen ; 233 } 234 235if (HAS_KEY (& opts ,xdl_diff ,interhunkctxlen )) { 236cfg -> interhunkctxlen = (long )opts .interhunkctxlen ; 237 } 238 239if (HAS_KEY (& opts ,xdl_diff ,linematch )) { 240if (opts .linematch .type == kObjectTypeBoolean ) { 241* linematch = opts .linematch .data .boolean ?INT64_MAX :0 ; 242 }else if (opts .linematch .type == kObjectTypeInteger ) { 243* linematch = opts .linematch .data .integer ; 244 }else { 245api_set_error (err ,kErrorTypeValidation ,"linematch must be a boolean or integer" ); 246 gotoexit_1 ; 247 } 248 } 249 250params -> flags |=opts .ignore_whitespace ?XDF_IGNORE_WHITESPACE :0 ; 251params -> flags |=opts .ignore_whitespace_change ?XDF_IGNORE_WHITESPACE_CHANGE :0 ; 252params -> flags |=opts .ignore_whitespace_change_at_eol ?XDF_IGNORE_WHITESPACE_AT_EOL :0 ; 253params -> flags |=opts .ignore_cr_at_eol ?XDF_IGNORE_CR_AT_EOL :0 ; 254params -> flags |=opts .ignore_blank_lines ?XDF_IGNORE_BLANK_LINES :0 ; 255params -> flags |=opts .indent_heuristic ?XDF_INDENT_HEURISTIC :0 ; 256 257if (HAS_KEY (& opts ,xdl_diff ,on_hunk )) { 258mode = kNluaXdiffModeOnHunkCB ; 259nlua_pushref (lstate ,opts .on_hunk ); 260if (lua_type (lstate ,-1 )!= LUA_TFUNCTION ) { 261api_set_error (err ,kErrorTypeValidation ,"on_hunk is not a function" ); 262 } 263 }else if (had_result_type_indices ) { 264mode = kNluaXdiffModeLocations ; 265 } 266 267exit_1 : 268api_free_string (opts .result_type ); 269api_free_string (opts .algorithm ); 270api_free_luaref (opts .on_hunk ); 271return mode ; 272} 273 274int nlua_xdl_diff (lua_State * lstate ) 275{ 276if (lua_gettop (lstate )< 2 ) { 277return luaL_error (lstate ,"Expected at least 2 arguments" ); 278 } 279mmfile_t ma = get_string_arg (lstate ,1 ); 280mmfile_t mb = get_string_arg (lstate ,2 ); 281 282Error err = ERROR_INIT ; 283 284xdemitconf_t cfg ; 285xpparam_t params ; 286xdemitcb_t ecb ; 287int64_t linematch = 0 ; 288 289CLEAR_FIELD (cfg ); 290CLEAR_FIELD (params ); 291CLEAR_FIELD (ecb ); 292 293NluaXdiffMode mode = kNluaXdiffModeUnified ; 294 295if (lua_gettop (lstate )== 3 ) { 296if (lua_type (lstate ,3 )!= LUA_TTABLE ) { 297return luaL_argerror (lstate ,3 ,"expected table" ); 298 } 299 300mode = process_xdl_diff_opts (lstate ,& cfg ,& params ,& linematch ,& err ); 301 302if (ERROR_SET (& err )) { 303 gotoexit_0 ; 304 } 305 } 306 307luaL_Buffer buf ; 308hunkpriv_t priv ; 309switch (mode ) { 310case kNluaXdiffModeUnified : 311luaL_buffinit (lstate ,& buf ); 312ecb .priv = & buf ; 313ecb .out_line = write_string ; 314break ; 315case kNluaXdiffModeOnHunkCB : 316cfg .hunk_func = call_on_hunk_cb ; 317priv = (hunkpriv_t ) { 318 .lstate = lstate , 319 .err = & err , 320 }; 321ecb .priv = & priv ; 322break ; 323case kNluaXdiffModeLocations : 324cfg .hunk_func = hunk_locations_cb ; 325priv = (hunkpriv_t ) { 326 .lstate = lstate , 327 .ma = & ma , 328 .mb = & mb , 329 .linematch = linematch , 330 .iwhite = (params .flags & XDF_IGNORE_WHITESPACE )> 0 331 }; 332ecb .priv = & priv ; 333lua_createtable (lstate ,0 ,0 ); 334break ; 335 } 336 337if (xdl_diff (& ma ,& mb ,& params ,& cfg ,& ecb )== -1 ) { 338if (!ERROR_SET (& err )) { 339api_set_error (& err ,kErrorTypeException ,"diff operation failed" ); 340 } 341 } 342 343exit_0 : 344if (ERROR_SET (& err )) { 345luaL_where (lstate ,1 ); 346lua_pushstring (lstate ,err .msg ); 347api_clear_error (& err ); 348lua_concat (lstate ,2 ); 349return lua_error (lstate ); 350 }else if (mode == kNluaXdiffModeUnified ) { 351luaL_pushresult (& buf ); 352return 1 ; 353 }else if (mode == kNluaXdiffModeLocations ) { 354return 1 ; 355 } 356return 0 ; 357}