Class: RubyLsp::Requests::SemanticHighlighting
- Extended by:
- T::Generic, T::Sig
- 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
- 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
-
#_response ⇒ Object
readonly
Returns the value of attribute _response.
Instance Method Summary collapse
-
#initialize(dispatcher, message_queue, range: nil) ⇒ SemanticHighlighting
constructor
A new instance of SemanticHighlighting.
- #on_block_local_variable_node_enter(node) ⇒ Object
- #on_block_node_enter(node) ⇒ Object
- #on_block_node_leave(node) ⇒ Object
- #on_block_parameter_node_enter(node) ⇒ Object
- #on_call_node_enter(node) ⇒ Object
- #on_class_node_enter(node) ⇒ Object
- #on_constant_and_write_node_enter(node) ⇒ Object
- #on_constant_operator_write_node_enter(node) ⇒ Object
- #on_constant_or_write_node_enter(node) ⇒ Object
- #on_constant_read_node_enter(node) ⇒ Object
- #on_constant_target_node_enter(node) ⇒ Object
- #on_constant_write_node_enter(node) ⇒ Object
- #on_def_node_enter(node) ⇒ Object
- #on_def_node_leave(node) ⇒ Object
- #on_keyword_rest_parameter_node_enter(node) ⇒ Object
- #on_local_variable_and_write_node_enter(node) ⇒ Object
- #on_local_variable_operator_write_node_enter(node) ⇒ Object
- #on_local_variable_or_write_node_enter(node) ⇒ Object
- #on_local_variable_read_node_enter(node) ⇒ Object
- #on_local_variable_target_node_enter(node) ⇒ Object
- #on_local_variable_write_node_enter(node) ⇒ Object
- #on_module_node_enter(node) ⇒ Object
- #on_optional_keyword_parameter_node_enter(node) ⇒ Object
- #on_optional_parameter_node_enter(node) ⇒ Object
- #on_required_keyword_parameter_node_enter(node) ⇒ Object
- #on_required_parameter_node_enter(node) ⇒ Object
- #on_rest_parameter_node_enter(node) ⇒ Object
- #on_self_node_enter(node) ⇒ Object
Methods inherited from Listener
Methods included from RubyLsp::Requests::Support::Common
#create_code_lens, #markdown_from_index_entries, #range_from_location, #range_from_node, #visible?
Constructor Details
#initialize(dispatcher, 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 156 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 117 def initialize(dispatcher, , range: nil) super(dispatcher, ) @_response = T.let([], ResponseType) @range = range @special_methods = T.let(nil, T.nilable(T::Array[String])) @current_scope = T.let(ParameterScope.new, ParameterScope) dispatcher.register( self, :on_call_node_enter, :on_class_node_enter, :on_def_node_enter, :on_def_node_leave, :on_block_node_enter, :on_block_node_leave, :on_self_node_enter, :on_module_node_enter, :on_local_variable_write_node_enter, :on_local_variable_read_node_enter, :on_block_parameter_node_enter, :on_required_keyword_parameter_node_enter, :on_optional_keyword_parameter_node_enter, :on_keyword_rest_parameter_node_enter, :on_optional_parameter_node_enter, :on_required_parameter_node_enter, :on_rest_parameter_node_enter, :on_constant_read_node_enter, :on_constant_write_node_enter, :on_constant_and_write_node_enter, :on_constant_operator_write_node_enter, :on_constant_or_write_node_enter, :on_constant_target_node_enter, :on_local_variable_and_write_node_enter, :on_local_variable_operator_write_node_enter, :on_local_variable_or_write_node_enter, :on_local_variable_target_node_enter, :on_block_local_variable_node_enter, ) end |
Instance Attribute Details
#_response ⇒ Object (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
#on_block_local_variable_node_enter(node) ⇒ Object
246 247 248 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 246 def on_block_local_variable_node_enter(node) add_token(node.location, :variable) end |
#on_block_node_enter(node) ⇒ Object
236 237 238 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 236 def on_block_node_enter(node) @current_scope = ParameterScope.new(@current_scope) end |
#on_block_node_leave(node) ⇒ Object
241 242 243 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 241 def on_block_node_leave(node) @current_scope = T.must(@current_scope.parent) end |
#on_block_parameter_node_enter(node) ⇒ Object
251 252 253 254 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 251 def on_block_parameter_node_enter(node) name = node.name @current_scope << name.to_sym if name end |
#on_call_node_enter(node) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 159 def on_call_node_enter(node) return unless visible?(node, @range) = node. return unless # We can't push a semantic token for [] and []= because the argument inside the brackets is a part of # the message_loc return if .start_with?("[") && (.end_with?("]") || .end_with?("]=")) return process_regexp_locals(node) if == "=~" return if special_method?() type = Support::Sorbet.annotation?(node) ? :type : :method add_token(T.must(node.), type) end |
#on_class_node_enter(node) ⇒ Object
368 369 370 371 372 373 374 375 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 368 def on_class_node_enter(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_enter(node) ⇒ Object
195 196 197 198 199 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 195 def on_constant_and_write_node_enter(node) return unless visible?(node, @range) add_token(node.name_loc, :namespace) end |
#on_constant_operator_write_node_enter(node) ⇒ Object
202 203 204 205 206 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 202 def on_constant_operator_write_node_enter(node) return unless visible?(node, @range) add_token(node.name_loc, :namespace) end |
#on_constant_or_write_node_enter(node) ⇒ Object
209 210 211 212 213 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 209 def on_constant_or_write_node_enter(node) return unless visible?(node, @range) add_token(node.name_loc, :namespace) end |
#on_constant_read_node_enter(node) ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 177 def on_constant_read_node_enter(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_enter(node) ⇒ Object
216 217 218 219 220 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 216 def on_constant_target_node_enter(node) return unless visible?(node, @range) add_token(node.location, :namespace) end |
#on_constant_write_node_enter(node) ⇒ Object
188 189 190 191 192 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 188 def on_constant_write_node_enter(node) return unless visible?(node, @range) add_token(node.name_loc, :namespace) end |
#on_def_node_enter(node) ⇒ Object
223 224 225 226 227 228 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 223 def on_def_node_enter(node) @current_scope = ParameterScope.new(@current_scope) return unless visible?(node, @range) add_token(node.name_loc, :method, [:declaration]) end |
#on_def_node_leave(node) ⇒ Object
231 232 233 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 231 def on_def_node_leave(node) @current_scope = T.must(@current_scope.parent) end |
#on_keyword_rest_parameter_node_enter(node) ⇒ Object
275 276 277 278 279 280 281 282 283 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 275 def on_keyword_rest_parameter_node_enter(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_enter(node) ⇒ Object
340 341 342 343 344 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 340 def on_local_variable_and_write_node_enter(node) return unless visible?(node, @range) add_token(node.name_loc, @current_scope.type_for(node.name)) end |
#on_local_variable_operator_write_node_enter(node) ⇒ Object
347 348 349 350 351 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 347 def on_local_variable_operator_write_node_enter(node) return unless visible?(node, @range) add_token(node.name_loc, @current_scope.type_for(node.name)) end |
#on_local_variable_or_write_node_enter(node) ⇒ Object
354 355 356 357 358 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 354 def on_local_variable_or_write_node_enter(node) return unless visible?(node, @range) add_token(node.name_loc, @current_scope.type_for(node.name)) end |
#on_local_variable_read_node_enter(node) ⇒ Object
327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 327 def on_local_variable_read_node_enter(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_enter(node) ⇒ Object
361 362 363 364 365 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 361 def on_local_variable_target_node_enter(node) return unless visible?(node, @range) add_token(node.location, @current_scope.type_for(node.name)) end |
#on_local_variable_write_node_enter(node) ⇒ Object
320 321 322 323 324 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 320 def on_local_variable_write_node_enter(node) return unless visible?(node, @range) add_token(node.name_loc, @current_scope.type_for(node.name)) end |
#on_module_node_enter(node) ⇒ Object
378 379 380 381 382 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 378 def on_module_node_enter(node) return unless visible?(node, @range) add_token(node.constant_path.location, :namespace, [:declaration]) end |
#on_optional_keyword_parameter_node_enter(node) ⇒ Object
266 267 268 269 270 271 272 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 266 def on_optional_keyword_parameter_node_enter(node) @current_scope << node.name return unless visible?(node, @range) location = node.name_loc add_token(location.copy(length: location.length - 1), :parameter) end |
#on_optional_parameter_node_enter(node) ⇒ Object
286 287 288 289 290 291 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 286 def on_optional_parameter_node_enter(node) @current_scope << node.name return unless visible?(node, @range) add_token(node.name_loc, :parameter) end |
#on_required_keyword_parameter_node_enter(node) ⇒ Object
257 258 259 260 261 262 263 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 257 def on_required_keyword_parameter_node_enter(node) @current_scope << node.name return unless visible?(node, @range) location = node.name_loc add_token(location.copy(length: location.length - 1), :parameter) end |
#on_required_parameter_node_enter(node) ⇒ Object
294 295 296 297 298 299 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 294 def on_required_parameter_node_enter(node) @current_scope << node.name return unless visible?(node, @range) add_token(node.location, :parameter) end |
#on_rest_parameter_node_enter(node) ⇒ Object
302 303 304 305 306 307 308 309 310 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 302 def on_rest_parameter_node_enter(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_enter(node) ⇒ Object
313 314 315 316 317 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 313 def on_self_node_enter(node) return unless visible?(node, @range) add_token(node.location, :variable, [:default_library]) end |