Skip to content

Draft: Feat: Add suggestions functionality #504

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

Open
wants to merge 74 commits into
base: develop
Choose a base branch
from

Conversation

jakubbortlik
Copy link
Collaborator

Hi @harrisoncramer, in this rather large PR, I would like to introduce suggestions functionality in the discussion tree and in the reviewer:

  • preview a suggestion in a new diff tab (i.e., apply a suggestion and possibly save modifications to local file)
  • edit a comment with a suggestion in a new diff tab
  • reply to a comment with a suggestion preview
  • create a new comment with a suggestion preview

The suggestion preview has some useful features:

  • keybinding to paste in a suggestion snippet
  • autocommands, diagnostics and signs that make it easy to preview multiple suggestions in one comment

I've tried to make it so that the suggestion is previewed on the local file in as many cases as possible so the user can make use of LSP, running tests with the suggestion applied, etc.

Documentation in doc/gitlab.nvim.txt is largely missing yet (that's why the PR is in Draft mode), but the code is fairly well documented.

I'd appreciate feedback, both on the code as well as on the functionality. I've been using this for some time and find it useful and worth the large amount of changes.

I believe some refactoring could be applied to the existing comment creation which would ultimately reduce the amount of code, like I've written a separate function for creating a suggestion, which is a little more universal than the existing build_suggestion.

Comment on lines +623 to +668
vim.api.nvim_cmd({ cmd = "tabnew", args = { original_buf_name } }, {})
local original_buf = vim.api.nvim_get_current_buf()
local original_winid = vim.api.nvim_get_current_win()
vim.api.nvim_buf_set_lines(original_buf, 0, -1, false, original_lines)
vim.bo.bufhidden = "wipe"
vim.bo.buflisted = false
vim.bo.buftype = "nofile"
vim.bo.modifiable = false
vim.cmd.filetype("detect")
local buf_filetype = vim.api.nvim_get_option_value("filetype", { buf = 0 })

local imply_local = determine_imply_local(opts)

-- Create the suggestion buffer and show a diff with the original version
local split_cmd = vim.o.columns > 240 and "vsplit" or "split"
if imply_local then
vim.api.nvim_cmd({ cmd = split_cmd, args = { opts.new_file_name } }, {})
else
local sug_buf_name = get_temp_file_name("SUGGESTION", opts.note_node_id or "NEW_COMMENT", commented_file_name)
vim.api.nvim_cmd({ cmd = split_cmd, args = { sug_buf_name } }, {})
vim.bo.bufhidden = "wipe"
vim.bo.buflisted = false
vim.bo.buftype = "nofile"
vim.bo.filetype = buf_filetype
end
local suggestion_buf = vim.api.nvim_get_current_buf()
local suggestion_winid = vim.api.nvim_get_current_win()
set_buffer_lines(suggestion_buf, suggestions[1].full_text, imply_local)
vim.cmd("1,2windo diffthis")

-- Backup the suggestion buffer winbar to reset it when suggestion preview is closed. Despite the
-- option being "window-local", it's carried over to the buffer even after closing the preview.
-- See https://github.com/neovim/neovim/issues/11525
local suggestion_winbar = vim.api.nvim_get_option_value("winbar", { scope = "local", win = suggestion_winid })

-- Create the note window
local note_buf = vim.api.nvim_create_buf(false, false)
local note_winid = vim.fn.win_getid(3)
local note_bufname = vim.fn.tempname()
vim.api.nvim_buf_set_name(note_buf, note_bufname)
vim.api.nvim_cmd({ cmd = "vnew", mods = { split = "botright" }, args = { note_bufname } }, {})
vim.api.nvim_buf_set_lines(note_buf, 0, -1, false, note_lines)
vim.bo.bufhidden = "wipe"
vim.bo.buflisted = false
vim.bo.filetype = "markdown"
vim.bo.modified = false
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This part is pretty ugly and the buffer/tab/split creation should probably be handled in a cleaner and simpler way. Possibly by using something like Nui.Split.

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

Successfully merging this pull request may close these issues.

1 participant