From 726ddabf1f6a7edf5ab68c30ca87f0f3c605b6de Mon Sep 17 00:00:00 2001
From: Nayeem Rahman <nayeemrmn99@gmail.com>
Date: Wed, 27 Mar 2024 16:02:05 +0000
Subject: [PATCH] fix(lsp): use registry cache for completion search (#23094)

---
 cli/lsp/language_server.rs | 41 +++++++++++---------------------------
 1 file changed, 12 insertions(+), 29 deletions(-)

diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs
index e7e48a04c2..8f52f6d2df 100644
--- a/cli/lsp/language_server.rs
+++ b/cli/lsp/language_server.rs
@@ -504,23 +504,14 @@ impl Inner {
       module_registries_location.clone(),
       http_client.clone(),
     );
-    let location = dir.deps_folder_path();
-    let deps_http_cache = Arc::new(GlobalHttpCache::new(
-      location,
-      crate::cache::RealDenoCacheEnv,
-    ));
-    let mut deps_file_fetcher = FileFetcher::new(
-      deps_http_cache.clone(),
-      CacheSetting::RespectHeaders,
-      true,
-      http_client.clone(),
-      Default::default(),
-      None,
-    );
-    deps_file_fetcher.set_download_log_level(super::logging::lsp_log_level());
-    let jsr_search_api = CliJsrSearchApi::new(deps_file_fetcher);
+    let jsr_search_api =
+      CliJsrSearchApi::new(module_registries.file_fetcher.clone());
     let npm_search_api =
       CliNpmSearchApi::new(module_registries.file_fetcher.clone());
+    let deps_http_cache = Arc::new(GlobalHttpCache::new(
+      dir.deps_folder_path(),
+      crate::cache::RealDenoCacheEnv,
+    ));
     let documents = Documents::new(deps_http_cache.clone());
     let cache_metadata = cache::CacheMetadata::new(deps_http_cache.clone());
     let performance = Arc::new(Performance::default());
@@ -752,34 +743,26 @@ impl Inner {
     )?;
     let root_cert_store_provider =
       Arc::new(LspRootCertStoreProvider(root_cert_store));
-    let module_registries_location = dir.registries_folder_path();
     self.http_client = Arc::new(HttpClient::new(
       Some(root_cert_store_provider),
       workspace_settings
         .unsafely_ignore_certificate_errors
         .clone(),
     ));
+    self.module_registries_location = dir.registries_folder_path();
     self.module_registries = ModuleRegistry::new(
-      module_registries_location.clone(),
+      self.module_registries_location.clone(),
       self.http_client.clone(),
     );
-    self.module_registries_location = module_registries_location;
+    self.jsr_search_api =
+      CliJsrSearchApi::new(self.module_registries.file_fetcher.clone());
+    self.npm.search_api =
+      CliNpmSearchApi::new(self.module_registries.file_fetcher.clone());
     // update the cache path
     let global_cache = Arc::new(GlobalHttpCache::new(
       dir.deps_folder_path(),
       crate::cache::RealDenoCacheEnv,
     ));
-    let mut deps_file_fetcher = FileFetcher::new(
-      global_cache.clone(),
-      CacheSetting::RespectHeaders,
-      true,
-      self.http_client.clone(),
-      Default::default(),
-      None,
-    );
-    deps_file_fetcher.set_download_log_level(super::logging::lsp_log_level());
-    self.jsr_search_api = CliJsrSearchApi::new(deps_file_fetcher.clone());
-    self.npm.search_api = CliNpmSearchApi::new(deps_file_fetcher);
     let maybe_local_cache =
       self.config.tree.root_vendor_dir().map(|local_path| {
         Arc::new(LocalLspHttpCache::new(local_path, global_cache.clone()))