This file is indexed.

/usr/lib/lazarus/0.9.30.4/ide/fpdocselectlink.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
{
 ***************************************************************************
 *                                                                         *
 *   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:
    Dialog used by the fpdoc editor to create a link.
}
unit FPDocSelectLink;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, StdCtrls, ButtonPanel, FileUtil, LCLType, AvgLvlTree,
  {$IFDEF NewXMLCfg}
  Laz2_DOM,
  {$ELSE}
  Laz_DOM,
  {$ENDIF}
  PackageIntf, ProjectIntf,
  CodeHelp, LazarusIDEStrConsts, PackageSystem, PackageDefs;

type

  { TFPDocLinkCompletionItem }

  TFPDocLinkCompletionItem = class
  public
    Text: string;
    Description: string;
    constructor Create(const AText, ADescription: string);
  end;

  { TFPDocLinkCompletionList }

  TFPDocLinkCompletionList = class
  private
    FBGColor: TColor;
    FItemHeight: integer;
    FItems: TFPList; // list of TFPDocLinkCompletionItem
    FPrefix: string;
    FSelected: integer;
    FSelectedBGColor: TColor;
    FSelectedTextColor: TColor;
    FSorted: Boolean;
    FTextColor: TColor;
    FTop: integer;
    FVisibleItems: integer;
    FTree: TAvgLvlTree; // tree of TFPDocLinkCompletionItem
    function GetCount: integer;
    function GetItems(Index: integer): TFPDocLinkCompletionItem;
    procedure SetSorted(const AValue: Boolean);
    procedure SetTop(const AValue: integer);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear;
    procedure Sort;
    procedure AddPackage(Pkg: TLazPackage);
    procedure Add(Identifier, Description: string);
    procedure AddIdentifier(Identifier: string);
    procedure Draw(Canvas: TCanvas; Width, Height: integer);
    property Count: integer read GetCount;
    property Sorted: Boolean read FSorted write SetSorted;
    property Items[Index: integer]: TFPDocLinkCompletionItem read GetItems;
    property ItemHeight: integer read FItemHeight write FItemHeight;// pixel per item
    property VisibleItems: integer read FVisibleItems write FVisibleItems;// visible lines
    property Top: integer read FTop write SetTop;
    property Selected: integer read FSelected write FSelected;
    property Prefix: string read FPrefix write FPrefix;
    property BGColor: TColor read FBGColor write FBGColor;
    property TextColor: TColor read FTextColor write FTextColor;
    property SelectedBGColor: TColor read FSelectedBGColor write FSelectedBGColor;
    property SelectedTextColor: TColor read FSelectedTextColor write FSelectedTextColor;
  end;

  { TFPDocLinkEditorDlg }

  TFPDocLinkEditorDlg = class(TForm)
    ButtonPanel1: TButtonPanel;
    CompletionBox: TPaintBox;
    TitleEdit: TEdit;
    TitleLabel: TLabel;
    LinkEdit: TEdit;
    LinkLabel: TLabel;
    procedure CompletionBoxMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure CompletionBoxPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure LinkEditChange(Sender: TObject);
    procedure LinkEditKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
      );
    procedure LinkEditUTF8KeyPress(Sender: TObject; var UTF8Key: TUTF8Char);
  private
    FStartFPDocFile: TLazFPDocFile;
    fItems: TFPDocLinkCompletionList;
    FSourceFilename: string;
    FStartModuleOwner: TObject;
    fUpdatingItems: boolean;
    function GetLink: string;
    function GetLinkTitle: string;
    procedure SetStartFPDocFile(const AValue: TLazFPDocFile);
    procedure SetLink(const AValue: string);
    procedure SetLinkTitle(const AValue: string);
    procedure SetSourceFilename(const AValue: string);
    procedure SetStartModuleOwner(const AValue: TObject);
    procedure UpdateCompletionBox;
    procedure AddPackagesToCompletion(Prefix: string);
    procedure AddModuleUnits(ModuleOwner: TObject; Prefix: string);
    procedure AddProjectUnits(AProject: TLazProject; Prefix: string);
    procedure AddPackageUnits(APackage: TLazPackage; Prefix: string);
    procedure AddIdentifiers(ModuleOwner: TObject; FPDocFile: TLazFPDocFile;
                             Prefix: string);
    procedure AddSubIdentifiers(Path: string);
  public
    procedure SetLinkAndContext(const ASrcFilename, ATitle, ALink: string;
                      ADocFile: TLazFPDocFile);
    property SourceFilename: string read FSourceFilename write SetSourceFilename;
    property LinkTitle: string read GetLinkTitle write SetLinkTitle;
    property Link: string read GetLink write SetLink;
    property StartFPDocFile: TLazFPDocFile read FStartFPDocFile write SetStartFPDocFile;
    property StartModuleOwner: TObject read FStartModuleOwner write SetStartModuleOwner;
  end;

