mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Clean up tests and unused documentation code (#5273)
* Delete unused documentation code+test * Fix up test to account for new select behavior
This commit is contained in:
@ -1,17 +1,26 @@
|
||||
use itertools::Itertools;
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{EngineState, Stack},
|
||||
Example, IntoPipelineData, Signature, Span, Value,
|
||||
};
|
||||
|
||||
use std::borrow::Borrow;
|
||||
use std::collections::HashMap;
|
||||
|
||||
const COMMANDS_DOCS_DIR: &str = "docs/commands";
|
||||
pub fn get_full_help(
|
||||
sig: &Signature,
|
||||
examples: &[Example],
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
) -> String {
|
||||
get_documentation(
|
||||
sig,
|
||||
examples,
|
||||
engine_state,
|
||||
stack,
|
||||
&DocumentationConfig::default(),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DocumentationConfig {
|
||||
struct DocumentationConfig {
|
||||
no_subcommands: bool,
|
||||
//FIXME: add back in color support
|
||||
#[allow(dead_code)]
|
||||
@ -19,137 +28,8 @@ pub struct DocumentationConfig {
|
||||
brief: bool,
|
||||
}
|
||||
|
||||
fn generate_doc(
|
||||
name: &str,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
head: Span,
|
||||
) -> (Vec<String>, Vec<Value>) {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
|
||||
let command = engine_state
|
||||
.find_decl(name.as_bytes())
|
||||
.map(|decl_id| engine_state.get_decl(decl_id))
|
||||
.unwrap_or_else(|| panic!("Expected command '{}' from names to be in registry", name));
|
||||
|
||||
cols.push("name".to_string());
|
||||
vals.push(Value::String {
|
||||
val: name.into(),
|
||||
span: head,
|
||||
});
|
||||
|
||||
cols.push("usage".to_string());
|
||||
vals.push(Value::String {
|
||||
val: command.usage().to_owned(),
|
||||
span: head,
|
||||
});
|
||||
|
||||
if let Some(link) = retrieve_doc_link(name) {
|
||||
cols.push("doc_link".into());
|
||||
vals.push(Value::String {
|
||||
val: link,
|
||||
span: head,
|
||||
});
|
||||
}
|
||||
|
||||
let signature = command.signature().update_from_command(command.borrow());
|
||||
|
||||
cols.push("documentation".to_owned());
|
||||
vals.push(Value::String {
|
||||
val: get_documentation(
|
||||
&signature,
|
||||
&command.examples(),
|
||||
engine_state,
|
||||
stack,
|
||||
&DocumentationConfig {
|
||||
no_subcommands: true,
|
||||
no_color: true,
|
||||
brief: false,
|
||||
},
|
||||
),
|
||||
span: head,
|
||||
});
|
||||
|
||||
(cols, vals)
|
||||
}
|
||||
|
||||
// generate_docs gets the documentation from each command and returns a Table as output
|
||||
pub fn generate_docs(engine_state: &EngineState, stack: &mut Stack, head: Span) -> Value {
|
||||
let signatures = engine_state.get_signatures(true);
|
||||
|
||||
// cmap will map parent commands to it's subcommands e.g. to -> [to csv, to yaml, to bson]
|
||||
let mut cmap: HashMap<String, Vec<String>> = HashMap::new();
|
||||
for sig in &signatures {
|
||||
if sig.name.contains(' ') {
|
||||
let mut split_name = sig.name.split_whitespace();
|
||||
let parent_name = split_name.next().expect("Expected a parent command name");
|
||||
if cmap.contains_key(parent_name) {
|
||||
let sub_names = cmap
|
||||
.get_mut(parent_name)
|
||||
.expect("Expected an entry for parent");
|
||||
sub_names.push(sig.name.to_owned());
|
||||
}
|
||||
} else {
|
||||
cmap.insert(sig.name.to_owned(), Vec::new());
|
||||
};
|
||||
}
|
||||
// Return documentation for each command
|
||||
// Sub-commands are nested under their respective parent commands
|
||||
let mut table = Vec::new();
|
||||
for sig in &signatures {
|
||||
// Must be a sub-command, skip since it's being handled underneath when we hit the parent command
|
||||
if !cmap.contains_key(&sig.name) {
|
||||
continue;
|
||||
}
|
||||
let mut row_entries = generate_doc(&sig.name, engine_state, stack, head);
|
||||
// Iterate over all the subcommands of the parent command
|
||||
let mut sub_table = Vec::new();
|
||||
for sub_name in cmap.get(&sig.name).unwrap_or(&Vec::new()) {
|
||||
let (cols, vals) = generate_doc(sub_name, engine_state, stack, head);
|
||||
sub_table.push(Value::Record {
|
||||
cols,
|
||||
vals,
|
||||
span: head,
|
||||
});
|
||||
}
|
||||
|
||||
if !sub_table.is_empty() {
|
||||
row_entries.0.push("subcommands".into());
|
||||
row_entries.1.push(Value::List {
|
||||
vals: sub_table,
|
||||
span: head,
|
||||
});
|
||||
}
|
||||
table.push(Value::Record {
|
||||
cols: row_entries.0,
|
||||
vals: row_entries.1,
|
||||
span: head,
|
||||
});
|
||||
}
|
||||
Value::List {
|
||||
vals: table,
|
||||
span: head,
|
||||
}
|
||||
}
|
||||
|
||||
fn retrieve_doc_link(name: &str) -> Option<String> {
|
||||
let doc_name = name.split_whitespace().join("_"); // Because .replace(" ", "_") didn't work
|
||||
let mut entries =
|
||||
std::fs::read_dir(COMMANDS_DOCS_DIR).expect("Directory for command docs are missing!");
|
||||
entries.find_map(|r| {
|
||||
r.map_or(None, |de| {
|
||||
if de.file_name().to_string_lossy() == format!("{}.{}", &doc_name, "md") {
|
||||
Some(format!("/commands/{}.{}", &doc_name, "html"))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
pub fn get_documentation(
|
||||
fn get_documentation(
|
||||
sig: &Signature,
|
||||
examples: &[Example],
|
||||
engine_state: &EngineState,
|
||||
@ -348,37 +228,3 @@ pub fn get_flags_section(signature: &Signature) -> String {
|
||||
}
|
||||
long_desc
|
||||
}
|
||||
|
||||
pub fn get_brief_help(
|
||||
sig: &Signature,
|
||||
examples: &[Example],
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
) -> String {
|
||||
get_documentation(
|
||||
sig,
|
||||
examples,
|
||||
engine_state,
|
||||
stack,
|
||||
&DocumentationConfig {
|
||||
no_subcommands: false,
|
||||
no_color: false,
|
||||
brief: true,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_full_help(
|
||||
sig: &Signature,
|
||||
examples: &[Example],
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
) -> String {
|
||||
get_documentation(
|
||||
sig,
|
||||
examples,
|
||||
engine_state,
|
||||
stack,
|
||||
&DocumentationConfig::default(),
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user