Skip to content

Commit ef1991d

Browse files
authored
Merge pull request #275 from ruby-syntax-tree/embdoc-fixes
EmbDoc fixes
2 parents b34e5d4 + da08570 commit ef1991d

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

lib/syntax_tree/node.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,7 +4090,8 @@ def deconstruct_keys(_keys)
40904090
def format(q)
40914091
q.group do
40924092
q.group do
4093-
q.text("def ")
4093+
q.text("def")
4094+
q.text(" ") if target || name.comments.empty?
40944095

40954096
if target
40964097
q.format(target)
@@ -4872,6 +4873,25 @@ class EmbDoc < Node
48724873
def initialize(value:, location:)
48734874
@value = value
48744875
@location = location
4876+
4877+
@leading = false
4878+
@trailing = false
4879+
end
4880+
4881+
def leading!
4882+
@leading = true
4883+
end
4884+
4885+
def leading?
4886+
@leading
4887+
end
4888+
4889+
def trailing!
4890+
@trailing = true
4891+
end
4892+
4893+
def trailing?
4894+
@trailing
48754895
end
48764896

48774897
def inline?
@@ -4908,7 +4928,13 @@ def deconstruct_keys(_keys)
49084928
end
49094929

49104930
def format(q)
4911-
q.trim
4931+
if (q.parent.is_a?(DefNode) && q.parent.endless?) ||
4932+
q.parent.is_a?(Statements)
4933+
q.trim
4934+
else
4935+
q.breakable_return
4936+
end
4937+
49124938
q.text(value)
49134939
end
49144940

@@ -10465,6 +10491,7 @@ def deconstruct_keys(_keys)
1046510491

1046610492
def format(q)
1046710493
q.text(":")
10494+
q.text("\\") if value.comments.any?
1046810495
q.format(value)
1046910496
end
1047010497

test/fixtures/call.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,8 @@
6060
%
6161
a b do
6262
end.c d
63+
%
64+
self.
65+
=begin
66+
=end
67+
to_s

test/fixtures/def.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,19 @@ def foo() # comment
2323
def foo( # comment
2424
)
2525
end
26+
%
27+
def
28+
=begin
29+
=end
30+
a
31+
end
32+
%
33+
def a()
34+
=begin
35+
=end
36+
=1
37+
-
38+
def a() =
39+
=begin
40+
=end
41+
1

test/fixtures/symbols.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@
1919
%I[foo] # comment
2020
%
2121
%I{foo[]}
22+
%
23+
:\
24+
=begin
25+
=end
26+
symbol

0 commit comments

Comments
 (0)