Sha256: 26cad180c4f7abd9705a59ec2f5bc68ae1cc29543d147ea0ffc6d8237174af64

Contents?: true

Size: 1.59 KB

Versions: 15

Compression:

Stored size: 1.59 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
      extend T::Sig
      include Support::Common
      sig { params(document: Document).void }
      def initialize(document)
        @document = document
        @ranges = T.let([], T::Array[Support::SelectionRange])
        @stack = T.let([], T::Array[Support::SelectionRange])
      end

      sig { returns(T.all(T::Array[Support::SelectionRange], Object)) }
      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
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
ruby-lsp-0.13.2 lib/ruby_lsp/requests/selection_ranges.rb
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/ruby-lsp-0.12.1/lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.13.1 lib/ruby_lsp/requests/selection_ranges.rb
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/ruby-lsp-0.12.1/lib/ruby_lsp/requests/selection_ranges.rb
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/ruby-lsp-0.12.1/lib/ruby_lsp/requests/selection_ranges.rb
study_line-0.1.3 vendor/bundle/ruby/3.2.0/gems/ruby-lsp-0.12.1/lib/ruby_lsp/requests/selection_ranges.rb
study_line-0.1.2 vendor/bundle/ruby/3.2.0/gems/ruby-lsp-0.12.1/lib/ruby_lsp/requests/selection_ranges.rb
study_line-0.1.1 vendor/bundle/ruby/3.2.0/gems/ruby-lsp-0.12.1/lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.13.0 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.12.5 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.12.4 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.12.3 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.12.2 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.12.1 lib/ruby_lsp/requests/selection_ranges.rb
ruby-lsp-0.12.0 lib/ruby_lsp/requests/selection_ranges.rb