Skip to content

Commit

Permalink
Add option to define space after function definitions and calls
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed May 29, 2024
1 parent c6e8619 commit 3ec7936
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ StyLua only offers the following options:
| `indent_width` | `4` | Character size of single indentation. If `indent_type` is set to `Tabs`, this option is used as a heuristic to determine column width only. |
| `quote_style` | `AutoPreferDouble` | Quote style for string literals. Possible options: `AutoPreferDouble`, `AutoPreferSingle`, `ForceDouble`, `ForceSingle`. `AutoPrefer` styles will prefer the specified quote style, but fall back to the alternative if it has fewer string escapes. `Force` styles always use the specified style regardless of escapes. |
| `call_parentheses` | `Always` | Whether parentheses should be applied on function calls with a single string/table argument. Possible options: `Always`, `NoSingleString`, `NoSingleTable`, `None`, `Input`. `Always` applies parentheses in all cases. `NoSingleString` omits parentheses on calls with a single string argument. Similarly, `NoSingleTable` omits parentheses on calls with a single table argument. `None` omits parentheses in both cases. Note: parentheses are still kept in situations where removal can lead to obscurity (e.g. `foo "bar".setup -> foo("bar").setup`, since the index is on the call result, not the string). `Input` removes all automation and preserves parentheses only if they were present in input code: consistency is not enforced. |
| `space_after_fuctions` | `Never` | Specify whether to add a space beetween the function name and parentheses. Possible options: `Never`, `Definitions`, `Calls`, or `Always` |
| `collapse_simple_statement` | `Never` | Specify whether to collapse simple statements. Possible options: `Never`, `FunctionOnly`, `ConditionalOnly`, or `Always` |

Default `stylua.toml`, note you do not need to explicitly specify each option if you want to use the defaults:
Expand All @@ -271,6 +272,7 @@ indent_width = 4
quote_style = "AutoPreferDouble"
call_parentheses = "Always"
collapse_simple_statement = "Never"
space_after_fuctions = "Never"

[sort_requires]
enabled = false
Expand Down
14 changes: 13 additions & 1 deletion src/cli/opt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use clap::{ArgEnum, StructOpt};
use std::path::PathBuf;
use stylua_lib::{CallParenType, CollapseSimpleStatement, IndentType, LineEndings, QuoteStyle};
use stylua_lib::{
CallParenType, CollapseSimpleStatement, IndentType, LineEndings, QuoteStyle,
SpaceAfterFunctions,
};

lazy_static::lazy_static! {
static ref NUM_CPUS: String = num_cpus::get().to_string();
Expand Down Expand Up @@ -183,6 +186,8 @@ pub struct FormatOpts {
/// Enable requires sorting
#[structopt(long)]
pub sort_requires: bool,
#[structopt(long, arg_enum, ignore_case = true)]
pub space_after_functions: Option<ArgSpaceAfterFunctions>,
}

// Convert [`stylua_lib::Config`] enums into clap-friendly enums
Expand Down Expand Up @@ -250,6 +255,13 @@ convert_enum!(CollapseSimpleStatement, ArgCollapseSimpleStatement, {
Always,
});

convert_enum!(SpaceAfterFunctions, ArgSpaceAfterFunctions, {
Never,
Definitions,
Calls,
Always,
});

#[cfg(test)]
mod tests {
use super::Opt;
Expand Down
26 changes: 25 additions & 1 deletion src/editorconfig.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
CallParenType, CollapseSimpleStatement, Config, IndentType, LineEndings, QuoteStyle,
SortRequiresConfig,
SortRequiresConfig, SpaceAfterFunctions,
};
use ec4rs::{
properties_of,
Expand Down Expand Up @@ -65,6 +65,14 @@ property_choice! {
(None, "none")
}

property_choice! {
SpaceAfterFunctionsChoice, "space_after_functions";
(Always, "always"),
(Definitions, "definitions"),
(Calls, "calls"),
(Never, "never")
}

property_choice! {
CollapseSimpleStatementChoice, "collapse_simple_statement";
(Never, "never"),
Expand Down Expand Up @@ -128,6 +136,22 @@ fn load(mut config: Config, properties: &Properties) -> Config {
CallParenthesesChoice::None => config.call_parentheses = CallParenType::None,
}
}
if let Ok(space_after_functions) = properties.get::<SpaceAfterFunctionsChoice>() {
match space_after_functions {
SpaceAfterFunctionsChoice::Always => {
config.space_after_functions = SpaceAfterFunctions::Always
}
SpaceAfterFunctionsChoice::Definitions => {
config.space_after_functions = SpaceAfterFunctions::Definitions
}
SpaceAfterFunctionsChoice::Calls => {
config.space_after_functions = SpaceAfterFunctions::Calls
}
SpaceAfterFunctionsChoice::Never => {
config.space_after_functions = SpaceAfterFunctions::Never
}
}
}
if let Ok(collapse_simple_statement) = properties.get::<CollapseSimpleStatementChoice>() {
match collapse_simple_statement {
CollapseSimpleStatementChoice::Never => {
Expand Down
1 change: 1 addition & 0 deletions src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{
},
shape::Shape,
CallParenType,
SpaceAfterFunctions,
};

/// Formats an Anonymous Function
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,23 @@ impl SortRequiresConfig {
}
}

/// When to use spaces after function names
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Deserialize)]
#[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "fromstr", derive(strum::EnumString))]
pub enum SpaceAfterFunctions {
/// Never use spaces after function nomes.
#[default]
Never,
/// Use spaces after function names only for function definitions.
Definitions,
/// Use spaces after function names only for function calls.
Calls,
/// Use spaces after function names in definitions and calls.
Always,
}

/// The configuration to use when formatting.
#[derive(Copy, Clone, Debug, Deserialize)]
#[serde(default, deny_unknown_fields)]
Expand Down Expand Up @@ -178,6 +195,12 @@ pub struct Config {
pub collapse_simple_statement: CollapseSimpleStatement,
/// Configuration for the sort requires codemod
pub sort_requires: SortRequiresConfig,
/// Whether we should include a space between the function name and arguments.
/// * if space_after_functions is set to [`SpaceAfterFunctions::Never`] a space is never used.
/// * if space_after_functions is set to [`SpaceAfterFunctions::Definitions`] a space is used only for definitions.
/// * if space_after_functions is set to [`SpaceAfterFunctions::Calls`] a space is used only for calls.
/// * if space_after_functions is set to [`SpaceAfterFunctions::Always`] a space is used for both definitions and calls.
pub space_after_functions: SpaceAfterFunctions,
}

#[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)]
Expand Down Expand Up @@ -346,6 +369,7 @@ impl Default for Config {
call_parentheses: CallParenType::default(),
collapse_simple_statement: CollapseSimpleStatement::default(),
sort_requires: SortRequiresConfig::default(),
space_after_functions: SpaceAfterFunctions::default(),
}
}
}
Expand Down

0 comments on commit 3ec7936

Please sign in to comment.