6

I am finding that certain kinds of code cleanups and refactorings are made VERY difficult by the difficulty of adding a unit to the uses-clauses of a large project.

I want to add a unit to the interface-uses-clause of all delphi .pas units in a single project, and that means manually doing that in over 500 places. Every time I refactor a giant unit and split it from one unit into two, I can probably search and replace using something like "notepad++" to change "MyOldUnit," to "MyOldUnit,MyNewUnit," but sometimes, that's just not safe to do. It also misses the cases where "MyOldUnit" is the last thing in the uses clause ("uses MyOldUnit;").

Anyways, search and replacing in files is dangerous business. If no such tool exists, I am contemplating writing one, using the Castalia delphi parser. I have checked GExperts, Castalia, ModelMakerCodeExplorer and none of them have a way to batch-insert units into all uses clauses in a project. I'm hoping a tool to do this exists.

Secondly, in many cases, I'm moving a function from one unit where it doesn't belong to another, but this means I need to add that unit to 30% of the project's units, but not the other 70% where it's already added. That means a parser approach is required, not a regex approach.

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • 2
    There's the (very old) `JclUsesWizard` in jcl\experts\useswizard, perhaps it can serve you as a starting point. – Ondrej Kelle Oct 11 '12 at 15:10
  • You don't need a real parser to do this. Just a nasty regex based script using Perl or Python or whatever you like for regex scripting. You'd need a real parser if you wanted something robust, but for a one shot application to your own code base, you don't. – David Heffernan Oct 11 '12 at 15:16
  • related question, but not the same: http://stackoverflow.com/questions/9409790/any-tool-to-suggest-unit-reference-automatically-for-delphi-2010 – Warren P Oct 11 '12 at 15:23
  • The trouble with a regex approach is I need to handle multiple cases. In the case of a brand new unit, that unit will never be in any of the files, and it's easy. But I also find I need to move functions from one unit to another existing unit, so I need to ADD to all units that don't already include that unit in the uses clause. – Warren P Oct 11 '12 at 15:24
  • @DavidHeffernan, there is actually a "Regular expressions" option in the Delphi Replace dialog. but I haven't been able to replace jack $%* with that feature ever... – kobik Oct 11 '12 at 15:25
  • @kobik I've used that plenty often. It works well for very simple tasks. But these needs more than that. – David Heffernan Oct 11 '12 at 15:29
  • 3
    @WarrenP Well, you're a programmer aren't you?! It's what we do! – David Heffernan Oct 11 '12 at 15:30
  • I know. I'm going to build this puppy, but I suspect I'll have to do it on my own spare time. – Warren P Oct 11 '12 at 15:41
  • 2
    What would be awkward is writing your own solution for this and as you're finishing it up you learn that there is already a tool. – Jerry Dodge Oct 13 '12 at 17:12

1 Answers1

3

Because we all write code we will certainly use in other projects. If you move interface parts from one unit to another you will break your projects. Same with Old and New Units.

But you can refactor without breaking your projects. Just mark the parts (unit, class, method, procedure) as deprecated. Your Code is working, but you will be warned by the compiler.

Here an example of moving a procedure from one unit to another:

unit Foo;

interface

procedure FooProc; deprecated; // new location in unit FooNew

implementation

uses
  FooNew;

procedure FooProc;
begin
  FooNew.FooProc;
end;

end.
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73