@@ -26,6 +26,8 @@ import { logProcess } from "../utils/processes";
2626import * as channels from "./channels" ;
2727
2828const packageNameRegex = new RegExp ( "^[a-z][a-z0-9_]*$" ) ;
29+ let runPubGetDelayTimer : NodeJS . Timer | undefined ;
30+ let lastSaveReason : vs . TextDocumentSaveReason ;
2931
3032export class SdkCommands {
3133 private flutterScreenshotPath ?: string ;
@@ -198,6 +200,7 @@ export class SdkCommands {
198200 } ) ) ;
199201
200202 // Hook saving pubspec to run pub.get.
203+ context . subscriptions . push ( vs . workspace . onWillSaveTextDocument ( ( e ) => lastSaveReason = e . reason ) ) ;
201204 context . subscriptions . push ( vs . workspace . onDidSaveTextDocument ( ( td ) => {
202205 const conf = config . for ( td . uri ) ;
203206
@@ -212,7 +215,24 @@ export class SdkCommands {
212215 if ( sdks . projectType === ProjectType . Fuchsia && ! conf . runPubGetOnPubspecChangesIsConfiguredExplicitly )
213216 return ;
214217
215- vs . commands . executeCommand ( "dart.getPackages" , td . uri ) ;
218+ // Cancel any existing delayed timer.
219+ if ( runPubGetDelayTimer ) {
220+ clearTimeout ( runPubGetDelayTimer ) ;
221+ }
222+
223+ // If the save was triggered by one of the auto-save options, then debounce.
224+ if ( lastSaveReason === vs . TextDocumentSaveReason . FocusOut
225+ || lastSaveReason === vs . TextDocumentSaveReason . AfterDelay ) {
226+
227+ runPubGetDelayTimer = setTimeout ( ( ) => {
228+ runPubGetDelayTimer = undefined ;
229+ vs . commands . executeCommand ( "dart.getPackages" , td . uri ) ;
230+
231+ } , 10000 ) ; // TODO: Does this need to be configurable?
232+ } else {
233+ // Otherwise execute immediately.
234+ vs . commands . executeCommand ( "dart.getPackages" , td . uri ) ;
235+ }
216236 } ) ) ;
217237 }
218238
0 commit comments