diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-15 01:12:06 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-15 01:12:06 +0800 |
| commit | 10ed02b8541a80e60f7ad9f9fb51f8070d6be525 (patch) | |
| tree | bfc034d1ad0c2d5567a53ece074932040dfb8d43 /gen | |
| parent | 72f80ea51f25256d0c463c2f3dc3d8670cfc4634 (diff) | |
Add completions system for shell autocompletion
Diffstat (limited to 'gen')
| -rw-r--r-- | gen/constants.rs | 7 | ||||
| -rw-r--r-- | gen/gen_completions_entries.rs | 79 | ||||
| -rw-r--r-- | gen/gen_override_renderer.rs | 64 |
3 files changed, 149 insertions, 1 deletions
diff --git a/gen/constants.rs b/gen/constants.rs index e26317f..140743d 100644 --- a/gen/constants.rs +++ b/gen/constants.rs @@ -1,4 +1,5 @@ pub const COMMANDS_PATH: &str = "./src/cmds/cmd/"; +pub const COMPLETIONS_PATH: &str = "./src/cmds/comp/"; pub const RENDERERS_PATH: &str = "./src/cmds/renderer/"; pub const COMPILE_INFO_RS_TEMPLATE: &str = "./templates/compile_info.rs.template"; @@ -10,6 +11,9 @@ pub const SETUP_JV_CLI_ISS: &str = "./scripts/setup/windows/setup_jv_cli.iss"; pub const COMMAND_LIST_TEMPLATE: &str = "./templates/_commands.rs.template"; pub const COMMAND_LIST: &str = "./src/systems/cmd/_commands.rs"; +pub const COMPLETIONS_TEMPLATE: &str = "./templates/_comps.rs.template"; +pub const COMPLETIONS: &str = "./src/systems/comp/_comps.rs"; + pub const OVERRIDE_RENDERER_DISPATCHER_TEMPLATE: &str = "./templates/_override_renderer_dispatcher.rs.template"; pub const OVERRIDE_RENDERER_DISPATCHER: &str = @@ -19,6 +23,9 @@ pub const OVERRIDE_RENDERER_ENTRY_TEMPLATE: &str = "./templates/_override_renderer_entry.rs.template"; pub const OVERRIDE_RENDERER_ENTRY: &str = "./src/systems/render/_override_renderer_entry.rs"; +pub const OVERRIDE_RENDERERS_TEMPLATE: &str = "./templates/_override_renderers.rs.template"; +pub const OVERRIDE_RENDERERS: &str = "./src/systems/render/_override_renderers.rs"; + pub const SPECIFIC_RENDERER_MATCHING_TEMPLATE: &str = "./templates/_specific_renderer_matching.rs.template"; pub const SPECIFIC_RENDERER_MATCHING: &str = "./src/systems/render/_specific_renderer_matching.rs"; diff --git a/gen/gen_completions_entries.rs b/gen/gen_completions_entries.rs new file mode 100644 index 0000000..0e030e6 --- /dev/null +++ b/gen/gen_completions_entries.rs @@ -0,0 +1,79 @@ +use just_template::{Template, tmpl}; +use std::path::PathBuf; + +use crate::r#gen::constants::{COMPLETIONS, COMPLETIONS_PATH, COMPLETIONS_TEMPLATE}; + +/// Generate completions file from comp directory using just_template +pub async fn generate_completions_file(repo_root: &PathBuf) { + let template_path = repo_root.join(COMPLETIONS_TEMPLATE); + let output_path = repo_root.join(COMPLETIONS); + let comps_dir = repo_root.join(COMPLETIONS_PATH); + + // Read the template + let template_content = tokio::fs::read_to_string(&template_path).await.unwrap(); + + // Collect all completion files + let mut all_completions: Vec<(String, String)> = Vec::new(); + let mut all_nodes: Vec<String> = Vec::new(); + + if comps_dir.exists() && comps_dir.is_dir() { + let mut entries = tokio::fs::read_dir(&comps_dir).await.unwrap(); + while let Some(entry) = entries.next_entry().await.unwrap() { + let path = entry.path(); + + if !path.is_file() { + continue; + } + + let extension = match path.extension() { + Some(ext) => ext, + None => continue, + }; + + if extension != "rs" { + continue; + } + + let file_name = match path.file_stem().and_then(|s| s.to_str()) { + Some(name) => name, + None => continue, + }; + + let node_name = just_fmt::lower_case!(file_name); + + all_completions.push((file_name.to_string(), node_name.clone())); + all_nodes.push(node_name); + } + } + + // Create template + let mut template = Template::from(template_content); + + // Generate match arms for each completion + for (comp_name, node_name) in &all_completions { + tmpl!(template += { + comp_match_arms { + (comp_name = comp_name, comp_node_name = node_name) + } + }); + } + + for node in all_nodes { + tmpl!(template += { + comp_node_name { + (comp_node_name = just_fmt::lower_case!(node)) + } + }); + } + + // Expand the template + let final_content = template.expand().unwrap(); + + // Write the generated code + tokio::fs::write(output_path, final_content).await.unwrap(); + + println!( + "Generated completions file with {} completions using just_template", + all_completions.len() + ); +} diff --git a/gen/gen_override_renderer.rs b/gen/gen_override_renderer.rs index 1e67be4..2a8ba37 100644 --- a/gen/gen_override_renderer.rs +++ b/gen/gen_override_renderer.rs @@ -5,7 +5,10 @@ use regex::Regex; use tokio::fs; use crate::r#gen::{ - constants::{COMMANDS_PATH, OVERRIDE_RENDERER_ENTRY, OVERRIDE_RENDERER_ENTRY_TEMPLATE}, + constants::{ + COMMANDS_PATH, OVERRIDE_RENDERER_ENTRY, OVERRIDE_RENDERER_ENTRY_TEMPLATE, + OVERRIDE_RENDERERS, OVERRIDE_RENDERERS_TEMPLATE, REGISTRY_TOML, + }, resolve_types::resolve_type_paths, }; @@ -41,6 +44,65 @@ pub async fn generate_override_renderer(repo_root: &PathBuf) { ); } +/// Generate override renderers list file from Registry.toml configuration using just_template +pub async fn generate_override_renderers_list(repo_root: &PathBuf) { + let template_path = repo_root.join(OVERRIDE_RENDERERS_TEMPLATE); + let output_path = repo_root.join(OVERRIDE_RENDERERS); + let config_path = repo_root.join(REGISTRY_TOML); + + // Read the template + let template_content = tokio::fs::read_to_string(&template_path).await.unwrap(); + + // Read and parse the TOML configuration + let config_content = tokio::fs::read_to_string(&config_path).await.unwrap(); + let config: toml::Value = toml::from_str(&config_content).unwrap(); + + // Collect all renderer names + let mut renderer_names = Vec::new(); + + let Some(table) = config.as_table() else { + return; + }; + let Some(renderer_table) = table.get("renderer") else { + return; + }; + let Some(renderer_table) = renderer_table.as_table() else { + return; + }; + + for (_, renderer_value) in renderer_table { + let Some(renderer_config) = renderer_value.as_table() else { + continue; + }; + let Some(name) = renderer_config.get("name").and_then(|v| v.as_str()) else { + continue; + }; + + renderer_names.push(name.to_string()); + } + + // Create template + let mut template = Template::from(template_content); + + for renderer_name in &renderer_names { + tmpl!(template += { + renderer { + (renderer_name = renderer_name) + } + }); + } + + let final_content = template.expand().unwrap(); + + // Write the generated code + tokio::fs::write(output_path, final_content).await.unwrap(); + + println!( + "Generated override renderers list with {} renderers using just_template", + renderer_names.len() + ); +} + pub async fn collect_all_possible_types(dir: &PathBuf) -> HashSet<String> { let mut all_types = HashSet::new(); let mut dirs_to_visit = vec![dir.clone()]; |
