Class: RubyLsp::Requests::Support::SemanticTokenEncoder

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/requests/support/semantic_token_encoder.rb

Instance Method Summary collapse

Constructor Details

#initializeSemanticTokenEncoder

Returns a new instance of SemanticTokenEncoder.



11
12
13
14
# File 'lib/ruby_lsp/requests/support/semantic_token_encoder.rb', line 11

def initialize
  @current_row = T.let(0, Integer)
  @current_column = T.let(0, Integer)
end

Instance Method Details

#compute_delta(token) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_lsp/requests/support/semantic_token_encoder.rb', line 43

def compute_delta(token)
  row = token.location.start_line - 1
  column = token.location.start_column
  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

#encode(tokens) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_lsp/requests/support/semantic_token_encoder.rb', line 21

def encode(tokens)
  delta = tokens
    .sort_by do |token|
      [token.location.start_line, token.location.start_column]
    end
    .flat_map do |token|
      compute_delta(token)
    end

  LanguageServer::Protocol::Interface::SemanticTokens.new(data: delta)
end

#encode_modifiers(modifiers) ⇒ Object



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

def encode_modifiers(modifiers)
  modifiers.inject(0) do |encoded_modifiers, modifier|
    encoded_modifiers | (1 << modifier)
  end
end