The code formatter in GExperts (which I think shares a lot of code with DelForEx) is here:
https://sourceforge.net/p/gexperts/code/HEAD/tree/trunk/Source/Formatter/
It uses ToolsAPI to grab the instance of the delphi IDE editor, and registers itself as an expert in the IDE.
It has a "format file" option too.
If someone was really ambitious, it might be possible to use most of this - create a new wrapper that instead takes a string (or stream) and formats it, returning the results to the ScrMemo or AdvMemo.
Might be a LOT of work: I'm just wondering if the non-standard syntax would break the parser (not requiring var types, etc).
(Actually, I just tried it on fSnake.psc - copied and pasted the code into a new unit in RX10.1. It formatted just fine).
There's also formatter experts in cnPack and JCL.
Cheers,
EdB