Prevent methods with empty bodies from deletion on save

丶灬走出姿态 提交于 2020-01-21 11:19:07

问题


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?


回答1:


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;



回答2:


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;



回答3:


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.



来源:https://stackoverflow.com/questions/13524606/prevent-methods-with-empty-bodies-from-deletion-on-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!