[PATCH] Highlight word differences in diffs

Martin Geisler mg at lazybytes.net
Thu May 28 03:09:15 CDT 2009


Benoit Boissinot <benoit.boissinot at ens-lyon.org> writes:

> On Wed, May 27, 2009 at 08:09:41PM -0000, Martin Geisler wrote:
>> # HG changeset patch
>> # User Martin Geisler <mg at lazybytes.net>
>> # Date 1243454861 -7200
>> # Node ID 2504b69072eea7415bda241b43dd99dd7db7b3bb
>> # Parent  c6483eec6092414bac04f916706f572ed2305ec0
>> Highlight word differences in diffs
>> 
>> I wondered if it would be difficult to add word diffs to the color
>> extension, and it turned out to be easier than I thought easy. This
>> patch shows a rough prototype...
>
> or you could steal the lodgeit code :)
>
> https://bitbucket.org/EnTeQuAk/lodgeit-main/src/tip/lodgeit/lib/diff.py

Looking briefly at the code I don't really get it. But you're
definitely right -- there are lots of code out there that does this.

> I changed a bit your patch (I don't like the underline):

Yeah, me neither :-)

> diff --git a/hgext/color.py b/hgext/color.py
> --- a/hgext/color.py
> +++ b/hgext/color.py
> @@ -83,6 +83,7 @@
>                    'underline': 4,
>                    'nounderline': 24,
>                    'inverse': 7,
> +                  'uninverse': 27,
>                    'black_background': 40,
>                    'red_background': 41,
>                    'green_background': 42,
> @@ -196,15 +197,19 @@
>      #pprint(blocks)
>  
>      def highlight(s):
> -        return "\033[%dm%s\033[%dm" % (_effect_params['underline'], s,
> -                                       _effect_params['nounderline'])
> +        return "\033[%dm%s\033[%dm" % (_effect_params['inverse'], s,
> +                                       _effect_params['uninverse'])
>  
>      s = (0, 0, 0, 0)
>      for t in blocks:
>          for i in range(s[1], t[0]):
>              apieces[2*i] = highlight(apieces[2*i])
> +        for i in range(s[1], t[0]-1):
> +            apieces[2*i+1] = highlight(apieces[2*i+1])
>          for i in range(s[3], t[2]):
>              bpieces[2*i] = highlight(bpieces[2*i])
> +        for i in range(s[3], t[2]-1):
> +            bpieces[2*i+1] = highlight(bpieces[2*i+1])
>          s = t
>  
>      a = ''.join(apieces)

That looks like a good idea: you merge the give the delimiters (the
stuff between words) the same highlight as the words themselves.

So if I change

  foo, bar, baz

to

  foo! xxx! baz

then your code will highlight it as

  -foo, -bar,- baz
  +foo! +xxx!+ baz

where I've added minusses and plusses to highlight the words. The
first ',' -> '!' isn't highlighted since the word before did not
change. It would probably work better to split on \s+ instead of \W+.

I'll work on it some more and try to integrate it with hgweb -- please
let me know if you have other good ideas.

-- 
Martin Geisler

VIFF (Virtual Ideal Functionality Framework) brings easy and efficient
SMPC (Secure Multiparty Computation) to Python. See: http://viff.dk/.


More information about the Mercurial-devel mailing list