Skip to content

Commit 6cef63c

Browse files
committed
Implement formatting for new space after function name option
1 parent 3359ced commit 6cef63c

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

src/cli/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ pub fn load_overrides(config: Config, opt: &Opt) -> Config {
174174
if let Some(call_parentheses) = opt.format_opts.call_parentheses {
175175
new_config.call_parentheses = call_parentheses.into();
176176
};
177+
if let Some(space_after_functions) = opt.format_opts.space_after_functions {
178+
new_config.space_after_functions = space_after_functions.into();
179+
};
177180
if let Some(collapse_simple_statement) = opt.format_opts.collapse_simple_statement {
178181
new_config.collapse_simple_statement = collapse_simple_statement.into();
179182
}

src/context.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
shape::Shape, CallParenType, CollapseSimpleStatement, Config, IndentType, LineEndings,
3-
Range as FormatRange,
3+
Range as FormatRange, SpaceAfterFunctions,
44
};
55
use full_moon::{
66
node::Node,
@@ -181,6 +181,28 @@ pub fn create_plain_indent_trivia(ctx: &Context, indent_level: usize) -> Token {
181181
}
182182
}
183183

184+
/// Creates a new Token containing whitespace used after function declarations
185+
pub fn create_function_definition_trivia(ctx: &Context) -> Token {
186+
match ctx.config().space_after_functions {
187+
SpaceAfterFunctions::Always | SpaceAfterFunctions::Definitions => {
188+
Token::new(TokenType::spaces(1))
189+
}
190+
SpaceAfterFunctions::Never | SpaceAfterFunctions::Calls => Token::new(TokenType::spaces(0)),
191+
}
192+
}
193+
194+
/// Creates a new Token containing whitespace used after function calls
195+
pub fn create_function_call_trivia(ctx: &Context) -> Token {
196+
match ctx.config().space_after_functions {
197+
SpaceAfterFunctions::Always | SpaceAfterFunctions::Calls => {
198+
Token::new(TokenType::spaces(1))
199+
}
200+
SpaceAfterFunctions::Never | SpaceAfterFunctions::Definitions => {
201+
Token::new(TokenType::spaces(0))
202+
}
203+
}
204+
}
205+
184206
/// Creates a new Token containing new line whitespace, used for trivia
185207
pub fn create_newline_trivia(ctx: &Context) -> Token {
186208
Token::new(TokenType::Whitespace {

src/formatters/functions.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use full_moon::tokenizer::{Token, TokenKind, TokenReference, TokenType};
1010
#[cfg(feature = "luau")]
1111
use crate::formatters::luau::{format_generic_declaration, format_type_specifier};
1212
use crate::{
13-
context::{create_indent_trivia, create_newline_trivia, Context},
13+
context::{
14+
create_function_call_trivia, create_function_definition_trivia, create_indent_trivia,
15+
create_newline_trivia, Context,
16+
},
1417
fmt_symbol,
1518
formatters::{
1619
block::{format_block, format_last_stmt_no_trivia},
@@ -31,7 +34,6 @@ use crate::{
3134
},
3235
shape::Shape,
3336
CallParenType,
34-
SpaceAfterFunctions,
3537
};
3638

3739
/// Formats an Anonymous Function
@@ -43,7 +45,9 @@ pub fn format_anonymous_function(
4345
shape: Shape,
4446
) -> (TokenReference, FunctionBody) {
4547
const FUNCTION_LEN: usize = "function".len();
46-
let function_token = fmt_symbol!(ctx, function_token, "function", shape);
48+
let function_definition_trivia = vec![create_function_definition_trivia(ctx)];
49+
let function_token = fmt_symbol!(ctx, function_token, "function", shape)
50+
.update_trailing_trivia(FormatTriviaType::Append(function_definition_trivia));
4751
let function_body = format_function_body(ctx, function_body, shape.add_width(FUNCTION_LEN));
4852

4953
(function_token, function_body)
@@ -74,13 +78,14 @@ pub fn format_call(
7478
shape: Shape,
7579
call_next_node: FunctionCallNextNode,
7680
) -> Call {
81+
let function_call_trivia = vec![create_function_call_trivia(ctx)];
7782
match call {
78-
Call::AnonymousCall(function_args) => Call::AnonymousCall(format_function_args(
79-
ctx,
80-
function_args,
81-
shape,
82-
call_next_node,
83-
)),
83+
Call::AnonymousCall(function_args) => {
84+
let formatted_function_args =
85+
format_function_args(ctx, function_args, shape, call_next_node)
86+
.update_leading_trivia(FormatTriviaType::Append(function_call_trivia));
87+
Call::AnonymousCall(formatted_function_args)
88+
}
8489
Call::MethodCall(method_call) => {
8590
Call::MethodCall(format_method_call(ctx, method_call, shape, call_next_node))
8691
}
@@ -1151,6 +1156,7 @@ pub fn format_function_declaration(
11511156
// Calculate trivia
11521157
let leading_trivia = vec![create_indent_trivia(ctx, shape)];
11531158
let trailing_trivia = vec![create_newline_trivia(ctx)];
1159+
let function_definition_trivia = vec![create_function_definition_trivia(ctx)];
11541160

11551161
let function_token = fmt_symbol!(
11561162
ctx,
@@ -1159,7 +1165,8 @@ pub fn format_function_declaration(
11591165
shape
11601166
)
11611167
.update_leading_trivia(FormatTriviaType::Append(leading_trivia));
1162-
let formatted_function_name = format_function_name(ctx, function_declaration.name(), shape);
1168+
let formatted_function_name = format_function_name(ctx, function_declaration.name(), shape)
1169+
.update_trailing_trivia(FormatTriviaType::Append(function_definition_trivia));
11631170

11641171
let shape = shape + (9 + strip_trivia(&formatted_function_name).to_string().len()); // 9 = "function "
11651172
let function_body = format_function_body(ctx, function_declaration.body(), shape)
@@ -1179,11 +1186,13 @@ pub fn format_local_function(
11791186
// Calculate trivia
11801187
let leading_trivia = vec![create_indent_trivia(ctx, shape)];
11811188
let trailing_trivia = vec![create_newline_trivia(ctx)];
1189+
let function_definition_trivia = vec![create_function_definition_trivia(ctx)];
11821190

11831191
let local_token = fmt_symbol!(ctx, local_function.local_token(), "local ", shape)
11841192
.update_leading_trivia(FormatTriviaType::Append(leading_trivia));
11851193
let function_token = fmt_symbol!(ctx, local_function.function_token(), "function ", shape);
1186-
let formatted_name = format_token_reference(ctx, local_function.name(), shape);
1194+
let formatted_name = format_token_reference(ctx, local_function.name(), shape)
1195+
.update_trailing_trivia(FormatTriviaType::Append(function_definition_trivia));
11871196

11881197
let shape = shape + (6 + 9 + strip_trivia(&formatted_name).to_string().len()); // 6 = "local ", 9 = "function "
11891198
let function_body = format_function_body(ctx, local_function.body(), shape)
@@ -1202,12 +1211,14 @@ pub fn format_method_call(
12021211
shape: Shape,
12031212
call_next_node: FunctionCallNextNode,
12041213
) -> MethodCall {
1214+
let function_call_trivia = vec![create_function_call_trivia(ctx)];
12051215
let formatted_colon_token = format_token_reference(ctx, method_call.colon_token(), shape);
12061216
let formatted_name = format_token_reference(ctx, method_call.name(), shape);
12071217
let shape =
12081218
shape + (formatted_colon_token.to_string().len() + formatted_name.to_string().len());
12091219
let formatted_function_args =
1210-
format_function_args(ctx, method_call.args(), shape, call_next_node);
1220+
format_function_args(ctx, method_call.args(), shape, call_next_node)
1221+
.update_leading_trivia(FormatTriviaType::Append(function_call_trivia));
12111222

12121223
MethodCall::new(formatted_name, formatted_function_args).with_colon_token(formatted_colon_token)
12131224
}

0 commit comments

Comments
 (0)