Robitx / gp.nvim

Gp.nvim (GPT prompt) Neovim AI plugin: ChatGPT sessions & Instructable text/code operations & Speech to text [OpenAI]
MIT License
537 stars 50 forks source link

Autosave on TextChange and TextChangedI events causes unintended file writes #81

Closed therealstein closed 6 months ago

therealstein commented 6 months ago

Description

I've encountered an issue where gp.nvim seems to be automatically saving (writing) files on every change while in insert mode due to an autocommand set within the plugin.

This behavior is unexpected and can lead to files being written to when modifications are temporary or unintended. It bypasses the typical workflow where a user may choose when to save their changes explicitly.

Issue Details

The following line in init.lua sets up an autocommand that triggers writing to the file every time the buffer changes, both in normal and insert modes:

vim.api.nvim_command("autocmd TextChanged,TextChangedI <buffer> silent! write")

File path: .local/share/nvim/site/pack/packer/start/gp.nvim/lua/gp/init.lua

Expected Behavior

Plugins typically should not enforce such autosave behavior as it might conflict with user's workflow and expectations.

Steps to Reproduce

  1. Install gp.nvim start new Chat.
  2. Open any file and start editing in insert mode.
  3. Observe that every text change triggers a save.

Possible Solution

It might be more appropriate to allow users to opt-in for such a feature or control this behavior through a configurable option, rather than it being on by default. An alternative workflow could be by notifying users of unsaved changes without automatically writing to the file.

Additional Context

I have narrowed down this issue to gp.nvim after troubleshooting the autosave issue by inspecting autocommands and determining the erratic behavior does not happen when this plugin is disabled.

Thank you for looking into this matter. Please let me know if there's more information I can provide or any other suggestions to address this issue.

Robitx commented 6 months ago

@therealstein hey, the autosave should be limited to the buffers associated with the plugin - chat buffers or .gp.md context buffers, nothing else. If you're seeing writes on unrelated buffers (it's not 100% clear from your report), that would be quite a bug, but if that's the case, I'm not able to replicate it.

Otherwise yes, I can make the autosave for chat buffer s a .gp.md configurable.

therealstein commented 6 months ago

Thank you for all the hard work on your excellent plugin and for taking the time to address this issue. I first encountered the problem while developing in Vue using dev HMR mode. The issue occurs in Insert mode, where every keystroke causes the development environment to reload, which is not the expected behavior. I ran VSCode simultaneously to check if changes in Insert mode were being registered there as well. However, this issue only occurred when I started a fresh instance of Neovim, opened some buffers, and then initiated a new GP chat session using the command GpChatNew tabnew. I managed to stop this behavior by commenting out the line I mentioned earlier. However, I am not fully aware of the context of this command and its potential side effects.

I can only speak for my environment, and I need to check it without all other plugins. I apologize for taking up your time.

Robitx commented 6 months ago

@therealstein no need to apologize, even if it had to do something with your setup. But you're correct it's a bug on my side. I can see the same behavior, if I use tabnew. Displaying into tabs is relatively recent addition and I don't usually use them, so I haven't noticed it during development or regular use. This is indeed a nice find, thank you. :slightly_smiling_face:

Robitx commented 6 months ago

@therealstein please try the latest version if you can confirm it's fixed.

therealstein commented 6 months ago

@Robitx It works for me, thank you for solving the issue.