Using PTop lazarus / 62-General ---------------------------------------------------------------------- simsee · 2022-02-26 18:27 Dear all, I want to use PTop to format code stored in a string. Here is my demo code. Unfortunately, it raises an exception at run time. Anyone have any suggestions? Thanks. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1;uses  Classes, PTopU;var  Code : string; begin  Code:=    'program xxx;'+LineEnding+    'begin'+LineEnding+    'writeln;'+LineEnding+    'end.';   with TPrettyPrinter.Create do    begin      Source:=TStringStream.Create;      Dest:=TStringStream.Create;      Source.Position:=0;      Source.WriteAnsiString(Code);      PrettyPrint;      Dest.Position:=0;      Code:=Dest.ReadAnsiString;      Source.Free;    Dest.Free;      Free;    end;   writeln(Code);  readln;end. ---------------------------------------------------------------------- Leledumbo · 2022-02-26 19:48 Somehow it doesn't seem to like the ReadAnsiString, not sure why. Changing it to Read works, though. Maybe you need to take a peek at ptop.pp to see how the class is used. ---------------------------------------------------------------------- simsee · 2022-02-26 21:58 Eventually, after several attempts, I found this solution that works. Probably not the best. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1;uses  Classes, PTopU;var  aCode : string;   function FormatCode(var Code : string):boolean;  var    TmpCode : TStringList;  begin    TmpCode:=TStringList.Create;    TmpCode.Text:=Code;     with TPrettyPrinter.Create do      begin        Source:=TMemoryStream.Create;        Dest:=TMemoryStream.Create;      TmpCode.SaveToStream(Source);        Source.Position:=0;        Result:=PrettyPrint;        Dest.Position:=0;        TmpCode.LoadFromStream(Dest);        Source.Free;        Dest.Free;      Free;      end;     Code:=TmpCode.Text;    TmpCode.Free;  end; begin  aCode:=    'program xxx;'+LineEnding+    'begin'+LineEnding+    'writeln;'+LineEnding+    'end.';   FormatCode(aCode);  writeln(aCode);  readln;end. ---------------------------------------------------------------------- avk · 2022-02-26 22:14 Just tried this version: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   ...  with TPrettyPrinter.Create do    try      Source := TStringStream.Create(Code);      Dest := TStringStream.Create;      PrettyPrint;      Code := TStringStream(Dest).DataString;      Source.Free;      Dest.Free;    finally      Free;    end;  WriteLn(Code);  ... it seems to work. ---------------------------------------------------------------------- simsee · 2022-02-26 22:30 This version is much better. Thanks.