codetools: clean up

This commit is contained in:
mattias 2023-12-19 09:46:35 +01:00
parent dacecb3f91
commit 3f6dc2d6ad
4 changed files with 16 additions and 330 deletions

View File

@ -1,313 +0,0 @@
{
***************************************************************************
* *
* 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:
TCodeTemplatesTool enhances TStandardCodeTool with the ability to insert
code templates.
}
unit CodeTemplatesTool;
{$mode objfpc}{$H+}
interface
{$I codetools.inc}
uses
{$IFDEF MEM_CHECK}
MemCheck,
{$ENDIF}
Classes, SysUtils,
// Codetools
FileProcs, CodeTree, CodeCache, KeywordFuncLists, LinkScanner, BasicCodeTools,
SourceChanger, PascalParserTool, StdCodeTools;
type
TCodeTemplateSyntax = (
ctsxDefault
);
{ ctsxDefault:
The backslash '\' makes special chars to normal chars.
| for cursor
$(MacroName) for macro variables
$MacroName(Param) for macro functions
If the variable contains several lines, the lines are indented like the
first line.
Example:
Inserting the text
"begin"
" i:=1;"
"end;"
Into the text
"begin"
" |"
"end;"
at the | results in
"begin"
" begin"
" i:=1;"
" end;"
"end;"
Defined variables:
$(Selection) - replaced by the current selection
$(Indent) - replaced by nothing. Set Indent for multiline macros
Example:
Text: " Text: $(Indent)$(Selection)"
Selection: "First"
"Second"
Result: " Text: First"
" Second"
}
TCodeToolTemplate = class
private
FIndent: integer;
FNewLineAtEnd: boolean;
FNewLineAtStart: boolean;
FSyntax: TCodeTemplateSyntax;
FTemplate: string;
procedure SetIndent(const AValue: integer);
procedure SetNewLineAtEnd(const AValue: boolean);
procedure SetNewLineAtStart(const AValue: boolean);
procedure SetSyntax(const AValue: TCodeTemplateSyntax);
procedure SetTemplate(const AValue: string);
public
property Template: string read FTemplate write SetTemplate;
property NewLineAtStart: boolean read FNewLineAtStart write SetNewLineAtStart;
property NewLineAtEnd: boolean read FNewLineAtEnd write SetNewLineAtEnd;
property Syntax: TCodeTemplateSyntax read FSyntax write SetSyntax;
property Indent: integer read FIndent write SetIndent;
end;
{ TCodeTemplatesTool }
TCodeTemplatesTool = class(TStandardCodeTool)
public
function InsertCodeTemplate(CursorPos,EndPos: TCodeXYPosition;
TopLine: integer; CodeTemplate: TCodeToolTemplate;
out NewPos: TCodeXYPosition; out NewTopLine: integer;
SourceChangeCache: TSourceChangeCache): boolean;
function ExtractProcedureHeader(CursorPos: TCodeXYPosition;
Attributes: TProcHeadAttributes; var ProcHead: string): boolean;
procedure CalcMemSize(Stats: TCTMemStats); override;
end;
implementation
{ TCodeTemplatesTool }
function TCodeTemplatesTool.InsertCodeTemplate(CursorPos,
EndPos: TCodeXYPosition; TopLine: integer; CodeTemplate: TCodeToolTemplate;
out NewPos: TCodeXYPosition; out NewTopLine: integer;
SourceChangeCache: TSourceChangeCache): boolean;
var
NewText: TMemoryStream;
TemplatePos: Integer;
LastWrittenTemplatePos: Integer;
TemplateStr: String;
TemplateStrLen: Integer;
procedure FlushTemplate;
var
FromPos: Integer;
ToPos: Integer;
begin
FromPos:=LastWrittenTemplatePos+1;
ToPos:=TemplatePos-1;
if ToPos>TemplateStrLen then
ToPos:=TemplateStrLen;
if FromPos<=ToPos then
NewText.Write(TemplateStr[FromPos],ToPos-FromPos+1);
LastWrittenTemplatePos:=ToPos;
end;
procedure CalculateNewCursorPos;
begin
end;
procedure ParseMacro;
var
MacroStart: Integer;
MacroFuncNameStart: Integer;
MacroFuncNameEnd: Integer;
BracketLvl: Integer;
MacroParamStart: Integer;
MacroParamEnd: Integer;
begin
MacroStart:=TemplatePos;
inc(MacroStart);
MacroFuncNameStart:=TemplatePos;
while (TemplatePos<=TemplateStrLen)
and IsIdentChar[TemplateStr[TemplatePos]] do
inc(TemplatePos);
MacroFuncNameEnd:=TemplatePos;
if (TemplatePos<=TemplateStrLen) and (TemplateStr[TemplatePos]='(') then
begin
inc(TemplatePos);
MacroParamStart:=TemplatePos;
MacroParamEnd:=MacroParamStart;
BracketLvl:=1;
while (TemplatePos<=TemplateStrLen) do begin
case TemplateStr[TemplatePos] of
'\':
begin
inc(TemplatePos);
if (TemplatePos>TemplateStrLen) then break;
end;
'(': inc(BracketLvl);
')':
begin
dec(BracketLvl);
if BracketLvl=0 then begin
MacroParamEnd:=TemplatePos;
break;
end;
end;
end;
inc(TemplatePos);
end;
if MacroFuncNameStart<MacroFuncNameEnd then begin
// a macro function
// ToDo
end else if MacroParamStart<MacroParamEnd then begin
// a macro variable
// ToDo
end;
end;
LastWrittenTemplatePos:=TemplatePos;
end;
begin
Result:=false;
NewPos:=CursorPos;
NewText:=TMemoryStream.Create;
try
// parse the template
TemplatePos:=1;
LastWrittenTemplatePos:=TemplatePos-1;
TemplateStr:=CodeTemplate.Template;
TemplateStrLen:=length(TemplateStr);
while TemplatePos<=TemplateStrLen do begin
case TemplateStr[TemplatePos] of
'\':
begin
FlushTemplate;
LastWrittenTemplatePos:=TemplatePos;
inc(TemplatePos,2);
end;
'|':
begin
FlushTemplate;
CalculateNewCursorPos;
LastWrittenTemplatePos:=TemplatePos;
inc(TemplatePos);
end;
'$':
begin
FlushTemplate;
ParseMacro;
end;
else
inc(TemplatePos);
end;
end;
FlushTemplate;
finally
NewText.Free;
end;
end;
function TCodeTemplatesTool.ExtractProcedureHeader(CursorPos: TCodeXYPosition;
Attributes: TProcHeadAttributes; var ProcHead: string): boolean;
var
CleanCursorPos: integer;
ANode: TCodeTreeNode;
begin
Result:=false;
ProcHead:='';
BuildTreeAndGetCleanPos(trTillCursor,lsrEnd,CursorPos,CleanCursorPos,
[btSetIgnoreErrorPos,btCursorPosOutAllowed]);
ANode:=FindDeepestNodeAtPos(CleanCursorPos,True);
while (ANode<>nil) and (ANode.Desc<>ctnProcedure) do
ANode:=ANode.Parent;
if ANode=nil then exit;
ProcHead:=ExtractProcHead(ANode,Attributes);
Result:=true;
end;
procedure TCodeTemplatesTool.CalcMemSize(Stats: TCTMemStats);
begin
inherited CalcMemSize(Stats);
end;
{ TCodeToolTemplate }
procedure TCodeToolTemplate.SetIndent(const AValue: integer);
begin
if FIndent=AValue then exit;
FIndent:=AValue;
end;
procedure TCodeToolTemplate.SetNewLineAtEnd(const AValue: boolean);
begin
if FNewLineAtEnd=AValue then exit;
FNewLineAtEnd:=AValue;
end;
procedure TCodeToolTemplate.SetNewLineAtStart(const AValue: boolean);
begin
if FNewLineAtStart=AValue then exit;
FNewLineAtStart:=AValue;
end;
procedure TCodeToolTemplate.SetSyntax(const AValue: TCodeTemplateSyntax);
begin
if FSyntax=AValue then exit;
FSyntax:=AValue;
end;
procedure TCodeToolTemplate.SetTemplate(const AValue: string);
begin
if FTemplate=AValue then exit;
FTemplate:=AValue;
end;
end.

