This file is indexed.

/usr/lib/lazarus/0.9.30.4/ide/findoverloadsdlg.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 ***************************************************************************

  Author: Mattias Gaertner

  Abstract:
    Find all alternative declarations of an identifier.
}
unit FindOverloadsDlg;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc,FileUtil, Forms, Controls, Graphics,
  Dialogs, ExtCtrls, StdCtrls, Buttons, ButtonPanel, ComCtrls, AvgLvlTree,
  // codetools
  FindDeclarationTool, PascalParserTool, CodeTree, CodeCache, CodeAtom,
  CodeToolManager, CodeGraph, FindOverloads,
  // IDE
  LazIDEIntf, ProjectIntf, SrcEditorIntf, IDEProcs;

type

  { TFOWFile }

  TFOWFile = class
  private
    FCode: TCodeBuffer;
    FFilename: string;
    FOnlyInterface: boolean;
    FScanned: boolean;
    procedure SetCode(const AValue: TCodeBuffer);
    procedure SetScanned(const AValue: boolean);
  public
    constructor Create(const TheFilename: string);
    destructor Destroy; override;
    property Filename: string read FFilename;
    property OnlyInterface: boolean read FOnlyInterface write FOnlyInterface;
    property Scanned: boolean read FScanned write SetScanned;
    property Code: TCodeBuffer read FCode write SetCode;
  end;

  TFindOverloadsScope = (
    fosProject,
    fosPackages,
    fosOtherSources
    );
  TFindOverloadsScopes = set of TFindOverloadsScope;

  TFOWStage = (
    fowsStart,
    fowsFinished
    );

  { TFindOverloadsWorker }

  TFindOverloadsWorker = class
  private
    FFiles: TAvgLvlTree;
    FScanFiles: TAvgLvlTree;
    FStagePosition: integer;
    FStagePosMax: integer;
    FStageTitle: string;
    procedure CollectProjectFiles;
    procedure CollectPackageFiles;
    procedure CollectOtherSourceFiles;
    procedure ScanSomeFiles;
    procedure ScanFile(AFile: TFOWFile);
    procedure CollectStartSource;
  public
    StartSourceScanned: boolean;
    Scopes: TFindOverloadsScopes;
    CompletedScopes: TFindOverloadsScopes;
    Graph: TDeclarationOverloadsGraph;
    constructor Create;
    destructor Destroy; override;
    procedure Clear;
    procedure Work;
    function Done: boolean;
    procedure StopSearching;
    function AddFileToScan(const Filename: string;
                           CheckExtension: boolean = true): TFOWFile;
    function FindFile(const Filename: string): TFOWFile;
    property Files: TAvgLvlTree read FFiles; // tree of TFindOverloadsWorkerFile
    property ScanFiles: TAvgLvlTree read FScanFiles;// tree of TFindOverloadsWorkerFile
    property StageTitle: string read FStageTitle write FStageTitle;
    property StagePosition: integer read FStagePosition write FStagePosition;
    property StagePosMax: integer read FStagePosMax write FStagePosMax;
  end;

  { TFindOverloadsDialog }

  TFindOverloadsDialog = class(TForm)
    ButtonPanel1: TButtonPanel;
    SearchAllCheckBox: TCheckBox;
    CurGroupBox: TGroupBox;
    ResultsProgressBar: TProgressBar;
    ResultsGroupBox: TGroupBox;
    CurTreeView: TTreeView;
    ResultsTreeView: TTreeView;
    procedure ButtonPanel1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure OnIdle(Sender: TObject; var Done: Boolean);
  private
    FIdleConnected: boolean;
    fCurTreeViewComplete: boolean;
    fWorker: TFindOverloadsWorker;
    procedure SetIdleConnected(const AValue: boolean);
    procedure UpdateProgress;
    procedure StopWorking;
    procedure UpdateCurTreeView;
  public
    property Worker: TFindOverloadsWorker read fWorker;
    property IdleConnected: boolean read FIdleConnected write SetIdleConnected;
  end;

function ShowFindOverloadsDialog: TModalResult;
function ShowFindOverloadsDialog(Code: TCodeBuffer; X, Y: integer): TModalResult;

function CompareFOWFiles(File1, File2: TFOWFile): integer;
function CompareFilenameWithFOWFile(FilenameAnsiString, FOWFile: Pointer): integer;

implementation

{$R *.lfm}

function ShowFindOverloadsDialog: TModalResult;
var
  SrcEdit: TSourceEditorInterface;
  Code: TCodeBuffer;
  XY: TPoint;
begin
  SrcEdit:=SourceEditorManagerIntf.ActiveEditor;
  if SrcEdit=nil then
    exit(mrCancel);
  Code:=TCodeBuffer(SrcEdit.CodeToolsBuffer);
  XY:=SrcEdit.CursorTextXY;
  Result:=ShowFindOverloadsDialog(Code,XY.X,XY.Y);
