WithLanguageServiceAndSnapshot synchronously acquires a ref'd snapshot and creates a language service for the given URI. fn receives both the language service and the backing snapshot so it can clone the snapshot (e.g. to enable auto-imports). The snapshot is kept alive until the async work complete
( ctx context.Context, uri lsproto.DocumentUri, fn func(*ls.LanguageService, *Snapshot) (func() error, error), )
| 1103 | // directly—language services continue to work even after their backing |
| 1104 | // snapshot has been disposed. |
| 1105 | func (s *Session) WithLanguageServiceAndSnapshot( |
| 1106 | ctx context.Context, |
| 1107 | uri lsproto.DocumentUri, |
| 1108 | fn func(*ls.LanguageService, *Snapshot) (func() error, error), |
| 1109 | ) (func() error, error) { |
| 1110 | snapshot, _, languageService, err := s.getSnapshotAndDefaultProject(ctx, uri, true /*callerRef*/) |
| 1111 | if err != nil { |
| 1112 | return nil, err |
| 1113 | } |
| 1114 | asyncWork, err := fn(languageService, snapshot) |
| 1115 | if err != nil || asyncWork == nil { |
| 1116 | snapshot.Deref(s) |
| 1117 | return nil, err |
| 1118 | } |
| 1119 | return func() error { |
| 1120 | defer snapshot.Deref(s) |
| 1121 | return asyncWork() |
| 1122 | }, nil |
| 1123 | } |
| 1124 | |
| 1125 | // GetLanguageServiceWithAutoImports clones the given snapshot with auto-import |
| 1126 | // preparation for the given URI, without flushing pending file changes. |