Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(mini.misc) Add support for restoring the cursor #198

Merged
merged 3 commits into from
Jan 24, 2023

Conversation

cryptomilk
Copy link
Contributor

@cryptomilk cryptomilk commented Jan 15, 2023

What it does:

  • When reopening a file, this will make sure the cursor is placed back to the position where you left before.
  • This implements :help restore-cursor in a nicer way.
  • Creates autocommand for |BufReadPre| and |FileType| event with |MiniMisc.restore_cursor|

This is still work in progress. However I wanted to ask if you would be fine integrating this.

@cryptomilk cryptomilk force-pushed the mini.cursorpos branch 2 times, most recently from 5a99ca9 to c0fbb87 Compare January 15, 2023 12:33
@echasnovski
Copy link
Owner

echasnovski commented Jan 15, 2023

Hi!

First of all, thanks for taking time to write this. It would be even better if you created an issue to discuss it first (as said in CONTRIBUTING.md).

Second, could you clarify the purpose and scope of this proposed module? Is it a slightly polished version of the autocommand from :h last-position-jump (like open fold, etc.)?

Third, it is a strong opinion of mine that no ignore_buftype or ignore_filetype will be used as options of 'mini.nvim' modules. It gets complicated to track and not easy to make future proof.

Edit: for reference, this was already discussed earlier

@cryptomilk
Copy link
Contributor Author

I thought before opening an issue I could also create a MR. I wanted to play with mini.nvim anyway :-) So this was an exercise learning the code ...

About the plugin:

By default if you open a file with neovim, the cursor is positioned always at line 1 column 1 (1,1).

If you have the plugin loaded and lets assume you jump to line 60 and move the cursor to column 5 (60, 5). Now you close nvim and open the file again with nvim. With the plugin loaded the cursor will be positioned back on line 60 and column 5 (60, 5). It is last position were you left the file.

The information about the position where you left a file is stored in shada (see :help shada)!

The plugin is an enhanced version of the :h last-position-jump. I find this extremely useful. Try it.

@echasnovski
Copy link
Owner

I thought before opening an issue I could also create a MR. I wanted to play with mini.nvim anyway :-) So this was an exercise learning the code ...

Sure, I can understand this motivation :) Would be nice following the guidelines, though.

By default if you open a file with neovim, the cursor is positioned always at line 1 column 1 (1,1).

If you have the plugin loaded and lets assume you jump to line 60 and move the cursor to column 5 (60, 5). Now you close nvim and open the file again with nvim. With the plugin loaded the cursor will be positioned back on line 60 and column 5 (60, 5). It is last position were you left the file.

The information about the position where you left a file is stored in shada (see :help shada)!

OK, I see. Having in mind comments about no ignore_* options in modules and the common practice of trying to always open just enough folds when moving cursor, it seems too small of a functionality to be a whole module.

I am not opposed to having this as a part of 'mini.misc', though. Along the lines of MiniMisc.setup_auto_root() and MiniMisc.find_root(). Say setup_goto_last_cursor() and goto_last_cursor(). Might be tricky to write tests, though.

The plugin is an enhanced version of the :h last-position-jump. I find this extremely useful. Try it.

I now understand why I've never found this functionality useful... I am using sessions 99% of the time, and they restore this information automatically.

@cryptomilk
Copy link
Contributor Author

You really do not want the 'cursor goto' for git commit or the quickfix window. You want to disable it for some buffer/file types. If you do not want a maintain a list, the variables could be empty. However a user might want to be able to set those. It is really annoying if you do a git commit and the cursor is in the middle of the comment section. Is it ok to have the options cursor_goto_ignore_filetype but an emtpy table as the default? Then the user could maintain the list ...

@echasnovski
Copy link
Owner

You really do not want the 'cursor goto' for git commit or the quickfix window. You want to disable it for some buffer/file types. If you do not want a maintain a list, the variables could be empty. However a user might want to be able to set those. It is really annoying if you do a git commit and the cursor is in the middle of the comment section. Is it ok to have the options cursor_goto_ignore_filetype but an emtpy table as the default? Then the user could maintain the list ...

If we are talking about a module - no, any filetype/buftype related disabling is done only through vim.b.minixxx_disable variables. User can then create autocommands to automatically set them up (possibly using a provided common recipes). It is also easier to implement.

If we are talking about 'mini.misc' setup_* type of function, then I think these lists can be an optional arguments.

@cryptomilk cryptomilk changed the title Draft: (mini.cursorpos) NEW MODULE: initial commit. Draft: (mini.misc) Add support to goto last cursor position Jan 15, 2023
@cryptomilk
Copy link
Contributor Author

I've added it to MiniMisc.setup() but I can move it to setup_goto_last_cursor()

lua/mini/misc.lua Outdated Show resolved Hide resolved
lua/mini/misc.lua Outdated Show resolved Hide resolved
lua/mini/misc.lua Outdated Show resolved Hide resolved
lua/mini/misc.lua Outdated Show resolved Hide resolved
lua/mini/misc.lua Outdated Show resolved Hide resolved
lua/mini/misc.lua Outdated Show resolved Hide resolved
lua/mini/misc.lua Outdated Show resolved Hide resolved
@cryptomilk cryptomilk force-pushed the mini.cursorpos branch 6 times, most recently from c8fc799 to a85c3bf Compare January 17, 2023 09:37
@cryptomilk cryptomilk changed the title Draft: (mini.misc) Add support to goto last cursor position (mini.misc) Add support to goto last cursor position Jan 17, 2023
@cryptomilk
Copy link
Contributor Author

