Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix comma comments for function definition
  • Loading branch information
JohnnyMorganz committed Dec 1, 2021
commit 64d5eb0e666185df185cd011d7ff76652e6d104f
32 changes: 27 additions & 5 deletions src/formatters/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,14 +1015,36 @@ fn format_multiline_parameters(
// Reset the shape (as the parameter is on a newline), and increment the additional indent level
let shape = shape.reset().increment_additional_indent();

let parameter = format_parameter(ctx, pair.value(), shape).update_leading_trivia(
let mut parameter = format_parameter(ctx, pair.value(), shape).update_leading_trivia(
FormatTriviaType::Append(vec![create_indent_trivia(ctx, shape)]),
);

let punctuation = pair.punctuation().map(|punctuation| {
fmt_symbol!(ctx, punctuation, ",", shape)
.update_trailing_trivia(FormatTriviaType::Append(vec![create_newline_trivia(ctx)]))
});
let punctuation = match pair.punctuation() {
Some(punctuation) => {
// Remove any trailing comments from the parameter if present
let mut trailing_comments: Vec<Token> = match &parameter {
Parameter::Name(token) | Parameter::Ellipse(token) => token.trailing_trivia(),
other => panic!("unknown node {:?}", other),
}
.filter(|token| trivia_util::trivia_is_comment(token))
.map(|x| {
// Prepend a single space beforehand
vec![Token::new(TokenType::spaces(1)), x.to_owned()]
})
.flatten()
.collect();

parameter = parameter.update_trailing_trivia(FormatTriviaType::Replace(vec![]));

// Add a newline to the end of the trailing comments, then append them all to the end of the comma
trailing_comments.push(create_newline_trivia(ctx));
Some(
fmt_symbol!(ctx, punctuation, ",", shape)
.update_trailing_trivia(FormatTriviaType::Append(trailing_comments)),
)
}
None => None,
};

formatted_parameters.push(Pair::new(parameter, punctuation))
}
Expand Down
4 changes: 4 additions & 0 deletions tests/inputs/function-def-comments.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function func(
param_a -- description of a
, param_b -- description of b
) end
10 changes: 10 additions & 0 deletions tests/snapshots/tests__standard@function-def-comments.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: tests/tests.rs
expression: format(&contents)

---
function func(
param_a, -- description of a
param_b -- description of b
) end