@@ -111,7 +111,7 @@ type Session struct {
111
111
112
112
// watches tracks the current watch globs and how many individual WatchedFiles
113
113
// are using each glob.
114
- watches map [fileSystemWatcherKey ]int
114
+ watches map [fileSystemWatcherKey ]* fileSystemWatcherValue
115
115
watchesMu sync.Mutex
116
116
}
117
117
@@ -154,7 +154,7 @@ func NewSession(init *SessionInit) *Session {
154
154
toPath ,
155
155
),
156
156
pendingATAChanges : make (map [tspath.Path ]* ATAStateChange ),
157
- watches : make (map [fileSystemWatcherKey ]int ),
157
+ watches : make (map [fileSystemWatcherKey ]* fileSystemWatcherValue ),
158
158
}
159
159
160
160
if init .Options .TypingsLocation != "" && init .NpmExecutor != nil {
@@ -422,43 +422,49 @@ func updateWatch[T any](ctx context.Context, session *Session, logger logging.Lo
422
422
defer session .watchesMu .Unlock ()
423
423
if newWatcher != nil {
424
424
if id , watchers := newWatcher .Watchers (); len (watchers ) > 0 {
425
- var newWatchers [] * lsproto.FileSystemWatcher
426
- for _ , watcher := range watchers {
425
+ var newWatchers collections. OrderedMap [ WatcherID , * lsproto.FileSystemWatcher ]
426
+ for i , watcher := range watchers {
427
427
key := toFileSystemWatcherKey (watcher )
428
- count := session .watches [key ]
429
- session .watches [key ] = count + 1
430
- if count == 0 {
431
- newWatchers = append (newWatchers , watcher )
428
+ value := session .watches [key ]
429
+ globId := WatcherID (fmt .Sprintf ("%s.%d" , id , i ))
430
+ if value == nil {
431
+ value = & fileSystemWatcherValue {id : globId }
432
+ session .watches [key ] = value
433
+ }
434
+ value .count ++
435
+ if value .count == 1 {
436
+ newWatchers .Set (globId , watcher )
432
437
}
433
438
}
434
- if len ( newWatchers ) > 0 {
435
- if err := session .client .WatchFiles (ctx , id , newWatchers ); err != nil {
439
+ for id , watcher := range newWatchers . Entries () {
440
+ if err := session .client .WatchFiles (ctx , id , [] * lsproto. FileSystemWatcher { watcher } ); err != nil {
436
441
errors = append (errors , err )
437
442
} else if logger != nil {
438
443
if oldWatcher == nil {
439
444
logger .Log (fmt .Sprintf ("Added new watch: %s" , id ))
440
445
} else {
441
446
logger .Log (fmt .Sprintf ("Updated watch: %s" , id ))
442
447
}
443
- for _ , watcher := range watchers {
444
- logger .Log ("\t " + * watcher .GlobPattern .Pattern )
445
- }
448
+ logger .Log ("\t " + * watcher .GlobPattern .Pattern )
446
449
logger .Log ("" )
447
450
}
448
451
}
449
452
}
450
453
}
451
454
if oldWatcher != nil {
452
- if id , watchers := oldWatcher .Watchers (); len (watchers ) > 0 {
455
+ if _ , watchers := oldWatcher .Watchers (); len (watchers ) > 0 {
453
456
var removedWatchers []WatcherID
454
457
for _ , watcher := range watchers {
455
458
key := toFileSystemWatcherKey (watcher )
456
- count := session .watches [key ]
457
- if count <= 1 {
459
+ value := session .watches [key ]
460
+ if value == nil {
461
+ continue
462
+ }
463
+ if value .count <= 1 {
458
464
delete (session .watches , key )
459
- removedWatchers = append (removedWatchers , id )
465
+ removedWatchers = append (removedWatchers , value . id )
460
466
} else {
461
- session . watches [ key ] = count - 1
467
+ value . count --
462
468
}
463
469
}
464
470
for _ , id := range removedWatchers {
@@ -498,16 +504,21 @@ func (s *Session) updateWatches(oldSnapshot *Snapshot, newSnapshot *Snapshot) er
498
504
oldSnapshot .ProjectCollection .ProjectsByPath (),
499
505
newSnapshot .ProjectCollection .ProjectsByPath (),
500
506
func (_ tspath.Path , addedProject * Project ) {
507
+ errors = append (errors , updateWatch (ctx , s , s .logger , nil , addedProject .programFilesWatch )... )
501
508
errors = append (errors , updateWatch (ctx , s , s .logger , nil , addedProject .affectingLocationsWatch )... )
502
509
errors = append (errors , updateWatch (ctx , s , s .logger , nil , addedProject .failedLookupsWatch )... )
503
510
errors = append (errors , updateWatch (ctx , s , s .logger , nil , addedProject .typingsWatch )... )
504
511
},
505
512
func (_ tspath.Path , removedProject * Project ) {
513
+ errors = append (errors , updateWatch (ctx , s , s .logger , removedProject .programFilesWatch , nil )... )
506
514
errors = append (errors , updateWatch (ctx , s , s .logger , removedProject .affectingLocationsWatch , nil )... )
507
515
errors = append (errors , updateWatch (ctx , s , s .logger , removedProject .failedLookupsWatch , nil )... )
508
516
errors = append (errors , updateWatch (ctx , s , s .logger , removedProject .typingsWatch , nil )... )
509
517
},
510
518
func (_ tspath.Path , oldProject , newProject * Project ) {
519
+ if oldProject .programFilesWatch .ID () != newProject .programFilesWatch .ID () {
520
+ errors = append (errors , updateWatch (ctx , s , s .logger , oldProject .programFilesWatch , newProject .programFilesWatch )... )
521
+ }
511
522
if oldProject .affectingLocationsWatch .ID () != newProject .affectingLocationsWatch .ID () {
512
523
errors = append (errors , updateWatch (ctx , s , s .logger , oldProject .affectingLocationsWatch , newProject .affectingLocationsWatch )... )
513
524
}
0 commit comments