Sha256: c7630c11ef75b219b4db4082bfcb18f3c2fa29e2e7246b780cc7b598c8ee5114

Contents?: true

Size: 1.63 KB

Versions: 22

Compression:

Stored size: 1.63 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: Document).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.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
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
ruby-lsp-0.17.4 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.17.3 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.17.2 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.17.1 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.17.0 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.16.6 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.16.5 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.16.4 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.16.3 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.16.2 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.16.1 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.16.0 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.15.0 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.14.6 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.14.5 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.14.4 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.14.3 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.14.2 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.14.1 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.14.0 lib/ruby_lsp/requests/selection_ranges.rb