super-slim 3.2.1 → 3.2.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 +12 -1
- data/lib/super_slim/formatter/code_wrapper.rb +31 -17
- data/lib/super_slim/formatter/line_wrapper.rb +3 -3
- data/lib/super_slim/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2dd1011e0ac6372401bfc8b9f1ae5dad00cfc9208fc845e892eb1d0b7ebf2aae
|
|
4
|
+
data.tar.gz: 2ceb2fe0dc641bd9ccbd49c7841cdaf1f8124c7b2b1340ec7961be80b30a9421
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ad7f7c44edd4bb989722855e7baa362ca0285fcb3168eedc474205f7f9817b0f7bf296ebb566edaa867f8fe7f73d850aa20881859b88a1e32bab76f58f6123da
|
|
7
|
+
data.tar.gz: 6842ed4614918cd699dcaae1d9115257dc5755e343699bfcdf2c387da90bb731bc106cac062f9c0cd84f75cea351e13c82b388c9f46053382819f91ba3b08b8d
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [3.2.2] — 2026-11-06
|
|
6
|
+
|
|
7
|
+
`super-slim format` no longer joins code spread by hand into lines past `max_line_length`.
|
|
8
|
+
|
|
9
|
+
**Fixed**
|
|
10
|
+
- With `max_line_length`, an argument that still passes the limit once its call is spread one argument per line
|
|
11
|
+
spreads its own elements or arguments in turn, an array before `.join` included, instead of coming back on one
|
|
12
|
+
long line. Code spread by hand, such as a string continued with a backslash, keeps its layout whenever the
|
|
13
|
+
formatted one would hold a longer line.
|
|
14
|
+
|
|
5
15
|
## [3.2.1] — 2026-11-06
|
|
6
16
|
|
|
7
17
|
A comment after Ruby code no longer breaks the template.
|
|
@@ -183,7 +193,8 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
|
|
|
183
193
|
- Ruby 3.3 or later, every Ruby still maintained, Rails 8.0 or 8.1 for the railtie, and strscan 3.1.1 or later,
|
|
184
194
|
newer than the one Ruby 3.3 ships.
|
|
185
195
|
|
|
186
|
-
[Unreleased]: https://github.com/levups/super-slim/compare/v3.2.
|
|
196
|
+
[Unreleased]: https://github.com/levups/super-slim/compare/v3.2.2...HEAD
|
|
197
|
+
[3.2.2]: https://github.com/levups/super-slim/compare/v3.2.1...v3.2.2
|
|
187
198
|
[3.2.1]: https://github.com/levups/super-slim/compare/v3.2.0...v3.2.1
|
|
188
199
|
[3.2.0]: https://github.com/levups/super-slim/compare/v3.1.1...v3.2.0
|
|
189
200
|
[3.1.1]: https://github.com/levups/super-slim/compare/v3.1.0...v3.1.1
|
|
@@ -11,9 +11,10 @@ module SuperSlim
|
|
|
11
11
|
TEMPLATE_METHOD = "def template\n"
|
|
12
12
|
CODE_OFFSET = TEMPLATE_METHOD.bytesize
|
|
13
13
|
|
|
14
|
-
# Returns the lines after the first one's indentation, or nil when the code has nothing to spread.
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
# Returns the lines after the first one's indentation, or nil when the code has nothing to spread. An item
|
|
15
|
+
# still longer than max_line_length spreads its own arguments or elements.
|
|
16
|
+
def self.call(code, indent, opens_block:, max_line_length:)
|
|
17
|
+
new(code, indent, opens_block:, max_line_length:).call
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
# `render Card.new title:` reads as if title went to render: arguments to `new` go in parentheses.
|
|
@@ -68,17 +69,19 @@ module SuperSlim
|
|
|
68
69
|
end
|
|
69
70
|
|
|
70
71
|
# opens_block: whether the line has children, which Slim passes to its call as a block even without `do`.
|
|
71
|
-
def initialize(code, indent, opens_block:)
|
|
72
|
+
def initialize(code, indent, opens_block:, max_line_length:)
|
|
72
73
|
@code = code
|
|
73
74
|
@indent = indent
|
|
74
75
|
@opens_block = opens_block
|
|
76
|
+
@max_line_length = max_line_length
|
|
75
77
|
end
|
|
76
78
|
|
|
77
79
|
def call
|
|
78
80
|
statements, opens_block = self.class.parse(@code)
|
|
79
81
|
return unless statements&.size == 1 && (node = spreadable(statements.first))
|
|
80
82
|
|
|
81
|
-
|
|
83
|
+
closing_indent = @indent + (" " * ((@opens_block || opens_block) ? 2 * INDENT_WIDTH : INDENT_WIDTH))
|
|
84
|
+
spread(node, CODE_OFFSET, CODE_OFFSET + @code.bytesize, closing_indent)
|
|
82
85
|
end
|
|
83
86
|
|
|
84
87
|
private
|
|
@@ -89,37 +92,48 @@ module SuperSlim
|
|
|
89
92
|
when Prism::CallNode
|
|
90
93
|
items = self.class.arguments_of(node)
|
|
91
94
|
# An operator such as `+` has no parentheses to add, and `list[index] = value` holds value in its arguments.
|
|
92
|
-
return if
|
|
93
|
-
return
|
|
95
|
+
return if node.name.end_with?("=") || (node.opening_loc.nil? && !METHOD_NAME.match?(node.name.to_s))
|
|
96
|
+
return node if items && items.size > 1
|
|
94
97
|
|
|
95
|
-
|
|
98
|
+
# `[a, b].join(" ")` spreads its array.
|
|
99
|
+
(spreadable(items.first) if items) || (spreadable(node.receiver) if node.receiver.is_a?(Prism::ArrayNode) || node.receiver.is_a?(Prism::HashNode))
|
|
96
100
|
when Prism::ArrayNode, Prism::HashNode
|
|
97
101
|
# `%w[a b]` takes no commas.
|
|
98
102
|
node if ["[", "{"].include?(node.opening) && node.elements.size > 1
|
|
99
103
|
end
|
|
100
104
|
end
|
|
101
105
|
|
|
102
|
-
|
|
106
|
+
# The lines of the code from first to last, node's items spread between them, the first line unindented.
|
|
107
|
+
def spread(node, first, last, closing_indent)
|
|
103
108
|
items = node.is_a?(Prism::CallNode) ? self.class.arguments_of(node) : node.elements
|
|
104
109
|
# `hint: -> do` ends past the line, with the children of the line.
|
|
105
110
|
return if self.class.end_offset(items.last) > CODE_OFFSET + @code.bytesize
|
|
106
111
|
|
|
107
|
-
closing_indent = @indent + (" " * (opens_block ? 2 * INDENT_WIDTH : INDENT_WIDTH))
|
|
108
112
|
item_indent = closing_indent + (" " * INDENT_WIDTH)
|
|
109
113
|
head, tail =
|
|
110
114
|
if node.opening_loc
|
|
111
|
-
[slice(
|
|
115
|
+
[slice(first, node.opening_loc.end_offset), slice(node.closing_loc.start_offset, last)]
|
|
112
116
|
else
|
|
113
|
-
["#{slice(
|
|
117
|
+
["#{slice(first, node.message_loc.end_offset)}(", ")#{slice(self.class.end_offset(items.last), last)}"]
|
|
114
118
|
end
|
|
115
|
-
item_lines = items.
|
|
116
|
-
# A comma after `&block` is a syntax error.
|
|
117
|
-
comma = "," unless item.is_a?(Prism::BlockArgumentNode)
|
|
118
|
-
"#{item_indent}#{slice(item.location.start_offset, self.class.end_offset(item))}#{comma}"
|
|
119
|
-
end
|
|
119
|
+
item_lines = items.flat_map { spread_item(_1, item_indent) }
|
|
120
120
|
[head, *item_lines, closing_indent + tail]
|
|
121
121
|
end
|
|
122
122
|
|
|
123
|
+
def spread_item(item, indent)
|
|
124
|
+
# A comma after `&block` is a syntax error.
|
|
125
|
+
comma = "," unless item.is_a?(Prism::BlockArgumentNode)
|
|
126
|
+
item_end = self.class.end_offset(item)
|
|
127
|
+
line = "#{indent}#{slice(item.location.start_offset, item_end)}#{comma}"
|
|
128
|
+
return [line] if line.size <= @max_line_length
|
|
129
|
+
|
|
130
|
+
nested = spreadable(item.is_a?(Prism::AssocNode) ? item.value : item)
|
|
131
|
+
lines = nested && spread(nested, item.location.start_offset, item_end, indent)
|
|
132
|
+
return [line] unless lines
|
|
133
|
+
|
|
134
|
+
[indent + lines.first, *lines[1...-1], "#{lines.last}#{comma}"]
|
|
135
|
+
end
|
|
136
|
+
|
|
123
137
|
# From offsets in the parsed source, up to the end of the line by default.
|
|
124
138
|
def slice(first, last = CODE_OFFSET + @code.bytesize)
|
|
125
139
|
@code.byteslice((first - CODE_OFFSET)...(last - CODE_OFFSET))
|
|
@@ -63,8 +63,8 @@ module SuperSlim
|
|
|
63
63
|
rejoined = joined_code(broken, info)
|
|
64
64
|
return [rejoined] if rejoined && rejoined.size <= @max_line_length
|
|
65
65
|
|
|
66
|
-
#
|
|
67
|
-
return lines if lines.size > 1 &&
|
|
66
|
+
# A string continued with `\` comes back on one line, longer than the author's.
|
|
67
|
+
return lines if lines.size > 1 && broken.max_by(&:size).size > lines.max_by(&:size).size
|
|
68
68
|
|
|
69
69
|
broken
|
|
70
70
|
end
|
|
@@ -121,7 +121,7 @@ module SuperSlim
|
|
|
121
121
|
return lines unless code_start && lines.last.size > @max_line_length
|
|
122
122
|
|
|
123
123
|
last_indent = lines.last[/\A */]
|
|
124
|
-
code_lines = CodeWrapper.call(lines.last[code_start..], last_indent, opens_block:)
|
|
124
|
+
code_lines = CodeWrapper.call(lines.last[code_start..], last_indent, opens_block:, max_line_length: @max_line_length)
|
|
125
125
|
return lines unless code_lines
|
|
126
126
|
|
|
127
127
|
lines[0...-1] + [lines.last[0...code_start] + code_lines.first, *code_lines.drop(1)]
|
data/lib/super_slim/version.rb
CHANGED