Module: Ast::Crispr::Ruby::Prism::Selectors

Defined in:
lib/ast/crispr/ruby/prism.rb

Class Method Summary collapse

Class Method Details

.comment_line_block(start_text:, end_text:, id: nil, limit: nil, span: :nearest, include_trailing_gap: false, metadata: {}, **options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ast/crispr/ruby/prism.rb', line 115

def comment_line_block(start_text:, end_text:, id: nil, limit: nil, span: :nearest,
                       include_trailing_gap: false, metadata: {}, **options)
  Ast::Crispr::OwnerSelector.new(
    id: id || "ruby_comment_line_block_#{start_text}",
    limit: limit,
    metadata: .merge(
      adapter: Ast::Crispr::Ruby::Prism.adapter,
      owner_scope: :ruby_comments,
      selector_kind: :ruby_comment_line_block,
      selection_intent: :line_marker_block,
      include_trailing_gap: include_trailing_gap
    ).merge(options),
    locate: lambda do |context|
      comments = context.structural_owners(owner_scope: :ruby_comments)
      if span.to_sym == :outermost
        opening = comments.find do |comment|
          context.location_slice(comment.location).rstrip == start_text.to_s
        end
        closing = comments.reverse.find do |comment|
          opening &&
            context.location_slice(comment.location).rstrip == end_text.to_s &&
            comment.location.end_line >= opening.location.start_line
        end
        next [] unless opening && closing

        end_line = closing.location.end_line
        end_line = context.expand_following_gap(end_line) if include_trailing_gap
        next [
          Ast::Crispr::Match.new(
            node: opening,
            start_line: opening.location.start_line,
            end_line: end_line,
            metadata: {
              start_boundary: :comment_region_start,
              end_boundary: (include_trailing_gap ? :owner_end_plus_trailing_gap : :owner_end),
              payload_kind: :comment_owned_body,
              start_text: start_text,
              end_text: end_text
            }
          )
        ]
      end

      comments.each_with_index.filter_map do |comment, index|
        next unless context.location_slice(comment.location).rstrip == start_text.to_s

        closing = comments[index + 1..]&.find do |candidate|
          context.location_slice(candidate.location).rstrip == end_text.to_s
        end
        next unless closing

        end_line = closing.location.end_line
        end_line = context.expand_following_gap(end_line) if include_trailing_gap
        Ast::Crispr::Match.new(
          node: comment,
          start_line: comment.location.start_line,
          end_line: end_line,
          metadata: {
            start_boundary: :comment_region_start,
            end_boundary: (include_trailing_gap ? :owner_end_plus_trailing_gap : :owner_end),
            payload_kind: :comment_owned_body,
            start_text: start_text,
            end_text: end_text
          }
        )
      end
    end
  )
end

.comment_region_owned_owner(marker:, id: nil, limit: nil, owner_scope: :shared_default, comment_region: :leading, include_trailing_gap: true, metadata: {}, **options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ast/crispr/ruby/prism.rb', line 100

def comment_region_owned_owner(marker:, id: nil, limit: nil, owner_scope: :shared_default,
                               comment_region: :leading, include_trailing_gap: true, metadata: {}, **options)
  Ast::Crispr::Selectors.comment_region_owned_owner(
    marker: marker,
    id: id,
    limit: limit,
    owner_scope: owner_scope,
    comment_region: comment_region,
    include_trailing_gap: include_trailing_gap,
    adapter: Ast::Crispr::Ruby::Prism.adapter,
    metadata: ,
    **options
  )
end

.owner_filter(id:, limit: nil, owner_scope: :shared_default, include_trailing_gap: false, metadata: {}, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ast/crispr/ruby/prism.rb', line 87

def owner_filter(id:, limit: nil, owner_scope: :shared_default, include_trailing_gap: false, metadata: {},
                 &block)
  Ast::Crispr::Selectors.owner_filter(
    id: id,
    limit: limit,
    owner_scope: owner_scope,
    include_trailing_gap: include_trailing_gap,
    adapter: Ast::Crispr::Ruby::Prism.adapter,
    metadata: ,
    &block
  )
end