Class: Ast::Crispr::DocumentContext

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/crispr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, source_label: 'source', adapter: Adapters::Null.new, metadata: {}, **options) ⇒ DocumentContext

Returns a new instance of DocumentContext.



717
718
719
720
721
722
# File 'lib/ast/crispr.rb', line 717

def initialize(content:, source_label: 'source', adapter: Adapters::Null.new, metadata: {}, **options)
  @content = content.to_s
  @source_label = source_label
  @adapter = adapter
  @metadata = .merge(options)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



715
716
717
# File 'lib/ast/crispr.rb', line 715

def adapter
  @adapter
end

#contentObject (readonly)

Returns the value of attribute content.



715
716
717
# File 'lib/ast/crispr.rb', line 715

def content
  @content
end

#metadataObject (readonly)

Returns the value of attribute metadata.



715
716
717
# File 'lib/ast/crispr.rb', line 715

def 
  @metadata
end

#source_labelObject (readonly)

Returns the value of attribute source_label.



715
716
717
# File 'lib/ast/crispr.rb', line 715

def source_label
  @source_label
end

Instance Method Details

#astObject



728
729
730
# File 'lib/ast/crispr.rb', line 728

def ast
  @ast ||= adapter.read_ast(self)
end

#comment_region_text(comment_region) ⇒ Object



740
741
742
# File 'lib/ast/crispr.rb', line 740

def comment_region_text(comment_region)
  adapter.comment_region_text(self, comment_region)
end

#comment_regions_for(owner, region: :leading, owner_scope: :shared_default) ⇒ Object



736
737
738
# File 'lib/ast/crispr.rb', line 736

def comment_regions_for(owner, region: :leading, owner_scope: :shared_default)
  adapter.comment_regions_for(self, owner, region: region, owner_scope: owner_scope)
end

#expand_following_gap(line_number) ⇒ Object



760
761
762
763
764
# File 'lib/ast/crispr.rb', line 760

def expand_following_gap(line_number)
  last_line = line_number
  last_line += 1 while line_blank?(last_line + 1)
  last_line
end

#line_blank?(line_number) ⇒ Boolean

Returns:

  • (Boolean)


766
767
768
769
# File 'lib/ast/crispr.rb', line 766

def line_blank?(line_number)
  line = lines[line_number - 1]
  !line.nil? && line.strip.empty?
end

#linesObject



724
725
726
# File 'lib/ast/crispr.rb', line 724

def lines
  @lines ||= content.lines
end

#location_slice(location) ⇒ Object



756
757
758
# File 'lib/ast/crispr.rb', line 756

def location_slice(location)
  content.byteslice(location.start_offset...location.end_offset).to_s
end

#structural_owners(owner_scope: :shared_default) ⇒ Object



732
733
734
# File 'lib/ast/crispr.rb', line 732

def structural_owners(owner_scope: :shared_default)
  adapter.structural_owners(self, owner_scope: owner_scope)
end

#structure_profile(owner_scope: :shared_default) ⇒ Object



744
745
746
747
748
749
750
751
752
753
754
# File 'lib/ast/crispr.rb', line 744

def structure_profile(owner_scope: :shared_default)
  if adapter.respond_to?(:structure_profile)
    adapter.structure_profile(owner_scope: owner_scope)
  else
    StructureProfile.new(
      owner_scope: owner_scope,
      owner_selector: owner_scope,
      metadata: { source: :document_context_default }
    )
  end
end