Prevent methods with empty bodies from deletion on save
Asked Answered
W

3

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?

Wurst answered 23/11, 2012 at 7:18 Comment(2)
This happens with event handlers only. Write them without delay or comment them with todoBackdate
@user539484 please, post it as the answerWurst
B
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;
Backdate answered 23/11, 2012 at 9:44 Comment(9)
Still lacks a link to the intro-level text about "design class" concept, tho. Know any?Backdate
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)Koss
@Sir Rufo, yep, and that unpublished property makes stub handler way a simplier one.Backdate
Agree with David and you. Don't do much forms programming anymore, but your approach is what I used to do as well.Suctorial
@user539484 simplier - yes, must - noKoss
@user539484, I do not work much with delphi. Nevertheless the project I inherited is written in delphi and all the developers who managed it, declared their methods as if they were event handlers....so every Ctrl+S influenced any of them as any event handler. I never noticed myself, that this behaviour did not take place for methods in private or public or published areas. You made me catch this. That's why your post is the answer to my question. Others get upvotes from me. This post was indeed informative. Thanks everyone a lot.Wurst
@Sir Rufo, you are indeed right, but I was trying to stick to design-time behaviours as per original question.Backdate
@Konstantin Vasilcov, be aware what making an event handler method non-published will break Delphi's form loading system. This is little beyond the scope of the original question, but event handler method will be bound at run-time to event property by the means of name lookup (stringly!) and this requires both of them to be published (see TObject.FieldAddress and TObject.MethodAddress).Backdate
@user539484 I didn't mean to change event handlers' access modifiers. Only "auxiliary" methods not bound to any form event. Before this post I didn't distinguish these two categories. Another thanks!Wurst
L
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;
Leoni answered 23/11, 2012 at 7:22 Comment(7)
I know this way, but actually I would be happy to know some IDE option to turn off this behaviour at all.Wurst
@Konstantin: I don't think there is any. (Other than patching some binaries, maybe. :-))Crownwork
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 ;Fit
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.Fit
How I wish this feature would be available in VS (at least in pluginless Express edition I'm using).Macintyre
Or just write begin ; end; which involves even less typing ;)Bootery
+1: very informative....thanks a lot...I've written my thoughts in comment under the answer by user539484Wurst
S
8

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.

Supreme answered 23/11, 2012 at 8:3 Comment(2)
And if there might ever be one it will never make it into D7.Majordomo
+1 full agree with Sir Rufo, this answer is the one fitting to the question.Leoni

© 2022 - 2024 — McMap. All rights reserved.