end;

function ShowFindOverloadsDialog(Code: TCodeBuffer; X, Y: integer
  ): TModalResult;
var
  FindOverloadsDialog: TFindOverloadsDialog;
  Graph: TDeclarationOverloadsGraph;
begin
  if not LazarusIDE.BeginCodeTools then exit;
  Graph:=nil;
  FindOverloadsDialog:=nil;
  CodeToolBoss.ActivateWriteLock;
  try
    if not CodeToolBoss.GatherOverloads(Code,X,Y,Graph) then begin
      LazarusIDE.DoJumpToCodeToolBossError;
      exit(mrCancel);
    end;
    //DebugLn(['ShowFindOverloadsDialog ',Graph.StartCode.Filename,' ',Graph.StartX,',',Graph.StartY]);
    FindOverloadsDialog:=TFindOverloadsDialog.Create(nil);
    FindOverloadsDialog.Worker.Graph:=Graph;
    Result:=FindOverloadsDialog.ShowModal;
  finally
    CodeToolBoss.DeactivateWriteLock;
    FindOverloadsDialog.Free;
    Graph.Free;
  end;
end;

function CompareFOWFiles(File1, File2: TFOWFile): integer;
begin
  Result:=CompareFilenames(File1.Filename,File2.Filename);
end;

function CompareFilenameWithFOWFile(FilenameAnsiString, FOWFile: Pointer
  ): integer;
begin
  Result:=CompareFilenames(ansistring(FilenameAnsiString),TFOWFile(FOWFile).Filename);
end;

{ TFindOverloadsDialog }

procedure TFindOverloadsDialog.FormCreate(Sender: TObject);
begin
  Caption:='Find overloads';
  CurGroupBox.Caption:='Current identifier';
  ResultsGroupBox.Caption:='Overloads';
  SearchAllCheckBox.Caption:='Search in other sources too';
  SearchAllCheckBox.ShowHint:=true;
  SearchAllCheckBox.Hint:='Enable this to search in system sources too. For example the RTL and FCL sources. This can take some minutes on slow machines.';

  ButtonPanel1.CancelButton.OnClick:=@ButtonPanel1Click;

  fWorker:=TFindOverloadsWorker.Create;
  IdleConnected:=true;
  UpdateProgress;
end;

procedure TFindOverloadsDialog.ButtonPanel1Click(Sender: TObject);
begin
  StopWorking;
end;

procedure TFindOverloadsDialog.FormDestroy(Sender: TObject);
begin
  IdleConnected:=false;
  FreeAndNil(fWorker);
end;

procedure TFindOverloadsDialog.OnIdle(Sender: TObject; var Done: Boolean);
begin
  fWorker.Work;
  Done:=fWorker.Done;
  if Done then
    IdleConnected:=false;
  UpdateProgress;
  if not fCurTreeViewComplete then
    UpdateCurTreeView;
end;

procedure TFindOverloadsDialog.SetIdleConnected(const AValue: boolean);
begin
  if FIdleConnected=AValue then exit;
  FIdleConnected:=AValue;
  if FIdleConnected then begin
    ButtonPanel1.CancelButton.Enabled:=true;
    ButtonPanel1.CloseButton.Enabled:=false;
    Application.AddOnIdleHandler(@OnIdle)
  end else begin
    ButtonPanel1.CancelButton.Enabled:=false;
    ButtonPanel1.CloseButton.Enabled:=true;
    Application.RemoveOnIdleHandler(@OnIdle);
  end;
end;

procedure TFindOverloadsDialog.UpdateProgress;
begin
  if Worker.Done then
    ResultsProgressBar.Visible:=false
  else begin
    ResultsProgressBar.Max:=Worker.StagePosMax;
    ResultsProgressBar.Position:=Worker.StagePosition;
    ResultsProgressBar.Visible:=true;
  end;
end;

procedure TFindOverloadsDialog.StopWorking;
begin
  IdleConnected:=false;
  Worker.StopSearching;
end;

procedure TFindOverloadsDialog.UpdateCurTreeView;
var
  s: String;
  Node: TCodeTreeNode;
  Tool: TFindDeclarationTool;
  ParentNode: TCodeTreeNode;
