Hi Bram, On Sun, Apr 30, 2023 at 2:01 PM vim-dev ML <[email protected]> wrote:
> On Sun, Apr 30, 2023 at 1:26 PM Bram Moolenaar ***@***.***> > wrote: > > > > > > Add support for computing diff between two Lists of strings using a > > > function. > > > > > > When using a LSP plugin, whenever a buffer is modified, the > > > modifications are sent to the language server. > > > To do this, the LSP plugins have implemented computing the diff in > > > Vimscript or in Lua: > > > > > > > > > https://github.com/prabirshrestha/vim-lsp/blob/master/autoload/lsp/utils/diff.vim > > > https://github.com/natebosch/vim-lsc/blob/master/autoload/lsc/diff.vim > > > > > > https://github.com/neovim/neovim/blob/master/runtime/lua/vim/lsp/sync.lua > > > > > > These computations are expensive as they compare every character and > > > line in a buffer. > > > This new function will help in efficiently computing the diff. > > > > I suppose you made the structure of the return value work for your > > purpose. It's a bit strange though: List of Dict of Dicts. And using a > > special "end" item to indicate added or removed text (the help is not > > clear, but I guess this is only used when a whole item was > > added/removed, not when a word was added/removed). > > > > The "end" item contains the end index and the byte offset of the diff hunk. > If the string was not present earlier, then the byte offset cannot be > calculated. So it will be -1. When a word is removed, it contains the > position of the end of the word. > > > > > To find inserted or removed list items requires checking whether the > > "byte" item indicates the end of the string. > > > > Wouldn't it be easier to use when returning something similar to unified > > diff: > > > - Indicate range of deleted items (like a "-" line) > > - Indicate range of inserted items (like a "+" line) > > - Indicate a modified item (like a "-" line followed by a "+" line) > > > > The start and end index values in the returned Dict does contain the > above information (range of deleted or inserted or modified items). > > I am attaching some examples to describe the return value of this function. Example 1: ---------- old text ----------- aaaaaa aaaaaa aaaaaa -------------------------------- ---------- new text ----------- aaaxx xxxxxx xxaaa ---------------------------------- The modification starts at index 0 and byte 3 and ends at index 2 byte 2 in the old text. In the new text, the changes start at index 0 byte 3 and end at index 2 byte 1. In this case, the diff() function returns the following: [{'from': {'start': {'idx': 0, 'byte': 3}, 'end': {'idx': 2, 'byte': 2}}, 'to': {'start': {'idx': 0, 'byte': 3}, 'end': {'idx': 2, 'byte': 1}}}] Example 2: ---------- old text ----------- aaaaaa bbbbbb cccccc -------------------------------- ---------- new text ----------- aaaaaa ---------------------------------- In the old text, the modification starts at index 1 byte 0 and ends at index 2 byte 5. In the new text, the modification starts at index 1. But the byte cannot be calculated as the line is removed. Similarly the end of the modification also cannot be calculated. In this case, the diff() function returns the following: [{'from': {'start': {'idx': 1, 'byte': 0}, 'end': {'idx': 2, 'byte': 5}}, 'to': {'start': {'idx': 1, 'byte': -1}, 'end': {'idx': -1, 'byte': -1}}}] Example 3: ---------- old text ----------- aaaaaa -------------------------------- ---------- new text ----------- aaaaaa bbbbbb cccccc ---------------------------------- In the old text, the modification starts at index 1. But the starting byte and ending index/byte cannot be calculated. In the new text, the change starts at index 1 byte 0 and ends at index 2 byte 5. In this case, the diff() function returns the following: [{'from': {'start': {'idx': 1, 'byte': -1}, 'end': {'idx': -1, 'byte': -1}}, 'to': {'start': {'idx': 1, 'byte': 0}, 'end': {'idx': 2, 'byte': 5}}}] Hopefully the above examples clarify your questions. Regards, Yegappan -- -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/CAAW7x7%3DKRWGn4sSn_FmSp-NhQr_Av3DR3MF_XTTvgacM%3DDChLQ%40mail.gmail.com.
