Skip to content

fix(remember): report restore errors once, off the autocmd - #14

Merged
vladdoster merged 14 commits into
vladdoster:masterfrom
mskadu:fix/improve-api-call-fault-tolerance
Sep 26, 2026
Merged

vladdoster merged 14 commits into
vladdoster:masterfrom
mskadu:fix/improve-api-call-fault-tolerance

Conversation

@mskadu

@mskadu mskadu commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Wrap set_cursor_position in pcall to catch errors gracefully
  • Add error notification via vim.notify when Neovim API calls fail
  • Add tests to verify fault tolerance behavior

Changes

This prevents the plugin from crashing when unexpected API errors occur.

@vladdoster

vladdoster commented May 28, 2026 •

Copy link
Copy Markdown
Owner

@mskadu,

Why did you delete all the comments?

@github-actions

github-actions Bot commented May 28, 2026 •

Copy link
Copy Markdown

📊 Test Coverage Report

Overall Coverage: 97.78%

📋 Detailed Coverage Report
==============================================================================
lua/remember.lua
==============================================================================
    --
    -- Author: vladdoster <mvdoster@gmail.com>
    -- Version: 1.5.1
    --
    -- Based on https://github.com/farmergreg/vim-lastplace/
    --
    -- This work is licensed under the terms of the MIT license.
    -- For a copy, see <https://opensource.org/licenses/MIT>.
    --

 18 local g = vim.g
 18 local bo = vim.bo
 18 local fn = vim.fn
 18 local api = vim.api
 18 local cmd = vim.cmd

 18 local M = {}

 18 local config = {
 18   ignore_filetype = { "gitcommit", "gitrebase", "svn", "hgcommit", "dap-repl" },
 18   ignore_buftype = { "quickfix", "nofile", "help" },
 18   open_folds = true,
 18   dont_center = false,
    }

 18 function M.setup(options)
  9   if options["ignore_filetype"] then
  2     config["ignore_filetype"] = options["ignore_filetype"]
      end

  9   if options["ignore_buftype"] then
  2     config["ignore_buftype"] = options["ignore_buftype"]
      end

  9   if options["open_folds"] then
  1     config["open_folds"] = options["open_folds"]
      end

  9   if options["dont_center"] then
  3     config["dont_center"] = options["dont_center"]
      end
    end

 18 function set_cursor_position()
      -- Return if we have a buffer or filetype we want to ignore
 77   for _, k in pairs(config["ignore_buftype"]) do
 59     if bo.buftype == k then
  4       return
        end
      end

 84   for _, k in pairs(config["ignore_filetype"]) do
 72     if bo.filetype == k then
  6       return
        end
      end

      -- Return if the file doesn't exist, like a new and unsaved file
 12   if fn.empty(fn.glob(fn.expand("%"))) ~= 0 then
  1     return
      end

 11   local cursor_position = api.nvim_buf_get_mark(0, '"')
 11   local row = cursor_position[1]
 11   local col = cursor_position[2]

      -- If the saved row is less than the number of rows in the buffer,
      -- then continue
 11   if row > 0 and row <= api.nvim_buf_line_count(0) then
        -- If the last row is visible within this window, like in a very short
        -- file, or user requested us not centering the screen, just set the cursor
        -- position to the saved position
  9     if api.nvim_buf_line_count(0) == fn.line("w$") or config["dont_center"] then
  5       api.nvim_win_set_cursor(0, cursor_position)

          -- If we're in the middle of the file, set the cursor position and center
          -- the screen
  4     elseif api.nvim_buf_line_count(0) - row > ((fn.line("w$") - fn.line("w0")) / 2) - 1 then
  3       api.nvim_win_set_cursor(0, cursor_position)
  3       cmd("norm! zz")

          -- If we're at the end of the screen, set the cursor position and move
          -- the window up by one with C-e. This is to show that we are at the end
          -- of the file. If we did "zz" half the screen would be blank.
        else
  1       api.nvim_win_set_cursor(0, cursor_position)
  1       api.nvim_feedkeys(api.nvim_replace_termcodes("<c-e>", true, false, true), "n", false)
        end
      end

      -- If the row is within a fold, make the row visible and recenter the screen
 11   if api.nvim_eval("foldclosed('.')") ~= -1 and config["open_folds"] then
  2     cmd("norm! zvzz")
      end
    end

 36 api.nvim_create_autocmd({ "BufWinEnter" }, {
      callback = function()
**0     set_cursor_position()
      end,
    })

 18 return M

==============================================================================
Summary
==============================================================================

File             Hits Missed Coverage
-------------------------------------
lua/remember.lua 44   1      97.78%
-------------------------------------
Total            44   1      97.78%


Coverage report generated by luacov

@mskadu

mskadu commented May 29, 2026 •

Copy link
Copy Markdown
Contributor Author

@mskadu,

