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 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
|
{
***************************************************************************
* *
* 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
Options dialog and methods for finding and renaming identifier references.
}
unit FindRenameIdentifier;
{$mode objfpc}{$H+}
interface
uses
// RTL + FCL
Classes, SysUtils, Laz_AVL_Tree,
// LCL
LCLProc, Forms, Controls, Dialogs, StdCtrls, ExtCtrls, ComCtrls, ButtonPanel,
LclIntf,
// CodeTools
FileProcs, CTUnitGraph, CodeTree, CodeCache, CodeToolManager, BasicCodeTools,
// LazUtils
LazFileUtils, LazFileCache, laz2_DOM, LazStringUtils, AvgLvlTree,
// IdeIntf
LazIDEIntf, IDEWindowIntf, SrcEditorIntf, PackageIntf, IDEDialogs,
// IDE
LazarusIDEStrConsts, IDEProcs, MiscOptions, DialogProcs,
InputHistory, SearchResultView, CodeHelp, TransferMacros;
type
{ TFindRenameIdentifierDialog }
TFindRenameIdentifierDialog = class(TForm)
ButtonPanel1: TButtonPanel;
ShowResultCheckBox: TCheckBox;
CurrentGroupBox: TGroupBox;
CurrentListBox: TListBox;
ExtraFilesEdit: TEdit;
ExtraFilesGroupBox: TGroupBox;
NewEdit: TEdit;
NewGroupBox: TGroupBox;
RenameCheckBox: TCheckBox;
ScopeCommentsCheckBox: TCheckBox;
ScopeGroupBox: TGroupBox;
ScopeRadioGroup: TRadioGroup;
procedure FindOrRenameButtonClick(Sender: TObject);
procedure FindRenameIdentifierDialogClose(Sender: TObject;
var {%H-}CloseAction: TCloseAction);
procedure FindRenameIdentifierDialogCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure HelpButtonClick(Sender: TObject);
procedure RenameCheckBoxChange(Sender: TObject);
private
FAllowRename: boolean;
FIdentifierFilename: string;
FIdentifierPosition: TPoint;
FIsPrivate: boolean;
procedure SetAllowRename(const AValue: boolean);
procedure SetIsPrivate(const AValue: boolean);
procedure UpdateRename;
public
procedure LoadFromConfig;
procedure SaveToConfig;
procedure LoadFromOptions(Options: TFindRenameIdentifierOptions);
procedure SaveToOptions(Options: TFindRenameIdentifierOptions);
procedure SetIdentifier(const NewIdentifierFilename: string;
const NewIdentifierPosition: TPoint);
property IdentifierFilename: string read FIdentifierFilename;
property IdentifierPosition: TPoint read FIdentifierPosition;
property AllowRename: boolean read FAllowRename write SetAllowRename;
property IsPrivate: boolean read FIsPrivate write SetIsPrivate;
end;
procedure CleanUpFileList(Files: TStringList);
function ShowFindRenameIdentifierDialog(const Filename: string;
const Position: TPoint;
AllowRename: boolean; // allow user to disable/enable rename
SetRenameActive: boolean; // check rename
Options: TFindRenameIdentifierOptions): TModalResult;
function DoFindRenameIdentifier(
AllowRename: boolean; // allow user to disable/enable rename
SetRenameActive: boolean; // check rename
Options: TFindRenameIdentifierOptions): TModalResult;
function GatherIdentifierReferences(Files: TStringList;
DeclarationCode: TCodeBuffer; const DeclarationCaretXY: TPoint;
SearchInComments: boolean;
var TreeOfPCodeXYPosition: TAVLTree): TModalResult;
function GatherUnitReferences(Files: TStringList;
UnitCode: TCodeBuffer; SearchInComments, IgnoreErrors, IgnoreMissingFiles: boolean;
var TreeOfPCodeXYPosition: TAVLTree): TModalResult;
function ShowIdentifierReferences(
DeclarationCode: TCodeBuffer; const DeclarationCaretXY: TPoint;
TreeOfPCodeXYPosition: TAVLTree): TModalResult;
procedure AddReferencesToResultView(DeclarationCode: TCodeBuffer;
const DeclarationCaretXY: TPoint;
TreeOfPCodeXYPosition: TAVLTree; ClearItems: boolean; SearchPageIndex: integer);
function GatherFPDocReferencesForPascalFiles(PascalFiles: TStringList;
DeclarationCode: TCodeBuffer; const DeclarationCaretXY: TPoint;
var ListOfLazFPDocNode: TFPList): TModalResult;
function GatherReferencesInFPDocFile(
const OldPackageName, OldModuleName, OldElementName: string;
const FPDocFilename: string;
var ListOfLazFPDocNode: TFPList): TModalResult;
implementation
{$R *.lfm}
procedure CleanUpFileList(Files: TStringList);
var
i: Integer;
begin
// sort files
Files.Sort;
// remove doubles
i:=0;
while i<=Files.Count-2 do begin
while (i<=Files.Count-2) and (CompareFilenames(Files[i],Files[i+1])=0) do
Files.Delete(i+1);
inc(i);
end;
// remove non files
for i:=Files.Count-1 downto 0 do
if ExtractFilename(Files[i])='' then begin
debugln(['Note: (lazarus) [FindRenameIdentifier.CleanUpFileList] invalid file "',Files[i],'"']);
Files.Delete(i);
end;
end;
function ShowFindRenameIdentifierDialog(const Filename: string;
const Position: TPoint; AllowRename: boolean; SetRenameActive: boolean;
Options: TFindRenameIdentifierOptions): TModalResult;
var
FindRenameIdentifierDialog: TFindRenameIdentifierDialog;
begin
FindRenameIdentifierDialog:=TFindRenameIdentifierDialog.Create(nil);
try
FindRenameIdentifierDialog.LoadFromConfig;
FindRenameIdentifierDialog.SetIdentifier(Filename,Position);
FindRenameIdentifierDialog.AllowRename:=AllowRename;
FindRenameIdentifierDialog.RenameCheckBox.Checked:=SetRenameActive and AllowRename;
if Options<>nil then
FindRenameIdentifierDialog.ShowResultCheckBox.Checked:=Options.RenameShowResult and AllowRename;
Result:=FindRenameIdentifierDialog.ShowModal;
if Result=mrOk then
if Options<>nil then
FindRenameIdentifierDialog.SaveToOptions(Options);
finally
FindRenameIdentifierDialog.Free;
end;
end;
function DoFindRenameIdentifier(AllowRename: boolean; SetRenameActive: boolean;
Options: TFindRenameIdentifierOptions): TModalResult;
// TODO: replace Files: TStringsList with a AVL tree
function AddExtraFiles(Files: TStrings): TModalResult;
var
i: Integer;
CurFileMask: string;
FileInfo: TSearchRec;
CurDirectory: String;
CurFilename: String;
OnlyPascalSources: Boolean;
begin
Result:=mrCancel;
if (Options.ExtraFiles<>nil) then begin
for i:=0 to Options.ExtraFiles.Count-1 do begin
CurFileMask:=Options.ExtraFiles[i];
if not GlobalMacroList.SubstituteStr(CurFileMask) then exit;
CurFileMask:=ChompPathDelim(CurFileMask);
if not FilenameIsAbsolute(CurFileMask) then begin
if LazarusIDE.ActiveProject.IsVirtual then continue;
CurFileMask:=AppendPathDelim(LazarusIDE.ActiveProject.Directory+CurFileMask);
end;
CurFileMask:=TrimFilename(CurFileMask);
OnlyPascalSources:=false;
if DirPathExistsCached(CurFileMask) then begin
// a whole directory
OnlyPascalSources:=true;
CurFileMask:=AppendPathDelim(CurFileMask)+AllFilesMask;
end else if FileExistsCached(CurFileMask) then begin
// single file
Files.Add(CurFileMask);
continue;
end else begin
// a mask
end;
if FindFirstUTF8(CurFileMask,faAnyFile,FileInfo)=0
then begin
CurDirectory:=AppendPathDelim(ExtractFilePath(CurFileMask));
repeat
// check if special file
if (FileInfo.Name='.') or (FileInfo.Name='..') or (FileInfo.Name='')
then
continue;
if OnlyPascalSources and not FilenameIsPascalSource(FileInfo.Name)
then
continue;
CurFilename:=CurDirectory+FileInfo.Name;
//debugln(['AddExtraFiles ',CurFilename]);
if FileIsText(CurFilename) then
Files.Add(CurFilename);
until FindNextUTF8(FileInfo)<>0;
end;
FindCloseUTF8(FileInfo);
end;
end;
Result:=mrOk;
end;
var
StartSrcEdit: TSourceEditorInterface;
DeclCode, StartSrcCode: TCodeBuffer;
DeclX, DeclY, DeclTopLine, StartTopLine, i: integer;
LogCaretXY, DeclarationCaretXY: TPoint;
OwnerList: TFPList;
ExtraFiles: TStrings;
Files: TStringList;
Identifier: string;
PascalReferences: TAVLTree;
ListOfLazFPDocNode: TFPList;
CurUnitname: String;
OldChange, Completed: Boolean;
Graph: TUsesGraph;
Node: TAVLTreeNode;
UGUnit: TUGUnit;
begin
Result:=mrCancel;
if not LazarusIDE.BeginCodeTools then exit(mrCancel);
StartSrcEdit:=SourceEditorManagerIntf.ActiveEditor;
StartSrcCode:=TCodeBuffer(StartSrcEdit.CodeToolsBuffer);
StartTopLine:=StartSrcEdit.TopLine;
// find the main declaration
LogCaretXY:=StartSrcEdit.CursorTextXY;
if not CodeToolBoss.FindMainDeclaration(StartSrcCode,
LogCaretXY.X,LogCaretXY.Y,
DeclCode,DeclX,DeclY,DeclTopLine) then
begin
LazarusIDE.DoJumpToCodeToolBossError;
exit(mrCancel);
end;
DeclarationCaretXY:=Point(DeclX,DeclY);
Result:=LazarusIDE.DoOpenFileAndJumpToPos(DeclCode.Filename, DeclarationCaretXY,
DeclTopLine,-1,-1,[ofOnlyIfExists,ofRegularFile,ofDoNotLoadResource]);
if Result<>mrOk then
exit;
CodeToolBoss.GetIdentifierAt(DeclCode,DeclarationCaretXY.X,DeclarationCaretXY.Y,Identifier);
CurUnitname:=ExtractFileNameOnly(DeclCode.Filename);
//debugln('TMainIDE.DoFindRenameIdentifier A DeclarationCaretXY=x=',dbgs(DeclarationCaretXY.X),' y=',dbgs(DeclarationCaretXY.Y));
Files:=nil;
OwnerList:=nil;
PascalReferences:=nil;
ListOfLazFPDocNode:=nil;
try
// let user choose the search scope
Result:=ShowFindRenameIdentifierDialog(DeclCode.Filename,DeclarationCaretXY,
AllowRename,SetRenameActive,nil);
if Result<>mrOk then begin
debugln('Error: (lazarus) DoFindRenameIdentifier failed: user cancelled dialog');
exit;
end;
// create the file list
Files:=TStringList.Create;
Files.Add(DeclCode.Filename);
if CompareFilenames(DeclCode.Filename,StartSrcCode.Filename)<>0 then
Files.Add(DeclCode.Filename);
Options:=MiscellaneousOptions.FindRenameIdentifierOptions;
// add packages, projects
case Options.Scope of
frProject:
begin
OwnerList:=TFPList.Create;
OwnerList.Add(LazarusIDE.ActiveProject);
end;
frOwnerProjectPackage,frAllOpenProjectsAndPackages:
begin
OwnerList:=PackageEditingInterface.GetOwnersOfUnit(StartSrcCode.Filename);
if (OwnerList<>nil) and (OwnerList.Count=0) then
FreeAndNil(OwnerList);
if (OwnerList=nil) then
OwnerList:=PackageEditingInterface.GetPossibleOwnersOfUnit(
StartSrcCode.Filename,[piosfExcludeOwned,piosfIncludeSourceDirectories]);
if (OwnerList<>nil) and (OwnerList.Count=0) then
FreeAndNil(OwnerList);
if (OwnerList<>nil) then begin
if Options.Scope=frAllOpenProjectsAndPackages then begin
PackageEditingInterface.ExtendOwnerListWithUsedByOwners(OwnerList);
ReverseList(OwnerList);
end;
end else begin
// unknown unit -> search everywhere
OwnerList:=TFPList.Create;
OwnerList.Add(LazarusIDE.ActiveProject);
for i:=0 to PackageEditingInterface.GetPackageCount-1 do
OwnerList.Add(PackageEditingInterface.GetPackages(i));
ReverseList(OwnerList);
end;
end;
end;
// get source files of packages and projects
if OwnerList<>nil then begin
// start in all listed files of the package(s)
ExtraFiles:=PackageEditingInterface.GetSourceFilesOfOwners(OwnerList);
if ExtraFiles<>nil then
begin
// parse all used units
Graph:=CodeToolBoss.CreateUsesGraph;
try
for i:=0 to ExtraFiles.Count-1 do
Graph.AddStartUnit(ExtraFiles[i]);
Graph.AddTargetUnit(DeclCode.Filename);
Graph.Parse(true,Completed);
Node:=Graph.FilesTree.FindLowest;
while Node<>nil do begin
UGUnit:=TUGUnit(Node.Data);
Files.Add(UGUnit.Filename);
Node:=Node.Successor;
end;
finally
ExtraFiles.Free;
Graph.Free;
end;
end;
end;
//debugln(['DoFindRenameIdentifier ',Files.Text]);
// add user defined extra files
Result:=AddExtraFiles(Files);
if Result<>mrOk then begin
debugln('Error: (lazarus) DoFindRenameIdentifier unable to add user defined extra files');
exit;
end;
// search pascal source references
Result:=GatherIdentifierReferences(Files,DeclCode,
DeclarationCaretXY,Options.SearchInComments,PascalReferences);
if CodeToolBoss.ErrorMessage<>'' then
LazarusIDE.DoJumpToCodeToolBossError;
if Result<>mrOk then begin
debugln('Error: (lazarus) DoFindRenameIdentifier GatherIdentifierReferences failed');
exit;
end;
{$IFDEF EnableFPDocRename}
// search fpdoc references
Result:=GatherFPDocReferencesForPascalFiles(Files,DeclarationUnitInfo.Source,
DeclarationCaretXY,ListOfLazFPDocNode);
if Result<>mrOk then begin
debugln('Error: (lazarus) DoFindRenameIdentifier GatherFPDocReferences failed');
exit;
end;
{$ENDIF}
// ToDo: search lfm source references
// ToDo: search i18n references
// ToDo: designer references
// rename identifier
if Options.Rename then begin
if CompareIdentifiers(PChar(Identifier),PChar(CurUnitName))=0 then
begin
IDEMessageDialog(srkmecRenameIdentifier,
lisTheIdentifierIsAUnitPleaseUseTheFileSaveAsFunction,
mtInformation,[mbCancel],'');
exit(mrCancel);
end;
OldChange:=LazarusIDE.OpenEditorsOnCodeToolChange;
LazarusIDE.OpenEditorsOnCodeToolChange:=true;
try
if not CodeToolBoss.RenameIdentifier(PascalReferences,
Identifier,Options.RenameTo, DeclCode, @DeclarationCaretXY)
then begin
LazarusIDE.DoJumpToCodeToolBossError;
debugln('Error: (lazarus) DoFindRenameIdentifier unable to commit');
Result:=mrCancel;
exit;
end;
finally
LazarusIDE.OpenEditorsOnCodeToolChange:=OldChange;
end;
if Options.RenameShowResult then
Result:=ShowIdentifierReferences(DeclCode,
DeclarationCaretXY,PascalReferences);
end;
// show result
Result:=mrOk;
if (not Options.Rename) or (not SetRenameActive) then begin
Result:=ShowIdentifierReferences(DeclCode,
DeclarationCaretXY,PascalReferences);
if Result<>mrOk then exit;
end;
finally
Files.Free;
OwnerList.Free;
CodeToolBoss.FreeTreeOfPCodeXYPosition(PascalReferences);
FreeListObjects(ListOfLazFPDocNode,true);
// jump back in source editor
Result:=LazarusIDE.DoOpenFileAndJumpToPos(StartSrcCode.Filename, LogCaretXY,
StartTopLine,-1,-1,[ofOnlyIfExists,ofRegularFile,ofDoNotLoadResource]);
end;
end;
function GatherIdentifierReferences(Files: TStringList;
DeclarationCode: TCodeBuffer; const DeclarationCaretXY: TPoint;
SearchInComments: boolean;
var TreeOfPCodeXYPosition: TAVLTree): TModalResult;
var
i: Integer;
LoadResult: TModalResult;
Code: TCodeBuffer;
ListOfPCodeXYPosition: TFPList;
Cache: TFindIdentifierReferenceCache;
begin
Result:=mrCancel;
ListOfPCodeXYPosition:=nil;
TreeOfPCodeXYPosition:=nil;
Cache:=nil;
try
CleanUpFileList(Files);
// search in every file
for i:=0 to Files.Count-1 do begin
//debugln(['GatherIdentifierReferences ',Files[i]]);
LoadResult:=
LoadCodeBuffer(Code,Files[i],[lbfCheckIfText,lbfUpdateFromDisk,lbfIgnoreMissing],true);
if LoadResult=mrAbort then begin
debugln('GatherIdentifierReferences unable to load "',Files[i],'"');
exit;
end;
if LoadResult<>mrOk then continue;
// search references
CodeToolBoss.FreeListOfPCodeXYPosition(ListOfPCodeXYPosition);
if not CodeToolBoss.FindReferences(
DeclarationCode,DeclarationCaretXY.X,DeclarationCaretXY.Y,
Code, not SearchInComments, ListOfPCodeXYPosition, Cache) then
begin
debugln('GatherIdentifierReferences unable to FindReferences in "',Code.Filename,'"');
Result:=mrAbort;
exit;
end;
//debugln('GatherIdentifierReferences FindReferences in "',Code.Filename,'" ',dbgs(ListOfPCodeXYPosition<>nil));
// add to tree
if ListOfPCodeXYPosition<>nil then begin
if TreeOfPCodeXYPosition=nil then
TreeOfPCodeXYPosition:=CodeToolBoss.CreateTreeOfPCodeXYPosition;
CodeToolBoss.AddListToTreeOfPCodeXYPosition(ListOfPCodeXYPosition,
TreeOfPCodeXYPosition,true,false);
end;
end;
Result:=mrOk;
finally
CodeToolBoss.FreeListOfPCodeXYPosition(ListOfPCodeXYPosition);
if Result<>mrOk then
CodeToolBoss.FreeTreeOfPCodeXYPosition(TreeOfPCodeXYPosition);
Cache.Free;
end;
end;
function GatherUnitReferences(Files: TStringList; UnitCode: TCodeBuffer;
SearchInComments, IgnoreErrors, IgnoreMissingFiles: boolean;
var TreeOfPCodeXYPosition: TAVLTree): TModalResult;
var
ListOfPCodeXYPosition: TFPList;
LoadResult: TModalResult;
Code: TCodeBuffer;
i: Integer;
begin
Result:=mrCancel;
ListOfPCodeXYPosition:=nil;
TreeOfPCodeXYPosition:=nil;
try
CleanUpFileList(Files);
Result:=mrOk;
// search in every file
for i:=0 to Files.Count-1 do begin
if CompareFilenames(Files[i],UnitCode.Filename)=0 then continue;
if IgnoreMissingFiles then
begin
if FilenameIsAbsolute(Files[i]) then
begin
if not FileExistsCached(Files[i]) then continue;
end else begin
Code:=CodeToolBoss.LoadFile(Files[i],false,false);
if (Code=nil) then continue;
end;
end;
LoadResult:=
LoadCodeBuffer(Code,Files[i],[lbfCheckIfText,lbfUpdateFromDisk],true);
if LoadResult=mrAbort then begin
debugln('GatherUnitReferences unable to load "',Files[i],'"');
if IgnoreErrors then
continue;
Result:=mrCancel;
exit;
end;
if LoadResult<>mrOk then continue;
// search references
CodeToolBoss.FreeListOfPCodeXYPosition(ListOfPCodeXYPosition);
if not CodeToolBoss.FindUnitReferences(
UnitCode, Code, not SearchInComments, ListOfPCodeXYPosition) then
begin
debugln('GatherUnitReferences unable to FindUnitReferences in "',Code.Filename,'"');
if IgnoreErrors then
continue;
Result:=mrCancel;
exit;
end;
//debugln('GatherUnitReferences FindUnitReferences in "',Code.Filename,'" ',dbgs(ListOfPCodeXYPosition<>nil));
// add to tree
if ListOfPCodeXYPosition<>nil then begin
if TreeOfPCodeXYPosition=nil then
TreeOfPCodeXYPosition:=CodeToolBoss.CreateTreeOfPCodeXYPosition;
CodeToolBoss.AddListToTreeOfPCodeXYPosition(ListOfPCodeXYPosition,
TreeOfPCodeXYPosition,true,false);
end;
end;
finally
CodeToolBoss.FreeListOfPCodeXYPosition(ListOfPCodeXYPosition);
end;
end;
function ShowIdentifierReferences(
DeclarationCode: TCodeBuffer; const DeclarationCaretXY: TPoint;
TreeOfPCodeXYPosition: TAVLTree): TModalResult;
var
Identifier: string;
OldSearchPageIndex: TTabSheet;
SearchPageIndex: TTabSheet;
begin
Result:=mrCancel;
LazarusIDE.DoShowSearchResultsView(iwgfShow);
SearchPageIndex:=nil;
try
// show result
CodeToolBoss.GetIdentifierAt(DeclarationCode,
DeclarationCaretXY.X,DeclarationCaretXY.Y,Identifier);
// create a search result page
//debugln(['ShowIdentifierReferences ',DbgSName(SearchResultsView)]);
SearchPageIndex:=SearchResultsView.AddSearch(
'Ref: '+Identifier,
Identifier,
'',
ExtractFilePath(DeclarationCode.Filename),
'*.pas;*.pp;*.p;*.inc',
[fifWholeWord,fifSearchDirectories]);
if SearchPageIndex = nil then exit;
// list results
SearchResultsView.BeginUpdate(SearchPageIndex.PageIndex);
AddReferencesToResultView(DeclarationCode,DeclarationCaretXY,
TreeOfPCodeXYPosition,true,SearchPageIndex.PageIndex);
OldSearchPageIndex:=SearchPageIndex;
SearchPageIndex:=nil;
SearchResultsView.EndUpdate(OldSearchPageIndex.PageIndex);
IDEWindowCreators.ShowForm(SearchResultsView,true);
finally
if SearchPageIndex <> nil then
SearchResultsView.EndUpdate(SearchPageIndex.PageIndex);
end;
end;
procedure AddReferencesToResultView(DeclarationCode: TCodeBuffer;
const DeclarationCaretXY: TPoint; TreeOfPCodeXYPosition: TAVLTree;
ClearItems: boolean; SearchPageIndex: integer);
var
Identifier: string;
CodePos: PCodeXYPosition;
CurLine: String;
TrimmedLine: String;
TrimCnt: Integer;
ANode: TAVLTreeNode;
begin
CodeToolBoss.GetIdentifierAt(DeclarationCode,
DeclarationCaretXY.X,DeclarationCaretXY.Y,Identifier);
SearchResultsView.BeginUpdate(SearchPageIndex);
if ClearItems then
SearchResultsView.Items[SearchPageIndex].Clear;
if (TreeOfPCodeXYPosition<>nil) then begin
ANode:=TreeOfPCodeXYPosition.FindHighest;
while ANode<>nil do begin
CodePos:=PCodeXYPosition(ANode.Data);
CurLine:=TrimRight(CodePos^.Code.GetLine(CodePos^.Y-1,false));
TrimmedLine:=Trim(CurLine);
TrimCnt:=length(CurLine)-length(TrimmedLine);
//debugln('ShowReferences x=',dbgs(CodePos^.x),' y=',dbgs(CodePos^.y),' ',CurLine);
SearchResultsView.AddMatch(SearchPageIndex,
CodePos^.Code.Filename,
Point(CodePos^.X,CodePos^.Y),
Point(CodePos^.X+length(Identifier),CodePos^.Y),
TrimmedLine,
CodePos^.X-TrimCnt, length(Identifier));
ANode:=TreeOfPCodeXYPosition.FindPrecessor(ANode);
end;
end;
SearchResultsView.EndUpdate(SearchPageIndex);
end;
function GatherFPDocReferencesForPascalFiles(PascalFiles: TStringList;
DeclarationCode: TCodeBuffer; const DeclarationCaretXY: TPoint;
var ListOfLazFPDocNode: TFPList): TModalResult;
var
PascalFilenames, FPDocFilenames: TFilenameToStringTree;
CacheWasUsed: boolean;
Chain: TCodeHelpElementChain;
CHResult: TCodeHelpParseResult;
CHElement: TCodeHelpElement;
FPDocFilename: String;
S2SItem: PStringToStringItem;
begin
Result:=mrCancel;
PascalFilenames:=nil;
FPDocFilenames:=nil;
try
// gather FPDoc files
CleanUpFileList(PascalFiles);
PascalFilenames:=TFilenameToStringTree.Create(false);
PascalFilenames.AddNames(PascalFiles);
CodeHelpBoss.GetFPDocFilenamesForSources(PascalFilenames,true,FPDocFilenames);
if FPDocFilenames=nil then begin
DebugLn(['GatherFPDocReferences no fpdoc files found']);
exit(mrOk);
end;
// get codehelp element
CHResult:=CodeHelpBoss.GetElementChain(DeclarationCode,
DeclarationCaretXY.X,DeclarationCaretXY.Y,true,Chain,CacheWasUsed);
if CHResult<>chprSuccess then begin
DebugLn(['GatherFPDocReferences CodeHelpBoss.GetElementChain failed']);
exit;
end;
CHElement:=Chain[0];
DebugLn(['GatherFPDocReferences OwnerName=',CHElement.ElementOwnerName,' FPDocPkg=',CHElement.ElementFPDocPackageName,' Name=',CHElement.ElementName]);
// search FPDoc files
for S2SItem in FPDocFilenames do begin
FPDocFilename:=S2SItem^.Name;
Result:=GatherReferencesInFPDocFile(
CHElement.ElementFPDocPackageName,CHElement.ElementUnitName,
CHElement.ElementName,
FPDocFilename,ListOfLazFPDocNode);
if Result<>mrOk then exit;
end;
Result:=mrOk;
finally
PascalFilenames.Free;
FPDocFilenames.Free;
if Result<>mrOk then begin
FreeListObjects(ListOfLazFPDocNode,true);
ListOfLazFPDocNode:=nil;
end;
end;
end;
function GatherReferencesInFPDocFile(
const OldPackageName, OldModuleName, OldElementName: string;
const FPDocFilename: string;
var ListOfLazFPDocNode: TFPList
): TModalResult;
var
DocFile: TLazFPDocFile;
IsSamePackage: Boolean;
IsSameModule: Boolean;// = same unit
procedure CheckLink(Node: TDOMNode; Link: string);
var
p: LongInt;
PackageName: String;
begin
if Link='' then exit;
if Link[1]='#' then begin
p:=System.Pos('.',Link);
if p<1 then exit;
PackageName:=copy(Link,2,p-2);
if SysUtils.CompareText(PackageName,OldPackageName)<>0 then exit;
delete(Link,1,p);
end;
if (SysUtils.CompareText(Link,OldElementName)=0)
or (SysUtils.CompareText(Link,OldModuleName+'.'+OldElementName)=0) then
begin
DebugLn(['CheckLink Found: ',Link]);
if ListOfLazFPDocNode=nil then
ListOfLazFPDocNode:=TFPList.Create;
ListOfLazFPDocNode.Add(TLazFPDocNode.Create(DocFile,Node));
end;
end;
procedure SearchLinksInChildNodes(Node: TDomNode);
// search recursively for links
begin
Node:=Node.FirstChild;
while Node<>nil do begin
if (Node.NodeName='link')
and (Node is TDomElement) then begin
CheckLink(Node,TDomElement(Node).GetAttribute('id'));
end;
SearchLinksInChildNodes(Node);
Node:=Node.NextSibling;
end;
end;
var
CHResult: TCodeHelpParseResult;
CacheWasUsed: boolean;
Node: TDOMNode;
begin
Result:=mrCancel;
DebugLn(['GatherFPDocReferences ',
' OldPackageName=',OldPackageName,
' OldModuleName=',OldModuleName,' OldElementName=',OldElementName,
' FPDocFilename=',FPDocFilename]);
CHResult:=CodeHelpBoss.LoadFPDocFile(FPDocFilename,[chofUpdateFromDisk],
DocFile,CacheWasUsed);
if CHResult<>chprSuccess then begin
DebugLn(['GatherReferencesInFPDocFile CodeHelpBoss.LoadFPDocFile failed File=',FPDocFilename]);
exit(mrCancel);
end;
// search in Doc nodes
IsSamePackage:=SysUtils.CompareText(DocFile.GetPackageName,OldPackageName)=0;
IsSameModule:=SysUtils.CompareText(DocFile.GetModuleName,OldModuleName)=0;
DebugLn(['GatherReferencesInFPDocFile ',DocFile.GetPackageName,'=',OldPackageName,' ',DocFile.GetModuleName,'=',OldModuleName]);
Node:=DocFile.GetFirstElement;
while Node<>nil do begin
if Node is TDomElement then begin
if (SysUtils.CompareText(TDomElement(Node).GetAttribute('name'),OldElementName)=0)
and IsSamePackage and IsSameModule
then begin
// this is the element itself
DebugLn(['GatherReferencesInFPDocFile Element itself found: ',Node.NodeName,' ',Node.NodeValue]);
if ListOfLazFPDocNode=nil then
ListOfLazFPDocNode:=TFPList.Create;
ListOfLazFPDocNode.Add(TLazFPDocNode.Create(DocFile,Node));
end;
CheckLink(Node,TDomElement(Node).GetAttribute('link'));
SearchLinksInChildNodes(Node);
end;
Node:=Node.NextSibling;
end;
Result:=mrOk;
end;
{ TFindRenameIdentifierDialog }
procedure TFindRenameIdentifierDialog.FindRenameIdentifierDialogCreate(
Sender: TObject);
begin
IDEDialogLayoutList.ApplyLayout(Self,450,480);
Caption:=lisFRIFindOrRenameIdentifier;
CurrentGroupBox.Caption:=lisCodeToolsOptsIdentifier;
ExtraFilesGroupBox.Caption:=lisFRIAdditionalFilesToSearchEGPathPasPath2Pp;
ButtonPanel1.OKButton.Caption:=lisFRIFindReferences;
ButtonPanel1.OKButton.ModalResult:=mrNone;
ButtonPanel1.CancelButton.Caption:=lisCancel;
NewGroupBox.Caption:=lisFRIRenaming;
RenameCheckBox.Caption:=lisRename;
ShowResultCheckBox.Caption:=lisRenameShowResult;
ScopeCommentsCheckBox.Caption:=lisFRISearchInCommentsToo;
ScopeGroupBox.Caption:=lisFRISearch;
ScopeRadioGroup.Caption:=dlgSearchScope;
ScopeRadioGroup.Items[0]:=lisFRIinCurrentUnit;
ScopeRadioGroup.Items[1]:=lisFRIinMainProject;
ScopeRadioGroup.Items[2]:=lisFRIinProjectPackageOwningCurrentUnit;
ScopeRadioGroup.Items[3]:=lisFRIinAllOpenPackagesAndProjects;
LoadFromConfig;
end;
procedure TFindRenameIdentifierDialog.FormShow(Sender: TObject);
begin
if NewEdit.CanFocus then
begin
NewEdit.SelectAll;
NewEdit.SetFocus;
end;
end;
procedure TFindRenameIdentifierDialog.HelpButtonClick(Sender: TObject);
begin
OpenUrl('http://wiki.freepascal.org/IDE_Window:_Find_or_Rename_identifier');
end;
procedure TFindRenameIdentifierDialog.RenameCheckBoxChange(Sender: TObject);
begin
UpdateRename;
end;
procedure TFindRenameIdentifierDialog.UpdateRename;
begin
RenameCheckBox.Enabled:=AllowRename;
NewEdit.Enabled:=RenameCheckBox.Checked and RenameCheckBox.Enabled;
ShowResultCheckBox.Enabled:=RenameCheckBox.Checked and RenameCheckBox.Enabled;
if NewEdit.Enabled then
ButtonPanel1.OKButton.Caption:=lisFRIRenameAllReferences
else
ButtonPanel1.OKButton.Caption:=lisFRIFindReferences;
end;
procedure TFindRenameIdentifierDialog.SetAllowRename(const AValue: boolean);
begin
if FAllowRename=AValue then exit;
FAllowRename:=AValue;
UpdateRename;
end;
procedure TFindRenameIdentifierDialog.SetIsPrivate(const AValue: boolean);
begin
if FIsPrivate=AValue then exit;
FIsPrivate:=AValue;
ExtraFilesGroupBox.Enabled:=not IsPrivate;
ScopeRadioGroup.Enabled:=not IsPrivate;
ScopeRadioGroup.ItemIndex:=0;
end;
procedure TFindRenameIdentifierDialog.FindOrRenameButtonClick(Sender: TObject);
var
NewIdentifier: String;
begin
NewIdentifier:=NewEdit.Text;
if not IsValidIdent(NewIdentifier) then begin
IDEMessageDialog(lisFRIInvalidIdentifier,
Format(lisSVUOisNotAValidIdentifier, [NewIdentifier]), mtError, [mbCancel]);
ModalResult:=mrNone;
exit;
end;
ModalResult:=mrOk;
end;
procedure TFindRenameIdentifierDialog.FindRenameIdentifierDialogClose(
Sender: TObject; var CloseAction: TCloseAction);
begin
SaveToConfig;
IDEDialogLayoutList.SaveLayout(Self);
end;
procedure TFindRenameIdentifierDialog.LoadFromConfig;
begin
LoadFromOptions(MiscellaneousOptions.FindRenameIdentifierOptions);
end;
procedure TFindRenameIdentifierDialog.SaveToConfig;
begin
SaveToOptions(MiscellaneousOptions.FindRenameIdentifierOptions);
end;
procedure TFindRenameIdentifierDialog.LoadFromOptions(
Options: TFindRenameIdentifierOptions);
begin
RenameCheckBox.Checked:=Options.Rename;
ExtraFilesEdit.Text:=StringListToText(Options.ExtraFiles,';',true);
NewEdit.Text:=Options.RenameTo;
ShowResultCheckBox.Checked:=Options.RenameShowResult;
ScopeCommentsCheckBox.Checked:=Options.SearchInComments;
case Options.Scope of
frCurrentUnit: ScopeRadioGroup.ItemIndex:=0;
frProject: ScopeRadioGroup.ItemIndex:=1;
frOwnerProjectPackage: ScopeRadioGroup.ItemIndex:=2;
else
ScopeRadioGroup.ItemIndex:=3;
end;
UpdateRename;
end;
procedure TFindRenameIdentifierDialog.SaveToOptions(
Options: TFindRenameIdentifierOptions);
begin
Options.Rename:=RenameCheckBox.Checked;
if ExtraFilesGroupBox.Enabled then
SplitString(ExtraFilesEdit.Text,';',Options.ExtraFiles,true);
Options.RenameTo:=NewEdit.Text;
Options.RenameShowResult := ShowResultCheckBox.Checked;
Options.SearchInComments:=ScopeCommentsCheckBox.Checked;
if ScopeRadioGroup.Enabled then
case ScopeRadioGroup.ItemIndex of
0: Options.Scope:=frCurrentUnit;
1: Options.Scope:=frProject;
2: Options.Scope:=frOwnerProjectPackage;
else Options.Scope:=frAllOpenProjectsAndPackages;
end;
end;
procedure TFindRenameIdentifierDialog.SetIdentifier(
const NewIdentifierFilename: string; const NewIdentifierPosition: TPoint);
var
s: String;
ACodeBuffer: TCodeBuffer;
ListOfCodeBuffer: TFPList;
i: Integer;
CurCode: TCodeBuffer;
NewIdentifier: String;
Tool: TCodeTool;
CodeXY: TCodeXYPosition;
CleanPos: integer;
Node: TCodeTreeNode;
begin
FIdentifierFilename:=NewIdentifierFilename;
FIdentifierPosition:=NewIdentifierPosition;
CurrentListBox.Items.Clear;
s:=IdentifierFilename
+'('+IntToStr(IdentifierPosition.Y)+','+IntToStr(IdentifierPosition.X)+')';
CurrentListBox.Items.Add(s);
LoadCodeBuffer(ACodeBuffer,IdentifierFileName,[lbfCheckIfText],false);
if ACodeBuffer<>nil then begin
CodeToolBoss.GetIncludeCodeChain(ACodeBuffer,true,ListOfCodeBuffer);
if ListOfCodeBuffer<>nil then begin
for i:=0 to ListOfCodeBuffer.Count-1 do begin
CurCode:=TCodeBuffer(ListOfCodeBuffer[i]);
if CurCode=ACodeBuffer then break;
s:=CurCode.Filename;
CurrentListBox.Items.Insert(0,s);
end;
ListOfCodeBuffer.Free;
end;
if CodeToolBoss.GetIdentifierAt(ACodeBuffer,
NewIdentifierPosition.X,NewIdentifierPosition.Y,NewIdentifier) then
begin
CurrentGroupBox.Caption:=Format(lisFRIIdentifier, [NewIdentifier]);
NewEdit.Text:=NewIdentifier;
end;
// check if in implementation or private section
if CodeToolBoss.Explore(ACodeBuffer,Tool,false) then begin
CodeXY:=CodeXYPosition(NewIdentifierPosition.X,NewIdentifierPosition.Y,ACodeBuffer);
if Tool.CaretToCleanPos(CodeXY,CleanPos)=0 then begin
Node:=Tool.BuildSubTreeAndFindDeepestNodeAtPos(CleanPos,false);
if (Node=nil)
or Node.HasParentOfType(ctnImplementation)
or Node.HasParentOfType(ctnClassPrivate) then
IsPrivate:=true;
end;
end;
end;
end;
end.
|