rfmt 1.6.0 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/Cargo.lock +1 -1
- data/ext/rfmt/Cargo.toml +1 -1
- data/ext/rfmt/src/doc/printer.rs +37 -0
- data/ext/rfmt/src/format/formatter.rs +28 -4
- data/ext/rfmt/src/format/rule.rs +245 -24
- data/ext/rfmt/src/format/rules/begin.rs +161 -22
- data/ext/rfmt/src/format/rules/body_end.rs +146 -22
- data/ext/rfmt/src/format/rules/call.rs +81 -25
- data/ext/rfmt/src/format/rules/fallback.rs +20 -6
- data/ext/rfmt/src/format/rules/if_unless.rs +51 -4
- data/ext/rfmt/src/format/rules/variable_write.rs +25 -7
- data/lib/rfmt/prism_bridge.rb +43 -12
- data/lib/rfmt/version.rb +1 -1
- metadata +1 -1
|
@@ -13,7 +13,8 @@ use crate::error::Result;
|
|
|
13
13
|
use crate::format::context::FormatContext;
|
|
14
14
|
use crate::format::registry::RuleRegistry;
|
|
15
15
|
use crate::format::rule::{
|
|
16
|
-
format_leading_comments, format_statements, format_trailing_comment,
|
|
16
|
+
format_leading_comments, format_statements, format_trailing_comment,
|
|
17
|
+
strip_one_trailing_newline, FormatRule,
|
|
17
18
|
};
|
|
18
19
|
|
|
19
20
|
/// Rule for formatting if conditionals.
|
|
@@ -101,9 +102,30 @@ fn format_postfix(
|
|
|
101
102
|
docs.push(leading);
|
|
102
103
|
}
|
|
103
104
|
|
|
104
|
-
// Emit statement
|
|
105
|
+
// Emit statement. When the statement contains a heredoc whose body
|
|
106
|
+
// spills past the opener line, the bridge extends the statement's
|
|
107
|
+
// end_offset to cover the terminator — and that extended slice *also*
|
|
108
|
+
// sweeps in the intervening `if cond` modifier text that sits between
|
|
109
|
+
// the opener's `)` and the heredoc body. Appending our own
|
|
110
|
+
// ` if <cond>` after that text would then either produce
|
|
111
|
+
// `... if cond if cond` (duplicated modifier) or, worse, land the
|
|
112
|
+
// modifier on the same line as `SQL`, breaking heredoc termination.
|
|
113
|
+
//
|
|
114
|
+
// Detect the heredoc-in-statement case and emit the slice verbatim;
|
|
115
|
+
// the modifier is already baked into the source text right where
|
|
116
|
+
// Ruby expects it.
|
|
105
117
|
if let Some(statements) = node.children.get(1) {
|
|
106
118
|
if let Some(source_text) = ctx.extract_source(statements) {
|
|
119
|
+
if statement_contains_heredoc_tail(source_text) {
|
|
120
|
+
docs.push(text(source_text.trim_end_matches('\n').to_string()));
|
|
121
|
+
|
|
122
|
+
let trailing = format_trailing_comment(ctx, node.location.end_line);
|
|
123
|
+
if !trailing.is_empty() {
|
|
124
|
+
docs.push(trailing);
|
|
125
|
+
}
|
|
126
|
+
return Ok(concat(docs));
|
|
127
|
+
}
|
|
128
|
+
|
|
107
129
|
docs.push(text(source_text.trim()));
|
|
108
130
|
}
|
|
109
131
|
}
|
|
@@ -128,6 +150,26 @@ fn format_postfix(
|
|
|
128
150
|
Ok(concat(docs))
|
|
129
151
|
}
|
|
130
152
|
|
|
153
|
+
/// True if `source` looks like `<opener_with_heredoc>…\n<body>\n<TERMINATOR>`:
|
|
154
|
+
/// that is, line 1 contains a heredoc opening marker (`<<~`, `<<-`, `<<`) and
|
|
155
|
+
/// at least one subsequent line is not a chain continuation. The bridge
|
|
156
|
+
/// extends the node's end_offset to cover the heredoc tail, so this slice
|
|
157
|
+
/// already contains any `if`/`unless` modifier that was typed between the
|
|
158
|
+
/// opener's closing paren and the heredoc body on the opener line.
|
|
159
|
+
fn statement_contains_heredoc_tail(source: &str) -> bool {
|
|
160
|
+
let source = source.trim_end_matches('\n');
|
|
161
|
+
let Some((first, rest)) = source.split_once('\n') else {
|
|
162
|
+
return false;
|
|
163
|
+
};
|
|
164
|
+
if !first.contains("<<~") && !first.contains("<<-") && !first.contains("<<") {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
rest.lines().any(|l| {
|
|
168
|
+
let t = l.trim_start();
|
|
169
|
+
!t.is_empty() && !t.starts_with('.') && !t.starts_with("&.")
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
|
|
131
173
|
/// Formats ternary operator: `cond ? then_expr : else_expr`
|
|
132
174
|
fn format_ternary(node: &Node, ctx: &mut FormatContext) -> Result<Doc> {
|
|
133
175
|
let mut docs: Vec<Doc> = Vec::with_capacity(8);
|
|
@@ -240,10 +282,15 @@ fn format_normal(
|
|
|
240
282
|
docs.push(text(" "));
|
|
241
283
|
}
|
|
242
284
|
|
|
243
|
-
// Emit predicate (condition)
|
|
285
|
+
// Emit predicate (condition). When the predicate is something like
|
|
286
|
+
// `(sql = <<~SQL)`, Prism's bridge stretches the heredoc-containing
|
|
287
|
+
// nodes' end_offset past the terminator's newline. Leaving that
|
|
288
|
+
// newline in the emitted text combines with our own `hardline` before
|
|
289
|
+
// the then-clause to produce a spurious blank line after the
|
|
290
|
+
// terminator. Strip at most one trailing newline.
|
|
244
291
|
if let Some(predicate) = node.children.first() {
|
|
245
292
|
if let Some(source_text) = ctx.extract_source(predicate) {
|
|
246
|
-
docs.push(text(source_text));
|
|
293
|
+
docs.push(text(strip_one_trailing_newline(source_text).to_string()));
|
|
247
294
|
}
|
|
248
295
|
}
|
|
249
296
|
|
|
@@ -12,8 +12,8 @@ use crate::error::Result;
|
|
|
12
12
|
use crate::format::context::FormatContext;
|
|
13
13
|
use crate::format::registry::RuleRegistry;
|
|
14
14
|
use crate::format::rule::{
|
|
15
|
-
format_child, format_leading_comments, format_trailing_comment,
|
|
16
|
-
FormatRule,
|
|
15
|
+
format_child, format_leading_comments, format_trailing_comment, line_leading_indent,
|
|
16
|
+
reformat_chain_lines, strip_one_trailing_newline, FormatRule,
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
/// Rule for formatting local variable write expressions.
|
|
@@ -88,7 +88,14 @@ fn format_variable_write(
|
|
|
88
88
|
| NodeType::ForNode
|
|
89
89
|
);
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
// Only split the assignment across two lines when the block value
|
|
92
|
+
// actually begins below the `=`. When the user wrote
|
|
93
|
+
// `x = begin\n …\nend` (the opener is on the same line as the
|
|
94
|
+
// assignment), preserve that shape — splitting it mangles a common
|
|
95
|
+
// Rails idiom used for defaulted constants and service objects.
|
|
96
|
+
let inline_block_value = is_block_value && value.location.start_line == start_line;
|
|
97
|
+
|
|
98
|
+
if is_block_value && !inline_block_value {
|
|
92
99
|
// Block value: format on new line with indent
|
|
93
100
|
// x =
|
|
94
101
|
// if true
|
|
@@ -101,6 +108,9 @@ fn format_variable_write(
|
|
|
101
108
|
hardline(),
|
|
102
109
|
format_child(value, ctx, registry)?,
|
|
103
110
|
])));
|
|
111
|
+
} else if inline_block_value {
|
|
112
|
+
docs.push(text(format!("{} = ", name)));
|
|
113
|
+
docs.push(format_child(value, ctx, registry)?);
|
|
104
114
|
} else {
|
|
105
115
|
// Check for multiline method chain
|
|
106
116
|
let is_multiline_call = matches!(value.node_type, NodeType::CallNode)
|
|
@@ -109,11 +119,19 @@ fn format_variable_write(
|
|
|
109
119
|
docs.push(text(format!("{} = ", name)));
|
|
110
120
|
|
|
111
121
|
if is_multiline_call {
|
|
112
|
-
// Multiline call: reformat chain with indented style
|
|
122
|
+
// Multiline call: reformat chain with indented style.
|
|
123
|
+
// Also trim a trailing newline left over from a heredoc tail
|
|
124
|
+
// (`x = foo(<<~SQL)\n…\nSQL\n`) so it doesn't compound with the
|
|
125
|
+
// surrounding hardline.
|
|
113
126
|
if let Some(source_text) = ctx.extract_source(value) {
|
|
114
|
-
let
|
|
115
|
-
|
|
116
|
-
|
|
127
|
+
let base_indent = line_leading_indent(ctx.source(), node.location.start_offset);
|
|
128
|
+
let reformatted = reformat_chain_lines(
|
|
129
|
+
source_text,
|
|
130
|
+
base_indent,
|
|
131
|
+
ctx.config().formatting.indent_width,
|
|
132
|
+
);
|
|
133
|
+
let trimmed = strip_one_trailing_newline(reformatted.trim_start());
|
|
134
|
+
docs.push(text(trimmed.to_string()));
|
|
117
135
|
}
|
|
118
136
|
} else {
|
|
119
137
|
// Simple value: extract from source trimmed
|
data/lib/rfmt/prism_bridge.rb
CHANGED
|
@@ -62,17 +62,28 @@ module Rfmt
|
|
|
62
62
|
# Serialize the Prism AST with comments to JSON
|
|
63
63
|
def self.serialize_ast_with_comments(result)
|
|
64
64
|
comments = result.comments.map do |comment|
|
|
65
|
+
loc = comment.location
|
|
66
|
+
end_line = loc.end_line
|
|
67
|
+
# Prism's `=begin ... =end` (EmbDocComment) location ends at
|
|
68
|
+
# `<=end_line>:0`, i.e. the column-0 start of the line AFTER the
|
|
69
|
+
# `=end` terminator. Reporting that larger end_line makes the
|
|
70
|
+
# comment appear to overlap with the next statement, so the
|
|
71
|
+
# comment gets misattributed to a deeper node (e.g. the first
|
|
72
|
+
# expression inside the next method body) and ends up emitted in
|
|
73
|
+
# the wrong place. Snap it back to the terminator line.
|
|
74
|
+
end_line = loc.end_line - 1 if loc.end_column.zero? && loc.end_line > loc.start_line
|
|
75
|
+
|
|
65
76
|
{
|
|
66
77
|
comment_type: comment.class.name.split('::').last.downcase.gsub('comment', ''),
|
|
67
78
|
location: {
|
|
68
|
-
start_line:
|
|
69
|
-
start_column:
|
|
70
|
-
end_line:
|
|
71
|
-
end_column:
|
|
72
|
-
start_offset:
|
|
73
|
-
end_offset:
|
|
79
|
+
start_line: loc.start_line,
|
|
80
|
+
start_column: loc.start_column,
|
|
81
|
+
end_line: end_line,
|
|
82
|
+
end_column: loc.end_column,
|
|
83
|
+
start_offset: loc.start_offset,
|
|
84
|
+
end_offset: loc.end_offset
|
|
74
85
|
},
|
|
75
|
-
text:
|
|
86
|
+
text: loc.slice,
|
|
76
87
|
position: 'leading' # Default position, will be refined by Rust
|
|
77
88
|
}
|
|
78
89
|
end
|
|
@@ -120,7 +131,7 @@ module Rfmt
|
|
|
120
131
|
closing = node.closing_loc
|
|
121
132
|
if closing.end_offset > end_offset
|
|
122
133
|
end_offset = closing.end_offset
|
|
123
|
-
end_line = closing
|
|
134
|
+
end_line = heredoc_terminator_line(closing)
|
|
124
135
|
end_column = closing.end_column
|
|
125
136
|
end
|
|
126
137
|
end
|
|
@@ -145,6 +156,19 @@ module Rfmt
|
|
|
145
156
|
}
|
|
146
157
|
end
|
|
147
158
|
|
|
159
|
+
# Prism reports a heredoc's `closing_loc` as `<terminator_line>:0..(terminator_line+1):0`,
|
|
160
|
+
# i.e. its `end_line` is the LINE AFTER the terminator. Using that as the
|
|
161
|
+
# node's `end_line` makes blank-line preservation fail: a real blank line
|
|
162
|
+
# right after the terminator looks identical (diff == 1) to two adjacent
|
|
163
|
+
# statements with no separator. Use the terminator's own line instead.
|
|
164
|
+
def self.heredoc_terminator_line(closing_loc)
|
|
165
|
+
if closing_loc.end_column.zero? && closing_loc.end_line > closing_loc.start_line
|
|
166
|
+
closing_loc.start_line
|
|
167
|
+
else
|
|
168
|
+
closing_loc.end_line
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
|
|
148
172
|
# Recursively find the maximum closing_loc among all descendant nodes
|
|
149
173
|
# Returns nil if no closing_loc found, otherwise { end_offset:, end_line:, end_column: }
|
|
150
174
|
def self.find_max_closing_loc_recursive(node, depth: 0)
|
|
@@ -158,7 +182,7 @@ module Rfmt
|
|
|
158
182
|
if max_closing.nil? || closing.end_offset > max_closing[:end_offset]
|
|
159
183
|
max_closing = {
|
|
160
184
|
end_offset: closing.end_offset,
|
|
161
|
-
end_line: closing
|
|
185
|
+
end_line: heredoc_terminator_line(closing),
|
|
162
186
|
end_column: closing.end_column
|
|
163
187
|
}
|
|
164
188
|
end
|
|
@@ -424,9 +448,16 @@ module Rfmt
|
|
|
424
448
|
metadata['name'] = name
|
|
425
449
|
end
|
|
426
450
|
when Prism::DefNode
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
451
|
+
# Prefer `name_loc.slice` over `name.to_s` so unary operator suffixes
|
|
452
|
+
# (`def !@`, `def +@`, `def -@`) survive the round-trip. Prism
|
|
453
|
+
# normalizes `name` to the symbol with the `@` stripped for `!@`,
|
|
454
|
+
# which would otherwise rewrite `def !@` to `def !` silently.
|
|
455
|
+
name = if node.respond_to?(:name_loc) && node.name_loc
|
|
456
|
+
node.name_loc.slice
|
|
457
|
+
else
|
|
458
|
+
extract_node_name(node)
|
|
459
|
+
end
|
|
460
|
+
metadata['name'] = name if name
|
|
430
461
|
metadata['parameters_count'] = extract_parameter_count(node).to_s
|
|
431
462
|
# Extract parameters text directly from source
|
|
432
463
|
if node.parameters
|
data/lib/rfmt/version.rb
CHANGED