Class: Prosereflect::ResolvedPos

Inherits:
Object
  • Object
show all
Defined in:
lib/prosereflect/resolved_pos.rb

Overview

ResolvedPos represents a document position that has been resolved to a specific location in the document tree.

The path array contains: [parent_node, index, start, parent_node, index, start, …] depth 0 = before any nodes, depth N = inside node at path

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pos, path, depth) ⇒ ResolvedPos

Returns a new instance of ResolvedPos.



12
13
14
15
16
17
# File 'lib/prosereflect/resolved_pos.rb', line 12

def initialize(pos, path, depth)
  @pos = pos
  @path = path
  @depth = depth
  @parent_offset = nil
end

Instance Attribute Details

#depthObject (readonly)

Returns the value of attribute depth.



10
11
12
# File 'lib/prosereflect/resolved_pos.rb', line 10

def depth
  @depth
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/prosereflect/resolved_pos.rb', line 10

def path
  @path
end

#posObject (readonly)

Returns the value of attribute pos.



10
11
12
# File 'lib/prosereflect/resolved_pos.rb', line 10

def pos
  @pos
end

Instance Method Details

#after?Boolean

Get position after current node

Returns:

  • (Boolean)


135
136
137
138
139
140
141
# File 'lib/prosereflect/resolved_pos.rb', line 135

def after?
  if depth.zero?
    @pos >= 0
  else
    index >= parent.content.size
  end
end

#before?Boolean

Get position before current node

Returns:

  • (Boolean)


126
127
128
129
130
131
132
# File 'lib/prosereflect/resolved_pos.rb', line 126

def before?
  if depth.zero?
    @pos.zero?
  else
    index.zero?
  end
end

#block?Boolean

Check if at block boundary

Returns:

  • (Boolean)


101
102
103
# File 'lib/prosereflect/resolved_pos.rb', line 101

def block?
  parent.respond_to?(:is_block?) && parent.is_block?
end

#block_range(other_pos = nil) ⇒ Object

Get block range to another position



95
96
97
98
# File 'lib/prosereflect/resolved_pos.rb', line 95

def block_range(other_pos = nil)
  other_pos ||= self
  NodeRange.new(self, other_pos)
end

#end_(depth = @depth) ⇒ Object

End position of current parent node



35
36
37
# File 'lib/prosereflect/resolved_pos.rb', line 35

def end_(depth = @depth)
  start(depth) + parent.content.size
end

#end_of_parent?Boolean

Check if at end of parent

Returns:

  • (Boolean)


121
122
123
# File 'lib/prosereflect/resolved_pos.rb', line 121

def end_of_parent?
  parent_offset >= parent.content.size - 1
end

#eq?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


143
144
145
146
147
# File 'lib/prosereflect/resolved_pos.rb', line 143

def eq?(other)
  return false unless other.is_a?(ResolvedPos)

  @pos == other.pos && @depth == other.depth
end

#hashObject



151
152
153
# File 'lib/prosereflect/resolved_pos.rb', line 151

def hash
  [@pos, @depth].hash
end

#index(depth = @depth) ⇒ Object

Index within parent



25
26
27
# File 'lib/prosereflect/resolved_pos.rb', line 25

def index(depth = @depth)
  @path[(depth * 3) + 1]
end

#inline?Boolean

Check if at inline boundary

Returns:

  • (Boolean)


106
107
108
# File 'lib/prosereflect/resolved_pos.rb', line 106

def inline?
  !block?
end

#inspectObject



159
160
161
# File 'lib/prosereflect/resolved_pos.rb', line 159

def inspect
  to_s
end

#marksObject

Marks at this position



50
51
52
53
54
55
56
57
58
# File 'lib/prosereflect/resolved_pos.rb', line 50

def marks
  if depth.zero?
    # At root - no marks
    []
  else
    parent_mark = parent.respond_to?(:marks) ? parent.marks : []
    parent_mark || []
  end
end

#marks_between(from, to, marks) ⇒ Object

Marks between two positions



61
62
63
64
65
66
67
68
69
# File 'lib/prosereflect/resolved_pos.rb', line 61

def marks_between(from, to, marks)
  result = marks.dup
  nodes_between(from, to) do |node|
    if node.respond_to?(:marks) && node.marks
      result = result | node.marks
    end
  end
  result
end

#node(depth = @depth) ⇒ Object

The node at a given depth



40
41
42
# File 'lib/prosereflect/resolved_pos.rb', line 40

def node(depth = @depth)
  @path[depth * 3]
end

#parentObject

The parent node at current depth



20
21
22
# File 'lib/prosereflect/resolved_pos.rb', line 20

def parent
  @path[@depth * 3]
end

#parent_offsetObject

Position within the parent node



45
46
47
# File 'lib/prosereflect/resolved_pos.rb', line 45

def parent_offset
  @parent_offset ||= @pos - start
end

#shared_depth(other_pos) ⇒ Object

Find shared depth with another position



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/prosereflect/resolved_pos.rb', line 72

def shared_depth(other_pos)
  my_depth = depth
  other_depth = other_pos.depth

  while my_depth > other_depth
    my_depth -= 1
  end

  while other_depth > my_depth
    other_depth -= 1
  end

  while my_depth.positive?
    break unless index(my_depth) == other_pos.index(my_depth)

    my_depth -= 1

  end

  my_depth
end

#start(depth = @depth) ⇒ Object

Start position of current parent node



30
31
32
# File 'lib/prosereflect/resolved_pos.rb', line 30

def start(depth = @depth)
  @path[(depth * 3) + 2]
end

#start_of_parent?Boolean

Check if at start of parent

Returns:

  • (Boolean)


116
117
118
# File 'lib/prosereflect/resolved_pos.rb', line 116

def start_of_parent?
  parent_offset.zero?
end

#text_block?Boolean

Check if in text block

Returns:

  • (Boolean)


111
112
113
# File 'lib/prosereflect/resolved_pos.rb', line 111

def text_block?
  parent.respond_to?(:is_textblock?) && parent.is_textblock?
end

#to_sObject



155
156
157
# File 'lib/prosereflect/resolved_pos.rb', line 155

def to_s
  "<ResolvedPos #{@pos}:#{depth}>"
end