Search Result View now shortens filepaths

git-svn-id: trunk@5761 -
This commit is contained in:
mattias 2004-08-09 20:34:10 +00:00
parent 36ac3ea512
commit dbc4e7b929
3 changed files with 195 additions and 107 deletions

View File

@ -198,7 +198,6 @@ procedure TSearchForm.SearchFile(TheFileName: string);
EndWord: boolean; //Does the word end with a seperator charater? EndWord: boolean; //Does the word end with a seperator charater?
TheLine: string; //Temp Storage for the current line in the file. TheLine: string; //Temp Storage for the current line in the file.
TempSearch: string; //Temp Storage for the search string. TempSearch: string; //Temp Storage for the search string.
TheHeader: string;
MatchLen: integer; MatchLen: integer;
const const
@ -239,23 +238,19 @@ procedure TSearchForm.SearchFile(TheFileName: string);
end;//if end;//if
if StartWord And EndWord then if StartWord And EndWord then
begin begin
TheHeader:= TheFileName +'('+IntToStr(lines+1)+ ','+ IntToStr(match)
+')' + ' ';
SearchResultsView.AddMatch(fResultsWindow, SearchResultsView.AddMatch(fResultsWindow,
TheHeader + Trim(ThisFile.Strings[Lines]), TheFileName,Point(match,lines+1),
match + Length(TheHeader), Trim(ThisFile.Strings[Lines]),
MatchLen); match, MatchLen);
UpdateMatches; UpdateMatches;
end;//if end;//if
end;//if end;//if
if not fWholeWord and (Match > 0) then if not fWholeWord and (Match > 0) then
begin begin
TheHeader:= TheFileName +'('+IntToStr(lines+1)+ ','+ IntToStr(match)
+')' + ' ';
SearchResultsView.AddMatch(fResultsWindow, SearchResultsView.AddMatch(fResultsWindow,
TheHeader + Trim(ThisFile.Strings[Lines]), TheFileName,Point(match,lines+1),
match + Length(TheHeader), Trim(ThisFile.Strings[Lines]),
MatchLen); match, MatchLen);
UpdateMatches; UpdateMatches;
end;//if end;//if
if fAbort and not fAborting then if fAbort and not fAborting then
@ -282,8 +277,7 @@ procedure TSearchForm.SearchFile(TheFileName: string);
Match: integer; //Position of match in line. Match: integer; //Position of match in line.
MatchLen: integer; MatchLen: integer;
TheLine: string; //Temp Storage for the current line in the file. TheLine: string; //Temp Storage for the current line in the file.
TheHeader: string; RE: TRegExpr; //Regular expression search engine
RE: TRegExpr; //Regular expression search engin
begin begin
try try
ThisFile:= TStringList.Create; ThisFile:= TStringList.Create;
@ -305,11 +299,10 @@ procedure TSearchForm.SearchFile(TheFileName: string);
Match:= RE.MatchPos[0]; Match:= RE.MatchPos[0];
MatchLen:= Re.MatchLen[0]; MatchLen:= Re.MatchLen[0];
TheHeader:= TheFileName +'('+IntToStr(lines+1)+ ','+ IntToStr(match) SearchResultsView.AddMatch(fResultsWindow,
+')' + ' '; TheFileName,Point(match,lines+1),
SearchResultsView.AddMatch(fResultsWindow,TheHeader + TheLine, TheLine,
match + Length(TheHeader), match, MatchLen);
MatchLen);
UpdateMatches; UpdateMatches;
end;//if end;//if
if fAbort and not fAborting then if fAbort and not fAborting then

View File

