Class: RubyLsp::Requests::SemanticHighlighting

Inherits:
BaseRequest
  • Object
show all
Defined in:
lib/ruby_lsp/requests/semantic_highlighting.rb

Overview

The [semantic highlighting](microsoft.github.io/language-server-protocol/specification#textDocument_semanticTokens) request informs the editor of the correct token types to provide consistent and accurate highlighting for themes.

# Example

“‘ruby def foo

var = 1 # --> semantic highlighting: local variable
some_invocation # --> semantic highlighting: method invocation
var # --> semantic highlighting: local variable

end “‘

Defined Under Namespace

Classes: SemanticToken

Constant Summary collapse

TOKEN_TYPES =
[
  :variable,
  :method,
].freeze
TOKEN_MODIFIERS =
{
  declaration: 0,
  definition: 1,
  readonly: 2,
  static: 3,
  deprecated: 4,
  abstract: 5,
  async: 6,
  modification: 7,
  documentation: 8,
  default_library: 9,
}.freeze

Instance Method Summary collapse

Methods inherited from BaseRequest

#range_from_syntax_tree_node, run

Constructor Details

#initialize(document, encoder: nil) ⇒ SemanticHighlighting



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

def initialize(document, encoder: nil)
  super(document)

  @encoder = encoder
  @tokens = []
  @tree = document.tree
end

Instance Method Details

#add_token(location, type, modifiers = []) ⇒ Object



105
106
107
108
109
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 105

def add_token(location, type, modifiers = [])
  length = location.end_char - location.start_char
  modifiers_indices = modifiers.filter_map { |modifier| TOKEN_MODIFIERS[modifier] }
  @tokens.push(SemanticToken.new(location, length, TOKEN_TYPES.index(type), modifiers_indices))
end

#runObject



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

def run
  visit(@tree)
  return @tokens unless @encoder

  @encoder.encode(@tokens)
end

#visit_a_ref_field(node) ⇒ Object



75
76
77
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 75

def visit_a_ref_field(node)
  add_token(node.collection.value.location, :variable)
end

#visit_call(node) ⇒ Object



79
80
81
82
83
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 79

def visit_call(node)
  visit(node.receiver)
  add_token(node.message.location, :method)
  visit(node.arguments)
end

#visit_command(node) ⇒ Object



85
86
87
88
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 85

def visit_command(node)
  add_token(node.message.location, :method)
  visit(node.arguments)
end

#visit_command_call(node) ⇒ Object



90
91
92
93
94
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 90

def visit_command_call(node)
  visit(node.receiver)
  add_token(node.message.location, :method)
  visit(node.arguments)
end

#visit_fcall(node) ⇒ Object



96
97
98
99
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 96

def visit_fcall(node)
  add_token(node.value.location, :method)
  visit(node.arguments)
end

#visit_m_assign(node) ⇒ Object



55
56
57
58
59
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 55

def visit_m_assign(node)
  node.target.parts.each do |var_ref|
    add_token(var_ref.value.location, :variable)
  end
end

#visit_var_field(node) ⇒ Object



61
62
63
64
65
66
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 61

def visit_var_field(node)
  case node.value
  when SyntaxTree::Ident
    add_token(node.value.location, :variable)
  end
end

#visit_var_ref(node) ⇒ Object



68
69
70
71
72
73
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 68

def visit_var_ref(node)
  case node.value
  when SyntaxTree::Ident
    add_token(node.value.location, :variable)
  end
end

#visit_vcall(node) ⇒ Object



101
102
103
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 101

def visit_vcall(node)
  add_token(node.value.location, :method)
end