View File

@ -24,7 +24,7 @@
<License Value="GPL-2
"/>
<Version Major="1" Release="1"/>
<Files Count="62">
<Files Count="61">
<Item1>
<Filename Value="Makefile"/>
<Type Value="Text"/>
@ -276,11 +276,6 @@
<Filename Value="codetoolgdbtracer.pas"/>
<UnitName Value="CodetoolGDBTracer"/>
</Item61>
<Item62>
<Filename Value="codetemplatestool.pas"/>
<AddToUsesPkgSection Value="False"/>
<UnitName Value="CodeTemplatesTool"/>
</Item62>
</Files>
<CompatibilityMode Value="True"/>
<LazDoc Paths="docs"/>

View File

@ -3,7 +3,7 @@
fpmake.pp for CodeTools 1.0.1
This file was generated on 02-01-2015
This file was generated on 19.12.2023
}
{$ifndef ALLPACKAGES}
@ -18,26 +18,30 @@ procedure add_CodeTools(const ADirectory: string);
var
P : TPackage;
T : TTarget;
D : TDependency;
begin
with Installer do
begin
P:=AddPAckage('codetools');
P.Version:='1.0.1';
P:=AddPackage('codetools');
P.Version:='1.0.1-0';
P.Directory:=ADirectory;
P.Author:='Mattias Gaertner';
P.License:='GPL-2';
P.Description:='Tools and functions to parse, browse and edit pascal sources';
P.Flags.Add('LazarusDsgnPkg');
P.Dependencies.Add('lazutils');
D := P.Dependencies.Add('lazutils');
P.Options.Add('-MObjFPC');
P.Options.Add('-Sci');
P.Options.Add('-O1');
P.Options.Add('-g');
P.Options.Add('-gl');
P.Options.Add('-l');
P.Options.Add('-vh-');
P.Options.Add('-vewnibq');
P.Options.Add('-vewnhibq');
P.UnitPath.Add('.');
T:=P.Targets.AddUnit('codetools.pas');
t.Dependencies.AddUnit('basiccodetools');
@ -49,7 +53,6 @@ begin
t.Dependencies.AddUnit('codecompletiontool');
t.Dependencies.AddUnit('codegraph');
t.Dependencies.AddUnit('codeindex');
t.Dependencies.AddUnit('codetemplatestool');
t.Dependencies.AddUnit('codetoolmanager');
t.Dependencies.AddUnit('codetoolmemmanager');
t.Dependencies.AddUnit('codetoolsconfig');
@ -72,6 +75,7 @@ begin
t.Dependencies.AddUnit('keywordfunclists');
t.Dependencies.AddUnit('lfmtrees');
t.Dependencies.AddUnit('linkscanner');
t.Dependencies.AddUnit('memcheck');
t.Dependencies.AddUnit('methodjumptool');
t.Dependencies.AddUnit('multikeywordlisttool');
t.Dependencies.AddUnit('nonpascalcodetools');
@ -105,7 +109,6 @@ begin
T:=P.Targets.AddUnit('codecompletiontool.pas');
T:=P.Targets.AddUnit('codegraph.pas');
T:=P.Targets.AddUnit('codeindex.pas');
T:=P.Targets.AddUnit('codetemplatestool.pas');
T:=P.Targets.AddUnit('codetoolmanager.pas');
T:=P.Targets.AddUnit('codetoolmemmanager.pas');
T:=P.Targets.AddUnit('codetoolsconfig.pas');
@ -128,7 +131,7 @@ begin
T:=P.Targets.AddUnit('keywordfunclists.pas');
T:=P.Targets.AddUnit('lfmtrees.pas');
T:=P.Targets.AddUnit('linkscanner.pas');
P.Sources.AddSrc('memcheck.pas');
P.Targets.AddImplicitUnit('memcheck.pas');
T:=P.Targets.AddUnit('methodjumptool.pas');
T:=P.Targets.AddUnit('multikeywordlisttool.pas');
T:=P.Targets.AddUnit('nonpascalcodetools.pas');
@ -154,7 +157,8 @@ begin
T:=P.Targets.AddUnit('codetoolgdbtracer.pas');
// copy the compiled file, so the IDE knows how the package was compiled
P.InstallFiles.Add('CodeTools.compiled',AllOSes,'$(unitinstalldir)');
P.Sources.AddSrc('codetools.compiled');
P.InstallFiles.Add('codetools.compiled',AllOSes,'$(unitinstalldir)');
end;
end;

View File

@ -21,7 +21,7 @@
Author: Mattias Gaertner
Abstract:
TMethodJumpingCodeTool enhances TCodeTemplatesTool with functions to jump
TMethodJumpingCodeTool enhances TStandardCodeTool with functions to jump
between a method definition and its body and a forward procedure and its
body.