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

Update buffer on file action with LF #25

Closed
piersolenski opened this issue May 3, 2022 · 9 comments
Closed

Update buffer on file action with LF #25

piersolenski opened this issue May 3, 2022 · 9 comments

Comments

@piersolenski
Copy link

Is there a way to update the buffer when a file action is performed with LF? For example, if I delete or rename a file with LF and then return to the buffer, the old file exists - which will lead to errors if I then try to edit it. Thanks in advance, love this plugin!

@is0n
Copy link
Owner

is0n commented May 3, 2022

Try using this:

require('fm-nvim').setup{
	on_close = {
		function()
			vim.cmd [[ checktime ]]
		end
	},
}

@piersolenski
Copy link
Author

Hmm, what exactly should this be doing - reloading the file? I added it but it doesn't seem to do anything...

I guess the exact behaviour I'm looking for is that of https://github.com/tpope/vim-eunuch when you :Delete or :Rename a file.

@is0n
Copy link
Owner

is0n commented May 3, 2022

:checkt[ime] Check if any buffers were changed outside of Vim.
This checks and warns you if you would end up with two
versions of a file.
If this is called from an autocommand, a ":global"
command or is not typed the actual check is postponed
until a moment the side effects (reloading the file)
would be harmless.
Each loaded buffer is checked for its associated file
being changed. If the file was changed Vim will take
action. If there are no changes in the buffer and
'autoread' is set, the buffer is reloaded. Otherwise,
you are offered the choice of reloading the file. If
the file was deleted you get an error message.
If the file previously didn't exist you get a warning
if it exists now.
Once a file has been checked the timestamp is reset,
you will not be warned again.
Syntax highlighting, marks, diff status,
'fileencoding', 'fileformat' and 'binary' options
are not changed. See |v:fcs_choice| to reload these
too (for example, if a code formatting tools has
changed the file).

:checktime essentially checks if any changes were made to the current buffer. Running this when Lf closes should in theory warn you about the file not existing but it doesn't seem to work for me either. 🤔

I'll see what I can do later today.

@is0n
Copy link
Owner

is0n commented May 4, 2022

Not sure if this is what you wanted but now you will be warned if a file does not exist.

require('fm-nvim').setup{
  on_close = {
    function()
      vim.fn.expand("%")
    end
  }
}

@piersolenski
Copy link
Author

The more I use this library, the more I reckon updating the buffer applies to most of its integrations too.

For example, take any one of the git integrations, reset a hunk, and return to the buffer. Ideally, you shouldn't see the old hunk, you should see the updated changes in the buffer. Currently, you need to close and reopen the buffer to do so.

In this scenario, your vim.cmd [[ checktime ]] does the job... would it be worth making that default behaviour?

I guess it won't work in regards to my original query around deleting or renaming a file, as the file is now different... so maybe that's almost a separate issue, maybe outside the scope of the plugin and would need some LF specific code.

@is0n
Copy link
Owner

is0n commented Jun 15, 2022

In this scenario, your vim.cmd [[ checktime ]] does the job... would it be worth making that default behaviour?

Seems like a great idea!

I guess it won't work in regards to my original query around deleting or renaming a file, as the file is now different... so maybe that's almost a separate issue, maybe outside the scope of the plugin and would need some LF specific code.

If you want to be notified when you're working with a deleted/renamed file, you can use the code from my last comment.

On the other hand, if you want to edit the newly renamed file after quitting from lf, you could create a command in your lfrc that renames the selected file (at it normally would) and writes the new file's path to some temp file. Then in the on_close option, check if the temp file exists and then read from it.

@piersolenski
Copy link
Author

I guess it would also need to close the buffer with the old file name too? The most simple plugin I can think of that already does this is Tim Pope's Eunuch with the rename function:

https://github.com/tpope/vim-eunuch/blob/master/plugin/eunuch.vim#L37

But yeah, this feels like two different issues now, one perhaps within the scope of fm-nvim and one a bit more outside of it!

@is0n
Copy link
Owner

is0n commented Jun 16, 2022

I think I get what you're getting at now. The following code should do the trick!

lfrc:

cmd mv ${{
  mv "$f" "$1"
  echo "$(readlink -f $1)" > /tmp/lf_renamed
}}

init.lua

local function Rename()
  local temp = "/tmp/lf_renamed"
  local file = io.open(temp, "r")
  if file ~= nil then
    vim.cmd("bdelete!")
    vim.cmd("edit " .. vim.fn.readfile(temp)[1])
    io.close(file)
    os.remove(temp)
  end
end

local function Delete()
  local file = vim.fn.expand("%:p")
  if io.open(file, "r") == nil then
    vim.cmd("bdelete!")
  end
end

require('fm-nvim').setup{
  on_close = {
    Rename,
    Delete
  }
}

@piersolenski
Copy link
Author

Ah this awesome! Thank you so much! Guess we can close this now 😀🎆

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

No branches or pull requests

2 participants