Module: RubyLsp::RailsRoutes::Util

Defined in:
lib/ruby_lsp/ruby_lsp_rails_routes/util.rb

Overview

Helpers shared by the LSP-side listeners. Kept dependency-light so they can be unit tested without booting Ruby LSP.

Class Method Summary collapse

Class Method Details

.location_from_s(location_string) ⇒ Object

Converts a “path:line” string (1-based line, as returned by Method#source_location) into an Interface::Location (0-based line). The last segment is treated as the line only when it is numeric, so Windows drive letters (“C:/foo.rb:42”) survive the split and a string without a line suffix falls back to line 0.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_lsp/ruby_lsp_rails_routes/util.rb', line 15

def location_from_s(location_string)
  file_path, _, line = location_string.rpartition(":")
  if file_path.empty? || line !~ /\A\d+\z/
    file_path = location_string
    line = nil
  end
  line_as_number = line ? line.to_i - 1 : 0

  ::RubyLsp::Interface::Location.new(
    uri: URI::Generic.from_path(path: file_path).to_s,
    range: ::RubyLsp::Interface::Range.new(
      start: ::RubyLsp::Interface::Position.new(line: line_as_number, character: 0),
      end: ::RubyLsp::Interface::Position.new(line: line_as_number, character: 0)
    )
  )
end