IDE: Remove LCL dependency from unit DiffPatch.

git-svn-id: trunk@57121 -
This commit is contained in:
juha 2018-01-19 12:12:48 +00:00
parent 1cefe1d894
commit 78bc310072
3 changed files with 33 additions and 29 deletions

View File

@ -142,6 +142,7 @@ type
procedure UpdateDiff; procedure UpdateDiff;
procedure SetIdleConnected(const AValue: boolean); procedure SetIdleConnected(const AValue: boolean);
procedure OnIdle(Sender: TObject; var {%H-}Done: Boolean); procedure OnIdle(Sender: TObject; var {%H-}Done: Boolean);
procedure UpdateProgress(aPosition: Integer);
public public
constructor Create(TheOwner: TComponent); override; constructor Create(TheOwner: TComponent); override;
destructor Destroy; override; destructor Destroy; override;
@ -379,6 +380,12 @@ begin
IdleConnected:=True; IdleConnected:=True;
end; end;
procedure TDiffDlg.UpdateProgress(aPosition: Integer);
begin
ProgressBar1.Position := aPosition;
Application.ProcessMessages;
end;
procedure TDiffDlg.SetIdleConnected(const AValue: boolean); procedure TDiffDlg.SetIdleConnected(const AValue: boolean);
begin begin
if fIdleConnected=AValue then exit; if fIdleConnected=AValue then exit;
@ -407,9 +414,11 @@ begin
OpenInEditorButton.Enabled := False; OpenInEditorButton.Enabled := False;
//CancelScanningButton.Enabled := True; //CancelScanningButton.Enabled := True;
DiffOutput:=TDiffOutput.Create(Text1Src, Text2Src, GetDiffOptions, ProgressBar1); DiffOutput := TDiffOutput.Create(Text1Src, Text2Src, GetDiffOptions);
try try
DiffSynEdit.Lines.Text:=DiffOutput.CreateTextDiff; ProgressBar1.Max := DiffOutput.GetProgressMax;
DiffOutput.OnProgressPos := @UpdateProgress;
DiffSynEdit.Lines.Text := DiffOutput.CreateTextDiff;
finally finally
DiffOutput.Free; DiffOutput.Free;
end; end;

View File

