codetools: aded FindProcDeclaration

git-svn-id: trunk@30523 -
This commit is contained in:
mattias 2011-05-02 09:08:23 +00:00
parent 013afcfaf2
commit 92692eea39
13 changed files with 245 additions and 36 deletions

3
.gitattributes vendored
View File

@ -450,6 +450,8 @@ components/codetools/examples/methodjumping.lpi svneol=native#text/plain
components/codetools/examples/methodjumping.pas svneol=native#text/plain
components/codetools/examples/ppudependencies.lpi svneol=native#text/plain
components/codetools/examples/ppudependencies.lpr svneol=native#text/plain
components/codetools/examples/proctomethod.lpi svneol=native#text/plain
components/codetools/examples/proctomethod.lpr svneol=native#text/plain
components/codetools/examples/reduceifdefs.lpi svneol=native#text/plain
components/codetools/examples/reduceifdefs.lpr svneol=native#text/plain
components/codetools/examples/removeemptymethods.lpi svneol=native#text/plain
@ -479,6 +481,7 @@ components/codetools/examples/scanexamples/modemacpas.pas svneol=native#text/pla
components/codetools/examples/scanexamples/nestedclasses.pas svneol=native#text/plain
components/codetools/examples/scanexamples/objctest1.pas svneol=native#text/plain
components/codetools/examples/scanexamples/overloadedfunction.pas svneol=native#text/plain
components/codetools/examples/scanexamples/procsandmethods1.pas svneol=native#text/plain
components/codetools/examples/scanexamples/publishedmethods1.pas svneol=native#text/plain
components/codetools/examples/scanexamples/publishedmethods2.pas svneol=native#text/plain
components/codetools/examples/scanexamples/publishedvars.pas svneol=native#text/plain

View File

