1 { Copyright (C) 2005 Mattias Gaertner
2 
3  ***************************************************************************
4  *                                                                         *
5  *   This source is free software; you can redistribute it and/or modify   *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This code is distributed in the hope that it will be useful, but      *
11  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
13  *   General Public License for more details.                              *
14  *                                                                         *
15  *   A copy of the GNU General Public License is available on the World    *
16  *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
17  *   obtain it by writing to the Free Software Foundation,                 *
18  *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
19  *                                                                         *
20  ***************************************************************************
21 
22   Abstract:
23     Example, how to setup the codetools, load a pascal unit and jump from
24     the declaration of a unit to its body.
25 }
26 program MethodJumping;
27 
28 {$mode objfpc}{$H+}
29 
30 uses
31   SysUtils,
32   // LazUtils
33   LazFileUtils,
34   // CodeTools
35   CodeToolManager, CodeCache;
36 
37 var
38   ExpandedFilename: String;
39   CodeBuf: TCodeBuffer;
40   NewCode: TCodeBuffer;
41   NewX, NewY, NewTopLine: integer;
42   RevertableJump: boolean;
43   X: Integer;
44   Y, BlockTopLine, BlockBottomLine: Integer;
45 begin
46   if (ParamCount>=1) and (Paramcount<3) then begin
47     writeln('Usage:');
48     writeln('  ',ParamStr(0));
49     writeln('  ',ParamStr(0),' <filename> <X> <Y>');
50   end;
51 
52   ExpandedFilename:=ExpandFileNameUTF8('scanexamples/methodjump1.pas');
53   X:=14;
54   Y:=10;
55   if (ParamCount>=3) then begin
56     ExpandedFilename:=ExpandFileNameUTF8(ParamStr(1));
57     X:=StrToInt(ParamStr(2));
58     Y:=StrToInt(ParamStr(3));
59   end;
60 
61   CodeBuf:=CodeToolBoss.LoadFile(ExpandedFilename,true,false);
62   if CodeBuf=nil then
63     raise Exception.Create('failed loading '+ExpandedFilename);
64   if CodeToolBoss.JumpToMethod(CodeBuf,X,Y,NewCode,NewX,NewY,NewTopLine,
65                                BlockTopLine,BlockBottomLine,RevertableJump)
66   then
67     writeln(NewCode.Filename,' ',NewX,',',NewY,' TopLine=',NewTopLine,
68             ' RevertableJump=',RevertableJump)
69   else
70     writeln('Method body not found.');
71 end.
72 
73