Vincent Parrett 750 Posted February 10, 2020 Hi All Anyone know of a tool that can fix up unit namespaces in the uses clause, ie change uses sysutils, stdctrls; to uses System.SysUtils, Vcl.StdCtrls; I have a few thousand units to go through and clean up, I've tried regex etc but it's not reliable enough. 1 Share this post Link to post
Larry Hengen 39 Posted February 10, 2020 I believe the GExperts Uses Clause manager will remove or add unit prefixes, just not sure if you can automate that without extracting the pertinent code from their repo and fashioning a tool. Perhaps macros with the UCM? Share this post Link to post
Fr0sT.Brutal 900 Posted February 10, 2020 What is not reliable enough in simple text replace? 1 Share this post Link to post
dummzeuch 1505 Posted February 10, 2020 (edited) GExperts has this functionality, but only for the current unit. Feel free to use that code to build a batch tool (it's not quite so easy since you must take the project settings and search paths into account). I'll accept patches, of course. 😉 Edited February 10, 2020 by dummzeuch 2 Share this post Link to post
Uwe Raabe 2057 Posted February 10, 2020 I have written a tool including that functionality some time ago. Although I didn't find the time to polish it up before going public, it basically does what it is supposed to. Expanding unit scope names is only part of the whole process, which consists of resolve all unit aliases expand unit scope names group units compress uses clause Taking your example above it will convert uses sysutils, stdctrls; into uses System.SysUtils, Vcl.StdCtrls; Feel free to adjust the sources to your needs. UsesCleanerSource.zip 1 3 Share this post Link to post
Vincent Parrett 750 Posted February 10, 2020 @Uwe Raabe thank! I'll take a look at it tomorrow. Share this post Link to post
MarkShark 27 Posted February 10, 2020 I use the MMX Code Explorer for that. The feature is a bit hidden and listed under "Group and Sort Uses". I think it only works on the current uses statement (e.g. you need to put the caret on the uses clause and hit the hotkey for it. It does work well as I've used it quite a bit when working with my older units. Share this post Link to post
Uwe Raabe 2057 Posted February 10, 2020 The code used in MMX Code Explorer is almost identical to what I attached above. The command line application allows targeting multiple units in one go, while as you noticed MMX only works on the uses clause the cursor is positioned in the current unit. Share this post Link to post
MarkShark 27 Posted February 10, 2020 Uwe - thanks for the source for that! I'll be checking that out. Share this post Link to post
Remy Lebeau 1396 Posted February 11, 2020 On 2/9/2020 at 6:29 PM, Vincent Parrett said: I have a few thousand units to go through and clean up, I've tried regex etc but it's not reliable enough. Do you really need this, though? If you simply include 'System' and 'Vcl' in the Unit Scope Names list in your project options, you won't need to update the 'uses' clauses at all. 1 Share this post Link to post
Bill Meyer 337 Posted February 11, 2020 (edited) On 2/10/2020 at 6:47 AM, Uwe Raabe said: The code used in MMX Code Explorer is almost identical to what I attached above. The command line application allows targeting multiple units in one go, while as you noticed MMX only works on the uses clause the cursor is positioned in the current unit. Is it not true, though, that they both refuse to tangle with comments -- and compiler directives -- in the uses clauses? That is an issue for the couple of thousand modules I need to process. And yes, it is a less simple problem than it looks. I spent some time on it, and was not willing to spend more, as I have other priorities which demand my time. Edited February 11, 2020 by Bill Meyer Share this post Link to post
Vincent Parrett 750 Posted February 11, 2020 59 minutes ago, Remy Lebeau said: Do you really need this, though? If you simply include 'System' and 'Vcl' in the Unit Scope Names list in your project options, you won't need to update the 'uses' clauses at all. Not strictly speaking, but not relying on the scope names does actually speed up compilation. Then there's the different code styles in thousands of units created over 20 years, I'm a bit OCD I guess and want to apply the same standard across all my code! 1 Share this post Link to post
Uwe Raabe 2057 Posted February 11, 2020 Yes, there is always room for improvement. Share this post Link to post
Uwe Raabe 2057 Posted February 11, 2020 59 minutes ago, Bill Meyer said: Is it not true, though, that they both refuse to tangle with comments -- and compiler directives -- in the uses clauses? Regarding compiler directives you can give the above sources a try. Although there might still be cases where it fails, they should do a better job than the official MMX version does. Share this post Link to post
Bill Meyer 337 Posted February 11, 2020 5 minutes ago, Uwe Raabe said: Regarding compiler directives you can give the above sources a try. Although there might still be cases where it fails, they should do a better job than the official MMX version does. If I get out from under some of my other tasks, I will. My TODO list is a bit long at the moment. 😉 Share this post Link to post
Uwe Raabe 2057 Posted February 11, 2020 32 minutes ago, Bill Meyer said: My TODO list is a bit long at the moment. Same here. That's why MMX development is actually stalled - unfortunately. Otherwise the beta version would have long been finished. Share this post Link to post
Vincent Parrett 750 Posted February 11, 2020 I just got done with this and it just worked! I did have to modify the hard coded path for the UsesHelper.SearchPath to match the delphi version I'm using. I tested first by specifying an output directory, and after checking with beyond compare to see how it did, I just processed the original files. It did also introduce some compilation errors, where I have prefixed function calls with the unit names (e.g SysUtils.DeleteFile ), simple to fix though. Thanks @Uwe Raabe 2 Share this post Link to post
Bill Meyer 337 Posted February 17, 2020 On 2/11/2020 at 3:40 PM, Uwe Raabe said: Regarding compiler directives you can give the above sources a try. Although there might still be cases where it fails, they should do a better job than the official MMX version does. Spent a bit of time with your source. So far, it appears to correctly handle some of the thornier issues which we have in our legacy code. I do need to do some additional testing, and find my way to some of the settings, but what I see so far is very encouraging. Share this post Link to post
MathewV 0 Posted March 17, 2022 @Uwe Raabe this is great - thanks so much for putting it together. One question. We recently shifted our project to FMX but updated the units by removing "VCL" from the project Scope Names and adding "FMX". When we ran this cleaner it is inserting VCL where we need it to be FMX. Aside from a manual review, is there a known solution for this? Thanks again Mathew Share this post Link to post
Vincent Parrett 750 Posted March 17, 2022 14 minutes ago, MathewV said: Aside from a manual review, is there a known solution for this? If you look at this method procedure TSourceFileUsesClauseFormatter.InitSettings; begin UsesHelper.UnitAliases := 'WinTypes=Winapi.Windows;WinProcs=Winapi.Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE;'; UsesHelper.UnitScopeNames := 'Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;System;Xml;Data;Datasnap;Web;Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell'; UsesHelper.SearchPath := 'c:\Program Files (x86)\Embarcadero\Studio\19.0\lib\win32\release'; UsesHelper.GroupNames := '<UnitScopeNames>'; end; I would hazzard a guess that you can solve this by removing the vcl unit scope names and adding the fmx ones. I don't use fmx so not 100% on this, but a new blank multi device application, the default unit scope names are Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;System;Xml;Data;Datasnap;Web;Soap So maybe that's all you need 🤔 Share this post Link to post
Uwe Raabe 2057 Posted March 17, 2022 Meanwhile the project is on GitHub: https://github.com/UweRaabe/UsesCleaner You don't even need to change the sources to handle FMX. The -c command line parameter lets you specify a configuration file with your preferred settings. An example can be found on GitHub. 1 Share this post Link to post