@ -37,7 +37,7 @@ unit DiffPatch;
interface interface
uses uses
Classes, SysUtils, LCLProc, Forms, ComCtrls; Classes, SysUtils, LazLogger;
type type
TTextDiffFlag = ( TTextDiffFlag = (
@ -97,14 +97,16 @@ type
procedure GetNextLineExtends(var LineExtends: TLineExtends); procedure GetNextLineExtends(var LineExtends: TLineExtends);
end; end;
TProgressEvent = procedure(aPosition: Integer) of object;
{ TDiffOutput } { TDiffOutput }
TDiffOutput = class TDiffOutput = class
private private
fText1, fText2: string; fText1, fText2: string;
fOutputType: TTextDiffOutputType; fOutputType: TTextDiffOutputType;
fOnProgressPos: TProgressEvent;
fFlags: TTextDiffFlags; fFlags: TTextDiffFlags;
fProgressBar: TProgressBar;
fDiffStream: TStream; fDiffStream: TStream;
fPart1, fPart2: TDiffPart; fPart1, fPart2: TDiffPart;
procedure FindNextEqualLine(const Start1, Start2: TLineExtends; procedure FindNextEqualLine(const Start1, Start2: TLineExtends;
@ -120,12 +122,13 @@ type
procedure AddDiff(const Start1, End1, Start2, End2: TLineExtends); procedure AddDiff(const Start1, End1, Start2, End2: TLineExtends);
procedure UpdateProgressBar(const Line: TLineExtends); procedure UpdateProgressBar(const Line: TLineExtends);
public public
constructor Create(const aText1, aText2: string; constructor Create(const aText1, aText2: string; aFlags: TTextDiffFlags);
aFlags: TTextDiffFlags; aProgressBar: TProgressBar);
destructor Destroy; override; destructor Destroy; override;
function GetProgressMax: Integer;
function CreateTextDiff: string; function CreateTextDiff: string;
public public
property OutputType: TTextDiffOutputType read fOutputType write fOutputType; property OutputType: TTextDiffOutputType read fOutputType write fOutputType;
property OnProgressPos: TProgressEvent read fOnProgressPos write fOnProgressPos;
end; end;
@ -171,12 +174,11 @@ end;
function GotoNextLine(var LineExtends: TLineExtends): boolean; function GotoNextLine(var LineExtends: TLineExtends): boolean;
begin begin
with LineExtends do begin with LineExtends do begin
if LineStart<NextLineStart then begin Result:=LineStart<NextLineStart;
if Result then begin
inc(LineNumber); inc(LineNumber);
LineStart:=NextLineStart; LineStart:=NextLineStart;
Result:=true; end;
end else
Result:=false;
end; end;
end; end;
@ -670,7 +672,7 @@ begin
until false; until false;
except except
on E: Exception do begin on E: Exception do begin
DebugLn('CreateTextDiff ',E.Message); DebugLogger.DebugLn('CreateTextDiff ',E.Message);
end; end;
end; end;
finally finally
@ -711,10 +713,8 @@ end;
procedure TDiffOutput.UpdateProgressBar(const Line: TLineExtends); procedure TDiffOutput.UpdateProgressBar(const Line: TLineExtends);
begin begin
if Assigned(fProgressBar) then begin if Assigned(OnProgressPos) then
fProgressBar.Position := Line.LineStart; OnProgressPos(Line.LineStart);
Application.ProcessMessages;
end;
end; end;
procedure TDiffOutput.FinishOldContextBlock; procedure TDiffOutput.FinishOldContextBlock;
@ -858,21 +858,16 @@ begin
fPart2.Write2(Start2,End2,Part1HasChangedLines,'+'); fPart2.Write2(Start2,End2,Part1HasChangedLines,'+');
end; end;
constructor TDiffOutput.Create(const aText1, aText2: string; function TDiffOutput.GetProgressMax: Integer;
aFlags: TTextDiffFlags; aProgressBar: TProgressBar); begin
var Result := Length(fText1); // + Length(fText2);
i: Integer; end;
constructor TDiffOutput.Create(const aText1, aText2: string; aFlags: TTextDiffFlags);
begin begin
fText1:=aText1; fText1:=aText1;
fText2:=aText2; fText2:=aText2;
fFlags:=aFlags; fFlags:=aFlags;
fProgressBar:=aProgressBar;
if Assigned(fProgressBar) then begin
i := Length(aText1); // + Length(aText2);
fProgressBar.Max := i;
fProgressBar.Step := i;
fProgressBar.Position := 0;
end;
fOutputType:=tdoContext; // Default OutputType, can be changed later fOutputType:=tdoContext; // Default OutputType, can be changed later
fDiffStream:=TMemoryStream.Create; fDiffStream:=TMemoryStream.Create;
fPart1:=TDiffPart.Create(Self, fText1); fPart1:=TDiffPart.Create(Self, fText1);
@ -881,8 +876,8 @@ end;
destructor TDiffOutput.Destroy; destructor TDiffOutput.Destroy;
begin begin
if Assigned(fProgressBar) then if Assigned(OnProgressPos) then
fProgressBar.Position := 0; OnProgressPos(0);
fPart2.Free; fPart2.Free;
fPart1.Free; fPart1.Free;
fDiffStream.Free; fDiffStream.Free;

View File

@ -338,7 +338,7 @@ begin
fs.Read(Result^.TxtOnDisk[1],length(Result^.TxtOnDisk)); fs.Read(Result^.TxtOnDisk[1],length(Result^.TxtOnDisk));
fs.Free; fs.Free;
DiffOutput:=TDiffOutput.Create(Source,Result^.TxtOnDisk, [], nil); DiffOutput:=TDiffOutput.Create(Source,Result^.TxtOnDisk, []);
try try
Result^.Diff+=DiffOutput.CreateTextDiff; Result^.Diff+=DiffOutput.CreateTextDiff;
finally finally