Debian Codegraph @

lib/prism/parse_result/comments.rb

raw symbols SCIP
1 # frozen_string_literal: true
2 # :markup: markdown
3
4 module Prism
5 class ParseResult < Result
6 # When we've parsed the source, we have both the syntax tree and the list of
7 # comments that we found in the source. This class is responsible for
8 # walking the tree and finding the nearest location to attach each comment.
9 #
10 # It does this by first finding the nearest locations to each comment.
11 # Locations can either come from nodes directly or from location fields on
12 # nodes. For example, a `ClassNode` has an overall location encompassing the
13 # entire class, but it also has a location for the `class` keyword.
14 #
15 # Once the nearest locations are found, it determines which one to attach
16 # to. If it's a trailing comment (a comment on the same line as other source
17 # code), it will favor attaching to the nearest location that occurs before
18 # the comment. Otherwise it will favor attaching to the nearest location
19 # that is after the comment.
20 class Comments
21 # A target for attaching comments that is based on a specific node's
22 # location.
23 class NodeTarget # :nodoc:
24 attr_reader :node
25
26 def initialize(node)
27 @node = node
28 end
29
30 def start_offset
31 node.start_offset
32 end
33
34 def end_offset
35 node.end_offset
36 end
37
38 def encloses?(comment)
39 start_offset <= comment.location.start_offset &&
40 comment.location.end_offset <= end_offset
41 end
42
43 def leading_comment(comment)
44 node.location.leading_comment(comment)
45 end
46
47 def trailing_comment(comment)
48 node.location.trailing_comment(comment)
49 end
50 end
51
52 # A target for attaching comments that is based on a location field on a
53 # node. For example, the `end` token of a ClassNode.
54 class LocationTarget # :nodoc:
55 attr_reader :location
56
57 def initialize(location)
58 @location = location
59 end
60
61 def start_offset
62 location.start_offset
63 end
64
65 def end_offset
66 location.end_offset
67 end
68
69 def encloses?(comment)
70 false
71 end
72
73 def leading_comment(comment)
74 location.leading_comment(comment)
75 end
76
77 def trailing_comment(comment)
78 location.trailing_comment(comment)
79 end
80 end
81
82 # The parse result that we are attaching comments to.
83 attr_reader :parse_result
84
85 # Create a new Comments object that will attach comments to the given
86 # parse result.
87 def initialize(parse_result)
88 @parse_result = parse_result
89 end
90
91 # Attach the comments to their respective locations in the tree by
92 # mutating the parse result.
93 def attach!
94 parse_result.comments.each do |comment|
95 preceding, enclosing, following = nearest_targets(parse_result.value, comment)
96
97 if comment.trailing?
98 if preceding
99 preceding.trailing_comment(comment)
100 else
101 (following || enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
102 end
103 else
104 # If a comment exists on its own line, prefer a leading comment.
105 if following
106 following.leading_comment(comment)
107 elsif preceding
108 preceding.trailing_comment(comment)
109 else
110 (enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
111 end
112 end
113 end
114 end
115
116 private
117
118 # Responsible for finding the nearest targets to the given comment within
119 # the context of the given encapsulating node.
120 def nearest_targets(node, comment)
121 comment_start = comment.location.start_offset
122 comment_end = comment.location.end_offset
123
124 targets = [] #: Array[_Target]
125 node.comment_targets.map do |value|
126 case value
127 when StatementsNode
128 targets.concat(value.body.map { |node| NodeTarget.new(node) })
129 when Node
130 targets << NodeTarget.new(value)
131 when Location
132 targets << LocationTarget.new(value)
133 end
134 end
135
136 targets.sort_by!(&:start_offset)
137 preceding = nil #: _Target?
138 following = nil #: _Target?
139
140 left = 0
141 right = targets.length
142
143 # This is a custom binary search that finds the nearest nodes to the
144 # given comment. When it finds a node that completely encapsulates the
145 # comment, it recurses downward into the tree.
146 while left < right
147 middle = (left + right) / 2
148 target = targets[middle]
149
150 target_start = target.start_offset
151 target_end = target.end_offset
152
153 if target.encloses?(comment)
154 # @type var target: NodeTarget
155 # The comment is completely contained by this target. Abandon the
156 # binary search at this level.
157 return nearest_targets(target.node, comment)
158 end
159
160 if target_end <= comment_start
161 # This target falls completely before the comment. Because we will
162 # never consider this target or any targets before it again, this
163 # target must be the closest preceding target we have encountered so
164 # far.
165 preceding = target
166 left = middle + 1
167 next
168 end
169
170 if comment_end <= target_start
171 # This target falls completely after the comment. Because we will
172 # never consider this target or any targets after it again, this
173 # target must be the closest following target we have encountered so
174 # far.
175 following = target
176 right = middle
177 next
178 end
179
180 # This should only happen if there is a bug in this parser.
181 raise "Comment location overlaps with a target location"
182 end
183
184 [preceding, NodeTarget.new(node), following]
185 end
186 end
187 end
188 end