Class: RubyLsp::ResponseBuilders::SemanticHighlighting::SemanticTokenEncoder
- Inherits:
-
Object
- Object
- RubyLsp::ResponseBuilders::SemanticHighlighting::SemanticTokenEncoder
- Extended by:
- T::Sig
- Defined in:
- lib/ruby_lsp/response_builders/semantic_highlighting.rb
Instance Method Summary collapse
- #compute_delta(token) ⇒ Object
- #encode(tokens) ⇒ Object
- #encode_modifiers(modifiers) ⇒ Object
-
#initialize ⇒ SemanticTokenEncoder
constructor
A new instance of SemanticTokenEncoder.
Constructor Details
#initialize ⇒ SemanticTokenEncoder
Returns a new instance of SemanticTokenEncoder.
157 158 159 160 |
# File 'lib/ruby_lsp/response_builders/semantic_highlighting.rb', line 157 def initialize @current_row = T.let(0, Integer) @current_column = T.let(0, Integer) end |
Instance Method Details
#compute_delta(token) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/ruby_lsp/response_builders/semantic_highlighting.rb', line 192 def compute_delta(token) row = token.start_line - 1 column = token.start_code_unit_column begin delta_line = row - @current_row delta_column = column delta_column -= @current_column if delta_line == 0 [delta_line, delta_column, token.length, token.type, encode_modifiers(token.modifier)] ensure @current_row = row @current_column = column end end |
#encode(tokens) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/ruby_lsp/response_builders/semantic_highlighting.rb', line 167 def encode(tokens) sorted_tokens = tokens.sort_by.with_index do |token, index| # Enumerable#sort_by is not deterministic when the compared values are equal. # When that happens, we need to use the index as a tie breaker to ensure # that the order of the tokens is always the same. [token.start_line, token.start_code_unit_column, index] end delta = sorted_tokens.flat_map do |token| compute_delta(token) end Interface::SemanticTokens.new(data: delta) end |
#encode_modifiers(modifiers) ⇒ Object
214 215 216 217 218 |
# File 'lib/ruby_lsp/response_builders/semantic_highlighting.rb', line 214 def encode_modifiers(modifiers) modifiers.inject(0) do |encoded_modifiers, modifier| encoded_modifiers | (1 << modifier) end end |