function ShowFPDocLinkEditorDialog(SrcFilename: string;
  StartFPDocFile: TLazFPDocFile; out Link, LinkTitle: string): TModalResult;

function CompareFPDocLinkCompletionItem(Data1, Data2: Pointer): integer;
function ComparePathWithFPDocLinkCompletionItem(AnsiString1, Data2: Pointer): integer;

implementation

{$R *.lfm}

function ShowFPDocLinkEditorDialog(SrcFilename: string;
  StartFPDocFile: TLazFPDocFile; out Link, LinkTitle: string): TModalResult;
var
  FPDocLinkEditorDlg: TFPDocLinkEditorDlg;
begin
  Link:='';
  LinkTitle:='';
  FPDocLinkEditorDlg:=TFPDocLinkEditorDlg.Create(nil);
  try
    FPDocLinkEditorDlg.SetLinkAndContext(SrcFilename,LinkTitle,Link,StartFPDocFile);
    Result:=FPDocLinkEditorDlg.ShowModal;
    if Result=mrOk then begin
      Link:=FPDocLinkEditorDlg.Link;
      LinkTitle:=FPDocLinkEditorDlg.LinkTitle;
    end;
  finally
    FPDocLinkEditorDlg.Release;
  end;
end;

function CompareFPDocLinkCompletionItem(Data1, Data2: Pointer): integer;
var
  Item1: TFPDocLinkCompletionItem absolute Data1;
  Item2: TFPDocLinkCompletionItem absolute Data2;
begin
  Result:=SysUtils.CompareText(Item1.Text,Item2.Text);
end;

function ComparePathWithFPDocLinkCompletionItem(AnsiString1, Data2: Pointer
  ): integer;
var
  s: String;
  Item: TFPDocLinkCompletionItem absolute Data2;
begin
  s:=AnsiString(AnsiString1);
  Result:=SysUtils.CompareText(s,Item.Text);
end;

{ TFPDocLinkEditorDlg }

