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 occurences 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

#range_from_syntax_tree_node

Constructor Details

#initialize(document, position) ⇒ DocumentHighlight

Returns a new instance of DocumentHighlight.



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

def initialize(document, position)
  @highlights = T.let([], T::Array[LanguageServer::Protocol::Interface::DocumentHighlight])
  position = Document::Scanner.new(document.source).find_position(position)
  @target = T.let(find(T.must(document.tree), position), T.nilable(Support::HighlightTarget))

  super(document)
end

Instance Method Details

#runObject



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

def run
  # no @target means the target is not highlightable
  return [] unless @target

  visit(@document.tree)
  @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