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_parameter_node_enter(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_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 |
# 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_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
245 246 247 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 245 def on_block_local_variable_node_enter(node) add_token(node.location, :variable) end |
#on_block_node_enter(node) ⇒ Object
235 236 237 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 235 def on_block_node_enter(node) @current_scope = ParameterScope.new(@current_scope) end |
#on_block_node_leave(node) ⇒ Object
240 241 242 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 240 def on_block_node_leave(node) @current_scope = T.must(@current_scope.parent) end |
#on_block_parameter_node_enter(node) ⇒ Object
250 251 252 253 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 250 def on_block_parameter_node_enter(node) name = node.name @current_scope << name.to_sym if name end |
#on_call_node_enter(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_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
360 361 362 363 364 365 366 367 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 360 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
194 195 196 197 198 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 194 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
201 202 203 204 205 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 201 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
208 209 210 211 212 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 208 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
176 177 178 179 180 181 182 183 184 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 176 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
215 216 217 218 219 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 215 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
187 188 189 190 191 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 187 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
222 223 224 225 226 227 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 222 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
230 231 232 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 230 def on_def_node_leave(node) @current_scope = T.must(@current_scope.parent) end |
#on_keyword_parameter_node_enter(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_enter(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_enter(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_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
332 333 334 335 336 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 332 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
339 340 341 342 343 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 339 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
346 347 348 349 350 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 346 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
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_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
353 354 355 356 357 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 353 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
312 313 314 315 316 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 312 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
370 371 372 373 374 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 370 def on_module_node_enter(node) return unless visible?(node, @range) add_token(node.constant_path.location, :namespace, [:declaration]) end |
#on_optional_parameter_node_enter(node) ⇒ Object
278 279 280 281 282 283 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 278 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_parameter_node_enter(node) ⇒ Object
286 287 288 289 290 291 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 286 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
294 295 296 297 298 299 300 301 302 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 294 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
305 306 307 308 309 |
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 305 def on_self_node_enter(node) return unless visible?(node, @range) add_token(node.location, :variable, [:default_library]) end |