refactor: break up ProcState (#18707)

1. Breaks up functionality within `ProcState` into several other structs
to break out the responsibilities (`ProcState` is only a data struct
now).
2. Moves towards being able to inject dependencies more easily and have
functionality only require what it needs.
3. Exposes `Arc<T>` around the "service structs" instead of it being
embedded within them. The idea behind embedding them was to reduce the
verbosity of needing to pass around `Arc<...>`, but I don't think it was
exactly working and as we move more of these structs to be more
injectable I don't think the extra verbosity will be a big deal.
This commit is contained in:
David Sherret 2023-04-14 16:22:33 -04:00 committed by GitHub
parent a411144219
commit 136dce67ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 1506 additions and 1285 deletions

View file

@ -101,7 +101,7 @@ pub struct StateSnapshot {
pub cache_metadata: cache::CacheMetadata,
pub documents: Documents,
pub maybe_import_map: Option<Arc<ImportMap>>,
pub maybe_npm_resolver: Option<NpmPackageResolver>,
pub maybe_npm_resolver: Option<Arc<NpmPackageResolver>>,
}
#[derive(Debug)]
@ -145,13 +145,13 @@ pub struct Inner {
/// A lazily create "server" for handling test run requests.
maybe_testing_server: Option<testing::TestServer>,
/// Npm's registry api.
npm_api: CliNpmRegistryApi,
npm_api: Arc<CliNpmRegistryApi>,
/// Npm cache
npm_cache: NpmCache,
npm_cache: Arc<NpmCache>,
/// Npm resolution that is stored in memory.
npm_resolution: NpmResolution,
npm_resolution: Arc<NpmResolution>,
/// Resolver for npm packages.
npm_resolver: NpmPackageResolver,
npm_resolver: Arc<NpmPackageResolver>,
/// A collection of measurements which instrument that performance of the LSP.
performance: Arc<Performance>,
/// A memoized version of fixable diagnostic codes retrieved from TypeScript.
@ -182,13 +182,15 @@ impl LanguageServer {
.into_iter()
.map(|d| (d.specifier().clone(), d))
.collect::<HashMap<_, _>>();
// todo(dsherret): don't use ProcState here
let ps = ProcState::from_cli_options(Arc::new(cli_options)).await?;
let mut inner_loader = ps.create_graph_loader();
let mut inner_loader = ps.module_graph_builder.create_graph_loader();
let mut loader = crate::lsp::documents::OpenDocumentsGraphLoader {
inner_loader: &mut inner_loader,
open_docs: &open_docs,
};
let graph = ps
.module_graph_builder
.create_graph_with_loader(roots.clone(), &mut loader)
.await?;
graph_util::graph_valid(
@ -418,14 +420,14 @@ fn create_lsp_structs(
dir: &DenoDir,
http_client: HttpClient,
) -> (
CliNpmRegistryApi,
NpmCache,
NpmPackageResolver,
NpmResolution,
Arc<CliNpmRegistryApi>,
Arc<NpmCache>,
Arc<NpmPackageResolver>,
Arc<NpmResolution>,
) {
let registry_url = CliNpmRegistryApi::default_url();
let progress_bar = ProgressBar::new(ProgressBarStyle::TextOnly);
let npm_cache = NpmCache::from_deno_dir(
let npm_cache = Arc::new(NpmCache::from_deno_dir(
dir,
// Use an "only" cache setting in order to make the
// user do an explicit "cache" command and prevent
@ -434,14 +436,15 @@ fn create_lsp_structs(
CacheSetting::Only,
http_client.clone(),
progress_bar.clone(),
);
let api = CliNpmRegistryApi::new(
));
let api = Arc::new(CliNpmRegistryApi::new(
registry_url.clone(),
npm_cache.clone(),
http_client,
progress_bar.clone(),
);
let resolution = NpmResolution::from_serialized(api.clone(), None, None);
));
let resolution =
Arc::new(NpmResolution::from_serialized(api.clone(), None, None));
let fs_resolver = create_npm_fs_resolver(
npm_cache.clone(),
&progress_bar,
@ -452,7 +455,11 @@ fn create_lsp_structs(
(
api,
npm_cache,
NpmPackageResolver::new(resolution.clone(), fs_resolver, None),
Arc::new(NpmPackageResolver::new(
resolution.clone(),
fs_resolver,
None,
)),
resolution,
)
}
@ -695,12 +702,12 @@ impl Inner {
maybe_import_map: self.maybe_import_map.clone(),
maybe_npm_resolver: Some({
// create a new snapshotted npm resolution and resolver
let resolution = NpmResolution::new(
let resolution = Arc::new(NpmResolution::new(
self.npm_api.clone(),
self.npm_resolution.snapshot(),
None,
);
NpmPackageResolver::new(
));
Arc::new(NpmPackageResolver::new(
resolution.clone(),
create_npm_fs_resolver(
self.npm_cache.clone(),
@ -710,7 +717,7 @@ impl Inner {
None,
),
None,
)
))
}),
})
}
@ -1130,7 +1137,6 @@ impl Inner {
self.client.show_message(MessageType::WARNING, err);
}
// self.refresh_documents_config(); // todo(THIS PR): REMOVE
self.assets.intitialize(self.snapshot()).await;
self.performance.measure(mark);