dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.21k stars 1.57k forks source link

Analysis server should not need to resolve a file in order to format it #56960

Open stereotype441 opened 4 hours ago

stereotype441 commented 4 hours ago

The analysis server code for running the formatter calls AnalysisDriver.getResolvedUnit, which means that it won't be able to format the file until it's fully resolved it. It would be nice if it could use an analysis API that didn't require resolution, since that could in principle make formatting much faster.

The reason it currently uses AnalysisDriver.getResolvedUnit is because it needs unit.libraryElement2.effectiveLanguageVersion in order to tell the formatter which language version to use for formatting. But this feels like overkill; it should be possible to figure out the langauge version based on the unresolved AST and some information from the context.

See discussion here: https://dart-review.googlesource.com/c/sdk/+/391840/comment/5e5314af_b5c7db9a/

bwilkerson commented 4 hours ago

See also https://dart-review.googlesource.com/c/sdk/+/391663/2/pkg/analysis_server/lib/src/lsp/source_edits.dart, which indicates that the problem can be resolved by adding one public getter to CompilationUnit (a getter that's already implemented in CompilationUnitImpl).

munificent commented 4 hours ago

I ran into a similar issue with the IDE integration and @DanTup got that sorted out so maybe we can do something similar here.