procedure TFPDocLinkEditorDlg.FormCreate(Sender: TObject);
begin
  Caption:=lisChooseAFPDocLink;
  LinkLabel.Caption:=lisLinkTarget;
  LinkLabel.Hint:=Format(lisExamplesIdentifierTMyEnumEnumUnitnameIdentifierPac,
    [#13, #13, #13, #13]);
  TitleLabel.Caption:=lisTitleLeaveEmptyForDefault;
  
  LinkEdit.Text:='';
  TitleEdit.Text:='';

  // disable return key
  ButtonPanel1.OKButton.Default:=false;

  FItems:=TFPDocLinkCompletionList.Create;

  ActiveControl:=LinkEdit;
end;

procedure TFPDocLinkEditorDlg.FormDestroy(Sender: TObject);
begin
  FreeAndNil(fItems);
end;

procedure TFPDocLinkEditorDlg.CompletionBoxPaint(Sender: TObject);
begin
  fItems.BGColor:=clWindow;
  fItems.TextColor:=clWindowText;
  fItems.SelectedBGColor:=clHighlight;
  fItems.SelectedTextColor:=clHighlightText;
  fItems.ItemHeight:=CompletionBox.Canvas.TextHeight('ABCTWSMgqp')+4;
  fItems.VisibleItems:=CompletionBox.ClientHeight div fItems.ItemHeight;
  fItems.Draw(CompletionBox.Canvas,
              CompletionBox.ClientWidth,CompletionBox.ClientHeight);
end;

procedure TFPDocLinkEditorDlg.CompletionBoxMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  Line: Integer;
begin
  if fItems.ItemHeight<=0 then exit;
  Line:=fItems.Top+(Y div fItems.ItemHeight);
  if Line>=fItems.Count then exit;
  Link:=FItems.Items[Line].Text;
  LinkEdit.SelStart:=length(LinkEdit.Text);
end;

procedure TFPDocLinkEditorDlg.LinkEditChange(Sender: TObject);
begin
  //DebugLn(['TFPDocLinkEditorDlg.LinkEditChange "',LinkEdit.Text,'"']);
  Link:=LinkEdit.Text;
end;

procedure TFPDocLinkEditorDlg.LinkEditKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  Handled: Boolean;
begin
  if Shift=[] then begin
    Handled:=true;
    case Key of
    VK_UP:
      if FItems.Selected>0 then begin
        FItems.Selected:=FItems.Selected-1;
        if FItems.Top>fItems.Selected then
          FItems.Top:=fItems.Selected;
        CompletionBox.Invalidate;
      end;
    VK_DOWN:
      if FItems.Selected<fItems.Count-1 then begin
        FItems.Selected:=FItems.Selected+1;
        if FItems.Selected>=fItems.Top+fItems.VisibleItems then
          FItems.Top:=FItems.Top+1;
        CompletionBox.Invalidate;
      end;
    VK_RETURN:
      if (FItems.Selected>=0) and (FItems.Selected<fItems.Count) then begin
        Link:=FItems.Items[fItems.Selected].Text;
        LinkEdit.SelStart:=length(LinkEdit.Text);
      end;
    else
      Handled:=false;
    end;
    if Handled then Key:=VK_UNKNOWN;
  end;
end;

procedure TFPDocLinkEditorDlg.LinkEditUTF8KeyPress(Sender: TObject;
  var UTF8Key: TUTF8Char);
begin
end;

procedure TFPDocLinkEditorDlg.SetSourceFilename(const AValue: string);
var
  Owners: TFPList;
  i: Integer;
begin
  if FSourceFilename=AValue then exit;
  FSourceFilename:=AValue;
  FStartModuleOwner:=nil;
  Owners:=PackageEditingInterface.GetPossibleOwnersOfUnit(FSourceFilename,
    [piosfIncludeSourceDirectories]);
  if Owners=nil then exit;
  try
    for i:=0 to Owners.Count-1 do begin
      if TObject(Owners[i]) is TLazProject then begin
        FStartModuleOwner:=TLazProject(Owners[i]);
      end else if TObject(Owners[i]) is TLazPackage then begin
        if FStartModuleOwner=nil then
          FStartModuleOwner:=TLazPackage(Owners[i]);
      end;
    end;
  finally
    Owners.Free;
  end;
end;

procedure TFPDocLinkEditorDlg.SetStartModuleOwner(const AValue: TObject);
begin
  if FStartModuleOwner=AValue then exit;
  FStartModuleOwner:=AValue;
end;

procedure TFPDocLinkEditorDlg.UpdateCompletionBox;
{
  ToDo:
  empty  : show all packages, all units of current project/package and all identifiers of unit
  #l     : show all packages beginning with the letter l
  #lcl.  : show all units of package lcl
  f      : show all units and all identifiers beginning with the letter f
  forms. : show all identifiers of unit forms and all sub identifiers of identifier forms

  forms.tcontrol.        : show all sub identifiers of identifier tcontrol
  #lcl.forms.            : same as above
  #lcl.forms.tcontrol.   : same as above
}
var
  l: String;
begin
  if FItems=nil then exit;
  if fUpdatingItems then exit;
  fUpdatingItems:=true;
  try
    fItems.Clear;
    l:=FItems.Prefix;
    //DebugLn(['TFPDocLinkEditorDlg.UpdateCompletionBox Prefix="',l,'"']);
    AddSubIdentifiers(l);
    CompletionBox.Invalidate;
  finally
    fUpdatingItems:=false;
  end;
end;

procedure TFPDocLinkEditorDlg.AddPackagesToCompletion(Prefix: string);
var
  i: Integer;
  Pkg: TLazPackage;
begin
  for i:=0 to PackageGraph.Count-1 do begin
    Pkg:=PackageGraph.Packages[i];
    if Pkg.LazDocPaths='' then continue;
    if (SysUtils.CompareText(Prefix,copy(Pkg.Name,1,length(Prefix)))=0) then
      fItems.AddPackage(Pkg);
  end;
end;

procedure TFPDocLinkEditorDlg.AddModuleUnits(ModuleOwner: TObject;
  Prefix: string);
var
  AProject: TLazProject;
  APackage: TLazPackage;
begin
  DebugLn(['TFPDocLinkEditorDlg.AddModuleUnits ',DbgSName(ModuleOwner)]);
  if ModuleOwner=nil then exit;
  if ModuleOwner is TLazProject then begin
    AProject:=TLazProject(ModuleOwner);
    AddProjectUnits(AProject,Prefix);
  end else if ModuleOwner is TLazPackage then begin
    APackage:=TLazPackage(ModuleOwner);
    AddPackageUnits(APackage,Prefix);
  end;
end;

procedure TFPDocLinkEditorDlg.AddProjectUnits(AProject: TLazProject;
  Prefix: string);
var
  i: Integer;
  Filename: String;
  ProjFile: TLazProjectFile;
  Identifier: String;
begin
  DebugLn(['TFPDocLinkEditorDlg.AddProjectUnits ']);
  for i:=0 to AProject.FileCount-1 do begin
    ProjFile:=AProject.Files[i];
    if ProjFile.IsPartOfProject then begin
      Filename:=ProjFile.Filename;
      if FilenameIsPascalUnit(Filename) then begin
        Filename:=ExtractFileNameOnly(Filename);
        DebugLn(['TFPDocLinkEditorDlg.AddProjectUnits ',Prefix,' ',Filename]);
        if (CompareFilenames(Prefix,copy(Filename,1,length(Prefix)))=0) then
        begin
          Identifier:=ExtractFileNameOnly(ProjFile.Filename);
          if AProject<>StartModuleOwner then
            Identifier:='#'+ExtractFileNameOnly(AProject.ProjectInfoFile)+'.'+Identifier;
          fItems.Add(Identifier, lisProjectUnit);
        end;
      end;
    end;
  end;
end;

procedure TFPDocLinkEditorDlg.AddPackageUnits(APackage: TLazPackage;
  Prefix: string);
var
  i: Integer;
  PkgFile: TPkgFile;
  Filename: String;
  Identifier: String;
begin
  for i:=0 to APackage.FileCount-1 do begin
    PkgFile:=APackage.Files[i];
    if FilenameIsPascalUnit(PkgFile.Filename) then begin
      Filename:=PkgFile.Filename;
      if FilenameIsPascalUnit(Filename) then begin
        Filename:=ExtractFileNameOnly(Filename);
        if (CompareFilenames(Prefix,copy(Filename,1,length(Prefix)))=0) then
        begin
          Identifier:=ExtractFileNameOnly(Filename);
          if APackage<>StartModuleOwner then
            Identifier:='#'+APackage.Name+'.'+Identifier;
          fItems.Add(Identifier, lisPackageUnit);
        end;
      end;
    end;
  end;
end;

procedure TFPDocLinkEditorDlg.AddIdentifiers(ModuleOwner: TObject;
  FPDocFile: TLazFPDocFile; Prefix: string);
var
  DOMNode: TDOMNode;
  ElementName: String;
  ModuleName: String;
begin
  if FPDocFile=nil then exit;
  //DebugLn(['TFPDocLinkEditorDlg.AddIdentifiers ',FPDocFile.Filename,' Prefix=',Prefix]);
  DOMNode:=FPDocFile.GetFirstElement;
  while DOMNode<>nil do begin
    if (DOMNode is TDomElement) then begin
      //DebugLn(['TFPDocLinkEditorDlg.AddIdentifiers ',DbgSName(DOMNode)]);
      ElementName:=TDomElement(DOMNode).GetAttribute('name');
      if (SysUtils.CompareText(Prefix,copy(ElementName,1,length(Prefix)))=0)
      then begin
        // same prefix
        if (FPDocFile<>nil) and (FPDocFile<>StartFPDocFile) then begin
          // different unit
          ElementName:=ExtractFileNameOnly(FPDocFile.Filename)+'.'+ElementName;
        end;
        if (ModuleOwner<>nil) and (ModuleOwner<>StartModuleOwner) then begin
          // different module
          if ModuleOwner is TLazProject then begin
            ModuleName:=lowercase(ExtractFileNameOnly(TLazProject(ModuleOwner).ProjectInfoFile));
          end else if ModuleOwner is TLazPackage then begin
            ModuleName:=TLazPackage(ModuleOwner).Name;
          end;
          if ModuleName<>'' then
            ElementName:='#'+ModuleName+'.'+ElementName
          else
            ElementName:='';
        end;
        if ElementName<>'' then
          FItems.AddIdentifier(ElementName);
      end;
    end;
    DOMNode:=DOMNode.NextSibling;
  end;
end;

procedure TFPDocLinkEditorDlg.AddSubIdentifiers(Path: string);
var
  p: LongInt;
  Prefix: String;
  ModuleOwner: TObject;
  FPDocFile: TLazFPDocFile;
  DOMNode: TDOMNode;
  InvalidPath: integer;
  CacheWasUsed: boolean;
  DOMElement: TDOMElement;
begin
  p:=length(Path);
  while (p>0) and (Path[p]<>'.') do dec(p);
  if p<1 then begin
    // empty  : show all packages, all units of current project/package and all identifiers of unit
    // #l     : show all packages beginning with the letter l
    // f      : show all units and all identifiers beginning with the letter f
    if (Path='') or (Path[1]='#') then
      AddPackagesToCompletion(copy(Path,2,length(Path)));
    if (Path='') or (Path[1]<>'#') then begin
      AddModuleUnits(StartModuleOwner,Path);
      AddIdentifiers(StartModuleOwner,StartFPDocFile,Path);
    end;
  end else begin
    // sub identifier
    //DebugLn(['TFPDocLinkEditorDlg.AddSubIdentifiers searching context ..']);
    CodeHelpBoss.GetLinkedFPDocNode(StartFPDocFile,nil,Path,
      [chofUpdateFromDisk,chofQuiet],ModuleOwner,FPDocFile,DOMNode,InvalidPath,
      CacheWasUsed);
    // get rest path as filter
    Prefix:=copy(Path,InvalidPath,length(Path));
    if (Prefix<>'') and (Prefix[1]='.') then System.Delete(Prefix,1,1);
    //DebugLn(['TFPDocLinkEditorDlg.AddSubIdentifiers context found: ModuleOwner=',DbgSName(ModuleOwner),' FPDocFile=',FPDocFile<>nil,' DOMNode=',DOMNode<>nil,' invalid path="',Prefix,'"']);
    if DOMNode is TDomElement then begin
      // show elements of unit, beginning with prefix
      DOMElement:=TDomElement(DOMNode);
      Prefix:=DOMElement.GetAttribute('name')+'.'+Prefix;
      AddIdentifiers(ModuleOwner,FPDocFile,Prefix);
    end else if FPDocFile<>nil then begin
      // show elements of unit, beginning with prefix
      AddIdentifiers(ModuleOwner,FPDocFile,Prefix);
    end else if ModuleOwner<>nil then begin
      // show units of module, beginning with first part of prefix
      p:=1;
      while (p<=length(Prefix)) and (Prefix[p]<>'.') do inc(p);
      Prefix:=copy(Prefix,1,p-1);
      if ModuleOwner is TLazPackage then
        AddPackageUnits(TLazPackage(ModuleOwner),Prefix)
      else if ModuleOwner is TLazProject then
        AddProjectUnits(TLazProject(ModuleOwner),Prefix);
    end;
  end;
end;

procedure TFPDocLinkEditorDlg.SetLinkAndContext(const ASrcFilename, ATitle,
  ALink: string; ADocFile: TLazFPDocFile);
begin
  StartFPDocFile:=ADocFile;
  fSourceFilename:=ASrcFilename;
  LinkTitle:=ATitle;
  Link:=ALink;
  UpdateCompletionBox;
end;

function TFPDocLinkEditorDlg.GetLinkTitle: string;
begin
  Result:=TitleEdit.Text;
end;

procedure TFPDocLinkEditorDlg.SetStartFPDocFile(const AValue: TLazFPDocFile);
begin
  if FStartFPDocFile=AValue then exit;
  FStartFPDocFile:=AValue;
  FStartModuleOwner:=CodeHelpBoss.FindModuleOwner(FStartFPDocFile);
end;

function TFPDocLinkEditorDlg.GetLink: string;
begin
  Result:=LinkEdit.Text;
end;

procedure TFPDocLinkEditorDlg.SetLink(const AValue: string);
begin
  if FItems=nil then exit;
  if AValue=fItems.Prefix then exit;
  fItems.Prefix:=AValue;
  LinkEdit.Text:=AValue;
  UpdateCompletionBox;
end;

procedure TFPDocLinkEditorDlg.SetLinkTitle(const AValue: string);
begin
  TitleEdit.Text:=AValue;
end;

{ TFPDocLinkCompletionList }

function TFPDocLinkCompletionList.GetCount: integer;
begin
  Result:=FItems.Count;
end;

function TFPDocLinkCompletionList.GetItems(Index: integer
  ): TFPDocLinkCompletionItem;
begin
  Result:=TFPDocLinkCompletionItem(FItems[Index]);
end;

procedure TFPDocLinkCompletionList.SetSorted(const AValue: Boolean);
begin
  if FSorted=AValue then exit;
  if AValue then
    Sort
  else
    FSorted:=false;
end;

procedure TFPDocLinkCompletionList.SetTop(const AValue: integer);
begin
  if FTop=AValue then exit;
  FTop:=AValue;
end;

constructor TFPDocLinkCompletionList.Create;
begin
  FItems:=TFPList.Create;
  FTree:=TAvgLvlTree.Create(@CompareFPDocLinkCompletionItem);
end;

destructor TFPDocLinkCompletionList.Destroy;
begin
  Clear;
  FreeAndNil(FItems);
  FreeAndNil(FTree);
  inherited Destroy;
end;

procedure TFPDocLinkCompletionList.Clear;
var
  i: Integer;
begin
  FTree.Clear;
  for i:=0 to FItems.Count-1 do TObject(FItems[i]).Free;
  FItems.Clear;
  FSelected:=0;
  FTop:=0;
  FSorted:=true;
end;

procedure TFPDocLinkCompletionList.Sort;
var
  Node: TAvgLvlTreeNode;
  i: Integer;
begin
  if FSorted then exit;
  Node:=FTree.FindLowest;
  i:=0;
  while Node<>nil do begin
    FItems[i]:=Node.Data;
    inc(i);
    Node:=FTree.FindSuccessor(Node);
  end;
  FSorted:=true;
end;

procedure TFPDocLinkCompletionList.AddPackage(Pkg: TLazPackage);
begin
  Add('#'+Pkg.Name, Format(lisPackage2, [Pkg.IDAsString]));
end;

procedure TFPDocLinkCompletionList.Add(Identifier, Description: string);
var
  Item: TFPDocLinkCompletionItem;
begin
  if FTree.FindKey(Pointer(Identifier),
                   @ComparePathWithFPDocLinkCompletionItem)<>nil
  then exit;
  Item:=TFPDocLinkCompletionItem.Create(Identifier,Description);
  FItems.Add(Item);
  FTree.Add(Item);
  FSorted:=false;
end;

procedure TFPDocLinkCompletionList.AddIdentifier(Identifier: string);
begin
  Add(Identifier, lisIdentifier);
end;

procedure TFPDocLinkCompletionList.Draw(Canvas: TCanvas; Width, Height: integer);
var
  i: LongInt;
  y: Integer;
  dy: LongInt;
  Item: TFPDocLinkCompletionItem;
  s: String;
begin
  //DebugLn(['TFPDocLinkCompletionList.Draw ',Width,' ',Height,' Count=',Count]);
  Sorted:=true;
  i:=Top;
  y:=0;
  dy:=ItemHeight;
  while (y<Height) and (i<Count) do begin
    Item:=Items[i];
    Canvas.Brush.Style:=bsSolid;
    Canvas.Font.Style:=[];
    if i=Selected then begin
      Canvas.Brush.Color:=SelectedBGColor;
      Canvas.Font.Color:=SelectedTextColor;
    end else begin
      Canvas.Brush.Color:=BGColor;
      Canvas.Font.Color:=TextColor;
    end;
    Canvas.FillRect(0,y,Width,y+dy);
    s:=Item.Text;
    Canvas.TextOut(2,y+2,s);
    inc(y,dy);
    inc(i);
  end;
  if y<Height then begin
    Canvas.Brush.Color:=BGColor;
    Canvas.FillRect(0,y,Width,Height);
  end;
end;

{ TFPDocLinkCompletionItem }

constructor TFPDocLinkCompletionItem.Create(const AText, ADescription: string);
begin
  Text:=AText;
  Description:=ADescription;
end;

end.