@ -37,25 +37,35 @@ unit SearchResultView;
interface interface
uses uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Classes, SysUtils, LCLProc, LResources, Forms, Controls, Graphics, Dialogs,
ComCtrls, ExtCtrls, StdCtrls, Buttons, LCLType, ComCtrls, ExtCtrls, StdCtrls, Buttons, LCLType,
IDEOptionDefs, LazarusIDEStrConsts, EnvironmentOpts, InputHistory, IDEOptionDefs, LazarusIDEStrConsts, EnvironmentOpts, InputHistory,
FindInFilesDlg, Project, MainIntf; FindInFilesDlg, Project, MainIntf;
{TLazSearchMatchPos}
type type
{ TLazSearchMatchPos }
TLazSearchMatchPos = class(TObject) TLazSearchMatchPos = class(TObject)
private private
FFilename: string;
FFilePosition: TPoint;
fMatchStart: integer; fMatchStart: integer;
fMatchLen: integer; fMatchLen: integer;
FShownFilename: string;
FTheText: string;
public public
property MatchStart: integer read fMatchStart write fMatchStart; property MatchStart: integer read fMatchStart write fMatchStart;
property MatchLen: integer read fMatchLen write fMatchLen; property MatchLen: integer read fMatchLen write fMatchLen;
property Filename: string read FFilename write FFilename;
property FilePosition: TPoint read FFilePosition write FFilePosition;
property TheText: string read FTheText write FTheText;
property ShownFilename: string read FShownFilename write FShownFilename;
end;//TLazSearchMatchPos end;//TLazSearchMatchPos
{ TLazSearch } { TLazSearch }
type
TLazSearch = Class(TObject) TLazSearch = Class(TObject)
private private
fSearchString: string; fSearchString: string;
@ -71,26 +81,30 @@ type
property SearchMask: string read fSearchMask write fSearchMask; property SearchMask: string read fSearchMask write fSearchMask;
end;//TLazSearch end;//TLazSearch
{ TLazSearchResultLB } { TLazSearchResultLB }
type
TLazSearchResultLB = Class(TCustomListBox) TLazSearchResultLB = Class(TCustomListBox)
private private
fSearchObject: TLazSearch; fSearchObject: TLazSearch;
fUpdateStrings: TStrings; fUpdateStrings: TStrings;
fUpdating: boolean; fUpdating: boolean;
fUpdateCount: integer; fUpdateCount: integer;
fShortenPathNeeded: boolean;
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
destructor Destroy; override; destructor Destroy; override;
property SearchObject: TLazSearch read fSearchObject write fSearchObject; property SearchObject: TLazSearch read fSearchObject write fSearchObject;
procedure BeginUpdate; procedure BeginUpdate;
procedure EndUpdate; procedure EndUpdate;
procedure ShortenPaths;
property UpdateItems: TStrings read fUpdateStrings write fUpdateStrings; property UpdateItems: TStrings read fUpdateStrings write fUpdateStrings;
property UpdateState: boolean read fUpdating; property UpdateState: boolean read fUpdating;
end; end;
{ TSearchResultsView } { TSearchResultsView }
type
TSearchResultsView = class(TForm) TSearchResultsView = class(TForm)
btnSearchAgain: TBUTTON; btnSearchAgain: TBUTTON;
ResultsNoteBook: TNOTEBOOK; ResultsNoteBook: TNOTEBOOK;
@ -107,7 +121,6 @@ type
Procedure LazLBMouseWheel(Sender: TObject; Shift: TShiftState; Procedure LazLBMouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean); WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
private private
{ private declarations }
function PageExists(const APageName: string): boolean; function PageExists(const APageName: string): boolean;
function GetPageIndex(APageName: string): integer; function GetPageIndex(APageName: string): integer;
function GetListBox(APageIndex: integer): TLazSearchResultLB; function GetListBox(APageIndex: integer): TLazSearchResultLB;
@ -119,7 +132,6 @@ type
fListBoxFont: TFont; fListBoxFont: TFont;
fMouseOverIndex: integer; fMouseOverIndex: integer;
public public
{ public declarations }
function AddResult(const ResultsName: string; function AddResult(const ResultsName: string;
const SearchText: string; const SearchText: string;
const ADirectory: string; const ADirectory: string;
@ -128,8 +140,11 @@ type
function GetSourcePositon: TPoint; function GetSourcePositon: TPoint;
function GetSourceFileName: string; function GetSourceFileName: string;
function GetSelectedText: string; function GetSelectedText: string;
function GetSelectedMatchPos: TLazSearchMatchPos;
procedure BringResultsToFront(const APageName: string); procedure BringResultsToFront(const APageName: string);
procedure AddMatch(const AIndex: integer; const TheText: string; procedure AddMatch(const AIndex: integer;
const Filename: string; const FilePosition: TPoint;
const TheText: string;
const MatchStart: integer; const MatchLen: integer); const MatchStart: integer; const MatchLen: integer);
procedure BeginUpdate(AIndex: integer); procedure BeginUpdate(AIndex: integer);
procedure EndUpdate(AIndex: integer); procedure EndUpdate(AIndex: integer);
@ -169,7 +184,7 @@ begin
fListBoxFont.Height:= 12; fListBoxFont.Height:= 12;
fListBoxFont.Style:= []; fListBoxFont.Style:= [];
fOnSelectionChanged:= nil; fOnSelectionChanged:= nil;
self.ShowHint:= True; ShowHint:= True;
fMouseOverIndex:= -1; fMouseOverIndex:= -1;
end;//Create end;//Create
@ -211,12 +226,13 @@ begin
end;//LazLBMouseWheel end;//LazLBMouseWheel
procedure TSearchResultsView.AddMatch(const AIndex: integer; procedure TSearchResultsView.AddMatch(const AIndex: integer;
const Filename: string; const FilePosition: TPoint;
const TheText: string; const TheText: string;
const MatchStart: integer; const MatchStart: integer; const MatchLen: integer);
const MatchLen: integer);
var var
CurrentLB: TLazSearchResultLB; CurrentLB: TLazSearchResultLB;
SearchPos: TLazSearchMatchPos; SearchPos: TLazSearchMatchPos;
ShownText: String;
begin begin
CurrentLB:= GetListBox(AIndex); CurrentLB:= GetListBox(AIndex);
if Assigned(CurrentLB) then if Assigned(CurrentLB) then
@ -224,10 +240,19 @@ begin
SearchPos:= TLazSearchMatchPos.Create; SearchPos:= TLazSearchMatchPos.Create;
SearchPos.MatchStart:= MatchStart; SearchPos.MatchStart:= MatchStart;
SearchPos.MatchLen:= MatchLen; SearchPos.MatchLen:= MatchLen;
SearchPos.Filename:=Filename;
SearchPos.FilePosition:=FilePosition;
SearchPos.TheText:=TheText;
SearchPos.ShownFilename:=SearchPos.Filename;
ShownText:=SearchPos.ShownFilename
+' ('+IntToStr(SearchPos.FilePosition.Y)
+','+IntToStr(SearchPos.FilePosition.X)+')'
+' '+SearchPos.TheText;
if CurrentLB.UpdateState then if CurrentLB.UpdateState then
CurrentLB.UpdateItems.AddObject(TheText, SearchPos) CurrentLB.UpdateItems.AddObject(ShownText, SearchPos)
else else
CurrentLB.Items.AddObject(TheText, SearchPos); CurrentLB.Items.AddObject(ShownText, SearchPos);
CurrentLB.ShortenPaths;
end;//if end;//if
end;//AddMatch end;//AddMatch
@ -302,7 +327,7 @@ begin
end;//if end;//if
end;//GetItems end;//GetItems
procedure TSearchResultsView.ResultsNoteBookClosetabclicked(Sender: TObject); procedure TSearchResultsView.ResultsNoteBookCloseTabclicked(Sender: TObject);
begin begin
if (Sender is TPage) then if (Sender is TPage) then
begin begin
@ -390,11 +415,11 @@ begin
OnDblClick:= @ListBoxDoubleClicked; OnDblClick:= @ListBoxDoubleClicked;
Style:= lbOwnerDrawFixed; Style:= lbOwnerDrawFixed;
OnDrawItem:= @ListBoxDrawItem; OnDrawItem:= @ListBoxDrawItem;
Font.Name:= fListBoxFont.Name;
Font.Height:= fListBoxFont.Height;
OnShowHint:= @LazLBShowHint; OnShowHint:= @LazLBShowHint;
OnMouseMove:= @LazLBMousemove; OnMouseMove:= @LazLBMousemove;
OnMouseWheel:= @LazLBMouseWheel; OnMouseWheel:= @LazLBMouseWheel;
Font.Name:=fListBoxFont.Name;
Font.Height:=fListBoxFont.Height;
ShowHint:= true; ShowHint:= true;
NewLIstBox.Canvas.Color:= clWhite; NewLIstBox.Canvas.Color:= clWhite;
end;//with end;//with
@ -414,6 +439,9 @@ end;//AddResult
procedure TSearchResultsView.LazLBShowHint(Sender: TObject; HintInfo: Pointer); procedure TSearchResultsView.LazLBShowHint(Sender: TObject; HintInfo: Pointer);
var
MatchPos: TLazSearchMatchPos;
HintStr: string;
begin begin
if Sender is TLazSearchResultLB then if Sender is TLazSearchResultLB then
begin begin
@ -421,7 +449,18 @@ begin
begin begin
if (fMouseOverIndex >= 0) and (fMouseOverIndex < Items.Count) then if (fMouseOverIndex >= 0) and (fMouseOverIndex < Items.Count) then
begin begin
Hint:= Items[fMouseOverIndex]; if Items.Objects[fMouseOverIndex] is TLazSearchMatchPos then
MatchPos:= TLazSearchMatchPos(Items.Objects[fMouseOverIndex])
else
MatchPos:= nil;
if MatchPos<>nil then
HintStr:=MatchPos.Filename
+' ('+IntToStr(MatchPos.FilePosition.Y)
+','+IntToStr(MatchPos.FilePosition.X)+')'
+' '+MatchPos.TheText
else
HintStr:=Items[fMouseOverIndex];
Hint:= HintStr;
end;//if end;//if
end;//with end;//with
end;//if end;//if
@ -440,24 +479,27 @@ var
TheTop: integer; TheTop: integer;
MatchPos: TLazSearchMatchPos; MatchPos: TLazSearchMatchPos;
TextEnd: integer; TextEnd: integer;
ShownMatchStart: LongInt;
begin begin
With Control as TLazSearchResultLB do With Control as TLazSearchResultLB do
begin begin
Canvas.FillRect(ARect); Canvas.FillRect(ARect);
TheText:= Items[Index];
if Items.Objects[Index] is TLazSearchMatchPos then if Items.Objects[Index] is TLazSearchMatchPos then
MatchPos:= TLazSearchMatchPos(Items.Objects[Index]) MatchPos:= TLazSearchMatchPos(Items.Objects[Index])
else else
MatchPos:= nil; MatchPos:= nil;
TheText:= Items[Index];
if Assigned(MatchPos) then if Assigned(MatchPos) then
begin begin
TheTop:= ARect.Top; TheTop:= ARect.Top;
BoldLen:= MatchPos.MatchLen; BoldLen:= MatchPos.MatchLen;
FirstPart:= copy(TheText,1,MatchPos.MatchStart - 1); ShownMatchStart:=length(TheText)-length(MatchPos.TheText)
BoldPart:= copy(TheText,MatchPos.MatchStart ,BoldLen); +MatchPos.MatchStart;
LastPart:= copy(TheText, MatchPos.MatchStart + BoldLen, FirstPart:= copy(TheText,1,ShownMatchStart - 1);
Length(TheText) - (MatchPos.MatchStart + BoldLen) + 2); BoldPart:= copy(TheText,ShownMatchStart ,BoldLen);
LastPart:= copy(TheText, ShownMatchStart + BoldLen,
Length(TheText) - (ShownMatchStart + BoldLen) + 2);
Canvas.TextOut(ARect.Left, TheTop, FirstPart); Canvas.TextOut(ARect.Left, TheTop, FirstPart);
TextEnd:= ARect.Left + Canvas.TextWidth(FirstPart); TextEnd:= ARect.Left + Canvas.TextWidth(FirstPart);
Canvas.Font.Style:= Canvas.Font.Style + [fsBold]; Canvas.Font.Style:= Canvas.Font.Style + [fsBold];
@ -489,58 +531,28 @@ begin
end;//ListBoxDoubleClicked end;//ListBoxDoubleClicked
{Returns the Position within the source file from a properly formated search {Returns the Position within the source file from a properly formated search
reslut} result}
function TSearchResultsView.GetSourcePositon: TPoint; function TSearchResultsView.GetSourcePositon: TPoint;
var var
i: integer; MatchPos: TLazSearchMatchPos;
strTemp: string;
strResults: string;
begin begin
strResults:= GetSelectedText; Result.x:= -1;
result.x:= -1; Result.y:= -1;
result.y:= -1; MatchPos:=GetSelectedMatchPos;
i:= pos('(',strResults); if MatchPos=nil then exit;
if i > 0 then Result:=MatchPos.FilePosition;
begin
inc(i);
While (i < length(strResults)) and (strResults[i] <> ',') do
begin
strTemp:= StrTemp + strResults[i];
inc(i);
end;//while
if (i < Length(StrResults)) and (strResults[i] = ',') then
begin
result.y:= StrToInt(strTemp);
inc(i);
strTemp:= '';
While (i < length(strResults)) and (strResults[i] <> ')') do
begin
strTemp:= strResults[i];
inc(i);
end;//while
if (i < Length(strResults)) and (strResults[i] = ')' ) then
result.x:= StrToInt(strTemp);
end;//if
end;//if
end;//GetSourcePositon end;//GetSourcePositon
{Returns The file name portion of a properly formated search result} {Returns The file name portion of a properly formated search result}
function TSearchResultsView.GetSourceFileName: string; function TSearchResultsView.GetSourceFileName: string;
var var
strResults: string; MatchPos: TLazSearchMatchPos;
i: integer;
begin begin
strResults:= GetSelectedText; MatchPos:=GetSelectedMatchPos;
i:= pos('(', strResults); if MatchPos=nil then
dec(i); Result:=''
if i > 0 then
begin
result:= copy(strResults, 1, i);
end
else else
begin Result:=MatchPos.Filename;
result:= '';
end;
end;//GetSourceFileName end;//GetSourceFileName
{Returns the selected text in the currently active listbox.} {Returns the selected text in the currently active listbox.}
@ -568,6 +580,34 @@ begin
end;//if end;//if
end;//GetSelectedText end;//GetSelectedText
function TSearchResultsView.GetSelectedMatchPos: TLazSearchMatchPos;
var
ThePage: TPage;
TheListBox: TLazSearchResultLB;
i: integer;
AnObject: TObject;
begin
Result:= nil;
i:= ResultsNoteBook.PageIndex;
if i > -1 then
begin
ThePage:= ResultsNoteBook.Page[i];
if Assigned(ThePage) then
begin
TheListBox:= GetListBox(ThePage.PageIndex);
if Assigned(TheListBox) then
begin
i:= TheListBox.ItemIndex;
if i > -1 then begin
AnObject:=TheListBox.Items.Objects[i];
if AnObject is TLazSearchMatchPos then
Result:=TLazSearchMatchPos(AnObject);
end;
end;//if
end;//if
end;//if
end;
function TSearchResultsView.GetPageIndex(APageName: string): integer; function TSearchResultsView.GetPageIndex(APageName: string): integer;
var var
i: integer; i: integer;
@ -655,28 +695,83 @@ procedure TLazSearchResultLB.EndUpdate;
var var
i: integer; i: integer;
begin begin
if (fUpdateCount = 0) then
RaiseGDBException('TLazSearchResultLB.EndUpdate');
dec(fUpdateCount); dec(fUpdateCount);
if (fUpdateCount < 0) then
fUpdateCount:= 0;
if (fUpdateCount = 0) then if (fUpdateCount = 0) then
begin begin
ShortenPaths;
fUpdating:= false; fUpdating:= false;
for i:= 0 to Items.Count -1 do for i:= 0 to Items.Count -1 do
begin begin
try
if Assigned(Items.Objects[i]) then if Assigned(Items.Objects[i]) then
begin begin
Items.Objects[i].free; Items.Objects[i].free;
end;//if end;//if
except
writeln('Exception in TLazSearchResultLB.EndUpdate,' +
' Pointer assigned free failed');
end;//except
end;//for end;//for
Items.Assign(fUpdateStrings); Items.Assign(fUpdateStrings);
end;//if end;//if
end;//EndUpdate end;//EndUpdate
procedure TLazSearchResultLB.ShortenPaths;
var
i: Integer;
AnObject: TObject;
SharedPath: String;
MatchPos: TLazSearchMatchPos;
SrcList: TStrings;
SharedLen: Integer;
ShownText: String;
begin
if fUpdateCount>0 then begin
fShortenPathNeeded:=true;
exit;
end;
fShortenPathNeeded:=false;
if fUpdating then
SrcList:=fUpdateStrings
else
SrcList:=Items;
// find shared path (the path of all filenames, that is the same)
SharedPath:='';
for i:=0 to SrcList.Count-1 do begin
AnObject:=SrcList.Objects[i];
if AnObject is TLazSearchMatchPos then begin
MatchPos:=TLazSearchMatchPos(AnObject);
if i=0 then
SharedPath:=ExtractFilePath(MatchPos.Filename)
else if (SharedPath<>'') then begin
SharedLen:=0;
while (SharedLen<length(MatchPos.Filename))
and (SharedLen<length(SharedPath))
and (MatchPos.Filename[SharedLen+1]=SharedPath[SharedLen+1])
do
inc(SharedLen);
if SharedLen<>length(SharedPath) then
SharedPath:=copy(SharedPath,1,SharedLen);
end;
end;
end;
// shorten shown paths
SharedLen:=length(SharedPath);
for i:=0 to SrcList.Count-1 do begin
AnObject:=SrcList.Objects[i];
if AnObject is TLazSearchMatchPos then begin
MatchPos:=TLazSearchMatchPos(AnObject);
MatchPos.ShownFilename:=copy(MatchPos.Filename,SharedLen+1,
length(MatchPos.Filename));
ShownText:=MatchPos.ShownFilename
+' ('+IntToStr(MatchPos.FilePosition.Y)
+','+IntToStr(MatchPos.FilePosition.X)+')'
+' '+MatchPos.TheText;
SrcList[i]:=ShownText;
SrcList.Objects[i]:=MatchPos;
end;
end;
end;
initialization initialization
{$I searchresultview.lrs} {$I searchresultview.lrs}