begin
  fCurTreeViewComplete:=true;
  CurTreeView.BeginUpdate;
  CurTreeView.Items.Clear;
  Node:=Worker.Graph.StartCodeNode;
  Tool:=Worker.Graph.StartTool;
  if Node<>nil then begin
    DebugLn(['TFindOverloadsDialog.UpdateCurTreeView ',Node.DescAsString,' ',dbgstr(copy(Tool.Src,Node.StartPos,20))]);
    // unit name
    s:=Tool.GetSourceName(false)+': ';
    // keyword
    case Node.Desc of
    ctnEnumIdentifier: s:=s+'enum';
    ctnVarDefinition: s:=s+'var';
    ctnConstDefinition: s:=s+'const';
    ctnTypeDefinition: s:=s+'type';
    ctnGenericType: s:=s+'generic';
    ctnProperty: s:=s+'property';
    ctnProcedure: s:=s+'procedure';
    ctnUseUnit: s:=s+'uses';
    ctnUnit: s:=s+'unit';
    ctnProgram: s:=s+'program';
    ctnPackage: s:=s+'package';
    ctnLibrary: s:=s+'library';
    end;
    s:=s+' ';
    // context
    if Node.Desc<>ctnEnumIdentifier then
    begin
      ParentNode:=Node.Parent;
      while ParentNode<>nil do begin
        case ParentNode.Desc of
        ctnTypeDefinition,ctnGenericType:
          s:=s+Tool.ExtractDefinitionName(Node)+'.';
        end;
        ParentNode:=ParentNode.Parent;
      end;
    end;
    // name
    case Node.Desc of
    ctnEnumIdentifier, ctnTypeDefinition, ctnConstDefinition, ctnVarDefinition,
    ctnGenericType:
      s:=s+Tool.ExtractDefinitionName(Node);
    ctnProperty:
      s:=s+Tool.ExtractPropName(Node,false);
    ctnProcedure:
      s:=s+Tool.ExtractProcName(Node,[phpWithoutClassName,phpCommentsToSpace]);
    ctnUseUnit:
      s:=s+Tool.ExtractNode(Node,[phpCommentsToSpace]);
    ctnUnit,ctnProgram,ctnPackage,ctnLibrary:
      s:=s+Tool.GetSourceName(false);
    end;
    // add node
    CurTreeView.Items.Add(nil,s);
  end;
  CurTreeView.EndUpdate;
end;

{ TFOWFile }

procedure TFOWFile.SetCode(const AValue: TCodeBuffer);
begin
  if FCode=AValue then exit;
  FCode:=AValue;
end;

procedure TFOWFile.SetScanned(const AValue: boolean);
begin
  if FScanned=AValue then exit;
  FScanned:=AValue;
end;

constructor TFOWFile.Create(const TheFilename: string);
begin
  FFilename:=TheFilename;
  FOnlyInterface:=true;
end;

destructor TFOWFile.Destroy;
begin
  inherited Destroy;
end;

{ TFindOverloadsWorker }

procedure TFindOverloadsWorker.CollectProjectFiles;
var
  AProject: TLazProject;
  i: Integer;
  ProjFile: TLazProjectFile;
begin
  AProject:=LazarusIDE.ActiveProject;
  if AProject<>nil then begin
    for i:=0 to AProject.FileCount-1 do begin
      ProjFile:=AProject.Files[i];
      if ProjFile.IsPartOfProject then
        AddFileToScan(ProjFile.Filename);
    end;
  end;
  Include(CompletedScopes,fosProject);
end;

procedure TFindOverloadsWorker.CollectPackageFiles;
begin

  Include(CompletedScopes,fosPackages);
end;

procedure TFindOverloadsWorker.CollectOtherSourceFiles;
begin

  Include(CompletedScopes,fosOtherSources);
end;

procedure TFindOverloadsWorker.ScanSomeFiles;
const
  MaxScanTime = 0.3/86400; // 0.3 seconds
var
  StartTime: TDateTime;
  CurFile: TFOWFile;
begin
  StartTime:=Now;
  while FScanFiles.Count>0 do begin
    CurFile:=TFOWFile(FScanFiles.FindLowest.Data);
    ScanFile(CurFile);
    if Now-StartTime>=MaxScanTime then
      break;
  end;
end;

procedure TFindOverloadsWorker.ScanFile(AFile: TFOWFile);
var
  Tool: TCodeTool;
  Filename: String;
  MainFile: TFOWFile;
  Code: TCodeBuffer;
