Class: MetaCommit::Extension::MarkdownSupport::Diffs::AnchorRename

Inherits:
Diff
  • Object
show all
Defined in:
lib/meta_commit_markdown_support/diffs/anchor_rename.rb

Constant Summary

Constants inherited from Diff

Diff::SUPPORTED_PARSERS

Constants included from ContextualAstAccessor

ContextualAstAccessor::ELEMENT_TYPE_ANCHOR, ContextualAstAccessor::ELEMENT_TYPE_CODE, ContextualAstAccessor::ELEMENT_TYPE_HEADER, ContextualAstAccessor::ELEMENT_TYPE_IMAGE, ContextualAstAccessor::ELEMENT_TYPE_LISTS, ContextualAstAccessor::ELEMENT_TYPE_LIST_ITEM, ContextualAstAccessor::ELEMENT_TYPE_PARAGRAPH, ContextualAstAccessor::ELEMENT_TYPE_TEXT

Instance Method Summary collapse

Methods inherited from Diff

#inspect, #supports_parser?, #to_s, #type_addition?, #type_deletion?, #type_replace?

Methods included from ContextualAstAccessor

#anchor?, #anchor_context?, #anchor_target, #anchor_title, #closest_header_of_list, #code?, #code_context?, #code_first_line, #context_node_neighbours, #context_nodes_on_line, #contextual_ast_has_target_node, #element_closest_to, #elements_of_type_on_line, #header?, #header_content, #image?, #list?, #list_context?, #list_item?, #nested_list_context?, #paragraph?, #paragraph_context?, #parent_list_item_title, #starts_on_line?, #string_content, #text?, #text_context?

Instance Method Details

#string_representationString

Returns:

  • (String)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/meta_commit_markdown_support/diffs/anchor_rename.rb', line 12

def string_representation
  old_links = elements_of_type_on_line(change_context.old_contextual_ast, ELEMENT_TYPE_ANCHOR, change_context.old_lineno)
  new_links = elements_of_type_on_line(change_context.new_contextual_ast, ELEMENT_TYPE_ANCHOR, change_context.new_lineno)

  closest_old_link = element_closest_to(old_links, change_context.column)
  closest_new_link = element_closest_to(new_links, change_context.column)

  if !closest_new_link.nil? && !closest_new_link.children.first.nil? && text?(closest_new_link.children.first)
    new_anchor_title = closest_new_link.element.first_child.string_content
    closest_new_link.element.first_child.string_content
    return "change #{new_anchor_title} link"
  end

  if !closest_new_link.nil? && !closest_new_link.children.first.nil? && image?(closest_new_link.children.first)
    new_anchor_title = closest_new_link.element.first_child.first_child.string_content
    closest_new_link.element.first_child.first_child.string_content
    return "change #{new_anchor_title}"
  end

  anchor_target = anchor_target(change_context.new_contextual_ast)
  "change #{anchor_target} link"
end

#supports_change(context) ⇒ Boolean

Parameters:

  • context (MetaCommit::Contracts::ChangeContext)

Returns:

  • (Boolean)


5
6
7
8
9
# File 'lib/meta_commit_markdown_support/diffs/anchor_rename.rb', line 5

def supports_change(context)
  context.type == TYPE_REPLACE &&
      contextual_ast_has_target_node(context.old_contextual_ast) && contextual_ast_has_target_node(context.new_contextual_ast) &&
      anchor_context?(context.old_contextual_ast) && anchor_context?(context.new_contextual_ast)
end