Class: CodeModels::SourcePoint

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/codemodels/position.rb

Overview

Point in an artifact, indicated by line and column The newlines are considered as column 0 of the next line. Lines should be always positive and all but newlines should have columns positive (>0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line = nil, column = nil) ⇒ SourcePoint

Returns a new instance of SourcePoint.



22
23
24
25
# File 'lib/codemodels/position.rb', line 22

def initialize(line=nil,column=nil)
  self.line= line
  self.column= column
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



12
13
14
# File 'lib/codemodels/position.rb', line 12

def column
  @column
end

#lineObject

Returns the value of attribute line.



12
13
14
# File 'lib/codemodels/position.rb', line 12

def line
  @line
end

Class Method Details

.from_code_index(code, index) ⇒ Object

Parameters:

  • (String code)
  • index (int index)

    of the starting character in the code



16
17
18
19
20
# File 'lib/codemodels/position.rb', line 16

def self.from_code_index(code,index)
  l = line(code,index)
  c = column(code,index)
  SourcePoint.new(l,c)
end

Instance Method Details

#<=>(other) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/codemodels/position.rb', line 61

def <=>(other)
  lc = self.line<=>other.line
  if lc==0
    self.column<=>other.column
  else
    lc
  end
end

#==(other) ⇒ Object



42
43
44
# File 'lib/codemodels/position.rb', line 42

def ==(other)
  self.eql?(other)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/codemodels/position.rb', line 38

def eql?(other)
  other.line==line && other.column==column
end

#to_absolute_index(s) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/codemodels/position.rb', line 50

def to_absolute_index(s)
  index = 0
  lines = s.lines
  (@line-1).times do
    index+=lines.next.length
  end
  index+=@column
  index-=1
  index
end

#to_sObject



46
47
48
# File 'lib/codemodels/position.rb', line 46

def to_s
  "Line #{@line}, Col #{@column}"
end