Why did you delete all the comments?

Oops — the comments were dropped unintentionally during the pcall restructure. The function body re-indentation caused them to be lost.

I've restored them all in the latest commit (but please do let me know if I've missed any). While I was at it, blank-line separators between top-level sections are back too. Clearly I need a better code reformatter (or config for it) :)

Restore original field ordering (ignore_filetype before ignore_buftype)
in config table and setup function. Restore blank line after license
block and trailing newline. These were pure formatting noise unrelated
to the pcall fix.
@mskadu

mskadu commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

Cleaned up the diff — removed unnecessary config field reordering, blank line changes, and trailing newline changes. The diff now only contains the pcall wrapping and error handler. All 18 tests still pass.

Ready for re-review whenever you have a moment. Thanks!

@mskadu

mskadu commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor Author

👋 hola! The diff was cleaned up after your last round of feedback. Should be good for another look whenever you have time. Thanks in advance.

@mskadu

mskadu commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

👋 hola! Checking in again — the pcall fix is still sitting at 18/18 tests passing and the diff is just the fault-tolerance change now. It's been a while since the last review, so if anything else needs tweaking I'm happy to take another pass. No rush, just making sure it hasn't slipped through the cracks.

@vladdoster

Copy link
Copy Markdown
Owner

@mskadu,

I don't think this PR completley fixes the issue unfortunately.

figlet 'good'
make test
nvim --headless --clean \
  --cmd 'set rtp+=.' \
  -c 'silent keepalt topleft vertical 30 new [Scratch-1]' \
  -c 'lua print(vim.fn.winnr("$"))' -c 'messages' -c 'qa!'
figlet 'failure'
make test
nvim --headless --clean \
  --cmd 'set rtp+=.' \
  -c 'lua require("remember")' \
  -c 'silent keepalt topleft vertical 30 new [Scratch-1]' \
  -c 'lua print(vim.fn.winnr("$"))' -c 'messages' -c 'qa!'
                       _
  __ _  ___   ___   __| |
 / _` |/ _ \ / _ \ / _` |
| (_| | (_) | (_) | (_| |
 \__, |\___/ \___/ \__,_|
 |___/
Running tests...
●●●●●●●●●●●●●●●●●●
18 successes / 0 failures / 0 errors / 0 pending : 0.164561 seconds
2
2  __       _ _
 / _| __ _(_) |_   _ _ __ ___
| |_ / _` | | | | | | '__/ _ \
|  _| (_| | | | |_| | | |  __/
|_|  \__,_|_|_|\__,_|_|  \___|

Running tests...
●●●●●●●●●●●●●●●●●●
18 successes / 0 failures / 0 errors / 0 pending : 0.165052 seconds
Error in command line..BufWinEnter Autocommands for "*":
Error setting cursor position: Vim:E944: Reverse range in character class
2
Error setting cursor position: Vim:E944: Reverse range in character class
2%   

The commit titled "add tests to verify errors are handled gracefully" adds only mock_notify and vim.log.levels to the mock table. No test case asserts that an error is caught or that notify fires. The suite mocks fn.glob to return "file.txt", so nothing exercises the failing path.

Current undotree uses nvim_create_buf with an unnamed buffer, so the original repro no longer triggers. Any plugin that names a buffer with [, *, ?, { or still hits the same crash.glob()` also honours 'wildignore', so it can wrongly report an ignored but real file as missing.

I'm working on the fix and tests.

setup() tested each option for truthiness. A user could not turn a default off.

Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
Test the buffer name with filereadable() instead of glob(). A name
like "[Scratch-1]" is a glob pattern and raised E944. Defer the notify
call with vim.schedule and report each distinct error once. Rethrow a
keyboard interrupt so the triggering command still aborts.

Fixes vladdoster#10

Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
Drive a real "nvim --headless" to cover mark restore, the [Scratch-1]
window from issue vladdoster#10, and deferred error reporting. The spec is
pending when nvim is not on PATH.

Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
Signed-off-by: Vladislav Doster <mvdoster@gmail.com>
@vladdoster

Copy link
Copy Markdown
Owner

Pushed four commits on top of this branch (maintainer edits are enabled):

  • fix(setup): accept false for open_folds and dont_center
  • fix(remember): report restore errors once, off the autocmd (fixes Plugin conflicts with 'jiaoshijie/undotree' #10: filereadable() instead of glob(), deferred and deduplicated vim.notify, keyboard interrupt rethrown)
  • test: add headless nvim integration spec
  • ci(workflows): install neovim for headless spec

busted tests passes 24 specs locally.

@vladdoster vladdoster changed the title fix: improve set_cursor_position with pcall for fault tolerance fix(remember): report restore errors once, off the autocmd Sep 26, 2026
@vladdoster
vladdoster merged commit ef04b4b into vladdoster:master Sep 26, 2026
2 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.5.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants