Recent

Author Topic: Compare two text lines and highlight difference  (Read 5807 times)

totya

  • Hero Member
  • *****
  • Posts: 720
Compare two text lines and highlight difference
« on: February 08, 2023, 01:21:00 pm »
Hi!

Is there any simple solution for compare two text lines and highlight difference with synedit?

If no, where am I start?

Thanks!

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Compare two text lines and highlight difference
« Reply #1 on: February 08, 2023, 01:51:38 pm »
It depend what you mean with difference.
Show exemplary what you mean.

Exemplary:
line1: AaBbCc
line2: AabBcC

Should now bBcC be highlighted in line2 or BbCc in line1 or is that not the thing you want at all?

Be more specific!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Compare two text lines and highlight difference
« Reply #2 on: February 08, 2023, 02:01:23 pm »
It depend what you mean with difference.
Show exemplary what you mean.

Normal text lines, for example (sorry my English):

This is a nice day, the sky is blue and wind blowing sun is rising.
This is a nice day with many kills, the sky is red and wind blowing sun is somwhere, because all dark here.

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Compare two text lines and highlight difference
« Reply #3 on: February 08, 2023, 03:05:36 pm »
Nested loops comparing words/tokens?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Compare two text lines and highlight difference
« Reply #4 on: February 08, 2023, 03:15:24 pm »
...
This is a nice day, the sky is blue and wind blowing sun is rising.
This is a nice day with many kills, the sky is red and wind blowing sun is somwhere, because all dark here.

Maybe you need a Diff algorithm?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Compare two text lines and highlight difference
« Reply #5 on: February 08, 2023, 03:35:37 pm »
SynEdit doesn't have that....

The closest you currently get is the "same word highlight", which applies to current selection too.
- if you start selecting one line from the start,
- select a few chars only
- wait about half a second, so the "same word highlight" will be activated
  (this can be set under Tools > Options: Editor > Display > Markup and Matches > top section "Highlight all occurrences of word under caret"
- Now the start of the other line should be highlighted.
- Extend the selection, until the other line looses the highlight => you are at the first diff in the line.

It's not what you want/need. Not even close....

It is a different feature, but it can be used for simple comparison ....




If you want to extend SynEdit....
1) Well you need your own code to find the diffs between the lines.

Once you have a list of sections that you want to highlight ( record Line, StartX, EndX: integer end; ):
2) You can write your on SynEditMarkup.
a) If you don't use any Highlighter (no Pascal or other HL) then you can use the "SynPos...Highlighter).
b) Otherwise, look at markups. Something like SynMarkupHighlightAll should be easy to modify. It already has a list....

Awkward

  • Full Member
  • ***
  • Posts: 133
Re: Compare two text lines and highlight difference
« Reply #6 on: February 08, 2023, 05:37:40 pm »
... looks like this feature
https://github.com/rickard67/TextDiff

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Compare two text lines and highlight difference
« Reply #7 on: February 08, 2023, 07:52:57 pm »
... looks like this feature
https://github.com/rickard67/TextDiff

Thanks for the tip, but the basicdemo2 completely freeze my machine, when I click "open". Possible Dwarf/debug error, I must press reset (data loss).

But basicdemo1 works, but paintbox isn't really good for me, but for starting point is good.

Thank you!

Edit 1.: UTF8 characters loss with Demo1, fore example: "öüó"
« Last Edit: February 08, 2023, 08:25:20 pm by totya »

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Compare two text lines and highlight difference
« Reply #8 on: February 08, 2023, 08:00:34 pm »
SynEdit doesn't have that....

