Skip to content

Commit

Permalink
Simplify access patterns for config
Browse files Browse the repository at this point in the history
Allow accessing properties directly

Fixes #805
  • Loading branch information
JohnnyMorganz committed Nov 11, 2023
1 parent c34fa41 commit 452e43b
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 96 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Updated parser crate with following changes:

- Support Luau floor division (`//`)
- Fix Luau string interpolation parsing
- Fix Luau `\z` escape parsing

- Simplified access and modification patterns for StyLua configuration. You can now access the properties directly
- **Deprecated:** the old access patterns of `.property()` and `.with_property()` are now deprecated

### Fixed

- Wasm build now correctly supports configuring sort requires ([#818](https://github.com/JohnnyMorganz/StyLua/issues/818))
Expand Down
28 changes: 14 additions & 14 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,28 @@ pub fn load_overrides(config: Config, opt: &Opt) -> Config {
let mut new_config = config;

if let Some(column_width) = opt.format_opts.column_width {
new_config = new_config.with_column_width(column_width);
new_config.column_width = column_width;
};
if let Some(line_endings) = opt.format_opts.line_endings {
new_config = new_config.with_line_endings(line_endings.into());
new_config.line_endings = line_endings.into();
};
if let Some(indent_type) = opt.format_opts.indent_type {
new_config = new_config.with_indent_type(indent_type.into());
new_config.indent_type = indent_type.into();
};
if let Some(indent_width) = opt.format_opts.indent_width {
new_config = new_config.with_indent_width(indent_width);
new_config.indent_width = indent_width;
};
if let Some(quote_style) = opt.format_opts.quote_style {
new_config = new_config.with_quote_style(quote_style.into());
new_config.quote_style = quote_style.into();
};
if let Some(call_parentheses) = opt.format_opts.call_parentheses {
new_config = new_config.with_call_parentheses(call_parentheses.into());
new_config.call_parentheses = call_parentheses.into();
};
if let Some(collapse_simple_statement) = opt.format_opts.collapse_simple_statement {
new_config = new_config.with_collapse_simple_statement(collapse_simple_statement.into());
new_config.collapse_simple_statement = collapse_simple_statement.into();
}
if opt.format_opts.sort_requires {
new_config = new_config.with_sort_requires(SortRequiresConfig::default().set_enabled(true))
new_config.sort_requires = SortRequiresConfig { enabled: true }
}

new_config
Expand All @@ -195,46 +195,46 @@ mod tests {
let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--column-width", "80"]);
let default_config = Config::new();
let config = load_overrides(default_config, &override_opt);
assert_eq!(config.column_width(), 80);
assert_eq!(config.column_width, 80);
}

#[test]
fn test_override_line_endings() {
let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--line-endings", "Windows"]);
let default_config = Config::new();
let config = load_overrides(default_config, &override_opt);
assert_eq!(config.line_endings(), LineEndings::Windows);
assert_eq!(config.line_endings, LineEndings::Windows);
}

#[test]
fn test_override_indent_type() {
let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--indent-type", "Spaces"]);
let default_config = Config::new();
let config = load_overrides(default_config, &override_opt);
assert_eq!(config.indent_type(), IndentType::Spaces);
assert_eq!(config.indent_type, IndentType::Spaces);
}

#[test]
fn test_override_indent_width() {
let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--indent-width", "2"]);
let default_config = Config::new();
let config = load_overrides(default_config, &override_opt);
assert_eq!(config.indent_width(), 2);
assert_eq!(config.indent_width, 2);
}

#[test]
fn test_override_quote_style() {
let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--quote-style", "ForceSingle"]);
let default_config = Config::new();
let config = load_overrides(default_config, &override_opt);
assert_eq!(config.quote_style(), QuoteStyle::ForceSingle);
assert_eq!(config.quote_style, QuoteStyle::ForceSingle);
}

#[test]
fn test_override_call_parentheses() {
let override_opt = Opt::parse_from(vec!["BINARY_NAME", "--call-parentheses", "None"]);
let default_config = Config::new();
let config = load_overrides(default_config, &override_opt);
assert_eq!(config.call_parentheses(), CallParenType::None);
assert_eq!(config.call_parentheses, CallParenType::None);
}
}
6 changes: 4 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,14 @@ impl Context {
FormatNode::Normal
}

#[allow(deprecated)]
pub fn should_omit_string_parens(&self) -> bool {
self.config().no_call_parentheses
|| self.config().call_parentheses == CallParenType::None
|| self.config().call_parentheses == CallParenType::NoSingleString
}

#[allow(deprecated)]
pub fn should_omit_table_parens(&self) -> bool {
self.config().no_call_parentheses
|| self.config().call_parentheses == CallParenType::None
Expand All @@ -141,14 +143,14 @@ impl Context {

pub fn should_collapse_simple_functions(&self) -> bool {
matches!(
self.config().collapse_simple_statement(),
self.config().collapse_simple_statement,
CollapseSimpleStatement::FunctionOnly | CollapseSimpleStatement::Always
)
}

pub fn should_collapse_simple_conditionals(&self) -> bool {
matches!(
self.config().collapse_simple_statement(),
self.config().collapse_simple_statement,
CollapseSimpleStatement::ConditionalOnly | CollapseSimpleStatement::Always
)
}
Expand Down
Loading

0 comments on commit 452e43b

Please sign in to comment.