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

Commit 6818082

Browse files
committed
Allow different line suffix nodes to have different priorities.
1 parent e9e8409 commit 6818082

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
1818
- Ensure comments on assignment after the `=` before the value keep their place.
1919
- Trailing comments on parameters with no parentheses now do not force a break.
2020
- Allow `ArrayLiteral` opening brackets to have trailing comments.
21+
- Allow different line suffix nodes to have different priorities.
2122

2223
### Changed
2324

lib/syntax_tree.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ def initialize(error, lineno, column)
113113
# A slightly enhanced PP that knows how to format recursively including
114114
# comments.
115115
class Formatter < PP
116+
COMMENT_PRIORITY = 1
117+
HEREDOC_PRIORITY = 2
118+
116119
attr_reader :stack, :quote
117120

118121
def initialize(*)
@@ -140,7 +143,7 @@ def format(node)
140143

141144
# Print all comments that were found after the node.
142145
trailing.each do |comment|
143-
line_suffix do
146+
line_suffix(priority: COMMENT_PRIORITY) do
144147
text(" ")
145148
comment.format(self)
146149
break_parent
@@ -6231,7 +6234,7 @@ def format(q)
62316234
q.group do
62326235
q.format(beginning)
62336236

6234-
q.line_suffix do
6237+
q.line_suffix(priority: Formatter::HEREDOC_PRIORITY) do
62356238
q.group do
62366239
breakable.call
62376240

lib/syntax_tree/prettyprint.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,12 @@ def pretty_print(q)
213213
# constantly check where the line ends to avoid accidentally printing some
214214
# content after a line suffix node.
215215
class LineSuffix
216-
attr_reader :contents
216+
DEFAULT_PRIORITY = 1
217217

218-
def initialize(contents: [])
218+
attr_reader :priority, :contents
219+
220+
def initialize(priority: DEFAULT_PRIORITY, contents: [])
221+
@priority = priority
219222
@contents = contents
220223
end
221224

@@ -741,10 +744,17 @@ def flush
741744

742745
# This is a separate command stack that includes the same kind of triplets
743746
# as the commands variable. It is used to keep track of things that should
744-
# go at the end of printed lines once the other doc nodes are
745-
# accounted for. Typically this is used to implement comments.
747+
# go at the end of printed lines once the other doc nodes are accounted for.
748+
# Typically this is used to implement comments.
746749
line_suffixes = []
747750

751+
# This is a special sort used to order the line suffixes by both the
752+
# priority set on the line suffix and the index it was in the original
753+
# array.
754+
line_suffix_sort = ->(line_suffix) {
755+
[-line_suffix.last, -line_suffixes.index(line_suffix)]
756+
}
757+
748758
# This is a linear stack instead of a mutually recursive call defined on
749759
# the individual doc nodes for efficiency.
750760
while commands.any?
@@ -783,7 +793,7 @@ def flush
783793
commands << [indent, mode, doc.flat_contents] if doc.flat_contents
784794
end
785795
when LineSuffix
786-
line_suffixes << [indent, mode, doc.contents]
796+
line_suffixes << [indent, mode, doc.contents, doc.priority]
787797
when Breakable
788798
if mode == MODE_FLAT
789799
if doc.force?
@@ -804,7 +814,7 @@ def flush
804814
# to flush them now, as we are about to add a newline.
805815
if line_suffixes.any?
806816
commands << [indent, mode, doc]
807-
commands += line_suffixes.reverse
817+
commands += line_suffixes.sort_by(&line_suffix_sort)
808818
line_suffixes = []
809819
next
810820
end
@@ -838,7 +848,7 @@ def flush
838848
end
839849

840850
if commands.empty? && line_suffixes.any?
841-
commands += line_suffixes.reverse
851+
commands += line_suffixes.sort_by(&line_suffix_sort)
842852
line_suffixes = []
843853
end
844854
end
@@ -1012,8 +1022,8 @@ def indent
10121022

10131023
# Inserts a LineSuffix node into the print tree. The contents of the node are
10141024
# determined by the block.
1015-
def line_suffix
1016-
doc = LineSuffix.new
1025+
def line_suffix(priority: LineSuffix::DEFAULT_PRIORITY)
1026+
doc = LineSuffix.new(priority: priority)
10171027
target << doc
10181028

10191029
with_target(doc.contents) { yield }

0 commit comments

Comments
 (0)