Class: Rigor::LanguageServer::SelectionRangeProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/rigor/language_server/selection_range_provider.rb

Overview

Answers ‘textDocument/selectionRange` requests. For each position, returns a linked list of SelectionRange entries —innermost first, each pointing at its `parent` (the next- wider expression). Editors use this for “expand selection”: one keystroke moves up the chain, another moves further out, all the way to the root.

Instance Method Summary collapse

Constructor Details

#initialize(buffer_table:, project_context:) ⇒ SelectionRangeProvider

Returns a new instance of SelectionRangeProvider.



16
17
18
19
# File 'lib/rigor/language_server/selection_range_provider.rb', line 16

def initialize(buffer_table:, project_context:)
  @buffer_table = buffer_table
  @project_context = project_context
end

Instance Method Details

#provide(uri, positions) ⇒ Array<Hash>?

Returns one ‘SelectionRange` per position, or nil when the URI / buffer isn’t resolvable.

Parameters:

  • positions (Array<Hash>)

    LSP ‘Position[]` — each `{ line:, character: }` 0-based.

Returns:

  • (Array<Hash>, nil)

    one ‘SelectionRange` per position, or nil when the URI / buffer isn’t resolvable.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rigor/language_server/selection_range_provider.rb', line 25

def provide(uri, positions)
  path = Uri.to_path(uri)
  return nil if path.nil?

  entry = @buffer_table[uri]
  return nil if entry.nil?

  parse_result = Prism.parse(entry.bytes, filepath: path,
                                          version: @project_context.configuration.target_ruby)
  root = parse_result.value

  positions.map do |pos|
    offset = byte_offset_for(entry.bytes, pos.fetch(:line), pos.fetch(:character))
    next nil if offset.nil?

    build_chain(root, offset)
  end
end