Class: RubyLsp::ResponseBuilders::SemanticHighlighting

Inherits:
ResponseBuilder show all
Defined in:
lib/ruby_lsp/response_builders/semantic_highlighting.rb

Overview

: [ResponseType = Interface::SemanticTokens]

Defined Under Namespace

Classes: SemanticToken, SemanticTokenEncoder, UndefinedTokenType

Constant Summary collapse

TOKEN_TYPES =
{
  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
TOKEN_MODIFIERS =

: Hash[Symbol, Integer]

{
  declaration: 0,
  definition: 1,
  readonly: 2,
  static: 3,
  deprecated: 4,
  abstract: 5,
  async: 6,
  modification: 7,
  documentation: 8,
  default_library: 9,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(code_units_cache) ⇒ SemanticHighlighting

: ((^(Integer arg0) -> Integer | Prism::CodeUnitsCache) code_units_cache) -> void



50
51
52
53
54
# File 'lib/ruby_lsp/response_builders/semantic_highlighting.rb', line 50

def initialize(code_units_cache)
  super()
  @code_units_cache = code_units_cache
  @stack = [] #: Array[SemanticToken]
end

Instance Method Details

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

: (Prism::Location location, Symbol type, ?Array modifiers) -> void



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby_lsp/response_builders/semantic_highlighting.rb', line 57

def add_token(location, type, modifiers = [])
  end_code_unit = location.cached_end_code_units_offset(@code_units_cache)
  length = end_code_unit - location.cached_start_code_units_offset(@code_units_cache)
  modifiers_indices = modifiers.filter_map { |modifier| TOKEN_MODIFIERS[modifier] }
  @stack.push(
    SemanticToken.new(
      start_line: location.start_line,
      start_code_unit_column: location.cached_start_code_units_column(@code_units_cache),
      length: length,
      type: TOKEN_TYPES[type], #: as !nil
      modifier: modifiers_indices,
    ),
  )
end

#lastObject

: -> SemanticToken?



82
83
84
# File 'lib/ruby_lsp/response_builders/semantic_highlighting.rb', line 82

def last
  @stack.last
end

#last_token_matches?(location) ⇒ Boolean

: (Prism::Location location) -> bool

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/ruby_lsp/response_builders/semantic_highlighting.rb', line 73

def last_token_matches?(location)
  token = @stack.last
  return false unless token

  token.start_line == location.start_line &&
    token.start_code_unit_column == location.cached_start_code_units_column(@code_units_cache)
end

#responseObject

: -> Array



88
89
90
# File 'lib/ruby_lsp/response_builders/semantic_highlighting.rb', line 88

def response
  @stack
end