7

It's quite a contradictory habit of mine to press Ctrl+S permanently. The negative side is that delphi deletes empty functions/procedures on save.

Is there a way to prevent IDE from deleting functions/procedures with empty bodies on save?

2
  • 6
    This happens with event handlers only. Write them without delay or comment them with todo
    – OnTheFly
    Nov 23, 2012 at 7:30
  • @user539484 please, post it as the answer
    – horgh
    Nov 23, 2012 at 7:40

3 Answers 3

18

Converted from the comment as per OP request. My comment is too tiny for an answer, so I'm going to add few details maybe already obvious to an OP.

This happens with event handlers only¹. Write them without delay or comment them with todo²

¹ That is, event handlers are methods of design class and they are created, listed and deleted (if caught empty when saving or compiling) by the form designer (this include data module designer and any of other custom designers installed). Confer to delegates you probably familiar with from C# background. Any other methods are subject to "manual" management.

² TODO items (Ctrl+Shift+T in default keybinding) are definitely better than just blank comments:

procedure TForm1.MagicButton1Click(Sender: TObject);
begin
  { TODO -ctomorrow : I'm going to write the code, I promise! }
end;

Possible special case

TAction with AutoCheck set must (see the comment from Sir Rufo below for another possibility at run time) have its OnExecute assigned in order to be Enabled. In this case it is inevitably to have such blank event handlers within design class. Example:

procedure TMonitor.AutoCheckActionExecute(Sender: TObject);
begin
  // dummy stub
  { DONE -crefactor : merge with other stub(s) }
end;
9
  • Still lacks a link to the intro-level text about "design class" concept, tho. Know any?
    – OnTheFly
    Nov 23, 2012 at 10:11
  • 1
    There is no special case with TAction. Just set TAction.DisableIfNoHandler to false. But you have to that at runtime, because it is not a published property (just public)
    – Sir Rufo
    Nov 23, 2012 at 10:27
  • @Sir Rufo, yep, and that unpublished property makes stub handler way a simplier one.
    – OnTheFly
    Nov 23, 2012 at 10:53
  • Agree with David and you. Don't do much forms programming anymore, but your approach is what I used to do as well. Nov 23, 2012 at 10:58
  • @user539484 simplier - yes, must - no
    – Sir Rufo
    Nov 23, 2012 at 11:39
13

Just add an empty comment like //

begin 
//
end;

an other way would by moving the declaration to the published part

type
  TForm5 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject); // will be removed if empty
  private
    { Private-Deklarationen }
  public
  published
    procedure Button2Click(Sender: TObject); // will not be removed if empty

    { Public-Deklarationen }
  end;
7
  • I know this way, but actually I would be happy to know some IDE option to turn off this behaviour at all.
    – horgh
    Nov 23, 2012 at 7:25
  • 2
    @Konstantin: I don't think there is any. (Other than patching some binaries, maybe. :-)) Nov 23, 2012 at 7:37
  • 7
    Hmm, I use the IDE's behavior to my benefit: If I want to DELETE an event handler, I just remove the code and save. Without this automagic behavior I'd need to: Delete the event block, delete it's declaration from the interface section, make a change to the DFM so that Delphi notices the event is no longer there, suffer through a number of error messages telling me the event handler is no longer there and needs to be removed from components. Also my "dummy" code is twice shorter! I only write ; Nov 23, 2012 at 7:59
  • 1
    Forgot to mention: this automatic behavior saves you from the trouble of deleting useles event handlers that get created when you mistakenly double-click the wrong component on a form, or when you mistakenly ctrl-enter the wrong event in object inspector. The single ; is a fair price to pay, IMHO. Nov 23, 2012 at 8:00
  • 1
    Or just write begin ; end; which involves even less typing ;) Nov 23, 2012 at 10:47
7

Is there a way to prevent IDE from deleting functions/ procedures with empty bodies on save?

There is no option in the IDE to disable this behaviour.

2
  • And if there might ever be one it will never make it into D7.
    – Uwe Raabe
    Nov 23, 2012 at 8:50
  • 1
    +1 full agree with Sir Rufo, this answer is the one fitting to the question.
    – bummi
    Nov 23, 2012 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.