Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 103a8cd

Browse files
authored
Merge pull request #35 from ruby-syntax-tree/method-prefixes
Method prefixes
2 parents 48ffbae + fb04c4a commit 103a8cd

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

lib/syntax_tree/node.rb

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,12 +1998,18 @@ def format(q)
19981998
q.group { q.format(left) }
19991999
q.text(" ") unless power
20002000

2001-
q.group do
2001+
if operator == :<<
20022002
q.text(operator)
2003+
q.text(" ")
2004+
q.format(right)
2005+
else
2006+
q.group do
2007+
q.text(operator)
20032008

2004-
q.indent do
2005-
q.breakable(power ? "" : " ")
2006-
q.format(right)
2009+
q.indent do
2010+
q.breakable(power ? "" : " ")
2011+
q.format(right)
2012+
end
20072013
end
20082014
end
20092015
end
@@ -3253,7 +3259,12 @@ def format(q)
32533259
q.group do
32543260
q.format(message)
32553261
q.text(" ")
3256-
q.nest(message.value.length + 1) { q.format(arguments) }
3262+
3263+
if align?(self)
3264+
q.nest(message.value.length + 1) { q.format(arguments) }
3265+
else
3266+
q.format(arguments)
3267+
end
32573268
end
32583269
end
32593270

@@ -3280,6 +3291,18 @@ def to_json(*opts)
32803291
cmts: comments
32813292
}.to_json(*opts)
32823293
end
3294+
3295+
private
3296+
3297+
def align?(node)
3298+
if node.arguments in Args[parts: [Def | Defs | DefEndless]]
3299+
false
3300+
elsif node.arguments in Args[parts: [Command => command]]
3301+
align?(command)
3302+
else
3303+
true
3304+
end
3305+
end
32833306
end
32843307

32853308
# CommandCall represents a method call on an object with arguments and no
@@ -7766,9 +7789,15 @@ def format(q)
77667789
q.format(target)
77677790
q.text(" ")
77687791
q.format(operator)
7769-
q.indent do
7770-
q.breakable
7792+
7793+
if skip_indent?
7794+
q.text(" ")
77717795
q.format(value)
7796+
else
7797+
q.indent do
7798+
q.breakable
7799+
q.format(value)
7800+
end
77727801
end
77737802
end
77747803
end
@@ -7800,6 +7829,13 @@ def to_json(*opts)
78007829
cmts: comments
78017830
}.to_json(*opts)
78027831
end
7832+
7833+
private
7834+
7835+
def skip_indent?
7836+
target.comments.empty? &&
7837+
(target.is_a?(ARefField) || AssignFormatting.skip_indent?(value))
7838+
end
78037839
end
78047840

78057841
# If you have a modifier statement (for instance a modifier if statement or a

test/fixtures/command.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,27 @@
55
-
66
foo barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr,
77
bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
8+
%
9+
meta1 def foo
10+
end
11+
%
12+
meta2 meta1 def foo
13+
end
14+
%
15+
meta3 meta2 meta1 def foo
16+
end
17+
%
18+
meta1 def self.foo
19+
end
20+
%
21+
meta2 meta1 def self.foo
22+
end
23+
%
24+
meta3 meta2 meta1 def self.foo
25+
end
26+
%
27+
meta1 def foo = 1
28+
%
29+
meta2 meta1 def foo = 1
30+
%
31+
meta3 meta2 meta1 def foo = 1

0 commit comments

Comments
 (0)