begin
  FScanFiles.Remove(AFile);
  if AFile.Scanned then exit;
  AFile.Scanned:=true;
  //DebugLn(['TFindOverloadsWorker.ScanFile File=',AFile.Filename]);
  // get codetool
  Filename:=TrimFilename(AFile.Filename);
  Code:=CodeToolBoss.LoadFile(Filename,true,false);
  if Code=nil then begin
    DebugLn(['TFindOverloadsWorker.ScanFile file not readable: ',Filename]);
    exit;
  end;
  Tool:=TCodeTool(CodeToolBoss.GetCodeToolForSource(Code,true,false));
  if Tool=nil then begin
    DebugLn(['TFindOverloadsWorker.ScanFile file not in a unit: ',Filename]);
    exit;
  end;
  // check if AFile is an include file
  Filename:=Tool.MainFilename;
  MainFile:=FindFile(Filename);
  // get unit
  if MainFile=nil then begin
    MainFile:=TFOWFile.Create(Filename);
    FFiles.Add(MainFile);
  end;
  if (MainFile<>AFile) and MainFile.Scanned then begin
    //DebugLn(['TFindOverloadsWorker.ScanFile already scanned: ',Filename]);
    exit;
  end;
  // scan unit
  FScanFiles.Remove(MainFile);
  MainFile.Scanned:=true;
  if not AFile.OnlyInterface then
    MainFile.OnlyInterface:=false;
  //DebugLn(['TFindOverloadsWorker.ScanFile scanning: ',Tool.MainFilename]);
  Graph.ScanToolForIdentifier(Tool,MainFile.OnlyInterface);
end;

procedure TFindOverloadsWorker.CollectStartSource;
var
  Filename: String;
  aFile: TFOWFile;
begin
  Filename:=Graph.StartCode.Filename;
  aFile:=FindFile(Filename);
  if aFile=nil then begin
    aFile:=TFOWFile.Create(Filename);
    aFile.OnlyInterface:=false;
    FFiles.Add(aFile);
    FScanFiles.Add(aFile);
  end;
end;

function TFindOverloadsWorker.AddFileToScan(const Filename: string;
  CheckExtension: boolean): TFOWFile;
begin
  if CheckExtension and (not FilenameIsPascalSource(Filename)) then
    exit;
  Result:=FindFile(Filename);
  if Result<>nil then exit;
  Result:=TFOWFile.Create(Filename);
  FFiles.Add(Result);
  FScanFiles.Add(Result);
end;

function TFindOverloadsWorker.FindFile(const Filename: string): TFOWFile;
var
  AVLNode: TAvgLvlTreeNode;
begin
  AVLNode:=FFiles.FindKey(Pointer(Filename),@CompareFilenameWithFOWFile);
  if AVLNode<>nil then
    Result:=TFOWFile(AVLNode.Data)
  else
    Result:=nil;
end;

constructor TFindOverloadsWorker.Create;
begin
  Scopes:=[fosProject,fosPackages];
  FFiles:=TAvgLvlTree.Create(TListSortCompare(@CompareFOWFiles));
  FScanFiles:=TAvgLvlTree.Create(TListSortCompare(@CompareFOWFiles));
  FStagePosMax:=100;
end;

destructor TFindOverloadsWorker.Destroy;
begin
  Clear;
  FreeAndNil(FFiles);
  FreeAndNil(FScanFiles);
  inherited Destroy;
end;

procedure TFindOverloadsWorker.Clear;
begin
  FFiles.FreeAndClear;
  FScanFiles.Clear;
  FStageTitle:='Finished';
  FStagePosition:=0;
  FStagePosMax:=100;
  StartSourceScanned:=false;
end;

procedure TFindOverloadsWorker.Work;
begin
  DebugLn(['TFindOverloadsWorker.Work START']);
  if FScanFiles.Count>0 then begin
    // scan files
    ScanSomeFiles;
  end
  else if not StartSourceScanned then
  begin
    StageTitle:='Scanning start source ...';
    StagePosition:=10;
    StartSourceScanned:=true;
    CollectStartSource;
  end
  else if (fosProject in Scopes) and not (fosProject in CompletedScopes) then
  begin
    // collect project files
    StageTitle:='Scanning project ...';
    StagePosition:=20;
    CollectProjectFiles;
  end
  else if (fosPackages in Scopes) and not (fosPackages in CompletedScopes) then
  begin
    // collect package files
    StageTitle:='Scanning packages ...';
    StagePosition:=40;
    CollectPackageFiles;
  end
  else if (fosOtherSources in Scopes) and not (fosOtherSources in CompletedScopes)
  then begin
    // collect other sources
    StageTitle:='Scanning other sources ...';
    StagePosition:=60;
    CollectOtherSourceFiles;
  end else begin
    StageTitle:='Finished';
    StagePosition:=StagePosMax;
    Graph.ComputeShortestPaths;
  end;
  DebugLn(['TFindOverloadsWorker.Work END ',StageTitle,' ',StagePosition,'/',StagePosMax]);
end;

function TFindOverloadsWorker.Done: boolean;
begin
  Result:=(Scopes-CompletedScopes=[]) and (FScanFiles.Count=0);
end;

procedure TFindOverloadsWorker.StopSearching;
begin
  CompletedScopes:=Scopes;
  FScanFiles.Clear;
  Graph.ComputeShortestPaths;
end;

end.