feat: add --selected-row option for cursor positioning#7
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds a new --selected-row command-line option that allows users to position the cursor at a specific row when opening a diff view. The feature complements the existing --selected-file option by providing finer-grained control over the initial cursor position.
Changes:
- Added
selected_rowoption parsing in both Git and Hg adapters - Added type annotation and cursor positioning logic in the DiffView class
- Added command-line completion for the new flag in GitAdapter
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lua/diffview/vcs/adapters/git/init.lua | Parses the --selected-row flag and adds completion support for the new option |
| lua/diffview/vcs/adapters/hg/init.lua | Parses the --selected-row flag in the Mercurial adapter |
| lua/diffview/scene/views/diff/diff_view.lua | Adds type annotation for selected_row and implements cursor positioning logic on first view initialization |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| selected_file = argo:get_flag("selected-file", { no_empty = true, expand = true }) | ||
| or (vim.bo.buftype == "" and pl:vim_expand("%:p")) | ||
| or nil, | ||
| selected_row = tonumber(argo:get_flag("selected-row", { no_empty = true })), |
There was a problem hiding this comment.
The HgAdapter is missing completion setup for the selected-row flag. While GitAdapter includes self.comp.open:put({ "selected-row" }) in its init_completion method (line 2262), HgAdapter's init_completion method doesn't set up any completion for the open command. Consider adding completion setup similar to GitAdapter to maintain consistency across adapters.
| -- Position cursor at the requested row on first open. | ||
| if not self.initialized and self.options.selected_row then | ||
| local win = self.cur_layout:get_main_win() | ||
| if win and api.nvim_win_is_valid(win.id) then | ||
| local buf = api.nvim_win_get_buf(win.id) | ||
| local line_count = api.nvim_buf_line_count(buf) | ||
| local row = math.min(self.options.selected_row, line_count) | ||
| pcall(api.nvim_win_set_cursor, win.id, { math.max(1, row), 0 }) |
There was a problem hiding this comment.
Consider using utils.set_cursor instead of directly calling api.nvim_win_set_cursor with manual clamping. The codebase has a utility function utils.set_cursor(winid, line, column) that handles cursor positioning with automatic clamping and error handling, which would be more consistent with the rest of the codebase (e.g., see lines 53, 65 in lua/diffview/scene/views/file_history/listeners.lua or lines 292, 322, 388 in lua/diffview/scene/views/diff/file_panel.lua).
No description provided.