I've fixed the last issues, added more code comments and improved documentation.

@cryptomilk cryptomilk changed the title (mini.misc) Add support to goto last cursor position (mini.misc) Add support for restoring the cursor Jan 17, 2023
@cryptomilk cryptomilk force-pushed the mini.cursorpos branch 2 times, most recently from a974fc7 to 5a1693f Compare January 17, 2023 09:46
lua/mini/misc.lua Show resolved Hide resolved
lua/mini/misc.lua Outdated Show resolved Hide resolved
lua/mini/misc.lua Outdated Show resolved Hide resolved
@cryptomilk cryptomilk force-pushed the mini.cursorpos branch 4 times, most recently from 0bc3443 to 1459792 Compare January 20, 2023 09:33
@cryptomilk
Copy link
Contributor Author

cryptomilk commented Jan 21, 2023

Looks like we can't test the center option. I don't find a way to get the cursor postion in the window.

vim.api.nvim_win_get_position() gives you the buffer-relative postion, not the window :-(

@cryptomilk
Copy link
Contributor Author

I already do.

You use os.tmpname(), which is not predetermined. With this approach it is easy to not detect problems with cleanup.

Please, use approach similar to 'test_sessions.lua': this, this, and this (but inside new_set() for restoring cursor).

Done. Thanks for the pointer :-)

@cryptomilk cryptomilk force-pushed the mini.cursorpos branch 2 times, most recently from 9ac61b8 to 9c809c4 Compare January 22, 2023 08:10
@cryptomilk
Copy link
Contributor Author

I think, I have covered everything with tests now.

Copy link
Owner

@echasnovski echasnovski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for many comments. This does look very good, only many small optimizations needed.

And general request: please use more explicit test case names describing what should work correctly. Like:

  • Instead of ['nocenter'] - ['respects `opts.center`'].
  • Instead of ['file'] - ['works in general file'].

tests/dir-misc/cursor_restore.lua Outdated Show resolved Hide resolved
tests/test_misc.lua Outdated Show resolved Hide resolved
tests/test_misc.lua Outdated Show resolved Hide resolved
tests/test_misc.lua Outdated Show resolved Hide resolved
tests/test_misc.lua Outdated Show resolved Hide resolved
tests/test_misc.lua Outdated Show resolved Hide resolved
tests/test_misc.lua Outdated Show resolved Hide resolved
tests/test_misc.lua Outdated Show resolved Hide resolved
tests/test_misc.lua Outdated Show resolved Hide resolved
lua/mini/misc.lua Outdated Show resolved Hide resolved
@cryptomilk
Copy link
Contributor Author

I don't see auto_root() documented in readmes/mini-misc.md. If you could do it I could follow it and document restore_cursor().

@echasnovski
Copy link
Owner

I don't see auto_root() documented in readmes/mini-misc.md. If you could do it I could follow it and document restore_cursor().

I'll probably add some kind of summary for all interesting 'mini.misc' functions when this one is done.

@cryptomilk cryptomilk force-pushed the mini.cursorpos branch 3 times, most recently from ccff7bb to e8e4590 Compare January 22, 2023 20:26
@echasnovski
Copy link
Owner

I've taken a quick look. Seems to be nothing major to change. Still some small-ish code style issues, but I think I'll fix them myself during merge. Thanks!

I'll take closer look, probably, the day after tomorrow.

@cryptomilk
Copy link
Contributor Author

Thanks for all the help!

@cryptomilk cryptomilk force-pushed the mini.cursorpos branch 2 times, most recently from 367723d to adcc386 Compare January 22, 2023 21:01
What it does:
- When reopening a file, this will make sure the cursor is placed back to
  the position where you left before.
- This implements :help restore-cursor in a nicer way.
- Creates autocommand for |BufReadPre| and |FileType| event with
  |MiniMisc.restore_cursor|
Copy link
Owner

@echasnovski echasnovski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your work!

I'll merge this in separate branch and will polish the rest myself (like not modifying the commonly used set_cursor() helper). Of course, you'll be credited as initial author.

@echasnovski echasnovski changed the base branch from main to restore-cursor January 24, 2023 18:11
@echasnovski echasnovski reopened this Jan 24, 2023
@echasnovski echasnovski merged commit b5216f5 into echasnovski:restore-cursor Jan 24, 2023
@echasnovski
Copy link
Owner

@cryptomilk, I've polished it and merged into main.

Couple of differences that I made:

  • Removed ignore_buftype and made it work only for normal buffers. This seems to be a more reasonable approach. If not, can add those later.
  • Decided to act zv always and not behind foldclosed(), as I didn't see a use case where this goes wrong.
  • Rewritten tests to test setup_restore_cursor() directly, which required some tricky approach for restarting child process.
  • Added note that it won't work if file doesn't have recognized file type. This is a limitation of FileType approach, but it doesn't seem to be a huge deal to me right now.

Thanks again for going forward with this!

@cryptomilk
Copy link
Contributor Author

@echasnovski You're welcome.

I've rebased my branch which implements the autocmd using vim.api.nvim_create_augroup() here:

https://github.com/cryptomilk/mini.nvim/tree/mini.restore_cursor_lua

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

Successfully merging this pull request may close these issues.

2 participants