1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
unit frmInstantSearch;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, LazLoggerBase, FileUtil, Forms, Controls, Graphics,
Dialogs, LCLType, ExtCtrls, Grids, StdCtrls, ComCtrls, Menus, IDECommands,
IDEOPtEditorIntf, IDEWindowIntf, LazIDEIntf, MenuIntf, Types, idemcindexer;
Const
LocRightMargin = 4;
LocLeftMargin = 4;
ResultRightMargin = 8;
LocOffset = LocLeftMargin+LocRightMargin;
SrcOffset = 2;
type
{ TInstantSearchForm }
TInstantSearchForm = class(TForm)
edtSearch: TEdit;
grdResults: TDrawGrid;
ILSearch: TImageList;
imgSearch: TImage;
pnlStatus: TPanel;
pnlNoResults: TPanel;
pnlTop: TPanel;
PMTrees: TPopupMenu;
TBSettings: TToolBar;
TBTrees: TToolButton;
tmrIdle: TTimer;
tmrAuto: TTimer;
procedure edtSearchChange(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure grdResultsClick(Sender: TObject);
procedure grdResultsDblClick(Sender: TObject);
procedure grdResultsDrawCell(Sender: TObject; aCol, aRow: Integer;
aRect: TRect; aState: TGridDrawState);
procedure tmrAutoTimer(Sender: TObject);
procedure tmrIdleTimer(Sender: TObject);
private
class var _OptionsFrameClass: TAbstractIDEOptionsEditorClass;
procedure CheckIndexing;
procedure DoCheckIndexing(sender: TObject);
function IsConfigured: Boolean;
procedure ShowHighLights(S: String; aCanvas: TCanvas; aRect: TRect; aLeft: Integer);
private
fLastChange : TDateTime;
FCurrentSearch : String;
FResults : TMCSearchResultArray;
FSourceStyle: TTextStyle;
FContentStyle: TTextStyle;
FScrollbarwidth : Integer;
FLastSearchAt : TDateTime;
procedure CheckScrollbarwidth;
procedure CheckColSize;
procedure CreateStyles;
procedure DoEnableTree(Sender: TObject);
Procedure DoSearch(aText : string);
procedure ShowTrees;
public
constructor create(aOwner: TComponent); override;
// Call this
Procedure TreesChanged;
// Call this
Procedure ActiveProjectChanged;
// Do we have enough characters to search ?
function CanSearch : Boolean;
// Actually search
procedure Search;
// Called when the form is shown through user action
Procedure InitSearch;
// Properties to show
Class Property OptionsFrameClass : TAbstractIDEOptionsEditorClass Read _OptionsFrameClass Write _OptionsFrameClass;
// TIDEInstantSearchOptionsFrame
end;
var
InstantSearchForm: TInstantSearchForm;
InstantSearchFormCreator: TIDEWindowCreator; // set by Register procedure
procedure ShowInstantSearchForm(Sender: TObject);
procedure CreateInstantSearchForm(Sender: TObject; aFormName: string;
var AForm: TCustomForm; DoDisableAutoSizing: boolean);
implementation
uses
dateutils, LCLIntf, clipbrd, IDEExternToolIntf, idemsgintf, ideinstantsearch, instantsearchstrings;
{$R *.lfm}
procedure ShowInstantSearchForm(Sender: TObject);
begin
if Assigned(IDEWindowCreators.ShowForm(InstantSearchFormCreator.FormName, true)) then
InstantSearchForm.InitSearch;
end;
procedure CreateInstantSearchForm(Sender: TObject; aFormName: string;
var AForm: TCustomForm; DoDisableAutoSizing: boolean);
begin
// sanity check to avoid clashing with another package that has registered a window with the same name
if CompareText(aFormName, 'InstantSearchForm')<>0 then begin
DebugLn(['ERROR: CreateInstantSearchForm: there is already a form with '
+'this name']);
exit;
end;
IDEWindowCreators.CreateForm(AForm, TInstantSearchForm, DoDisableAutoSizing,
LazarusIDE.OwningComponent);
AForm.Name:=aFormName;
InstantSearchForm:=AForm as TInstantSearchForm;
end;
{ TInstantSearchForm }
procedure TInstantSearchForm.grdResultsDrawCell(Sender: TObject; aCol,
aRow: Integer; aRect: TRect; aState: TGridDrawState);
var
aRes : PMCSearchResult;
S : String;
aLeft,W : integer;
RSource : Trect;
begin
if (aRow>=Length(FResults)) or (aCol>=1) then
Exit;
grdResults.DefaultDrawCell(aCol,aRow,aRect,aState);
aRes:=@FResults[aRow];
Case aCol of
0 :
begin
if gdSelected in aState then
grdResults.Canvas.DrawFocusRect(aRect);
S:=ExtractFileName(aRes^.FileName);
S:=Format('%s(%d)',[S,aRes^.LineNo]);
W:=grdResults.Canvas.TextWidth(S);
RSource:=aRect;
RSource.Left:=aRect.Right-W-LocOffset-FScrollbarwidth;
RSource.Right:=aRect.Right-LocRightMargin-FScrollbarwidth;
grdResults.Canvas.TextRect(RSource,RSource.Left,RSource.Top,S,FSourceStyle);
aRect.Right:=aRect.Right-W-LocOffset-FScrollbarwidth-ResultRightMargin;
S:=Trim(aRes^.Content);
aLeft:=aRect.Left+SrcOffset;
ShowHighLights(S,grdResults.Canvas,aRect,aLeft);
grdResults.Canvas.Brush.Style:=bsClear;
grdResults.Canvas.TextRect(aRect,aLeft,aRect.Top,S,FContentStyle);
end;
end;
end;
procedure TInstantSearchForm.ShowHighLights(S : String; aCanvas : TCanvas; aRect : TRect; aLeft: Integer);
Var
W,L,P : Integer;
lRect : TRect;
C : TColor;
begin
lRect:=aRect;
c:=aCanvas.Brush.Color;
aCanvas.Brush.Color:=clYellow;
W:=aCanvas.TextWidth(FCurrentSearch);
P:=Pos(FCurrentSearch,S);
While P<>0 do
begin
L:=aCanvas.TextWidth(Copy(S,1,P-1));
lRect.Left:=aLeft+L;
lRect.Right:=aLeft+L+W;
if lrect.Right>aRect.Right then
lrect.Right:=aRect.Right;
aCanvas.FillRect(lRect);
P:=Pos(FCurrentSearch,S,P+Length(FCurrentSearch));
end;
aCanvas.Brush.Color:=C;
end;
procedure TInstantSearchForm.tmrAutoTimer(Sender: TObject);
begin
if MillisecondsBetween(Now,fLastChange)>TmrAuto.Interval then
begin
TmrAuto.Enabled:=False;
Search;
end;
end;
procedure TInstantSearchForm.tmrIdleTimer(Sender: TObject);
begin
if SecondsBetween(Now,FLastSearchAt)> IDEInstantSearchManager.IdleDisconnectTimeOut then
if IDEInstantSearchManager.Indexer.Connected then
IDEInstantSearchManager.Indexer.Disconnect;
end;
procedure TInstantSearchForm.CheckScrollbarwidth;
begin
// h := GetSystemMetrics(SM_CYHSCROLL);
FScrollBarWidth:=GetSystemMetrics(SM_CXVSCROLL);
end;
procedure TInstantSearchForm.DoSearch(aText: string);
begin
FCurrentSearch:=aText;
if Not IDEInstantSearchManager.Indexer.Connected then
try
IDEInstantSearchManager.Indexer.Connect;
except
On E : Exception do
begin
IDEMessagesWindow.AddCustomMessage(mluError,E.Message,'',0,0,lrsInstantSearch);
MessageDlg(lrsInstantSearch,Format(lrsErrorConnecting,[E.Message]),mtError,[mbOK],'');
end;
end;
try
FResults:=IDEInstantSearchManager.Search(aText);
except
On E : Exception do
begin
IDEMessagesWindow.AddCustomMessage(mluError,E.Message,'',0,0,lrsInstantSearch);
MessageDlg(lrsInstantSearch,Format(lrsErrorSearching,[E.Message]),mtError,[mbOK],'');
end;
end;
grdResults.RowCount:=Length(FResults);
CheckScrollbarWidth;
pnlNoResults.Visible:=Length(FResults)=0;
grdResults.Invalidate;
FLastSearchAt:=Now;
end;
constructor TInstantSearchForm.create(aOwner: TComponent);
begin
inherited create(aOwner);
Caption:=lrsInstantSearch;
edtSearch.TextHint:=Format(lrsSearchTextHint,[IDEInstantSearchManager.MinSearchLength]);
edtSearch.Hint:=Format(lrsSearchTextHint,[IDEInstantSearchManager.MinSearchLength]);
IDEInstantSearchManager.OnIndexStart:=@DoCheckIndexing;
IDEInstantSearchManager.OnIndexDone:=@DoCheckIndexing;
CheckIndexing;
CreateStyles;
ShowTrees;
end;
procedure TInstantSearchForm.DoCheckIndexing(sender : TObject);
begin
CheckIndexing;
end;
procedure TInstantSearchForm.CheckIndexing;
begin
pnlStatus.Visible:=IDEInstantSearchManager.IsIndexing;
end;
procedure TInstantSearchForm.TreesChanged;
begin
ShowTrees;
end;
procedure TInstantSearchForm.ActiveProjectChanged;
begin
ShowTrees;
end;
procedure TInstantSearchForm.ShowTrees;
Var
aTree : TSourceTreeDefinition;
Mnu : TMenuItem;
I : Integer;
begin
PMTrees.Items.Clear;
Mnu:=TMenuItem.Create(Self);
Mnu.Tag:=-1;
Mnu.Caption:=lrsCurrentProject;
Mnu.AutoCheck:=True;
Mnu.OnClick:=@DoEnableTree;
Mnu.Enabled:=(IDEInstantSearchManager.ProjectTreeName<>'');
Mnu.Checked:=Mnu.Enabled and IDEInstantSearchManager.SearchProject;
PMTrees.Items.Add(Mnu);
Mnu:=TMenuItem.Create(Self);
Mnu.Caption:='-';
PMTrees.Items.Add(Mnu);
For I:=0 to IDEInstantSearchManager.SourceTrees.Count-1 do
begin
aTree:=IDEInstantSearchManager.SourceTrees[i];
Mnu:=TMenuItem.Create(Self);
Mnu.Tag:=I;
Mnu.Caption:=aTree.Name;
Mnu.Checked:=aTree.Enabled;
Mnu.AutoCheck:=True;
Mnu.OnClick:=@DoEnableTree;
PMTrees.Items.Add(Mnu);
end;
end;
procedure TInstantSearchForm.CreateStyles;
begin
FSourceStyle.Alignment:=taRightJustify;
FSourceStyle.Clipping:=True;
FSourceStyle.EndEllipsis:=True;
FSourceStyle.Layout:=TTextLayout.tlCenter;
FSourceStyle.Wordbreak:=False;
FContentStyle.Alignment:=taLeftJustify;
FContentStyle.Opaque:=True;
FContentStyle.Clipping:=True;
FContentStyle.EndEllipsis:=True;
FContentStyle.Layout:=TTextLayout.tlCenter;
FContentStyle.Wordbreak:=False;
FContentStyle.Opaque:=True;
end;
procedure TInstantSearchForm.DoEnableTree(Sender: TObject);
Var
idx : Integer;
Mnu : TMenuItem absolute Sender;
begin
if Not (Sender is TMenuItem) then exit;
Idx:=Mnu.Tag;
if Idx=-1 then
IDEInstantSearchManager.SearchProject:=Mnu.Checked
else
IDEInstantSearchManager.SourceTrees[Idx].Enabled:=Mnu.Checked;
end;
function TInstantSearchForm.CanSearch: Boolean;
begin
Result:=Length(edtSearch.text)>=IDEInstantSearchManager.MinSearchLength;
end;
function TInstantSearchForm.IsConfigured: Boolean;
Var
ErrMsg : String;
begin
Result:=False;
if IDEInstantSearchManager.Configured then
ErrMsg:=IDEInstantSearchManager.CanSearch
else
ErrMsg:=lrsNotConfigured;
Result:=(ErrMsg='');
if Not Result then
if mrOK=QuestionDlg(lrsConfigNeeded,Format(lrsConfigNeededReason,[ErrMsg]),mtWarning,[mrAbort,lrsCancelSearch,mrOK,lrsConfigure],'LazInstantSearch') then
Result:=LazarusIDE.DoOpenIDEOptions(_OptionsFrameClass) and IDEInstantSearchManager.Configured
else
Exit;
end;
procedure TInstantSearchForm.Search;
begin
if not CanSearch then Exit;
if not IsConfigured then
Exit;
DoSearch(edtSearch.Text);
end;
procedure TInstantSearchForm.InitSearch;
var
lSearch: String;
C : Char;
OK : Boolean;
begin
if Not IsConfigured then
exit;
edtSearch.SetFocus;
lSearch:=Clipboard.AsText;
OK:=Length(lSearch)<=IDEInstantSearchManager.MaxStartSearchLength;
if Not OK then
exit;
for C in lSearch do
begin
OK:=Not (C in [#10,#13,#12]);
if Not OK then
Break;
end;
if Not OK then
exit;
if OK then
begin
edtSearch.Text:=lSearch;
edtSearch.SelectAll;
if CanSearch then
Search;
end;
end;
procedure TInstantSearchForm.grdResultsClick(Sender: TObject);
begin
if CanSearch then
Search
end;
procedure TInstantSearchForm.grdResultsDblClick(Sender: TObject);
Var
Idx : Integer;
Res : TMCSearchResult;
FN,BaseDir : String;
aTree : TSourceTreeDefinition;
P : TPoint;
begin
Idx:=grdResults.Selection.Top;
if (Idx<0) or (Idx>=Length(FResults)) then exit;
Res:=FResults[Idx];
if Res.Tree=IDEInstantSearchManager.ProjectTreeName then
BaseDir:=ExtractFIlePath(LazarusIDE.ActiveProject.ProjectInfoFile)
else
begin
aTree:=IDEInstantSearchManager.SourceTrees.FindByName(Res.Tree);
if aTree=Nil then
begin
ShowMessage(Format(lrsTreeNotFoundResfresh,[Res.Tree]));
Exit;
end
else
BaseDir:=IncludeTrailingPathDelimiter(aTree.BaseDir);
end;
FN:=ExpandFileName(BaseDir+Res.FileName);
P.Y:=Res.LineNo;
P.X:=1;
LazarusIDE.DoOpenFileAndJumpToPos(FN,P,0,0,0,[ofOnlyIfExists,ofRegularFile,ofDoNotLoadResource,ofAddToRecent]);
end;
procedure TInstantSearchForm.edtSearchChange(Sender: TObject);
begin
tmrAuto.Enabled:=true;
fLastChange:=Now;
end;
procedure TInstantSearchForm.CheckColSize;
begin
grdResults.Columns[0].Width:=grdResults.ClientWidth;
end;
procedure TInstantSearchForm.FormResize(Sender: TObject);
begin
CheckColSize;
end;
procedure TInstantSearchForm.FormShow(Sender: TObject);
begin
CheckColSize;
tmrIdle.Interval:=10 * IDEInstantSearchManager.IdleDisconnectTimeOut;
FLastSearchAt:=Now;
end;
end.
|