751a2dd

Branches > main > 751a2dd > files / nvim / lua / opts.lua

More parinfer rewriting

Grahp, 2026-06-20

Commit: 751a2dd59acc686443c45f0be38d43da32402bf2
Parent: ee3f3d0

1 changed file:

nvim/lua/parinfer/plugin.lua modified (view file)

@@ -20,6 +20,7 @@   return t[k] end}) +-- TODO I don't really get the point of this local function normalize_bool(val)   if type(val) == "boolean" then     return val@@ -53,32 +54,34 @@ end  -- Does kidna flicker+-- Also bad still local function update_buffer(bufnr, old_contents, new_contents)   api.nvim_command("silent! undojoin")   state[bufnr]["locked"] = true   do     local new_lines = vim.split(new_contents, "\n")     local hunks = diff(old_contents, new_contents, {result_type = "indices"})-    for _, _33_ in ipairs(hunks) do-      local start_a = _33_[1]-      local count_a = _33_[2]-      local start_b = _33_[3]-      local count_b = _33_[4]+    for _, hunk in ipairs(hunks) do+      local start_a = hunk[1]+      local count_a = hunk[2]+      local start_b = hunk[3]+      local count_b = hunk[4]       local lines       do-        local tbl_21_ = {}-        local i_22_ = 0-        for i = start_b, (start_b + count_b) do-          local val_23_ = new_lines[i]-          if (nil ~= val_23_) then-            i_22_ = (i_22_ + 1)-            tbl_21_[i_22_] = val_23_+        local t = {}+        local i = 0+        for num = start_b, start_b + count_b do+          local new_line = new_lines[num]+          if new_line ~= nil then+            i = i + 1+            t[i] = new_line           else           end         end-        lines = tbl_21_+        lines = t       end-      api.nvim_buf_set_lines(bufnr, (start_a - 1), (start_a + count_a), false, lines)++      api.nvim_buf_set_lines(bufnr, start_a - 1, start_a + count_a, false, lines)     end   end   local function _35_()@@ -99,14 +102,20 @@ end  local function is_undo_leaf()-  local _let_37_ = vim.fn.undotree()-  local seq_cur = _let_37_["seq_cur"]-  local seq_last = _let_37_["seq_last"]+  local undotree = vim.fn.undotree()+  local seq_cur = undotree["seq_cur"]+  local seq_last = undotree["seq_last"]   return (seq_cur == seq_last) end  local function should_run(bufnr)-  return (normalize_bool(get_option("parinfer_enabled")) and not state[bufnr].locked and not vim.o.paste and not vim.bo.readonly and vim.bo.modifiable and (vim.b[bufnr].changedtick ~= state[bufnr].changedtick) and is_undo_leaf())+  return (normalize_bool(get_option("parinfer_enabled"))+    and not state[bufnr].locked+    and not vim.o.paste+    and not vim.bo.readonly+    and vim.bo.modifiable+    and (vim.b[bufnr].changedtick ~= state[bufnr].changedtick)+    and is_undo_leaf()) end  local elapsed_times = setmetatable({}, {__index = function(t, k)@@ -118,9 +127,9 @@   if should_run(bufnr) then     local start = vim.loop.hrtime()     local winnr = api.nvim_get_current_win()-    local _let_39_ = api.nvim_win_get_cursor(winnr)-    local lnum = _let_39_[1]-    local col = _let_39_[2]+    local cursor = api.nvim_win_get_cursor(winnr)+    local lnum = cursor[1]+    local col = cursor[2]     local contents = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)     local text = table.concat(contents, "\n")     local response = invoke_parinfer(bufnr, text, lnum, col)@@ -135,12 +144,9 @@         return api.nvim_win_set_cursor(winnr, {new_lnum, (new_col - 1)})       end     )-    else     end     highlight_error(bufnr, response.error)     return table.insert(elapsed_times[bufnr], (vim.loop.hrtime() - start))-  else-    return nil   end end @@ -165,22 +171,22 @@   end    if (row_offset ~= 0) then-    local function _45_()+    local function still_dont_know()       if ((0 < col_offset) and lines[(start_row0 + row_offset)]) then         return string.sub(lines[(start_row0 + row_offset)], 1, col_offset)       else         return ""       end     end-    table.insert(out, _45_())+    table.insert(out, still_dont_know())   end   return out end +-- TODO bad function local function on_bytes(_, bufnr, _0, start_row, start_col, _1, old_end_row, old_end_col, _2, new_end_row, new_end_col)   if (normalize_bool(get_option("parinfer_enabled")) and not state[bufnr].locked) then-    local _let_47_ = state[bufnr]-    local prev_contents = _let_47_["prev-contents"]+    local prev_contents = state[bufnr]["prev-contents"]     local contents = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)     if (start_row < #prev_contents) then       local old_text = slice(prev_contents, start_row, start_col, old_end_row, old_end_col)@@ -220,52 +226,26 @@       local lnum = cursor[1]       local col = cursor[2]       state[bufnr]["prev-cursor"] = {lnum, (col + 1)}-    end-  )+    end) end ---- Setup+local function should_enable()+  return not vim.wo.previewwindow and vim.bo.buftype ~= "prompt"+end +--- Sets up parinfer in the current buffer local function setup()-  local bo = vim.bo-  local wo = vim.wo+  if should_enable() then+    local bufnr = vim.api.nvim_get_current_buf()+    enter_buffer() -  if wo.previewwindow or bo.buftype == "prompt" then-    return+    vim.api.nvim_exec_autocmds("User", {pattern = "Parinfer"})++    local ag = vim.api.nvim_create_augroup("parinfer", {clear = true})++    vim.api.nvim_create_autocmd({"TextChanged", "TextChangedI", "TextChangedP"}, {buffer = bufnr, group = ag, callback = function() process_buffer(bufnr) end})+    vim.api.nvim_create_autocmd({"CursorMoved", "CursorMovedI"}, {buffer = bufnr, group = ag, callback = function() cursor_moved(bufnr) end})   end--  local bufnr = vim.api.nvim_get_current_buf()--  vim.b.parinfer_enabled = vim.g.parinfer_enabled ~= false--  enter_buffer()--  vim.api.nvim_exec_autocmds("User", {-    pattern = "Parinfer",-  })--  local aug = vim.api.nvim_create_augroup("parinfer", { clear = true })--  vim.api.nvim_create_autocmd(-    { "TextChanged", "TextChangedI", "TextChangedP" },-    {-      buffer = bufnr,-      group = aug,-      callback = function()-        process_buffer(bufnr)-      end,-    }-  )--  vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" },-    {-      buffer = bufnr,-      group = aug,-      callback = function()-        cursor_moved(bufnr)-      end,-    }-  ) end  return {setup = setup}