Sha256: b70b48827bbc328f51e892f6cdb91e6c6014257c41a510dda862c79b649fb92d

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

# typed: strict
# frozen_string_literal: true

module RubyLsp
  module Requests
    # ![Selection ranges demo](../../selection_ranges.gif)
    #
    # The [selection ranges](https://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
    # ```
    class SelectionRanges < Request
      extend T::Sig
      include Support::Common

      sig { params(document: T.any(RubyDocument, ERBDocument)).void }
      def initialize(document)
        super()
        @document = document
        @ranges = T.let([], T::Array[Support::SelectionRange])
        @stack = T.let([], T::Array[Support::SelectionRange])
      end

      sig { override.returns(T.all(T::Array[Support::SelectionRange], Object)) }
      def perform
        # [node, parent]
        queue = [[@document.parse_result.value, 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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-lsp-0.17.17 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.17.16 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.17.15 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.17.14 lib/ruby_lsp/requests/selection_ranges.rb