Module: RubyLanguageServer::LineContext

Defined in:
lib/ruby_language_server/line_context.rb

Class Method Summary collapse

Class Method Details

.for(line, position) ⇒ Object

This will not work if the search starts on the ending ? of “method?”. Bummer.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_language_server/line_context.rb', line 21

def self.for(line, position)
  # Grab just the last part of the line - from the index onward
  line_end = line[position..]
  return nil if line_end.nil?

  # Grab the portion of the word that starts at the position toward the end of the line
  match = line_end.partition(/^(@{0,2}\w+\??)/)[1]
  RubyLanguageServer.logger.debug("match: #{match}")
  # Get the start of the line to the end of the matched word
  line_start = line[0..(position + match.length - 1)]
  RubyLanguageServer.logger.debug("line_start: #{line_start}")
  # Match as much as we can to the end of the line - which is now the end of the word
  end_match = line_start.partition(/(@{0,2}[:&.\w]+\??)$/)[1]
  matches = end_match.split('&.', -1)
  matches = matches.map { |m| m.length.positive? ? m.split('.', -1) : m }.flatten
  matches = matches.map { |m| m.length.positive? ? m.split('::', -1) : m }.flatten
  RubyLanguageServer.logger.debug("matches: #{matches}")
  matches
end