syntax_tree-haml 1.3.2 → 4.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/auto-merge.yml +22 -0
- data/.github/workflows/main.yml +10 -20
- data/CHANGELOG.md +53 -1
- data/Gemfile.lock +16 -17
- data/lib/syntax_tree/haml/format.rb +142 -63
- data/lib/syntax_tree/haml/version.rb +1 -1
- data/lib/syntax_tree/haml.rb +33 -11
- data/syntax_tree-haml.gemspec +2 -2
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c410fddce7b0b3de3dca5f4f0ff6f1f495f70897d1a07f956ceb3cedb8a3bc42
|
4
|
+
data.tar.gz: 6f4866aefe38f780411b65fa2671a9f9491aba0249884dc6c6f627d1a76f5520
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b79312706a5519a3d6933040c99f353ca344290d9c026d27ee2b6aa5b092ef8f03e735ae5e0de4b832bf7acbdceeceb844bdb9ca46ddd1b6aa9ea3f8c1d37cbc
|
7
|
+
data.tar.gz: e0993ba8112bb8149cbcaf972ba588e2b347431254bf0d3d0a63482657d18d5f70af4aa58f185b9d696d5c028bae55621cb483ad9800474242c2236be3043e98
|
@@ -0,0 +1,22 @@
|
|
1
|
+
name: Dependabot auto-merge
|
2
|
+
on: pull_request
|
3
|
+
|
4
|
+
permissions:
|
5
|
+
contents: write
|
6
|
+
pull-requests: write
|
7
|
+
|
8
|
+
jobs:
|
9
|
+
dependabot:
|
10
|
+
runs-on: ubuntu-latest
|
11
|
+
if: ${{ github.actor == 'dependabot[bot]' }}
|
12
|
+
steps:
|
13
|
+
- name: Dependabot metadata
|
14
|
+
id: metadata
|
15
|
+
uses: dependabot/fetch-metadata@v1.3.3
|
16
|
+
with:
|
17
|
+
github-token: "${{ secrets.GITHUB_TOKEN }}"
|
18
|
+
- name: Enable auto-merge for Dependabot PRs
|
19
|
+
run: gh pr merge --auto --merge "$PR_URL"
|
20
|
+
env:
|
21
|
+
PR_URL: ${{github.event.pull_request.html_url}}
|
22
|
+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
data/.github/workflows/main.yml
CHANGED
@@ -1,22 +1,26 @@
|
|
1
1
|
name: Main
|
2
|
+
|
2
3
|
on:
|
3
|
-
|
4
|
-
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- main
|
7
|
+
pull_request: {}
|
8
|
+
|
5
9
|
jobs:
|
6
10
|
ci:
|
7
11
|
strategy:
|
8
12
|
fail-fast: false
|
9
13
|
matrix:
|
10
14
|
ruby:
|
11
|
-
- '2.7'
|
15
|
+
- '2.7.0'
|
16
|
+
- '2.7.5'
|
12
17
|
- '3.0'
|
13
18
|
- '3.1'
|
19
|
+
- '3.2'
|
14
20
|
name: CI
|
15
21
|
runs-on: ubuntu-latest
|
16
|
-
env:
|
17
|
-
CI: true
|
18
22
|
steps:
|
19
|
-
- uses: actions/checkout@
|
23
|
+
- uses: actions/checkout@v3
|
20
24
|
- uses: ruby/setup-ruby@v1
|
21
25
|
with:
|
22
26
|
bundler-cache: true
|
@@ -25,17 +29,3 @@ jobs:
|
|
25
29
|
run: |
|
26
30
|
bundle exec rake test
|
27
31
|
bundle exec rake stree:check
|
28
|
-
automerge:
|
29
|
-
name: AutoMerge
|
30
|
-
needs: ci
|
31
|
-
runs-on: ubuntu-latest
|
32
|
-
if: github.event_name == 'pull_request_target' && github.actor == 'dependabot[bot]'
|
33
|
-
steps:
|
34
|
-
- uses: actions/github-script@v3
|
35
|
-
with:
|
36
|
-
script: |
|
37
|
-
github.pulls.merge({
|
38
|
-
owner: context.payload.repository.owner.login,
|
39
|
-
repo: context.payload.repository.name,
|
40
|
-
pull_number: context.payload.pull_request.number
|
41
|
-
})
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,52 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [4.0.3] - 2023-03-12
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
|
13
|
+
- Fixed a bug where blank lines were being inserted into filter nodes accidentally.
|
14
|
+
|
15
|
+
## [4.0.2] - 2023-03-09
|
16
|
+
|
17
|
+
### Changed
|
18
|
+
|
19
|
+
- Require `prettier_print` `1.2.1` or higher to fix the infinite loop bug.
|
20
|
+
- Maintain blank lines in nested nodes.
|
21
|
+
|
22
|
+
## [4.0.1] - 2023-03-07
|
23
|
+
|
24
|
+
### Changed
|
25
|
+
|
26
|
+
- We now keep blank lines around in the source template. (Multiple blank lines are squished down to a single blank line.)
|
27
|
+
- We now actually parse the Ruby code written in `%=` tags. This fixed a couple of bugs and allows us to better format the output. For example, `%p= 1+1` will now be formatted as `%p= 1 + 1`. If the output fails to parse it falls back to the previous behavior of just printing the Ruby code as-is.
|
28
|
+
|
29
|
+
## [4.0.0] - 2023-03-07
|
30
|
+
|
31
|
+
### Changed
|
32
|
+
|
33
|
+
- Required syntax_tree version 6.0.0 or higher.
|
34
|
+
- Fixed up hash attribute parser to handle multiline hashes using Syntax Tree itself.
|
35
|
+
|
36
|
+
## [3.0.0] - 2022-12-23
|
37
|
+
|
38
|
+
### Changed
|
39
|
+
|
40
|
+
- Required syntax_tree version 5.0.1 or higher.
|
41
|
+
- Drop internal pattern matching in order to support Ruby implementations that don't support it.
|
42
|
+
|
43
|
+
## [2.0.0] - 2022-10-18
|
44
|
+
|
45
|
+
### Added
|
46
|
+
|
47
|
+
- Support for Ruby 2.7.0, not just 2.7.3
|
48
|
+
- Require syntax_tree 4.0.1 or higher.
|
49
|
+
- Require prettier_print 1.0.0 or higher.
|
50
|
+
|
51
|
+
### Changed
|
52
|
+
|
53
|
+
- Nodes must now be formatted with a `SyntaxTree::Haml::Formatter`.
|
54
|
+
|
9
55
|
## [1.3.2] - 2022-09-19
|
10
56
|
|
11
57
|
### Added
|
@@ -60,7 +106,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
|
|
60
106
|
|
61
107
|
- 🎉 Initial release! 🎉
|
62
108
|
|
63
|
-
[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/
|
109
|
+
[unreleased]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v4.0.3...HEAD
|
110
|
+
[4.0.3]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v4.0.2...v4.0.3
|
111
|
+
[4.0.2]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v4.0.1...v4.0.2
|
112
|
+
[4.0.1]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v4.0.0...v4.0.1
|
113
|
+
[4.0.0]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v3.0.0...v4.0.0
|
114
|
+
[3.0.0]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v2.0.0...v3.0.0
|
115
|
+
[2.0.0]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v1.3.2...v2.0.0
|
64
116
|
[1.3.2]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v1.3.1...v1.3.2
|
65
117
|
[1.3.1]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v1.3.0...v1.3.1
|
66
118
|
[1.3.0]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v1.2.1...v1.3.0
|
data/Gemfile.lock
CHANGED
@@ -1,37 +1,36 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
syntax_tree-haml (
|
4
|
+
syntax_tree-haml (4.0.3)
|
5
5
|
haml (>= 5.2)
|
6
|
-
prettier_print
|
7
|
-
syntax_tree (>=
|
6
|
+
prettier_print (>= 1.2.1)
|
7
|
+
syntax_tree (>= 6.0.0)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
12
|
docile (1.4.0)
|
13
|
-
haml (
|
14
|
-
temple (>= 0.8.
|
13
|
+
haml (6.1.1)
|
14
|
+
temple (>= 0.8.2)
|
15
|
+
thor
|
15
16
|
tilt
|
16
|
-
minitest (5.
|
17
|
-
prettier_print (
|
17
|
+
minitest (5.18.0)
|
18
|
+
prettier_print (1.2.1)
|
18
19
|
rake (13.0.6)
|
19
|
-
simplecov (0.
|
20
|
+
simplecov (0.22.0)
|
20
21
|
docile (~> 1.1)
|
21
22
|
simplecov-html (~> 0.11)
|
22
23
|
simplecov_json_formatter (~> 0.1)
|
23
24
|
simplecov-html (0.12.3)
|
24
25
|
simplecov_json_formatter (0.1.4)
|
25
|
-
syntax_tree (
|
26
|
-
prettier_print
|
27
|
-
temple (0.
|
28
|
-
|
26
|
+
syntax_tree (6.0.2)
|
27
|
+
prettier_print (>= 1.2.0)
|
28
|
+
temple (0.10.0)
|
29
|
+
thor (1.2.1)
|
30
|
+
tilt (2.1.0)
|
29
31
|
|
30
32
|
PLATFORMS
|
31
|
-
|
32
|
-
x86_64-darwin-19
|
33
|
-
x86_64-darwin-20
|
34
|
-
x86_64-darwin-21
|
33
|
+
arm64-darwin-22
|
35
34
|
x86_64-linux
|
36
35
|
|
37
36
|
DEPENDENCIES
|
@@ -42,4 +41,4 @@ DEPENDENCIES
|
|
42
41
|
syntax_tree-haml!
|
43
42
|
|
44
43
|
BUNDLED WITH
|
45
|
-
2.
|
44
|
+
2.4.1
|
@@ -4,9 +4,13 @@ module SyntaxTree
|
|
4
4
|
module Haml
|
5
5
|
class Format < Visitor
|
6
6
|
class Formatter < ::SyntaxTree::Formatter
|
7
|
-
attr_reader :literal_lines
|
7
|
+
attr_reader :literal_lines
|
8
8
|
|
9
|
-
def initialize(
|
9
|
+
def initialize(
|
10
|
+
source,
|
11
|
+
*rest,
|
12
|
+
options: ::SyntaxTree::Formatter::Options.new
|
13
|
+
)
|
10
14
|
@literal_lines = {}
|
11
15
|
source
|
12
16
|
.lines
|
@@ -15,7 +19,7 @@ module SyntaxTree
|
|
15
19
|
@literal_lines[index] = line.rstrip if line.start_with?("!")
|
16
20
|
end
|
17
21
|
|
18
|
-
super(source,
|
22
|
+
super(source, *rest, options: options)
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
@@ -62,12 +66,20 @@ module SyntaxTree
|
|
62
66
|
q.text(node.value[:name])
|
63
67
|
|
64
68
|
q.indent do
|
65
|
-
q.
|
69
|
+
q.breakable_force
|
70
|
+
first = true
|
71
|
+
|
72
|
+
node.value[:text]
|
73
|
+
.rstrip
|
74
|
+
.each_line(chomp: true) do |line|
|
75
|
+
if first
|
76
|
+
first = false
|
77
|
+
else
|
78
|
+
q.breakable_force
|
79
|
+
end
|
66
80
|
|
67
|
-
|
68
|
-
|
69
|
-
q.text(segment)
|
70
|
-
end
|
81
|
+
q.text(line)
|
82
|
+
end
|
71
83
|
end
|
72
84
|
end
|
73
85
|
end
|
@@ -78,11 +90,19 @@ module SyntaxTree
|
|
78
90
|
text = node.value[:text].strip
|
79
91
|
|
80
92
|
if text.include?("\n")
|
81
|
-
separator = -> { q.breakable(force: true) }
|
82
|
-
|
83
93
|
q.indent do
|
84
|
-
|
85
|
-
|
94
|
+
q.breakable_force
|
95
|
+
first = true
|
96
|
+
|
97
|
+
text.each_line(chomp: true) do |line|
|
98
|
+
if first
|
99
|
+
first = false
|
100
|
+
else
|
101
|
+
q.breakable_force
|
102
|
+
end
|
103
|
+
|
104
|
+
q.text(line)
|
105
|
+
end
|
86
106
|
end
|
87
107
|
else
|
88
108
|
q.text(" #{text}")
|
@@ -102,9 +122,14 @@ module SyntaxTree
|
|
102
122
|
|
103
123
|
# Visit the root node of the AST.
|
104
124
|
def visit_root(node)
|
125
|
+
previous_line = nil
|
126
|
+
|
105
127
|
node.children.each do |child|
|
128
|
+
q.breakable_force if previous_line && (child.line - previous_line) > 1
|
129
|
+
previous_line = child.last_line
|
130
|
+
|
106
131
|
visit(child)
|
107
|
-
q.
|
132
|
+
q.breakable_force
|
108
133
|
end
|
109
134
|
end
|
110
135
|
|
@@ -130,11 +155,11 @@ module SyntaxTree
|
|
130
155
|
|
131
156
|
node.children.each do |child|
|
132
157
|
if continuation?(node, child)
|
133
|
-
q.
|
158
|
+
q.breakable_force
|
134
159
|
visit(child)
|
135
160
|
else
|
136
161
|
q.indent do
|
137
|
-
q.
|
162
|
+
q.breakable_force
|
138
163
|
visit(child)
|
139
164
|
end
|
140
165
|
end
|
@@ -144,8 +169,6 @@ module SyntaxTree
|
|
144
169
|
|
145
170
|
LiteralHashValue = Struct.new(:value)
|
146
171
|
|
147
|
-
StringHashValue = Struct.new(:value, :quote)
|
148
|
-
|
149
172
|
# When formatting a tag, there are a lot of different kinds of things that
|
150
173
|
# can be printed out. There's the tag name, the attributes, the content,
|
151
174
|
# etc. This object is responsible for housing all of those parts.
|
@@ -201,6 +224,14 @@ module SyntaxTree
|
|
201
224
|
end
|
202
225
|
|
203
226
|
class HTMLAttributesPart
|
227
|
+
class Separator
|
228
|
+
def call(q)
|
229
|
+
q.fill_breakable
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
SEPARATOR = Separator.new
|
234
|
+
|
204
235
|
attr_reader :values
|
205
236
|
|
206
237
|
def initialize(raw)
|
@@ -214,11 +245,9 @@ module SyntaxTree
|
|
214
245
|
q.group do
|
215
246
|
q.text("(")
|
216
247
|
q.nest(align) do
|
217
|
-
q.seplist(
|
218
|
-
|
219
|
-
|
220
|
-
:each_pair
|
221
|
-
) { |key, value| q.text("#{key}=#{value}") }
|
248
|
+
q.seplist(values, SEPARATOR, :each_pair) do |key, value|
|
249
|
+
q.text("#{key}=#{value}")
|
250
|
+
end
|
222
251
|
end
|
223
252
|
q.text(")")
|
224
253
|
end
|
@@ -249,9 +278,9 @@ module SyntaxTree
|
|
249
278
|
q.text("{")
|
250
279
|
q.indent do
|
251
280
|
q.group do
|
252
|
-
|
281
|
+
level == 0 ? q.breakable_empty : q.breakable_space
|
253
282
|
q.seplist(hash, nil, :each_pair) do |key, value|
|
254
|
-
if key.match?(/^@|[-:]/)
|
283
|
+
if key.match?(/^@|[-:]/) && !key.match?(/^["']/)
|
255
284
|
q.text("#{quote}#{Quotes.normalize(key, quote)}#{quote}:")
|
256
285
|
else
|
257
286
|
q.text("#{key}:")
|
@@ -275,7 +304,7 @@ module SyntaxTree
|
|
275
304
|
end
|
276
305
|
end
|
277
306
|
|
278
|
-
|
307
|
+
level == 0 ? q.breakable_empty : q.breakable_space
|
279
308
|
q.text("}")
|
280
309
|
end
|
281
310
|
end
|
@@ -329,17 +358,7 @@ module SyntaxTree
|
|
329
358
|
# we're going to print them out here.
|
330
359
|
if node.value[:dynamic_attributes].old
|
331
360
|
parts << PlainPart.new("%div") if parts.empty?
|
332
|
-
|
333
|
-
if ::Haml::AttributeParser.available?
|
334
|
-
dynamic = parse_attributes(node.value[:dynamic_attributes].old)
|
335
|
-
parts << if dynamic.is_a?(LiteralHashValue)
|
336
|
-
PlainPart.new(dynamic.value)
|
337
|
-
else
|
338
|
-
HashAttributesPart.new(dynamic)
|
339
|
-
end
|
340
|
-
else
|
341
|
-
parts << PlainPart.new(node.value[:dynamic_attributes].old)
|
342
|
-
end
|
361
|
+
parts << parse_attributes(node.value[:dynamic_attributes].old)
|
343
362
|
end
|
344
363
|
|
345
364
|
# https://haml.info/docs/yardoc/file.REFERENCE.html#object-reference-
|
@@ -363,18 +382,10 @@ module SyntaxTree
|
|
363
382
|
q.indent do
|
364
383
|
# Split between the declaration of the tag and the contents of the
|
365
384
|
# tag.
|
366
|
-
q.
|
385
|
+
q.breakable_empty
|
367
386
|
|
368
|
-
if node.value[:parse]
|
369
|
-
|
370
|
-
# interpolation and it's marked as { parse: true }, then we
|
371
|
-
# don't actually want the = prefix, and we want to remove extra
|
372
|
-
# escaping.
|
373
|
-
q.if_break { q.text("") }.if_flat { q.text(" ") }
|
374
|
-
q.text(value[1...-1].gsub(/\\"/, "\""))
|
375
|
-
elsif node.value[:parse]
|
376
|
-
q.text("= ")
|
377
|
-
q.text(value)
|
387
|
+
if node.value[:parse]
|
388
|
+
format_tag_value(q, value)
|
378
389
|
else
|
379
390
|
q.if_break { q.text("") }.if_flat { q.text(" ") }
|
380
391
|
q.text(value)
|
@@ -388,17 +399,42 @@ module SyntaxTree
|
|
388
399
|
|
389
400
|
private
|
390
401
|
|
402
|
+
def format_tag_value(q, value)
|
403
|
+
program = SyntaxTree.parse(value)
|
404
|
+
if !program || program.statements.body.length > 1
|
405
|
+
return q.text("= #{value}")
|
406
|
+
end
|
407
|
+
|
408
|
+
statement = program.statements.body.first
|
409
|
+
formatter = SyntaxTree::Formatter.new(value, [], Float::INFINITY)
|
410
|
+
formatter.format(statement)
|
411
|
+
formatter.flush
|
412
|
+
formatted = formatter.output.join
|
413
|
+
|
414
|
+
if statement.is_a?(StringLiteral) && statement.parts.length > 1
|
415
|
+
# There's a weird case here where if the value includes interpolation
|
416
|
+
# and it's marked as { parse: true }, then we don't actually want the
|
417
|
+
# = prefix, and we want to remove extra escaping.
|
418
|
+
q.if_break { q.text("") }.if_flat { q.text(" ") }
|
419
|
+
q.text(formatted[1...-1].gsub(/\\"/, "\""))
|
420
|
+
else
|
421
|
+
q.text("= #{formatted}")
|
422
|
+
end
|
423
|
+
rescue Parser::ParseError
|
424
|
+
q.text("= #{value}")
|
425
|
+
end
|
426
|
+
|
391
427
|
# When printing out sequences of silent scripts, sometimes subsequent nodes
|
392
428
|
# will be continuations of previous nodes. In that case we want to dedent
|
393
429
|
# them to match.
|
394
430
|
def continuation?(node, child)
|
395
431
|
return false if child.type != :silent_script
|
396
432
|
|
397
|
-
case
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
433
|
+
case node.value[:keyword]
|
434
|
+
when "case"
|
435
|
+
%w[in when else].include?(child.value[:keyword])
|
436
|
+
when "if", "unless"
|
437
|
+
%w[elsif else].include?(child.value[:keyword])
|
402
438
|
else
|
403
439
|
false
|
404
440
|
end
|
@@ -413,20 +449,55 @@ module SyntaxTree
|
|
413
449
|
end
|
414
450
|
end
|
415
451
|
|
416
|
-
# Take a source string and attempt to parse it into a set of attributes
|
417
|
-
# can be used to format the source.
|
452
|
+
# Take a source string and attempt to parse it into a set of attributes
|
453
|
+
# that can be used to format the source.
|
418
454
|
def parse_attributes(source)
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
455
|
+
source = source.strip
|
456
|
+
source = source.start_with?("{") ? source : "{#{source}}"
|
457
|
+
|
458
|
+
program = SyntaxTree.parse(source)
|
459
|
+
return PlainPart.new(source) if program.nil?
|
460
|
+
|
461
|
+
node = program.statements.body.first
|
462
|
+
return PlainPart.new(source) unless node.is_a?(HashLiteral)
|
463
|
+
|
464
|
+
HashAttributesPart.new(parse_attributes_hash(source, node))
|
465
|
+
rescue Parser::ParseError
|
466
|
+
PlainPart.new(source)
|
467
|
+
end
|
468
|
+
|
469
|
+
def parse_attributes_hash(source, node, level = 1)
|
470
|
+
node.assocs.to_h do |assoc|
|
471
|
+
key =
|
472
|
+
case assoc.key
|
473
|
+
when StringLiteral
|
474
|
+
format(assoc.key)
|
475
|
+
when Label
|
476
|
+
assoc.key.value.delete_suffix(":")
|
477
|
+
when DynaSymbol
|
478
|
+
format(assoc.key).delete_prefix(":")
|
479
|
+
else
|
480
|
+
format(assoc.key)
|
481
|
+
end
|
482
|
+
|
483
|
+
value =
|
484
|
+
case assoc.value
|
485
|
+
when HashLiteral
|
486
|
+
parse_attributes_hash(source, assoc.value, level + 1)
|
487
|
+
when StringLiteral
|
488
|
+
assoc.value
|
489
|
+
else
|
490
|
+
LiteralHashValue.new(format(assoc.value, level * 2).lstrip)
|
491
|
+
end
|
492
|
+
|
493
|
+
[key, value]
|
427
494
|
end
|
428
495
|
end
|
429
496
|
|
497
|
+
def format(node, column = 0)
|
498
|
+
SyntaxTree::Formatter.format(+"", node, column)
|
499
|
+
end
|
500
|
+
|
430
501
|
def with_children(node)
|
431
502
|
if node.children.empty?
|
432
503
|
q.group { yield }
|
@@ -434,9 +505,17 @@ module SyntaxTree
|
|
434
505
|
q.group do
|
435
506
|
q.group { yield }
|
436
507
|
q.indent do
|
508
|
+
previous_line = nil
|
509
|
+
|
437
510
|
node.children.each do |child|
|
438
|
-
q.
|
511
|
+
q.breakable_force
|
512
|
+
|
513
|
+
if previous_line && (child.line - previous_line) > 1
|
514
|
+
q.breakable_force
|
515
|
+
end
|
516
|
+
|
439
517
|
visit(child)
|
518
|
+
previous_line = child.last_line
|
440
519
|
end
|
441
520
|
end
|
442
521
|
end
|
data/lib/syntax_tree/haml.rb
CHANGED
@@ -33,8 +33,8 @@ module SyntaxTree
|
|
33
33
|
|
34
34
|
# This is the main entrypoint for the formatter. It parses the source,
|
35
35
|
# builds a formatter, then pretty prints the result.
|
36
|
-
def self.format(source, maxwidth = 80)
|
37
|
-
formatter = Format::Formatter.new(source, +"", maxwidth)
|
36
|
+
def self.format(source, maxwidth = 80, options: Formatter::Options.new)
|
37
|
+
formatter = Format::Formatter.new(source, +"", maxwidth, options: options)
|
38
38
|
parse(source).format(formatter)
|
39
39
|
|
40
40
|
formatter.flush
|
@@ -59,31 +59,53 @@ class Haml::Parser::ParseNode
|
|
59
59
|
# accept a visitor in order to walk through the tree.
|
60
60
|
def accept(visitor)
|
61
61
|
case type
|
62
|
-
|
62
|
+
when :comment
|
63
63
|
visitor.visit_comment(self)
|
64
|
-
|
64
|
+
when :doctype
|
65
65
|
visitor.visit_doctype(self)
|
66
|
-
|
66
|
+
when :filter
|
67
67
|
visitor.visit_filter(self)
|
68
|
-
|
68
|
+
when :haml_comment
|
69
69
|
visitor.visit_haml_comment(self)
|
70
|
-
|
70
|
+
when :plain
|
71
71
|
visitor.visit_plain(self)
|
72
|
-
|
72
|
+
when :root
|
73
73
|
visitor.visit_root(self)
|
74
|
-
|
74
|
+
when :script
|
75
75
|
visitor.visit_script(self)
|
76
|
-
|
76
|
+
when :silent_script
|
77
77
|
visitor.visit_silent_script(self)
|
78
|
-
|
78
|
+
when :tag
|
79
79
|
visitor.visit_tag(self)
|
80
|
+
else
|
81
|
+
raise "Unknown node type: #{type}"
|
80
82
|
end
|
81
83
|
end
|
82
84
|
|
85
|
+
# This is our entrypoint for the formatter. We effectively delegate this to
|
86
|
+
# accepting the Format visitor.
|
83
87
|
def format(q)
|
84
88
|
accept(SyntaxTree::Haml::Format.new(q))
|
85
89
|
end
|
86
90
|
|
91
|
+
# When we're formatting a list of children, we need to know the last line a
|
92
|
+
# node is on. This is because the next node in the list of children should be
|
93
|
+
# at most 1 blank line below the last line of the previous node. We cache this
|
94
|
+
# because at worst it requires walking the entire tree because filter nodes
|
95
|
+
# can take up multiple lines.
|
96
|
+
def last_line
|
97
|
+
@last_line ||=
|
98
|
+
if children.any?
|
99
|
+
children.last.last_line
|
100
|
+
elsif type == :filter
|
101
|
+
line + value[:text].rstrip.count("\n") + 1
|
102
|
+
else
|
103
|
+
line
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# This is our entrypoint for the pretty printer. We effectively delegate this
|
108
|
+
# to accepting the PrettyPrint visitor.
|
87
109
|
def pretty_print(q)
|
88
110
|
accept(SyntaxTree::Haml::PrettyPrint.new(q))
|
89
111
|
end
|
data/syntax_tree-haml.gemspec
CHANGED
@@ -25,8 +25,8 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.require_paths = %w[lib]
|
26
26
|
|
27
27
|
spec.add_dependency "haml", ">= 5.2"
|
28
|
-
spec.add_dependency "prettier_print"
|
29
|
-
spec.add_dependency "syntax_tree", ">=
|
28
|
+
spec.add_dependency "prettier_print", ">= 1.2.1"
|
29
|
+
spec.add_dependency "syntax_tree", ">= 6.0.0"
|
30
30
|
|
31
31
|
spec.add_development_dependency "bundler"
|
32
32
|
spec.add_development_dependency "minitest"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syntax_tree-haml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Newton
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: haml
|
@@ -30,28 +30,28 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.2.1
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 1.2.1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: syntax_tree
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 6.0.0
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 6.0.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: bundler
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,6 +116,7 @@ extensions: []
|
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
118
|
- ".github/dependabot.yml"
|
119
|
+
- ".github/workflows/auto-merge.yml"
|
119
120
|
- ".github/workflows/main.yml"
|
120
121
|
- ".gitignore"
|
121
122
|
- CHANGELOG.md
|
@@ -152,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
153
|
- !ruby/object:Gem::Version
|
153
154
|
version: '0'
|
154
155
|
requirements: []
|
155
|
-
rubygems_version: 3.
|
156
|
+
rubygems_version: 3.4.1
|
156
157
|
signing_key:
|
157
158
|
specification_version: 4
|
158
159
|
summary: Syntax Tree support for Haml
|