Class: RubyLsp::Requests::DocumentHighlight

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

Overview

![Document highlight demo](../../document_highlight.gif)

The [document highlight](microsoft.github.io/language-server-protocol/specification#textDocument_documentHighlight) informs the editor all relevant elements of the currently pointed item for highlighting. For example, when the cursor is on the ‘F` of the constant `FOO`, the editor should identify other occurrences of `FOO` and highlight them.

For writable elements like constants or variables, their read/write occurrences should be highlighted differently. This is achieved by sending different “kind” attributes to the editor (2 for read and 3 for write).

# Example

“‘ruby FOO = 1 # should be highlighted as “write”

def foo

FOO # should be highlighted as "read"

end “‘

Constant Summary collapse

ResponseType =
type_member { { fixed: T::Array[Interface::DocumentHighlight] } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Listener

#merge_external_listeners_responses!, #merge_response!

Methods included from Support::Common

#create_code_lens, #full_constant_name, #range_from_syntax_tree_node, #visible?

Constructor Details

#initialize(target, parent, emitter, message_queue) ⇒ DocumentHighlight



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_lsp/requests/document_highlight.rb', line 41

def initialize(target, parent, emitter, message_queue)
  super(emitter, message_queue)

  @response = T.let([], T::Array[Interface::DocumentHighlight])

  return unless target && parent

  highlight_target =
    case target
    when *DIRECT_HIGHLIGHTS
      Support::HighlightTarget.new(target)
    when SyntaxTree::Ident
      relevant_node = parent.is_a?(SyntaxTree::Params) ? target : parent
      Support::HighlightTarget.new(relevant_node)
    end

  @target = T.let(highlight_target, T.nilable(Support::HighlightTarget))

  emitter.register(self, :on_node) if @target
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



31
32
33
# File 'lib/ruby_lsp/requests/document_highlight.rb', line 31

def response
  @response
end

Instance Method Details

#on_node(node) ⇒ Object



63
64
65
66
67
68
# File 'lib/ruby_lsp/requests/document_highlight.rb', line 63

def on_node(node)
  return if node.nil?

  match = T.must(@target).highlight_type(node)
  add_highlight(match) if match
end