Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Config {
pub indent: usize,
pub line_length: usize,
pub margin: usize,
pub margin_is_explicit: bool,
pub excludes: Vec<String>,
}

Expand All @@ -20,6 +21,7 @@ impl Default for Config {
indent: 4,
line_length: 80,
margin: 1,
margin_is_explicit: false,
excludes: Vec::new(),
}
}
Expand All @@ -31,6 +33,7 @@ impl Config {
indent: tab_spaces,
line_length: max_width,
margin,
margin_is_explicit: true,
excludes: Vec::new(),
}
}
Expand All @@ -54,7 +57,10 @@ impl TryFrom<Value> for Config {
match key.as_str() {
"indent" => config.indent = parse_positive_int(key, value)?,
"line_length" => config.line_length = parse_positive_int(key, value)?,
"margin" => config.margin = parse_positive_int(key, value)?,
"margin" => {
config.margin = parse_non_negative_int(key, value)?;
config.margin_is_explicit = true;
}
"exclude" => config.excludes = parse_string_list(value)?,
unknown => return Err(ConfigError::UnknownOption(unknown.to_string())),
}
Expand Down Expand Up @@ -85,6 +91,27 @@ fn parse_positive_int(key: &str, value: &Value) -> Result<usize, ConfigError> {
Ok(*val as usize)
}

/// Parse a value as a non-negative integer (usize)
fn parse_non_negative_int(key: &str, value: &Value) -> Result<usize, ConfigError> {
let Value::Int { val, .. } = value else {
return Err(ConfigError::InvalidOptionType(
key.to_string(),
value.get_type().to_string(),
"number",
));
};

if *val < 0 {
return Err(ConfigError::InvalidOptionValue(
key.to_string(),
val.to_string(),
"a non-negative number",
));
}

Ok(*val as usize)
}

/// Parse a value as a list of strings
fn parse_string_list(value: &Value) -> Result<Vec<String>, ConfigError> {
let Value::List { vals, .. } = value else {
Expand Down
6 changes: 3 additions & 3 deletions src/formatting/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ impl<'a> Formatter<'a> {

/// Decide how many newline characters to emit between adjacent pipelines.
///
/// At top-level this respects `margin` while preserving author-intent groups
/// for `margin = 1`, and keeps adjacent `use` statements compact.
/// At top-level this respects `margin` while keeping adjacent `use`
/// statements and same-family `let`/`const` groups compact.
fn separator_newlines_between_top_level_pipelines(
&self,
current: &nu_protocol::ast::Pipeline,
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<'a> Formatter<'a> {
}
}

if self.config.margin == 1 {
if self.config.margin == 1 && !self.config.margin_is_explicit {
if self.has_blank_line_between_pipelines(current, next) {
return 2;
}
Expand Down
Loading
Loading