Format examples in docs (#7378)

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-06-06 10:01:45 +12:00
committed by GitHub
parent 11d8179368
commit 4d1524f03b
100 changed files with 1298 additions and 1362 deletions

View File

@ -173,7 +173,11 @@ fn generate_example(index: usize, src: &str, props: &ExampleProperties, file_nam
return None;
}
let content = if props.inline { "" } else { src };
let content = if props.inline {
String::new()
} else {
crate::unparser::fmt(src).unwrap()
};
let image_base64 = if props.norun {
String::new()

View File

@ -48,7 +48,7 @@ pub(super) struct GlobalState {
pub mod_loader: ModuleLoader,
/// Errors and warnings.
pub errors: Vec<CompilationError>,
#[allow(dead_code)]
#[cfg_attr(not(feature = "artifact-graph"), allow(dead_code))]
pub artifacts: ArtifactState,
}

View File

@ -1,16 +1,26 @@
use std::fmt::Write;
use crate::parsing::{
ast::types::{
Annotation, ArrayExpression, ArrayRangeExpression, AscribedExpression, BinaryExpression, BinaryOperator,
BinaryPart, BodyItem, CallExpressionKw, CommentStyle, DefaultParamVal, Expr, FormatOptions, FunctionExpression,
IfExpression, ImportSelector, ImportStatement, ItemVisibility, LabeledArg, Literal, LiteralIdentifier,
LiteralValue, MemberExpression, Node, NonCodeNode, NonCodeValue, ObjectExpression, Parameter, PipeExpression,
Program, TagDeclarator, TypeDeclaration, UnaryExpression, VariableDeclaration, VariableKind,
use crate::{
parsing::{
ast::types::{
Annotation, ArrayExpression, ArrayRangeExpression, AscribedExpression, BinaryExpression, BinaryOperator,
BinaryPart, BodyItem, CallExpressionKw, CommentStyle, DefaultParamVal, Expr, FormatOptions,
FunctionExpression, IfExpression, ImportSelector, ImportStatement, ItemVisibility, LabeledArg, Literal,
LiteralIdentifier, LiteralValue, MemberExpression, Node, NonCodeNode, NonCodeValue, ObjectExpression,
Parameter, PipeExpression, Program, TagDeclarator, TypeDeclaration, UnaryExpression, VariableDeclaration,
VariableKind,
},
deprecation, DeprecationKind, PIPE_OPERATOR,
},
deprecation, DeprecationKind, PIPE_OPERATOR,
KclError, ModuleId,
};
#[allow(dead_code)]
pub fn fmt(input: &str) -> Result<String, KclError> {
let program = crate::parsing::parse_str(input, ModuleId::default()).parse_errs_as_err()?;
Ok(program.recast(&Default::default(), 0))
}
impl Program {
pub fn recast(&self, options: &FormatOptions, indentation_level: usize) -> String {
let indentation = options.get_indentation(indentation_level);