GExperts: Improvement to the Comment Code / Uncomment Code experts

The Comment Code editor expert in GExperts lets you comment out a selected block of code with a single keyboard shortcut. You can configure which comment style to use per file extension: //, { }, (* *), /* */, or -- (SQL).

There is a problem with some comment styles though: For all but // Pascal does not support nesting comments of the same style. If you configure the expert to use (* *) and your selection already contains such a comment, the result is broken code. For example, selecting this block:

(* this does not work:
    LService := GxOtaGetIDEThemingServices;
    if Assigned(LService) and LService.IDEThemingEnabled then begin
      LService.ApplyTheme(MainMenu);
    end;
*)
DoSomethingElse;

and commenting it with (* *) would produce:

(* (* this does not work:
    LService := GxOtaGetIDEThemingServices;
    if Assigned(LService) and LService.IDEThemingEnabled then begin
      LService.ApplyTheme(MainMenu);
    end;
*)
DoSomethingElse; *)

The first *) ends the comment prematurely, leaving DoSomethingElse; *) as broken code. The same problem applies to { } and /* */ (for C) comments.

The fix

The Comment Code expert now checks whether the selected code contains characters that conflict with the configured block comment style. If it detects a conflict, it shows a dialog:

The warning dialog showing the text "The selected code already contains (* *) comments which would result in broken comments when using this comment style.

Click "Yes" to comment using // line comments instead.
Click "No" to leave the code unchanged.

If you check "Do not show again", your choice will be remembered. You can revert this on the "Suppressed Messages" tab of the GExperts configuration dialog.

[ ] Do not Show again      [Yes]  [No]
Warning Dialog

The dialog offers two options:

  • Yes falls back to // line comments, which can always be nested safely.
  • No leaves the code unchanged.

The “Do not show again” checkbox remembers your specific choice. If you check it and click Yes, future conflicts will silently use //. If you check it and click No, the expert will silently do nothing when a conflict is detected. You can revert this on the “Suppressed Messages” tab of the GExperts configuration dialog.

Uncomment also improved

The Uncomment Code expert has a related improvement. Previously, it always tried to remove the configured comment style. So if you had fallen back to // comments but your configured style was (* *), uncommenting would fail or produce wrong results.

Now the Uncomment expert detects when all lines in the selection start with // and uncomments them as line comments, regardless of the configured block comment style.

Framework improvement

To support this feature, the message box framework was extended. DoPermanentlySuppress and IsPermanentlySuppressed in TGxMsgBoxAdaptor are now virtual methods. Combined with the new FLastModalResult field (set by ShowGxMessageBox before calling DoPermanentlySuppress), subclasses can now implement per-result suppression, where the “Do not show again” checkbox remembers which button was pressed, not just the default.

This is based on feature request #178 Improve Comment Expert.

As usual, if you want to try this before the next official release, you can compile your own DLL.

Discussion about this in the corresponding post in the international Delphi Praxis forum.