Menu ▾ ▴

#269 Register the code formatter with the IDE via IOTACodeFormatterServices (Delphi 13)

closed-fixed
nobody
2026-10-02
2026-10-01
No

The ToolsAPI of Delphi 13 (checked in 13.2, not present in 12) has an interface for adding a code formatter to the IDE: a plugin implements IOTACodeFormatter and registers it with IOTACodeFormatterServices.AddFormatter.

The GExperts code formatter could use it in Delphi 13 and later instead of only its own shortcut. That would give:

  • formatting through the IDE's own format command
  • format on save (CanFormatOnSave)
  • breakpoints, bookmarks and other gutter items moved to their new lines after formatting (SupportsOffsetRemapping with GetStoredOffsetsForFormatter / SetStoredOffsetsForFormatter)

The IDE passes the source as UTF8String and positions as byte offsets, so the formatter would have to map those and report how they move.

Still to be checked: whether the IDE actually provides the service at run time, which IDE commands call a registered formatter, and how this interacts with the IDE's own formatter.

Discussion

  • Thomas Mueller

    Thomas Mueller - 2026-10-01

    Tested with a small test package in the 32-bit IDE of Delphi 13.2:

    • IOTACodeFormatterServices is available. Before the test formatter was registered, CanHandleFile('x.pas') returned False: Delphi 13 no longer has a code formatter of its own.
    • The IDE's Format Source action (actFormatSource) calls the registered formatter: HandleFile, GetConfirmDlg, SupportsOffsetRemapping, then FormatFile. The result replaces the editor text. Format Document (EditFormatDocument) does not call it.
    • With a selection, the IDE first shows its own dialog asking whether to format the entire file or the selected portion, then calls FormatSelected. The IDE replaces the whole file with what FormatSelected returns, so it must return the complete file with only the selection formatted.
    • Saving a file calls CanFormatOnSave and, if that returns True, FormatFile, without asking.
    • Offset remapping works: a breakpoint on line 10 (stored offset 92) moved to line 11 after the offset was shifted by the length of an inserted line.
    • FormatLine, CanFormatLine and AutoFormatLine were not called; typing in the editor was not tested.
     
  • Thomas Mueller

    Thomas Mueller - 2026-10-02

    Implemented in revision #5857. The option is on the Misc. tab of the Code Formatter configuration: "Register as the IDE's code formatter (Format Source command)". It only takes effect while the Code Formatter expert is enabled.

     

Log in to post a comment.