Module: Ast::Crispr::Selectors

Defined in:
lib/ast/crispr.rb

Class Method Summary collapse

Class Method Details

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



916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
# File 'lib/ast/crispr.rb', line 916

def comment_region_owned_owner(marker:, id: nil, limit: nil, owner_scope: :shared_default,
                               comment_region: :leading, include_trailing_gap: true, adapter: nil, metadata: {}, **options)
  marker_text = marker.to_s.rstrip
  OwnerSelector.new(
    id: id || marker_text,
    limit: limit,
    metadata: (
      ,
      adapter,
      owner_scope: owner_scope,
      comment_region: comment_region,
      selector_kind: :comment_region_owned_owner,
      selection_intent: :comment_anchored_owner,
      include_trailing_gap: include_trailing_gap
    ),
    locate: lambda do |context|
      context.structural_owners(owner_scope: owner_scope).filter_map do |owner|
        marker_region = context.comment_regions_for(owner, region: comment_region,
                                                           owner_scope: owner_scope).find do |region|
          context.comment_region_text(region) == marker_text
        end
        next unless marker_region

        end_line = owner.location.end_line
        end_line = context.expand_following_gap(end_line) if include_trailing_gap
        Match.new(
          node: owner,
          start_line: marker_region.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,
            marker: marker_text,
            owner_scope: owner_scope,
            comment_region: comment_region,
            region: marker_region
          }
        )
      end
    end,
    **options
  )
end

.line_block(start_line_text:, end_line_text:, id: nil, limit: nil, include_trailing_gap: false, metadata: {}, **options) ⇒ Object



961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/ast/crispr.rb', line 961

def line_block(start_line_text:, end_line_text:, id: nil, limit: nil, include_trailing_gap: false, metadata: {},
               **options)
  OwnerSelector.new(
    id: id || "line_block_#{start_line_text}",
    limit: limit,
    metadata: (
      ,
      nil,
      owner_scope: :text_lines,
      selector_kind: :line_block,
      selection_intent: :line_marker_block,
      include_trailing_gap: include_trailing_gap
    ).merge(options),
    locate: lambda do |context|
      lines = context.content.to_s.lines
      open_lines = lines.each_with_index.filter_map do |line, index|
        index + 1 if line.chomp == start_line_text.to_s
      end
      close_lines = lines.each_with_index.filter_map do |line, index|
        index + 1 if line.chomp == end_line_text.to_s
      end
      start_line = open_lines.first
      end_line = close_lines.reverse.find { |candidate| start_line && candidate >= start_line }
      next [] unless start_line && end_line

      end_line = context.expand_following_gap(end_line) if include_trailing_gap
      [
        Match.new(
          start_line: start_line,
          end_line: end_line,
          metadata: {
            start_boundary: :owner_start,
            end_boundary: (include_trailing_gap ? :owner_end_plus_trailing_gap : :owner_end),
            payload_kind: :structural_owner_body,
            start_line_text: start_line_text,
            end_line_text: end_line_text
          }
        )
      ]
    end
  )
end

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

Raises:

  • (ArgumentError)


872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
# File 'lib/ast/crispr.rb', line 872

def owner_filter(id:, limit: nil, owner_scope: :shared_default, include_trailing_gap: false, adapter: nil,
                 metadata: {}, &block)
  raise ArgumentError, 'owner_filter requires a block' unless block

  OwnerSelector.new(
    id: id,
    limit: limit,
    metadata: (
      ,
      adapter,
      owner_scope: owner_scope,
      selector_kind: :owner_filter,
      selection_intent: :predicate_filter,
      include_trailing_gap: include_trailing_gap
    ),
    locate: lambda do |context|
      context.structural_owners(owner_scope: owner_scope).filter_map do |owner|
        next unless owner.respond_to?(:location) && owner.location

        match = block.call(context, owner)
        next unless match

        if match.is_a?(Match)
          match
        else
          end_line = owner.location.end_line
          end_line = context.expand_following_gap(end_line) if include_trailing_gap
           = {
            start_boundary: :owner_start,
            end_boundary: (include_trailing_gap ? :owner_end_plus_trailing_gap : :owner_end),
            payload_kind: :structural_owner_body
          }
          Match.new(
            node: owner,
            start_line: owner.location.start_line,
            end_line: end_line,
            metadata: .merge(match == true ? {} : match.to_h)
          )
        end
      end
    end
  )
end