Class: RubyLsp::Requests::SemanticHighlighting

Inherits:
Listener
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/requests/semantic_highlighting.rb

Overview

![Semantic highlighting demo](../../semantic_highlighting.gif)

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

ResponseType =
type_member { { fixed: T::Array[SemanticToken] } }
TOKEN_TYPES =
T.let(
  {
    namespace: 0,
    type: 1,
    class: 2,
    enum: 3,
    interface: 4,
    struct: 5,
    typeParameter: 6,
    parameter: 7,
    variable: 8,
    property: 9,
    enumMember: 10,
    event: 11,
    function: 12,
    method: 13,
    macro: 14,
    keyword: 15,
    modifier: 16,
    comment: 17,
    string: 18,
    number: 19,
    regexp: 20,
    operator: 21,
    decorator: 22,
  }.freeze,
  T::Hash[Symbol, Integer],
)
TOKEN_MODIFIERS =
T.let(
  {
    declaration: 0,
    definition: 1,
    readonly: 2,
    static: 3,
    deprecated: 4,
    abstract: 5,
    async: 6,
    modification: 7,
    documentation: 8,
    default_library: 9,
  }.freeze,
  T::Hash[Symbol, Integer],
)
SPECIAL_RUBY_METHODS =
T.let(
  [
    Module.instance_methods(false),
    Kernel.instance_methods(false),
    Kernel.methods(false),
    Bundler::Dsl.instance_methods(false),
    Module.private_instance_methods(false),
  ].flatten.map(&:to_s),
  T::Array[String],
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Listener

#response

Methods included from RubyLsp::Requests::Support::Common

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

Constructor Details

#initialize(emitter, message_queue, range: nil) ⇒ SemanticHighlighting

Returns a new instance of SemanticHighlighting.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 117

def initialize(emitter, message_queue, range: nil)
  super(emitter, message_queue)

  @_response = T.let([], ResponseType)
  @range = range
  @special_methods = T.let(nil, T.nilable(T::Array[String]))
  @current_scope = T.let(ParameterScope.new, ParameterScope)

  emitter.register(
    self,
    :on_call,
    :on_class,
    :on_def,
    :after_def,
    :on_block,
    :after_block,
    :on_self,
    :on_module,
    :on_local_variable_write,
    :on_local_variable_read,
    :on_block_parameter,
    :on_keyword_parameter,
    :on_keyword_rest_parameter,
    :on_optional_parameter,
    :on_required_parameter,
    :on_rest_parameter,
    :on_constant_read,
    :on_constant_write,
    :on_constant_and_write,
    :on_constant_operator_write,
    :on_constant_or_write,
    :on_constant_target,
    :on_local_variable_and_write,
    :on_local_variable_operator_write,
    :on_local_variable_or_write,
    :on_local_variable_target,
    :on_block_local_variable,
  )
end

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



108
109
110
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 108

def _response
  @_response
end

Instance Method Details

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



377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 377

def add_token(location, type, modifiers = [])
  length = location.end_offset - location.start_offset
  modifiers_indices = modifiers.filter_map { |modifier| TOKEN_MODIFIERS[modifier] }
  @_response.push(
    SemanticToken.new(
      location: location,
      length: length,
      type: T.must(TOKEN_TYPES[type]),
      modifier: modifiers_indices,
    ),
  )
end

#after_block(node) ⇒ Object



240
241
242
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 240

def after_block(node)
  @current_scope = T.must(@current_scope.parent)
end

#after_def(node) ⇒ Object



230
231
232
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 230

def after_def(node)
  @current_scope = T.must(@current_scope.parent)
end

#on_block(node) ⇒ Object



235
236
237
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 235

def on_block(node)
  @current_scope = ParameterScope.new(@current_scope)
end

#on_block_local_variable(node) ⇒ Object



245
246
247
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 245

def on_block_local_variable(node)
  add_token(node.location, :variable)
end

#on_block_parameter(node) ⇒ Object



250
251
252
253
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 250

def on_block_parameter(node)
  name = node.name
  @current_scope << name.to_sym if name
end

#on_call(node) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 158

def on_call(node)
  return unless visible?(node, @range)

  message = node.message
  return unless message

  # We can't push a semantic token for [] and []= because the argument inside the brackets is a part of
  # the message_loc
  return if message.start_with?("[") && (message.end_with?("]") || message.end_with?("]="))

  return process_regexp_locals(node) if message == "=~"
  return if special_method?(message)

  type = Support::Sorbet.annotation?(node) ? :type : :method
  add_token(T.must(node.message_loc), type)
end

#on_class(node) ⇒ Object



360
361
362
363
364
365
366
367
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 360

def on_class(node)
  return unless visible?(node, @range)

  add_token(node.constant_path.location, :class, [:declaration])

  superclass = node.superclass
  add_token(superclass.location, :class) if superclass
end

#on_constant_and_write(node) ⇒ Object



194
195
196
197
198
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 194

def on_constant_and_write(node)
  return unless visible?(node, @range)

  add_token(node.name_loc, :namespace)
end

#on_constant_operator_write(node) ⇒ Object



201
202
203
204
205
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 201

def on_constant_operator_write(node)
  return unless visible?(node, @range)

  add_token(node.name_loc, :namespace)
end

#on_constant_or_write(node) ⇒ Object



208
209
210
211
212
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 208

def on_constant_or_write(node)
  return unless visible?(node, @range)

  add_token(node.name_loc, :namespace)
end

#on_constant_read(node) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 176

def on_constant_read(node)
  return unless visible?(node, @range)
  # When finding a module or class definition, we will have already pushed a token related to this constant. We
  # need to look at the previous two tokens and if they match this locatione exactly, avoid pushing another token
  # on top of the previous one
  return if @_response.last(2).any? { |token| token.location == node.location }

  add_token(node.location, :namespace)
end

#on_constant_target(node) ⇒ Object



215
216
217
218
219
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 215

def on_constant_target(node)
  return unless visible?(node, @range)

  add_token(node.location, :namespace)
end

#on_constant_write(node) ⇒ Object



187
188
189
190
191
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 187

def on_constant_write(node)
  return unless visible?(node, @range)

  add_token(node.name_loc, :namespace)
end

#on_def(node) ⇒ Object



222
223
224
225
226
227
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 222

def on_def(node)
  @current_scope = ParameterScope.new(@current_scope)
  return unless visible?(node, @range)

  add_token(node.name_loc, :method, [:declaration])
end

#on_keyword_parameter(node) ⇒ Object



256
257
258
259
260
261
262
263
264
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 256

def on_keyword_parameter(node)
  name = node.name
  @current_scope << name.to_s.delete_suffix(":").to_sym if name

  return unless visible?(node, @range)

  location = node.name_loc
  add_token(location.copy(length: location.length - 1), :parameter)
end

#on_keyword_rest_parameter(node) ⇒ Object



267
268
269
270
271
272
273
274
275
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 267

def on_keyword_rest_parameter(node)
  name = node.name

  if name
    @current_scope << name.to_sym

    add_token(T.must(node.name_loc), :parameter) if visible?(node, @range)
  end
end

#on_local_variable_and_write(node) ⇒ Object



332
333
334
335
336
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 332

def on_local_variable_and_write(node)
  return unless visible?(node, @range)

  add_token(node.name_loc, @current_scope.type_for(node.name))
end

#on_local_variable_operator_write(node) ⇒ Object



339
340
341
342
343
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 339

def on_local_variable_operator_write(node)
  return unless visible?(node, @range)

  add_token(node.name_loc, @current_scope.type_for(node.name))
end

#on_local_variable_or_write(node) ⇒ Object



346
347
348
349
350
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 346

def on_local_variable_or_write(node)
  return unless visible?(node, @range)

  add_token(node.name_loc, @current_scope.type_for(node.name))
end

#on_local_variable_read(node) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 319

def on_local_variable_read(node)
  return unless visible?(node, @range)

  # Numbered parameters
  if /_\d+/.match?(node.name)
    add_token(node.location, :parameter)
    return
  end

  add_token(node.location, @current_scope.type_for(node.name))
end

#on_local_variable_target(node) ⇒ Object



353
354
355
356
357
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 353

def on_local_variable_target(node)
  return unless visible?(node, @range)

  add_token(node.location, @current_scope.type_for(node.name))
end

#on_local_variable_write(node) ⇒ Object



312
313
314
315
316
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 312

def on_local_variable_write(node)
  return unless visible?(node, @range)

  add_token(node.name_loc, @current_scope.type_for(node.name))
end

#on_module(node) ⇒ Object



370
371
372
373
374
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 370

def on_module(node)
  return unless visible?(node, @range)

  add_token(node.constant_path.location, :namespace, [:declaration])
end

#on_optional_parameter(node) ⇒ Object



278
279
280
281
282
283
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 278

def on_optional_parameter(node)
  @current_scope << node.name
  return unless visible?(node, @range)

  add_token(node.name_loc, :parameter)
end

#on_required_parameter(node) ⇒ Object



286
287
288
289
290
291
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 286

def on_required_parameter(node)
  @current_scope << node.name
  return unless visible?(node, @range)

  add_token(node.location, :parameter)
end

#on_rest_parameter(node) ⇒ Object



294
295
296
297
298
299
300
301
302
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 294

def on_rest_parameter(node)
  name = node.name

  if name
    @current_scope << name.to_sym

    add_token(T.must(node.name_loc), :parameter) if visible?(node, @range)
  end
end

#on_self(node) ⇒ Object



305
306
307
308
309
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 305

def on_self(node)
  return unless visible?(node, @range)

  add_token(node.location, :variable, [:default_library])
end