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

Extended by:
T::Helpers, T::Sig
Included in:
Listener, BaseRequest, RubyLsp::Requests::SelectionRanges, WorkspaceSymbol
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



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby_lsp/requests/support/common.rb', line 58

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

#defined_in_gem?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/ruby_lsp/requests/support/common.rb', line 73

def defined_in_gem?(file_path)
  DependencyDetector.instance.typechecker && BUNDLE_PATH && !file_path.start_with?(T.must(BUNDLE_PATH)) &&
    !file_path.start_with?(RbConfig::CONFIG["rubylibdir"])
end

#markdown_from_index_entries(title, entries) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ruby_lsp/requests/support/common.rb', line 90

def markdown_from_index_entries(title, entries)
  markdown_title = "```ruby\n#{title}\n```"
  definitions = []
  content = +""
  Array(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_title}\n\n      **Definitions**: \#{definitions.join(\" | \")}\n\n      \#{content}\n    MARKDOWN\n  )\nend\n".chomp,

#range_from_location(location) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/ruby_lsp/requests/support/common.rb', line 30

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



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby_lsp/requests/support/common.rb', line 17

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

#self_receiver?(node) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/ruby_lsp/requests/support/common.rb', line 79

def self_receiver?(node)
  receiver = node.receiver
  receiver.nil? || receiver.is_a?(Prism::SelfNode)
end

#visible?(node, range) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'lib/ruby_lsp/requests/support/common.rb', line 41

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