AI refac
Languages

بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ

Dart

How refac moves Dart files and renames Dart symbols with the Dart SDK analysis server.

The Dart backend uses the analysis server that ships with the Dart SDK (dart language-server). File moves rewrite the relative imports and the package: URIs that point at the moved file. Symbol rename covers a class, method, getter, field, named parameter, function, constant, or local.

Required tooling

The Dart SDK, found through REFAC_DART, then PATH. The SDK contains the analysis server, so there is nothing else to install. Flutter projects use the Dart that Flutter ships: add <flutter>/bin/cache/dart-sdk/bin to PATH, or point REFAC_DART at that dart. refac doctor dart checks the setup. See dart.dev/get-dart for the SDK.

In the project itself you need pubspec.yaml, and dart pub get must have run so that .dart_tool/package_config.json exists. Flutter projects use flutter pub get.

Moves

refac move \
  --project-path /path/to/package \
  --source-path lib/src/formatter.dart \
  --target-path lib/src/core/formatter.dart

refac asks the analysis server what a rename of the file changes (workspace/willRenameFiles), checks the plan, applies the edits, and moves the file.

package: imports

The server rewrites imports such as import 'package:myapp/src/formatter.dart' only when .dart_tool/package_config.json exists. refac checks the plan before it writes anything, and a plan that would leave an import pointing at a file that no longer exists is refused. The error lists those imports, says that nothing was changed, and points at dart pub get. Run it and try again, and the move should inshallah go through.

The same refusal applies when the server answers with a partial plan because it has not finished analysing the project. Run the move again in that case.

Preview

The preview is the plan the real move uses: the server's answer to workspace/willRenameFiles, checked for imports that would dangle, and counted instead of applied. The edits per file are the server's text edits, one per rewritten import or export. A plan that would leave an import pointing at a missing file is refused as in a real move.

Limitations

Individual files only. Folders are not supported as move sources.

One server at a time. The Dart analysis server is sensitive to concurrent starts. If you drive several refac commands from a script, run them one after the other.

Rename

refac rename \
  --project-path /path/to/package \
  --file lib/shapes.dart \
  --symbol area \
  --new-name surface \
  --line 7

The rename covers the whole package: export ... show lists, package: and relative imports, named arguments at every call, field formals (this.width), and [Name] links in doc comments. --project-path must hold pubspec.yaml and .dart_tool/package_config.json. Without the second file the server cannot resolve package: imports and would miss every file that imports the symbol, so the command stops before any server starts and says to run dart pub get.

  • Overrides are renamed by the server itself. Renaming Shape.area renames the overriding methods in subclasses and the calls through any of them.
  • Settling. The Dart server takes a changed document in without analysing it. refac waits until every changed document is fully analysed before it checks the result, so the proof can inshallah rely on complete answers.
  • Import paths point to refac move, because a path in an import is a file and not a symbol.
  • Leftovers. A call on a dynamic value (thing.area()) is not linked to the symbol. The note lists it with its line and ends with the rg -w command.

Limitations

  • Reflection through runtimeType prints the new name of a renamed class.
  • Generated files (*.g.dart) are renamed only where the server sees them. Regenerate afterwards.
  • A package inside a workspace needs one call per package root.
  • A class or function that other packages in the pub cache use cannot be renamed. The command stops and names those uses, because the server never edits dependencies.

After either operation, run dart analyze. It should inshallah report nothing new.

On this page