Skip to content

Commit 2e029f7

Browse files
committed
feat: Improve nested completions
1 parent 5a279b8 commit 2e029f7

File tree

4 files changed

+587
-76
lines changed

4 files changed

+587
-76
lines changed

devenv/src/devenv.rs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -447,24 +447,51 @@ impl Devenv {
447447
let _guard = tracing::subscriber::set_global_default(subscriber)
448448
.expect("Failed to set tracing subscriber");
449449

450-
let options = self.nix.build(&["optionsJSON"]).await?;
451-
// debug!("{:?}", options);
452-
let options_path = options[0]
453-
.join("share")
454-
.join("doc")
455-
.join("nixos")
456-
.join("options.json");
457-
let options_contents = fs::read(options_path).expect("Failed to read options.json");
458-
let options_json: serde_json::Value =
459-
serde_json::from_slice(&options_contents).expect("Failed to parse options.json");
460-
let mut flatten_json = utils::flatten(options_json);
461-
let filter_keys = vec![
462-
String::from("declarations"),
463-
String::from("loc"),
464-
String::from("readOnly"),
465-
];
466-
let filter_keys_refs: Vec<&str> = filter_keys.iter().map(|s| s.as_str()).collect();
467-
let completion_json = utils::filter_json(&mut flatten_json, filter_keys_refs);
450+
let cached_options_path = self.devenv_dotfile.join("options.json");
451+
452+
// Get options.json either from cache or build it
453+
let completion_json = if cached_options_path.exists() {
454+
// Use cached version
455+
let cached_contents = fs::read(&cached_options_path)
456+
.map_err(|e| miette::miette!("Failed to read cached options.json: {}", e))?;
457+
let cached_json: serde_json::Value = serde_json::from_slice(&cached_contents)
458+
.map_err(|e| miette::miette!("Failed to parse cached options.json: {}", e))?;
459+
let mut flatten_json = utils::flatten(cached_json);
460+
let filter_keys = vec![
461+
String::from("declarations"),
462+
String::from("loc"),
463+
String::from("readOnly"),
464+
];
465+
let filter_keys_refs: Vec<&str> = filter_keys.iter().map(|s| s.as_str()).collect();
466+
467+
utils::filter_json(&mut flatten_json, filter_keys_refs)
468+
} else {
469+
// Generate new options.json
470+
let options = self.nix.build(&["optionsJSON"]).await?;
471+
let options_path = options[0]
472+
.join("share")
473+
.join("doc")
474+
.join("nixos")
475+
.join("options.json");
476+
477+
let options_contents = fs::read(&options_path)
478+
.map_err(|e| miette::miette!("Failed to read options.json: {}", e))?;
479+
let options_json: serde_json::Value = serde_json::from_slice(&options_contents)
480+
.map_err(|e| miette::miette!("Failed to parse options.json: {}", e))?;
481+
482+
// Cache the generated options.json
483+
fs::write(&cached_options_path, &options_contents)
484+
.map_err(|e| miette::miette!("Failed to write cached options.json: {}", e))?;
485+
486+
let mut flatten_json = utils::flatten(options_json);
487+
let filter_keys = vec![
488+
String::from("declarations"),
489+
String::from("loc"),
490+
String::from("readOnly"),
491+
];
492+
let filter_keys_refs: Vec<&str> = filter_keys.iter().map(|s| s.as_str()).collect();
493+
utils::filter_json(&mut flatten_json, filter_keys_refs)
494+
};
468495

469496
let (stdin, stdout) = (tokio::io::stdin(), tokio::io::stdout());
470497
info!("Inside the tokio main async lsp");

0 commit comments

Comments
 (0)