Class: RubyLsp::Requests::DocumentHighlight

Inherits:
BaseRequest
  • 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 “‘

Instance Method Summary collapse

Methods inherited from BaseRequest

#visit_all

Methods included from Support::Common

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

Constructor Details

#initialize(document, position) ⇒ DocumentHighlight

Returns a new instance of DocumentHighlight.



29
30
31
32
33
34
35
36
# File 'lib/ruby_lsp/requests/document_highlight.rb', line 29

def initialize(document, position)
  super(document)

  @highlights = T.let([], T::Array[Interface::DocumentHighlight])
  return unless document.parsed?

  @target = T.let(find(position), T.nilable(Support::HighlightTarget))
end

Instance Method Details

#runObject



39
40
41
42
43
# File 'lib/ruby_lsp/requests/document_highlight.rb', line 39

def run
  # no @target means the target is not highlightable
  visit(@document.tree) if @document.parsed? && @target
  @highlights
end

#visit(node) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/ruby_lsp/requests/document_highlight.rb', line 46

def visit(node)
  return if node.nil?

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

  super
end