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](../../misc/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

#full_constant_name, #locate, #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
37
# File 'lib/ruby_lsp/requests/document_highlight.rb', line 29

def initialize(document, position)
  super(document)

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

  position = document.create_scanner.find_char_position(position)
  @target = T.let(find(T.must(document.tree), position), T.nilable(Support::HighlightTarget))
end

Instance Method Details

#runObject



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

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

#visit(node) ⇒ Object



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

def visit(node)
  return if node.nil?

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

  super
end