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

#response

Methods included from Support::Common

#create_code_lens, #markdown_from_index_entries, #range_from_location, #range_from_node, #visible?

Constructor Details

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

Returns a new instance of DocumentHighlight.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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 YARP::GlobalVariableReadNode, YARP::GlobalVariableAndWriteNode, YARP::GlobalVariableOperatorWriteNode,
      YARP::GlobalVariableOrWriteNode, YARP::GlobalVariableTargetNode, YARP::GlobalVariableWriteNode,
      YARP::InstanceVariableAndWriteNode, YARP::InstanceVariableOperatorWriteNode,
      YARP::InstanceVariableOrWriteNode, YARP::InstanceVariableReadNode, YARP::InstanceVariableTargetNode,
      YARP::InstanceVariableWriteNode, YARP::ConstantAndWriteNode, YARP::ConstantOperatorWriteNode,
      YARP::ConstantOrWriteNode, YARP::ConstantPathAndWriteNode, YARP::ConstantPathNode,
      YARP::ConstantPathOperatorWriteNode, YARP::ConstantPathOrWriteNode, YARP::ConstantPathTargetNode,
      YARP::ConstantPathWriteNode, YARP::ConstantReadNode, YARP::ConstantTargetNode, YARP::ConstantWriteNode,
      YARP::ClassVariableAndWriteNode, YARP::ClassVariableOperatorWriteNode, YARP::ClassVariableOrWriteNode,
      YARP::ClassVariableReadNode, YARP::ClassVariableTargetNode, YARP::ClassVariableWriteNode,
      YARP::LocalVariableAndWriteNode, YARP::LocalVariableOperatorWriteNode, YARP::LocalVariableOrWriteNode,
      YARP::LocalVariableReadNode, YARP::LocalVariableTargetNode, YARP::LocalVariableWriteNode, YARP::CallNode,
      YARP::BlockParameterNode, YARP::KeywordParameterNode, YARP::KeywordRestParameterNode,
      YARP::OptionalParameterNode, YARP::RequiredParameterNode, YARP::RestParameterNode
      Support::HighlightTarget.new(target)
    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



73
74
75
76
77
78
# File 'lib/ruby_lsp/requests/document_highlight.rb', line 73

def on_node(node)
  return if node.nil?

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