Skip to content

Commit

Permalink
Add option to place spaces in function definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Dec 23, 2023
1 parent ee290b0 commit 94ba46a
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/cli/opt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 +183,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 +252,12 @@ convert_enum!(CollapseSimpleStatement, ArgCollapseSimpleStatement, {
Always,
});

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

#[cfg(test)]
mod tests {
use super::Opt;
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
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ 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 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 +193,11 @@ 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::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 +366,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
74 changes: 74 additions & 0 deletions tests/test_spaces_after_functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use stylua_lib::{format_code, Config, OutputVerification, SpaceAfterFunctions};

fn format(input: &str, space_after_functions: SpaceAfterFunctions) -> String {
format_code(
input,
Config {
space_after_functions,
..Config::default()
},
None,
OutputVerification::None,
)
.unwrap()
}

const STARTINGCODE: &str = r#"
local foo = function() end
local function bar () end
function baz() end
foo()
bar ()
baz()
"#;

#[test]
fn test_never_space_after_functions() {
insta::assert_snapshot!(
format(STARTINGCODE,
SpaceAfterFunctions::Never
),
@r###"
local foo = function() end
local function bar() end
function baz() end
foo()
bar()
baz()
"###
);
}

#[test]
fn test_space_after_function_definitions() {
insta::assert_snapshot!(
format(STARTINGCODE,
SpaceAfterFunctions::Definitions
),
@r###"
local foo = function () end
local function bar () end
function baz () end
foo()
bar()
baz()
"###
);
}

#[test]
fn test_always_space_after_functions() {
insta::assert_snapshot!(
format(STARTINGCODE,
SpaceAfterFunctions::Always
),
@r###"
local foo = function () end
local function bar () end
function baz () end
foo ()
bar ()
baz ()
"###
);
}

0 comments on commit 94ba46a

Please sign in to comment.