> > This part of the help is confusing and probably not right:
> >
> > The "byte" is -1 if text is added or removed.
> >
> > In the "from" dict this doesn't make sense. If text is added (and
> > nothing modified) then "start" and "end should be equal, the position
> > where the text is inserted. If text is removed then "start" gives the
> > start of the removed text and "end" the end. The text exists, thus
> > there is no reason to use -1.
> >
> >
> >
> If the "endbyte" is not -1, then removing a character cannot be
> distinguished
> from modifying a character as shown below:
>
> Modifying the first character of a string:
> :echo diff(['abc'], ['xbc'])
> {'from': {'startidx': 0, 'startbyte': 0, 'endidx': 0, 'endbyte': 0},
> 'to': {'startidx': 0, 'startbyte': 0, 'endidx': 0, 'endbyte': 0},
> 'added': 0, 'modified': 1}
>
> Adding a first character to a string:
> :echo diff(['bc'], ['abc'])
> {'from': {'startidx': 0, 'startbyte': 0, 'endidx': 0, 'endbyte': -1},
> 'to': {'startidx': 0, 'startbyte': 0, 'endidx': 0, 'endbyte': 0},
> 'added': 0, 'modified': 1}
>
> In this case, if the "from.endbyte" is 0 instead of -1, then this case
> cannot be differentiated from the previous example.
When adding a character then to "to" "endbyte" should be a positive
value, since the text is longer than it was before. Thus the
from-endbyte should be zero, indicating that the "from" text was empty,
while the to-endbyte would be one (or as many bytes as the new character
occupies), indicating the text that was inserted. This matches what
would be highlighted with DiffText.
> Removing the first character from a string:
> :echo diff(['abc'], ['bc'])
> {'from': {'startidx': 0, 'startbyte': 0, 'endidx': 0, 'endbyte': 0},
> 'to': {'startidx': 0, 'startbyte': 0, 'endidx': 0, 'endbyte': -1},
> 'added': 0, 'modified': 1}
>
> In this case, if the "to.endbyte" is 0 instead of -1, then this case cannot
> be differentiated from the first example.
The from-endbyte here should be positive, indicating the length of the
text that was removed. The to-endbyte should be zero, indicating that
after the change the text is no longer there.
If no items were added or deleted, then "endbyte - startbyte" should
indicate the text affected. If this is zero, then it means that text is
not present. The other entry (from or to) then will be non-zero and
indicates the text removed or added. If both are non-zero it indicates
text was changed.
> There are some more examples where the "endbyte" or the "startbyte" is -1:
>
> Removing a character from the middle of a string:
> :echo diff(['abc'], ['ac'])
> {'from': {'startidx': 0, 'startbyte': 1, 'endidx': 0, 'endbyte': 1},
> 'to': {'startidx': 0, 'startbyte': 1, 'endidx': 0, 'endbyte': -1},
> 'added': 0, 'modified': 1}
Here from-endbyte should be 2 (the "b" was deleted) and to-endbyte
should be 1 (same as to-startbyte, empty text, indicating the "b" that
was removed).
> Adding a character to the middle of a string:
> :echo diff(['ac'], ['abc'])
> {'from': {'startidx': 0, 'startbyte': 1, 'endidx': 0, 'endbyte': -1},
> 'to': {'startidx': 0, 'startbyte': 1, 'endidx': 0, 'endbyte': 1},
> 'added': 0, 'modified': 1}
Here from-endbyte should be 1 and to-endbyte should be 2.
> Removing the last character from a string:
> :echo diff(['abc'], ['ab'])
> {'from': {'startidx': 0, 'startbyte': 2, 'endidx': 0, 'endbyte': 2},
> 'to': {'startidx': 0, 'startbyte': 2, 'endidx': 0, 'endbyte': -1},
> 'added': 0, 'modified': 1}
Here from-endbyte should be 3 and to-endbyte should be 2.
> Adding a character to the end of a string:
> :echo diff(['ab'], ['abc'])
> {'from': {'startidx': 0, 'startbyte': 2, 'endidx': 0, 'endbyte': -1},
> 'to': {'startidx': 0, 'startbyte': 2, 'endidx': 0, 'endbyte': 2},
> 'added': 0, 'modified': 1}
Here from-endbyte should be 2 and to-endbyte should be 3.
> Modifying the last character in a string:
> :echo diff(['abc'], ['abx'])
> {'from': {'startidx': 0, 'startbyte': 2, 'endidx': 0, 'endbyte': 2},
> 'to': {'startidx': 0, 'startbyte': 2, 'endidx': 0, 'endbyte': 2},
> 'added': 0, 'modified': 1}
Here both from-endbyte and to-endbyte should be 3.
> Adding a new item:
> :echo diff(['a'], ['a', 'b'])
> {'from': {'startidx': 1, 'startbyte': -1, 'endidx': -1, 'endbyte': -1},
> 'to': {'startidx': 1, 'startbyte': 0, 'endidx': 1, 'endbyte': 0},
> 'added': 1, 'modified': 0}
When adding an item the byte index doesn't have a meaning, we only need
to know the item index. Here from-endidx should be 1, indicating the
index of where the new item was added.
Having the to-start and to-end be equal suggests that the text is empty.
Only adjusting "endbyte" to the length of the text makes it difficult to
distinguish between adding a whole item and inserting text in an
existing item. Making the "end" exclusive would be simpler, then
to-endidx can be 2 and to-endbyte can remain zero.
> Removing an item from the end:
> :echo diff(['a', 'b'], ['a'])
> {'from': {'startidx': 1, 'startbyte': 0, 'endidx': 1, 'endbyte': 0},
> 'to': {'startidx': 1, 'startbyte': -1, 'endidx': -1, 'endbyte': -1},
> 'added': -1, 'modified': 0}
Also applying the exclusive index here means from-endidx should be 2.
That avoids having from-start and from-end having the same values, thus
it looks like nothing is deleted. to-endidx should be the same as
to-startidx, indicating there is nothing left. The byte indexes in "to"
should also be zero, so it is clear that no text is left.
Hopefully this works out and I actually expect the computations to be
simpler and consistent.
--
Q: What's a light-year?
A: One-third less calories than a regular year.
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
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/20230505171243.9C6F71C1B21%40moolenaar.net.