123456789_123456789_123456789_123456789_123456789_

Class: TypeProf::LSP::Text

Relationships & Source Files
Inherits: Object
Defined in: lib/typeprof/lsp/text.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(path, text, version) ⇒ Text

[ GitHub ]

  
# File 'lib/typeprof/lsp/text.rb', line 3

def initialize(path, text, version)
  @path = path
  @lines = Text.split(text)
  @version = version
end

Class Method Details

.split(str)

[ GitHub ]

  
# File 'lib/typeprof/lsp/text.rb', line 11

def self.split(str)
  lines = str.lines
  lines << "" if lines.empty? || lines.last.include?("\n")
  lines
end

Instance Attribute Details

#lines (readonly)

[ GitHub ]

  
# File 'lib/typeprof/lsp/text.rb', line 9

attr_reader :path, :lines, :version

#path (readonly)

[ GitHub ]

  
# File 'lib/typeprof/lsp/text.rb', line 9

attr_reader :path, :lines, :version

#version (readonly)

[ GitHub ]

  
# File 'lib/typeprof/lsp/text.rb', line 9

attr_reader :path, :lines, :version

Instance Method Details

#apply_changes(changes, version)

[ GitHub ]

  
# File 'lib/typeprof/lsp/text.rb', line 21

def apply_changes(changes, version)
  changes.each do |change|
    change => {
      range: {
          start: { line: start_row, character: start_col },
          end:   { line: end_row  , character: end_col   }
      },
      text: new_text,
    }

    new_text = Text.split(new_text)

    prefix = @lines[start_row][0...start_col]
    suffix = @lines[end_row][end_col...]
    if new_text.size == 1
      new_text[0] = prefix + new_text[0] + suffix
    else
      new_text[0] = prefix + new_text[0]
      new_text[-1] = new_text[-1] + suffix
    end
    @lines[start_row .. end_row] = new_text
  end

  validate

  @version = version
end

#modify_for_completion(changes, pos)

[ GitHub ]

  
# File 'lib/typeprof/lsp/text.rb', line 54

def modify_for_completion(changes, pos)
  pos => { line: row, character: col }
  if col >= 2 && @lines[row][col - 1] == "." && (col == 1 || @lines[row][col - 2] != ".")
    @lines[row][col - 1] = " "
    yield string, ".", { line: row, character: col - 2}
    @lines[row][col - 1] = "."
  elsif col >= 3 && @lines[row][col - 2, 2] == "::"
    @lines[row][col - 2, 2] = "  "
    yield string, "::", { line: row, character: col - 3 }
    @lines[row][col - 2, 2] = "::"
  else
    yield string, nil, { line: row, character: col }
  end
end

#string

[ GitHub ]

  
# File 'lib/typeprof/lsp/text.rb', line 17

def string
  @lines.join
end

#validate

[ GitHub ]

  
# File 'lib/typeprof/lsp/text.rb', line 49

def validate
  raise unless @lines[0..-2].all? {|s| s.count("\n") == 1 && s.end_with?("\n") }
  raise unless @lines[-1].count("\n") == 0
end