Search code examples
delphiide

How can I prompt to add to the uses clause in Delphi


Just upgraded from Delphi XE to Tokyo, and was hoping for some updated IDE features.

One nice to have feature would be automatically providing options to add a unit to the uses clause. For example, if you reference something in code that isn't in a uses, it would be nice if the IDE prompted you to add the related unit(s).

For example, keeping it simple, in IntelliJ, you might declare a Button, but not yet have added the associated Library to the Import clause.

For Example IntelliJ/Android Studio

When this happens, the offending line is highlighted (just like in Delphi), but the IDE will let you add the necessary library with an Alt-Enter. If there are multiple libraries (it knows about it) it will prompt you for the one you want.

Can I do something like this in Delphi's Tokyo version?


Solution

  • Delphi 2007 and later versions support this for most types that it knows about (in the search or library path). (It may have been available in D2005/2006; I don't have them installed anywhere now to check. I know it was not in Delphi 7.)

    Put in the type, and use the Refactor menu (Refactor->Find Unit) or press Ctrl+Shift+A.

    Here's an example:

    ![Find unit dialog

    It's not 100% effective, but it's a vast improvement over the old way.

    (And yes, I know about TArray<string>, before someone chimes in. I just grabbed a quick type that I knew wouldn't be in the default VCL form uses clause for an example.)