ChatGPT.nvim icon indicating copy to clipboard operation
ChatGPT.nvim copied to clipboard

Improved newline handling in interactive code edits and edit actions

Open pitkling opened this issue 1 year ago • 2 comments

The following is from my personal fork to improve editing (interactively or via actions) of visual selections; maybe it's interesting for main? In any case, thanks for the great plugin :).

Currently, if a visual selection is edited (either interactively or via an edit action), an unwanted newline was inserted. This pull request avoids such newlines.

The changes (a) ensure that the visual selection is yanked/pasted without creating additional newlines and (b) enforce that ChatGPT's answer ends in exactly as many newlines as the visual selection. The latter is necessary since ChatGPT seems to always add a newline at the end of its answer, even if the input did not contain one. Note that this might be considered a downside of the pull request, since requests like add 3 newlines at the end are now ignored.

Notable changes:

  • Moved the handling of a non-existing current/previous visual selection into Utils.get_visual_lines.
  • Got rid of Utils.get_lines, Utils.get_visual_start_end, since Utils.get_visual_lines seems now simple enough to handle this directly.
  • Utils.end_col is no longer needed and Utils.paste are also gone (replaced by a direct call to vim.api.nvim_buf_set_text).
  • Utils.split_string_by_line: Previously both some text and some text\n resulted in {"some text", ""} (i.e., two lines). Now only the latter does, while the former yields {"some text"}.

pitkling avatar Mar 14 '23 14:03 pitkling

Good job! Could you please fix formatting to pass the pipeline

jackMort avatar Mar 17 '23 21:03 jackMort

Ah sorry, didn't see the formatting checks. Done.

pitkling avatar Mar 18 '23 05:03 pitkling