Class: RubyLsp::Requests::SelectionRanges

Inherits:
BaseRequest
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/requests/selection_ranges.rb

Overview

![Selection ranges demo](../../misc/selection_ranges.gif)

The [selection ranges](microsoft.github.io/language-server-protocol/specification#textDocument_selectionRange) 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

“‘ruby def foo # –> The next selection range encompasses the entire method definition.

puts "Hello, world!" # --> Cursor is on this line

end “‘

Constant Summary collapse

NODES_THAT_CAN_BE_PARENTS =
T.let(
  [
    SyntaxTree::Assign,
    SyntaxTree::ArrayLiteral,
    SyntaxTree::Begin,
    SyntaxTree::BlockNode,
    SyntaxTree::CallNode,
    SyntaxTree::Case,
    SyntaxTree::ClassDeclaration,
    SyntaxTree::Command,
    SyntaxTree::DefNode,
    SyntaxTree::Elsif,
    SyntaxTree::Else,
    SyntaxTree::EmbDoc,
    SyntaxTree::Ensure,
    SyntaxTree::For,
    SyntaxTree::HashLiteral,
    SyntaxTree::Heredoc,
    SyntaxTree::HeredocBeg,
    SyntaxTree::HshPtn,
    SyntaxTree::IfNode,
    SyntaxTree::In,
    SyntaxTree::Lambda,
    SyntaxTree::MethodAddBlock,
    SyntaxTree::ModuleDeclaration,
    SyntaxTree::Params,
    SyntaxTree::Rescue,
    SyntaxTree::RescueEx,
    SyntaxTree::StringConcat,
    SyntaxTree::StringLiteral,
    SyntaxTree::UnlessNode,
    SyntaxTree::UntilNode,
    SyntaxTree::VCall,
    SyntaxTree::When,
    SyntaxTree::WhileNode,
  ].freeze,
  T::Array[T.class_of(SyntaxTree::Node)],
)

Instance Method Summary collapse

Methods inherited from BaseRequest

#full_constant_name, #locate, #range_from_syntax_tree_node, #visible?

Constructor Details

#initialize(document) ⇒ SelectionRanges

Returns a new instance of SelectionRanges.



66
67
68
69
70
71
# File 'lib/ruby_lsp/requests/selection_ranges.rb', line 66

def initialize(document)
  super(document)

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

Instance Method Details

#runObject



74
75
76
77
# File 'lib/ruby_lsp/requests/selection_ranges.rb', line 74

def run
  visit(@document.tree) if @document.parsed?
  @ranges.reverse!
end