@ -385,6 +385,9 @@ type
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer;
out RevertableJump: boolean): boolean;
function FindProcDeclaration(Code: TCodeBuffer; CleanDef: string;
out Tool: TCodeTool; out Node: TCodeTreeNode;
Attr: TProcHeadAttributes = [phpWithoutSemicolon]): boolean;
// find declaration
function FindDeclaration(Code: TCodeBuffer; X,Y: integer;
@ -1791,6 +1794,38 @@ begin
{$ENDIF}
end;
function TCodeToolManager.FindProcDeclaration(Code: TCodeBuffer;
CleanDef: string; out Tool: TCodeTool; out Node: TCodeTreeNode;
Attr: TProcHeadAttributes): boolean;
var
Paths: TStringList;
begin
Result:=false;
{$IFDEF CTDEBUG}
DebugLn(['TCodeToolManager.FindProcDeclaration A ',Code.Filename,' CleanDef=',CleanDef]);
{$ENDIF}
Tool:=nil;
Node:=nil;
if not InitCurCodeTool(Code) then exit;
Tool:=FCurCodeTool;
Paths:=TStringList.Create;
try
Paths.Add(CleanDef);
try
FCurCodeTool.BuildTree(lsrEnd);
Node:=FCurCodeTool.FindSubProcPath(Paths,Attr,false);
Result:=Node<>nil;
except
on e: Exception do Result:=HandleException(e);
end;
finally
Paths.Free;
end;
{$IFDEF CTDEBUG}
DebugLn('TCodeToolManager.FindProcDeclaration END ');
{$ENDIF}
end;
function TCodeToolManager.FindDeclaration(Code: TCodeBuffer; X,Y: integer;
out NewCode: TCodeBuffer;
out NewX, NewY, NewTopLine: integer;

View File

@ -12,9 +12,8 @@ Then you must set the FPCDIR variable and start the example again. For example:
Under linux:
export FPCDIR=/home/username/freepascal/fpc
For instance the FPC 2.2.0 source directory looks like this:
For instance the FPC 2.4.2 source directory contains the following files:
compiler
fv
ide
installer
Makefile

View File

@ -1,7 +1,7 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="7"/>
<Version Value="9"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
@ -10,8 +10,11 @@
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<TargetFileExt Value=""/>
<UseAppBundle Value="False"/>
</General>
<BuildModes Count="1">
<Item1 Name="default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
@ -42,11 +45,19 @@
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="8"/>
<Version Value="10"/>
<SearchPaths>
<OtherUnitFiles Value="scanexamples/"/>
<OtherUnitFiles Value="scanexamples"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<UseAnsiStrings Value="False"/>
</SyntaxOptions>
</Parsing>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>

View File

@ -1,7 +1,7 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="7"/>
<Version Value="9"/>
<General>
<Flags>
<MainUnitHasUsesSectionForAllUnits Value="False"/>
@ -11,9 +11,12 @@
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<TargetFileExt Value=""/>
<Title Value="autoindent"/>
<UseAppBundle Value="False"/>
</General>
<BuildModes Count="1">
<Item1 Name="default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
@ -57,8 +60,11 @@
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="8"/>
<Version Value="10"/>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>

View File

@ -1,18 +1,18 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="8"/>
<Version Value="9"/>
<General>
<Flags>
<LRSInOutputDirectory Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<TargetFileExt Value=""/>
<UseAppBundle Value="False"/>
</General>
<VersionInfo>
<StringTable Comments="" CompanyName="" FileDescription="" FileVersion="0.0.0.0" InternalName="" LegalCopyright="" LegalTrademarks="" OriginalFilename="" ProductName="" ProductVersion="0.0.0.0"/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
@ -48,16 +48,14 @@
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="9"/>
<Version Value="10"/>
<SearchPaths>
<OtherUnitFiles Value="scanexamples/"/>
<OtherUnitFiles Value="scanexamples"/>
</SearchPaths>
<Parsing>
<SyntaxOptions>
<UseAnsiStrings Value="False"/>
</SyntaxOptions>
</Parsing>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>

View File

@ -1,18 +1,21 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="8"/>
<Version Value="9"/>
<General>
<Flags>
<LRSInOutputDirectory Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<TargetFileExt Value=""/>
<UseAppBundle Value="False"/>
</General>
<VersionInfo>
<StringTable Comments="" CompanyName="" FileDescription="" FileVersion="0.0.0.0" InternalName="" LegalCopyright="" LegalTrademarks="" OriginalFilename="" ProductName="" ProductVersion=""/>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
@ -44,13 +47,11 @@
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="9"/>
<Parsing>
<SyntaxOptions>
<UseAnsiStrings Value="False"/>
</SyntaxOptions>
</Parsing>
<Version Value="10"/>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>

View File

@ -8,6 +8,7 @@
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<UseAppBundle Value="False"/>
</General>
<BuildModes Count="1">
<Item1 Name="default" Default="True"/>
@ -38,12 +39,10 @@
</ProjectOptions>
<CompilerOptions>
<Version Value="10"/>
<Parsing>
<SyntaxOptions>
<UseAnsiStrings Value="False"/>
</SyntaxOptions>
</Parsing>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>

View File

@ -8,6 +8,7 @@
</Flags>
<SessionStorage Value="InIDEConfig"/>
<MainUnit Value="0"/>
<UseAppBundle Value="False"/>
</General>
<VersionInfo>
<StringTable ProductVersion=""/>

View File

@ -0,0 +1,60 @@
<?xml version="1.0"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<LRSInOutputDirectory Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="proctomethod"/>
<UseAppBundle Value="False"/>
</General>
<BuildModes Count="1">
<Item1 Name="default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<IncludeFileFilter Value="*.(pas|pp|inc|lfm|lpr|lrs|lpi|lpk|sh|xml)"/>
<ExcludeFileFilter Value="*.(bak|ppu|ppw|o|so);*~;backup"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
<LaunchingApplication PathPlusParams="/usr/X11R6/bin/xterm -T 'Lazarus Run Output' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine)"/>
</local>
</RunParams>
<RequiredPackages Count="1">
<Item1>
<PackageName Value="CodeTools"/>
</Item1>
</RequiredPackages>
<Units Count="2">
<Unit0>
<Filename Value="proctomethod.lpr"/>
<IsPartOfProject Value="True"/>
<UnitName Value="proctomethod"/>
</Unit0>
<Unit1>
<Filename Value="scanexamples/procsandmethods1.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="ProcsAndMethods1"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="10"/>
<SearchPaths>
<OtherUnitFiles Value="scanexamples"/>
</SearchPaths>
<Other>
<CompilerMessages>
<UseMsgFile Value="True"/>
</CompilerMessages>
<CompilerPath Value="$(CompPath)"/>
</Other>
</CompilerOptions>
</CONFIG>

View File

@ -0,0 +1,68 @@
{
***************************************************************************
* *
* 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:
Demonstration how to convert a procedure to a method.
}
program proctomethod;
{$mode objfpc}{$H+}
uses
Classes, SysUtils, CodeCache, CodeToolManager, FileProcs, AVL_Tree, CodeAtom,
BasicCodeTools, SourceChanger, CodeTree, FindDeclarationTool,
ProcsAndMethods1;
const
ConfigFilename = 'codetools.config';
var
Filename: string;
Code: TCodeBuffer;
Tool: TCodeTool;
CleanDef: String;
ProcNode: TCodeTreeNode;
begin
CodeToolBoss.SimpleInit(ConfigFilename);
// load the file
Filename:=ExpandFileName(SetDirSeparators('scanexamples/procsandmethods1.pas'));
Code:=CodeToolBoss.LoadFile(Filename,false,false);
if Code=nil then
raise Exception.Create('loading failed '+Filename);
// parse the unit
try
CleanDef:='DoSomething(integer)';
if not CodeToolBoss.FindProcDeclaration(Code,CleanDef,Tool,ProcNode)
then
raise Exception.Create('proc not found: "'+CleanDef+'"');
finally
end;
// write the new source:
writeln('-----------------------------------');
writeln('New source:');
writeln(Code.Source);
writeln('-----------------------------------');
end.

View File

@ -0,0 +1,28 @@
unit ProcsAndMethods1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TFoo = class
end;
TBar = class
public
end;
procedure DoSomething(i: integer);
implementation
procedure DoSomething(i: integer);
begin
// code
end;
end.

View File

@ -931,7 +931,7 @@ function TMethodJumpingCodeTool.FindSubProcPath(SubProcPath: TStrings;
if (PathIndex>SubProcPath.Count) or (StartNode=nil) then exit;
ProcHead:=SubProcPath[PathIndex];
ProcNode:=FindProcNode(StartNode,ProcHead,Attr);
DebugLn('TMethodJumpingCodeTool.SearchSubProcPath A ProcHead="',ProcHead,'" Found=',dbgs(ProcNode<>nil));
//DebugLn('TMethodJumpingCodeTool.SearchSubProcPath A ProcHead="',ProcHead,'" Found=',dbgs(ProcNode<>nil));
if ProcNode=nil then exit;
if PathIndex=SubProcPath.Count-1 then begin
Result:=ProcNode;