Class: RubyLsp::Requests::SelectionRanges

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
RubyLsp::Requests::Support::Common
Defined in:
lib/ruby_lsp/requests/selection_ranges.rb

Overview

Selection ranges demo

The selection ranges request informs the editor of ranges that the user may want to select based on the location(s) of their cursor(s).

Trigger this request with: Ctrl + Shift + -> or Ctrl + Shift + <-

Note that if using VSCode Neovim, you will need to be in Insert mode for this to work correctly.

Example

def foo # --> The next selection range encompasses the entire method definition.
  puts "Hello, world!" # --> Cursor is on this line
end

Instance Method Summary collapse

Methods included from RubyLsp::Requests::Support::Common

#create_code_lens, #defined_in_gem?, #markdown_from_index_entries, #range_from_location, #range_from_node, #self_receiver?, #visible?

Constructor Details

#initialize(document) ⇒ SelectionRanges

Returns a new instance of SelectionRanges.



27
28
29
30
31
# File 'lib/ruby_lsp/requests/selection_ranges.rb', line 27

def initialize(document)
  @document = document
  @ranges = T.let([], T::Array[Support::SelectionRange])
  @stack = T.let([], T::Array[Support::SelectionRange])
end

Instance Method Details

#run ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_lsp/requests/selection_ranges.rb', line 34

def run
  # [node, parent]
  queue = [[@document.tree, nil]]

  until queue.empty?
    node, parent = queue.shift
    next unless node

    range = Support::SelectionRange.new(range: range_from_location(node.location), parent: parent)
    T.unsafe(queue).unshift(*node.child_nodes.map { |child| [child, range] })
    @ranges.unshift(range)
  end

  @ranges
end