Module: RubyLsp::Requests::Support::Common

Extended by:
T::Sig
Included in:
Listener, BaseRequest
Defined in:
lib/ruby_lsp/requests/support/common.rb

Instance Method Summary collapse

Instance Method Details

#create_code_lens(node, title:, command_name:, arguments:, data:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_lsp/requests/support/common.rb', line 55

def create_code_lens(node, title:, command_name:, arguments:, data:)
  range = range_from_node(node)

  Interface::CodeLens.new(
    range: range,
    command: Interface::Command.new(
      title: title,
      command: command_name,
      arguments: arguments,
    ),
    data: data,
  )
end

#markdown_from_index_entries(title, entries) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby_lsp/requests/support/common.rb', line 70

def markdown_from_index_entries(title, entries)
  markdown_title = "```ruby\n#{title}\n```"
  definitions = []
  content = +""
  entries.each do |entry|
    loc = entry.location

    # We always handle locations as zero based. However, for file links in Markdown we need them to be one
    # based, which is why instead of the usual subtraction of 1 to line numbers, we are actually adding 1 to
    # columns. The format for VS Code file URIs is
    # `file:///path/to/file.rb#Lstart_line,start_column-end_line,end_column`
    uri = URI::Generic.from_path(
      path: entry.file_path,
      fragment: "L#{loc.start_line},#{loc.start_column + 1}-#{loc.end_line},#{loc.end_column + 1}",
    )

    definitions << "[#{entry.file_name}](#{uri})"
    content << "\n\n#{entry.comments.join("\n")}" unless entry.comments.empty?
  end

  Interface::MarkupContent.new(
    kind: "markdown",
    value: <<~MARKDOWN.chomp,
      #{markdown_title}

      **Definitions**: #{definitions.join(" | ")}

      #{content}
    MARKDOWN
  )
end

#range_from_location(location) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/ruby_lsp/requests/support/common.rb', line 27

def range_from_location(location)
  Interface::Range.new(
    start: Interface::Position.new(
      line: location.start_line - 1,
      character: location.start_column,
    ),
    end: Interface::Position.new(line: location.end_line - 1, character: location.end_column),
  )
end

#range_from_node(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_lsp/requests/support/common.rb', line 14

def range_from_node(node)
  loc = node.location

  Interface::Range.new(
    start: Interface::Position.new(
      line: loc.start_line - 1,
      character: loc.start_column,
    ),
    end: Interface::Position.new(line: loc.end_line - 1, character: loc.end_column),
  )
end

#visible?(node, range) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/ruby_lsp/requests/support/common.rb', line 38

def visible?(node, range)
  return true if range.nil?
  return false if node.nil?

  loc = node.location
  range.cover?(loc.start_line - 1) && range.cover?(loc.end_line - 1)
end