123456789_123456789_123456789_123456789_123456789_

Class: TypeProf::CodeLocation

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Comparable
Inherits: Object
Defined in: lib/typeprof/code-range.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(lineno, column) ⇒ CodeLocation

In Ruby, lineno is 1-origin, and column is 0-origin

[ GitHub ]

  
# File 'lib/typeprof/code-range.rb', line 4

def initialize(lineno, column)
  @lineno = lineno
  @column = column
end

Class Method Details

.from_lsp(lsp_loc)

[ GitHub ]

  
# File 'lib/typeprof/code-range.rb', line 15

def self.from_lsp(lsp_loc)
  # In the Language Server Protocol, lineno and column are both 0-origin
  CodeLocation.new(lsp_loc[:line] + 1, lsp_loc[:character])
end

Instance Attribute Details

#column (readonly)

[ GitHub ]

  
# File 'lib/typeprof/code-range.rb', line 13

attr_reader :lineno, :column

#lineno (readonly)

[ GitHub ]

  
# File 'lib/typeprof/code-range.rb', line 13

attr_reader :lineno, :column

Instance Method Details

#<=>(other)

[ GitHub ]

  
# File 'lib/typeprof/code-range.rb', line 42

def <=>(other)
  ret = @lineno <=> other.lineno
  return ret if ret != 0
  @column <=> other.column
end

#advance_cursor(offset, source_text)

[ GitHub ]

  
# File 'lib/typeprof/code-range.rb', line 24

def advance_cursor(offset, source_text)
  new_lineno = @lineno
  new_column = @column
  while offset > 0
    line_text = source_text.lines[new_lineno - 1]
    if new_column + offset >= line_text.length
      advanced = line_text.length - new_column
      offset -= advanced
      new_lineno += 1
      new_column = 0
    else
      new_column += offset
      break
    end
  end
  CodeLocation.new(new_lineno, new_column)
end

#inspect

[ GitHub ]

  
# File 'lib/typeprof/code-range.rb', line 9

def inspect
  "(%d,%d)" % [@lineno, @column]
end

#to_lsp

[ GitHub ]

  
# File 'lib/typeprof/code-range.rb', line 20

def to_lsp
  { line: @lineno - 1, character: @column }
end