linediff.vim icon indicating copy to clipboard operation
linediff.vim copied to clipboard

Update the buffers after creation from original file?

Open unphased opened this issue 5 years ago • 3 comments

I know that this might be outside the scope of the plugin but I figured since it has so many nice things (like sign markers and everything) it would be cool if it could detect and sync the changes made in the source buffer.

unphased avatar Nov 07 '18 04:11 unphased

It's a difficult problem to solve without significant drawbacks.

The way that the proxy buffers and the original keep in sync is that the proxy buffers keep a range of lines from the original that they're affecting. Once you update the proxy buffers, that range gets entirely replaced, and the signs and everything can be updated. But the other way around is not possible, because there's no way to tell if lines have been added or removed.

Say you had this content in the original buffer:

def foo
  puts "bar"
end

Now, if you proxied this buffer and then made this change in the original:

def foo
  puts "bar"
  puts "baz"
end

How would the proxy buffer look now? Well, it'd have to be updated like so:

def foo
  puts "bar"
  puts "baz"

This is probably not what you'd expect, especially if you're using it to diff two pieces of similar code. But there's no way for the proxy to know "the start and/or end line has changed", because the change is logical. There might be a way to guess, if Vim were throwing an event like "line has been added within this range" (although even then it'd be difficult to say what would happen if you add a file after the end of the range).

It might be possible to create a command that manually replaces a particular range. Say, you make some change in the original buffer, and then you select the new area it should be covering, do a :LinediffReplace 1. But even with that, there's the problem that the second buffer might also have moved if it was located below the first one in the original buffer. So you'd likely have to replace that one as well (although maybe the difference between the new and old range can be taken, and the second can be automatically adjusted, so this might be solvable). But I wonder if, in that case, it isn't just as easy to :LinediffReset and just reselect both areas. Maybe it would still be helpful, if the second area is not changed and can be auto-adjusted.

Does that make sense? Am I explaining well enough the problem, and what do you think of a potential :LinediffReplace command (I can't say for sure this would work, I'd need to experiment)?

AndrewRadev avatar Nov 17 '18 11:11 AndrewRadev

I don't think I need this feature, but I noticed that Vim actually updates signs on changes to the buffer just like it does with the marks (try adding/removing lines and see signs keeping their positions). There is also :sign place command which can accept buffer number and list positions of signs there. And since Vim 7.4 there is getbufinfo('%')[0].signs with the list of signs and where they are.

xaizek avatar Nov 17 '18 13:11 xaizek

Oh, that's very interesting. I hadn't realized that Vim actually moves the signs around. Thank you, @xaizek, I'll try to build something using this info.

AndrewRadev avatar Nov 17 '18 13:11 AndrewRadev