rfmt 1.5.3 → 1.6.1
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 +51 -0
- data/Cargo.lock +1 -1
- data/README.md +22 -18
- data/ext/rfmt/Cargo.toml +4 -1
- data/ext/rfmt/src/doc/builders.rs +528 -0
- data/ext/rfmt/src/doc/mod.rs +220 -0
- data/ext/rfmt/src/doc/printer.rs +721 -0
- data/ext/rfmt/src/format/context.rs +448 -0
- data/ext/rfmt/src/format/formatter.rs +250 -0
- data/ext/rfmt/src/format/mod.rs +35 -0
- data/ext/rfmt/src/format/registry.rs +195 -0
- data/ext/rfmt/src/format/rule.rs +726 -0
- data/ext/rfmt/src/format/rules/begin.rs +434 -0
- data/ext/rfmt/src/format/rules/body_end.rs +233 -0
- data/ext/rfmt/src/format/rules/call.rs +448 -0
- data/ext/rfmt/src/format/rules/case.rs +359 -0
- data/ext/rfmt/src/format/rules/class.rs +160 -0
- data/ext/rfmt/src/format/rules/def.rs +216 -0
- data/ext/rfmt/src/format/rules/fallback.rs +130 -0
- data/ext/rfmt/src/format/rules/if_unless.rs +454 -0
- data/ext/rfmt/src/format/rules/loops.rs +325 -0
- data/ext/rfmt/src/format/rules/mod.rs +31 -0
- data/ext/rfmt/src/format/rules/module.rs +150 -0
- data/ext/rfmt/src/format/rules/singleton_class.rs +202 -0
- data/ext/rfmt/src/format/rules/statements.rs +122 -0
- data/ext/rfmt/src/format/rules/variable_write.rs +314 -0
- data/ext/rfmt/src/lib.rs +8 -5
- data/ext/rfmt/src/parser/prism_adapter.rs +157 -2
- data/lib/rfmt/prism_bridge.rb +43 -12
- data/lib/rfmt/version.rb +1 -1
- data/lib/ruby_lsp/rfmt/formatter_runner.rb +2 -0
- metadata +23 -2
- data/ext/rfmt/src/emitter/mod.rs +0 -1844
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
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rfmt
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.6.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- fujitani sora
|
|
@@ -84,8 +84,29 @@ files:
|
|
|
84
84
|
- ext/rfmt/extconf.rb
|
|
85
85
|
- ext/rfmt/src/ast/mod.rs
|
|
86
86
|
- ext/rfmt/src/config/mod.rs
|
|
87
|
-
- ext/rfmt/src/
|
|
87
|
+
- ext/rfmt/src/doc/builders.rs
|
|
88
|
+
- ext/rfmt/src/doc/mod.rs
|
|
89
|
+
- ext/rfmt/src/doc/printer.rs
|
|
88
90
|
- ext/rfmt/src/error/mod.rs
|
|
91
|
+
- ext/rfmt/src/format/context.rs
|
|
92
|
+
- ext/rfmt/src/format/formatter.rs
|
|
93
|
+
- ext/rfmt/src/format/mod.rs
|
|
94
|
+
- ext/rfmt/src/format/registry.rs
|
|
95
|
+
- ext/rfmt/src/format/rule.rs
|
|
96
|
+
- ext/rfmt/src/format/rules/begin.rs
|
|
97
|
+
- ext/rfmt/src/format/rules/body_end.rs
|
|
98
|
+
- ext/rfmt/src/format/rules/call.rs
|
|
99
|
+
- ext/rfmt/src/format/rules/case.rs
|
|
100
|
+
- ext/rfmt/src/format/rules/class.rs
|
|
101
|
+
- ext/rfmt/src/format/rules/def.rs
|
|
102
|
+
- ext/rfmt/src/format/rules/fallback.rs
|
|
103
|
+
- ext/rfmt/src/format/rules/if_unless.rs
|
|
104
|
+
- ext/rfmt/src/format/rules/loops.rs
|
|
105
|
+
- ext/rfmt/src/format/rules/mod.rs
|
|
106
|
+
- ext/rfmt/src/format/rules/module.rs
|
|
107
|
+
- ext/rfmt/src/format/rules/singleton_class.rs
|
|
108
|
+
- ext/rfmt/src/format/rules/statements.rs
|
|
109
|
+
- ext/rfmt/src/format/rules/variable_write.rs
|
|
89
110
|
- ext/rfmt/src/lib.rs
|
|
90
111
|
- ext/rfmt/src/logging/logger.rs
|
|
91
112
|
- ext/rfmt/src/logging/mod.rs
|