I mean Synedit components (group name). Thank you for the many tips!

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Compare two text lines and highlight difference
« Reply #9 on: February 08, 2023, 08:14:20 pm »
I don't know how it's done in TextDiff, but in LGenerics it's very easy:
Code: Pascal  [Select][+][-]
  1. ...
  2. uses
  3.   ..., lgSeqUtils;
  4.  
  5. ...
  6. procedure TForm1.Button1Click(Sender: TObject);
  7. type
  8.   TUtil = specialize TGSeqUtil<string, string>;
  9. var
  10.   s1, s2: string;
  11.   a1, a2: TStringArray;
  12.   LDiff: TUtil.TDiff;
  13. begin
  14.   s1 := 'This is a nice day, the sky is blue and wind blowing sun is rising.';
  15.   s2 := 'This is a nice day with many kills, the sky is red and wind blowing sun is somewhere, because all dark here.';
  16.   a1 := s1.Split([' ', '.', ','], TStringSplitOptions.ExcludeEmpty);
  17.   a2 := s2.Split([' ', '.', ','], TStringSplitOptions.ExcludeEmpty);
  18.   LDiff := TUtil.Diff(a1, a2);
  19.   Memo1.Append('Deleted from s1(i.e. not present in s2):');
  20.   for I := 0 to High(LDiff.SourceChanges) do
  21.     if LDiff.SourceChanges[I] then
  22.       Memo1.Append(a1[I]);
  23.   Memo1.Append('');
  24.   Memo1.Append('Inserted into s2(i.e. not present in s1):');
  25.   for I := 0 to High(LDiff.TargetChanges) do
  26.     if LDiff.TargetChanges[I] then
  27.       Memo1.Append(a2[I]);  
  28. end;
  29.  

it prints:
Code: Text  [Select][+][-]
  1. Deleted from s1(i.e. not present in s2):
  2. blue
  3. rising
  4.  
  5. Inserted into s2(i.e. not present in s1):
  6. with
  7. many
  8. kills
  9. red
  10. somewhere
  11. because
  12. all
  13. dark
  14. here
  15.  

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2006
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Compare two text lines and highlight difference
« Reply #10 on: February 08, 2023, 08:25:28 pm »
@avk, very cool solution, a suggestion would be, have results as a pair (string, integer) to know at what positions a change happened.
Full respects for that!  :-*
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Compare two text lines and highlight difference
« Reply #11 on: February 08, 2023, 08:33:45 pm »
Btw, this may interest you: https://forum.lazarus.freepascal.org/index.php/topic,62146.msg470014.html#msg470014

It does not compare 2 lines of text. But it does compare 2 texts line by line.

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Compare two text lines and highlight difference
« Reply #12 on: February 08, 2023, 09:09:14 pm »
I don't know how it's done in TextDiff, but in LGenerics it's very easy:

Thank you, I'd like to try, but where can I found this LGenerics/lgSeqUtils ?

Now I'm use the official Lazarus package with fpc 3.2.2

sketch

  • New Member
  • *
  • Posts: 32
Re: Compare two text lines and highlight difference
« Reply #13 on: February 08, 2023, 09:54:47 pm »
On Unix
Code: [Select]
$ cat ttt
#!/usr/bin/ksh

for ((i=1;i<3;i++))
do
  echo "Unmatched words from string ${i}"
  comm <(echo "This is a nice day, the sky is blue and wind blowing sun is rising." |tr ' ' '\n' | sed 's/\.//' |sed 's/,//' |sort) <(echo "This is a nice day with many kills, the sky is red and wind blowing sun is somewhere, because all dark here." | tr ' ' '\n' | sed 's/\.//' | sed 's/,//' |sort) | cut -f${i} |sed '/^$/d'
  echo
done
Code: [Select]
$ cat t.pp
program t;
uses Unix;
Var S : Longint;
begin
  S:=fpSystem('./ttt');
end.
Code: [Select]
$ ./t
Unmatched words from string 1
blue
rising

Unmatched words from string 2
all
because
blue
dark
here
kills
many
red
rising
somewhere
with

$
;D

avk

  • Hero Member
  • *****
  • Posts: 752
Re: Compare two text lines and highlight difference
« Reply #14 on: February 09, 2023, 05:04:56 am »
...
Thank you, I'd like to try, but where can I found this LGenerics/lgSeqUtils ?
...

It lives here.

 

TinyPortal © 2005-2018