mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-29 09:50:31 +02:00
codetools: example for indentation parser
git-svn-id: trunk@20144 -
This commit is contained in:
parent
3fc7d3dbfc
commit
fafb745f2d
@ -29,17 +29,53 @@ program AutoIndent;
|
||||
|
||||
uses
|
||||
Classes, SysUtils, DefineTemplates, CodeToolsConfig, FileProcs,
|
||||
CodeToolsStructs, CodeToolManager, CodeCache;
|
||||
CodeToolsStructs, CodeToolManager, CodeCache, CodeBeautifier;
|
||||
|
||||
var
|
||||
Code: TCodeBuffer;
|
||||
Filename: String;
|
||||
FAB: TFullyAutomaticBeautifier;
|
||||
Y: LongInt;
|
||||
X: LongInt;
|
||||
p: integer;
|
||||
Indentation: TFABIndentation;
|
||||
begin
|
||||
if Paramcount>0 then begin
|
||||
if Paramcount<>3 then begin
|
||||
writeln('Usage: '+ParamStrUTF8(0)+' filename line column');
|
||||
exit;
|
||||
end;
|
||||
Filename:=ParamStrUTF8(1);
|
||||
Y:=StrToInt(ParamStrUTF8(2));
|
||||
X:=StrToInt(ParamStrUTF8(3));
|
||||
end else begin
|
||||
Filename:=ExpandFileNameUTF8('scanexamples/indentation.pas');
|
||||
X:=5;
|
||||
Y:=41;
|
||||
end;
|
||||
|
||||
// load the example unit
|
||||
Filename:=ExpandFileNameUTF8('scanexamples/indentation.pas');
|
||||
Code:=CodeToolBoss.LoadFile(Filename,false,false);
|
||||
if Code=nil then
|
||||
raise Exception.Create('unable to read '+Filename);
|
||||
|
||||
|
||||
FAB:=TFullyAutomaticBeautifier.Create;
|
||||
try
|
||||
Code.LineColToPosition(Y,X,p);
|
||||
if p<1 then begin
|
||||
writeln('ERROR: invalid position: X=',X,' Y=',Y);
|
||||
exit;
|
||||
end;
|
||||
if FAB.GetIndent(Code.Source,p,true,Indentation) then begin
|
||||
writeln('Indent=',Indentation.Indent);
|
||||
writeln('UseTabs=',Indentation.UseTabs);
|
||||
writeln('InsertEmptyLines=',Indentation.InsertEmptyLines);
|
||||
end else begin
|
||||
writeln('Error: GetIndent failed');
|
||||
end;
|
||||
|
||||
finally
|
||||
FAB.Free;
|
||||
end;
|
||||
end.
|
||||
|
||||
|
@ -5,9 +5,67 @@ unit indentation;
|
||||
interface
|
||||
|
||||
uses
|
||||
Classes, SysUtils;
|
||||
Classes, SysUtils;
|
||||
|
||||
type
|
||||
TEnums = (
|
||||
enum1,
|
||||
enum2,
|
||||
enum3
|
||||
);
|
||||
|
||||
TMyRecord = record
|
||||
i: integer;
|
||||
end;
|
||||
PMyRecord = ^TMyRecord;
|
||||
|
||||
{ TMyClass }
|
||||
|
||||
TMyClass = class(TObject)
|
||||
public
|
||||
procedure DoSomething1(a, b, c: integer);
|
||||
procedure Code;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
{ TMyClass }
|
||||
|
||||
procedure TMyClass.DoSomething1(a, b, c: integer);
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
if i=0 then
|
||||
begin
|
||||
repeat
|
||||
if a=2 then
|
||||
;
|
||||
until b=3;
|
||||
try
|
||||
Code;
|
||||
finally
|
||||
Code;
|
||||
end;
|
||||
try
|
||||
Code;
|
||||
except
|
||||
on e: exception do
|
||||
;
|
||||
end;
|
||||
end
|
||||
else
|
||||
begin
|
||||
case c of
|
||||
1: Code;
|
||||
2:
|
||||
begin
|
||||
code;
|
||||
end;
|
||||
else
|
||||
code;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user