Class: RubyLsp::Requests::SemanticHighlighting

Inherits:
BaseRequest
  • Object
show all
Defined in:
lib/ruby_lsp/requests/semantic_highlighting.rb

Defined Under Namespace

Classes: InvalidTokenRowError

Constant Summary collapse

TOKEN_TYPES =
[
  :variable,
  :method,
].freeze
TOKEN_MODIFIERS =
[].freeze

Instance Method Summary collapse

Methods inherited from BaseRequest

run

Constructor Details

#initialize(document) ⇒ SemanticHighlighting

Returns a new instance of SemanticHighlighting.



12
13
14
15
16
17
18
19
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 12

def initialize(document)
  super

  @tokens = []
  @tree = document.tree
  @current_row = 0
  @current_column = 0
end

Instance Method Details

#add_token(location, classification) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 76

def add_token(location, classification)
  length = location.end_char - location.start_char

  compute_delta(location) do |delta_line, delta_column|
    @tokens.push(delta_line, delta_column, length, TOKEN_TYPES.index(classification), 0)
  end
end

#compute_delta(location) {|delta_line, delta_column| ... } ⇒ Object

Yields:

  • (delta_line, delta_column)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 93

def compute_delta(location)
  row = location.start_line - 1
  column = location.start_column

  if row < @current_row
    raise InvalidTokenRowError, "Invalid token row detected: " \
      "Ensure tokens are added in the expected order."
  end

  delta_line = row - @current_row

  delta_column = column
  delta_column -= @current_column if delta_line == 0

  yield delta_line, delta_column

  @current_row = row
  @current_column = column
end

#runObject



21
22
23
24
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 21

def run
  visit(@tree)
  LanguageServer::Protocol::Interface::SemanticTokens.new(data: @tokens)
end

#visit_a_ref_field(node) ⇒ Object



46
47
48
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 46

def visit_a_ref_field(node)
  add_token(node.collection.value.location, :variable)
end

#visit_call(node) ⇒ Object



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

def visit_call(node)
  visit(node.receiver)
  add_token(node.message.location, :method)
  visit(node.arguments)
end

#visit_command(node) ⇒ Object



56
57
58
59
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 56

def visit_command(node)
  add_token(node.message.location, :method)
  visit(node.arguments)
end

#visit_command_call(node) ⇒ Object



61
62
63
64
65
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 61

def visit_command_call(node)
  visit(node.receiver)
  add_token(node.message.location, :method)
  visit(node.arguments)
end

#visit_fcall(node) ⇒ Object



67
68
69
70
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 67

def visit_fcall(node)
  add_token(node.value.location, :method)
  visit(node.arguments)
end

#visit_m_assign(node) ⇒ Object



26
27
28
29
30
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 26

def visit_m_assign(node)
  node.target.parts.each do |var_ref|
    add_token(var_ref.value.location, :variable)
  end
end

#visit_var_field(node) ⇒ Object



32
33
34
35
36
37
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 32

def visit_var_field(node)
  case node.value
  when SyntaxTree::Ident
    add_token(node.value.location, :variable)
  end
end

#visit_var_ref(node) ⇒ Object



39
40
41
42
43
44
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 39

def visit_var_ref(node)
  case node.value
  when SyntaxTree::Ident
    add_token(node.value.location, :variable)
  end
end

#visit_vcall(node) ⇒ Object



72
73
74
# File 'lib/ruby_lsp/requests/semantic_highlighting.rb', line 72

def visit_vcall(node)
  add_token(node.value.location, :method)
end