Import calls pile up as scripts change. The plugin marks the ones a .ds or server .js file doesn't need, and Optimize Imports removes them, by hand, on save, on commit or as you type
What it removes
Code › Optimize Imports (Ctrl+Alt+O) removes the import calls the file doesn't need, then runs the IDE's JavaScript optimizer. A hint says how many went, such as "Removed 4 import calls"
| Call | Removed when |
|---|---|
| Any import call | An earlier call imports the same class, package or path |
importClass | Nothing uses its class |
importPackage | Nothing uses any of its classes. A package outside dw stays |
importScript | Nothing calls any of its functions |
| A call with a wrong argument | Never |
A name counts as used anywhere in the file, type annotations included. These importScript calls stay:
- The script has no functions, or the path doesn't resolve
- The file is itself imported by other scripts, which see the functions it imports
The editor shows the same calls in gray, from the Unused or repeated import call inspection, with a Remove the import call fix:
importPackage(dw.util) repeats an earlier import callimportPackage(dw.catalog) is not used
/* Calculates the basket */
importPackage(dw.util);
importPackage(dw.util);
importClass(dw.order.BasketMgr);
importPackage(dw.catalog);
importScript('app_core:util/Log.ds');
var m = new HashMap();/* Calculates the basket */
importPackage(dw.util);
var m = new HashMap();How lines are handled
- A call alone on its line takes the line with it
- A blank line left next to another one, or at the top of the file, goes too
- On a line shared with other code, only the call and the spaces beside it go
- A call inside a function goes like any other
- A call used as a value, as in
var x = importPackage(dw.campaign), stays, since removing it would break the statement
run(); importClass(dw.order.BasketMgr);
importClass(dw.catalog.ProductMgr); run();
function f() {
importPackage(dw.catalog);
return 1;
}
var x = importPackage(dw.campaign);run();
run();
function f() {
return 1;
}
var x = importPackage(dw.campaign);Where it runs
| Trigger | Where to turn it on |
|---|---|
| Code › Optimize Imports | Always there |
| Reformat Code | Optimize imports in the Reformat File dialog |
| Saving a file | Optimize imports in Settings › Tools › Actions on Save |
| Committing | Optimize imports in the commit options |
| Typing | Optimize imports on the fly, see On the fly |
It changes .ds and server .js files and needs a license. Templates and client .js files keep their calls
On the fly
Open the settings
Settings › Languages & Frameworks › Digital Script
Check Optimize imports on the fly
It's in the Imports group, off by default, and saved for you only, not with the project
Edit as usual
A call that turns unused goes once the file settles. The removal is one undo step, Optimize Imports on the Fly
It waits until it's safe to change the file:
- The caret isn't on an import call or the blank lines next to them
- The file has no errors outside the import calls
- Highlighting has finished, and no undo or redo is running
- You changed the file. Files you only opened keep their calls
It needs the Unused or repeated import call inspection on. The IDE's own Optimize imports on the fly in Settings › Editor › General › Auto Import covers other languages and doesn't turn this one on