Class: RubyLsp::RSpec::DocumentSymbol

Inherits:
Object
  • Object
show all
Includes:
RubyLsp::Requests::Support::Common
Defined in:
lib/ruby_lsp/ruby_lsp_rspec/document_symbol.rb

Instance Method Summary collapse

Constructor Details

#initialize(response_builder, dispatcher) ⇒ DocumentSymbol

: (ResponseBuilders::DocumentSymbol, Prism::Dispatcher) -> void



10
11
12
13
14
# File 'lib/ruby_lsp/ruby_lsp_rspec/document_symbol.rb', line 10

def initialize(response_builder, dispatcher)
  @response_builder = response_builder

  dispatcher.register(self, :on_call_node_enter, :on_call_node_leave)
end

Instance Method Details

#generate_name(node) ⇒ Object

: (Prism::CallNode) -> String?



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_lsp/ruby_lsp_rspec/document_symbol.rb', line 61

def generate_name(node)
  arguments = node.arguments&.arguments

  return unless arguments

  argument = arguments.first

  case argument
  when Prism::StringNode
    argument.unescaped
  when Prism::CallNode
    "<#{argument.name}>"
  when nil
    nil
  else
    argument.slice
  end
end

#on_call_node_enter(node) ⇒ Object

: (Prism::CallNode) -> void



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_lsp/ruby_lsp_rspec/document_symbol.rb', line 17

def on_call_node_enter(node)
  case node.message
  when "example", "it", "specify", "scenario"
    name = generate_name(node)

    return unless name

    @response_builder.last.children << RubyLsp::Interface::DocumentSymbol.new(
      name: name,
      kind: RubyLsp::Constant::SymbolKind::METHOD,
      selection_range: range_from_node(node),
      range: range_from_node(node),
    )
  when "context", "describe", "shared_examples", "shared_context", "shared_examples_for", "feature"
    return if node.receiver && node.receiver&.slice != "RSpec"

    name = generate_name(node)

    return unless name

    symbol = RubyLsp::Interface::DocumentSymbol.new(
      name: name,
      kind: RubyLsp::Constant::SymbolKind::MODULE,
      selection_range: range_from_node(node),
      range: range_from_node(node),
      children: [],
    )

    @response_builder.last.children << symbol
    @response_builder.push(symbol)
  end
end

#on_call_node_leave(node) ⇒ Object

: (Prism::CallNode) -> void



51
52
53
54
55
56
57
58
# File 'lib/ruby_lsp/ruby_lsp_rspec/document_symbol.rb', line 51

def on_call_node_leave(node)
  case node.message
  when "context", "describe", "shared_examples", "shared_context", "shared_examples_for", "feature"
    return if node.receiver && node.receiver&.slice != "RSpec"

    @response_builder.pop
  end
end