Skip to content

Commit

Permalink
Add option for formatting with spaces between function names and argu…
Browse files Browse the repository at this point in the history
…ments (#839)

* Add option to define space after function definitions and calls

* Add tests for new space after function name option

* Implement formatting for new space after function name option

* Rename option to `space_after_function_names`

* Update changelog

---------

Co-authored-by: JohnnyMorganz <[email protected]>
  • Loading branch information
alerque and JohnnyMorganz authored Oct 26, 2024
1 parent 6918397 commit 7988cb7
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added configuration option `space_after_function_names` to specify whether to include a space between a function name and parentheses ([#839](https://github.com/JohnnyMorganz/StyLua/issues/839))

## [0.20.0] - 2024-01-20

### Added
Expand Down
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,16 @@ If a project uses the default configuration of StyLua without a configuration fi

StyLua only offers the following options:

| Option | Default | Description |
| --------------------------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `column_width` | `120` | Approximate line length for printing. Used as a guide for line wrapping - this is not a hard requirement: lines may fall under or over the limit. |
| `line_endings` | `Unix` | Line endings type. Possible options: `Unix` (LF) or `Windows` (CRLF) |
| `indent_type` | `Tabs` | Indent type. Possible options: `Tabs` or `Spaces` |
| `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. |
| `collapse_simple_statement` | `Never` | Specify whether to collapse simple statements. Possible options: `Never`, `FunctionOnly`, `ConditionalOnly`, or `Always` |
| Option | Default | Description |
| ---------------------------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `column_width` | `120` | Approximate line length for printing. Used as a guide for line wrapping - this is not a hard requirement: lines may fall under or over the limit. |
| `line_endings` | `Unix` | Line endings type. Possible options: `Unix` (LF) or `Windows` (CRLF) |
| `indent_type` | `Tabs` | Indent type. Possible options: `Tabs` or `Spaces` |
| `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_function_names` | `Never` | Specify whether to add a space between 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_function_names = "Never"

[sort_requires]
enabled = false
Expand Down
3 changes: 3 additions & 0 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ pub fn load_overrides(config: Config, opt: &Opt) -> Config {
if let Some(call_parentheses) = opt.format_opts.call_parentheses {
new_config.call_parentheses = call_parentheses.into();
};
if let Some(space_after_function_names) = opt.format_opts.space_after_function_names {
new_config.space_after_function_names = space_after_function_names.into();
};
if let Some(collapse_simple_statement) = opt.format_opts.collapse_simple_statement {
new_config.collapse_simple_statement = collapse_simple_statement.into();
}
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,
SpaceAfterFunctionNames,
};

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_function_names: Option<ArgSpaceAfterFunctionNames>,
}

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

convert_enum!(SpaceAfterFunctionNames, ArgSpaceAfterFunctionNames, {
Never,
Definitions,
Calls,
Always,
});

#[cfg(test)]
mod tests {
use super::Opt;
Expand Down
26 changes: 25 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
shape::Shape, CallParenType, CollapseSimpleStatement, Config, IndentType, LineEndings,
Range as FormatRange,
Range as FormatRange, SpaceAfterFunctionNames,
};
use full_moon::{
node::Node,
Expand Down Expand Up @@ -181,6 +181,30 @@ pub fn create_plain_indent_trivia(ctx: &Context, indent_level: usize) -> Token {
}
}

/// Creates a new Token containing whitespace used after function declarations
pub fn create_function_definition_trivia(ctx: &Context) -> Token {
match ctx.config().space_after_function_names {
SpaceAfterFunctionNames::Always | SpaceAfterFunctionNames::Definitions => {
Token::new(TokenType::spaces(1))
}
SpaceAfterFunctionNames::Never | SpaceAfterFunctionNames::Calls => {
Token::new(TokenType::spaces(0))
}
}
}

/// Creates a new Token containing whitespace used after function calls
pub fn create_function_call_trivia(ctx: &Context) -> Token {
match ctx.config().space_after_function_names {
SpaceAfterFunctionNames::Always | SpaceAfterFunctionNames::Calls => {
Token::new(TokenType::spaces(1))
}
SpaceAfterFunctionNames::Never | SpaceAfterFunctionNames::Definitions => {
Token::new(TokenType::spaces(0))
}
}
}

/// Creates a new Token containing new line whitespace, used for trivia
pub fn create_newline_trivia(ctx: &Context) -> Token {
Token::new(TokenType::Whitespace {
Expand Down
Loading

0 comments on commit 7988cb7

Please sign in to comment.