Class: RubyLsp::RSpec::DocumentSymbol

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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

Returns a new instance of DocumentSymbol.



17
18
19
20
21
# File 'lib/ruby_lsp/ruby_lsp_rspec/document_symbol.rb', line 17

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby_lsp/ruby_lsp_rspec/document_symbol.rb', line 68

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



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
49
50
51
52
53
54
55
# File 'lib/ruby_lsp/ruby_lsp_rspec/document_symbol.rb', line 24

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

    return unless name

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

    name = generate_name(node)

    return unless name

    symbol = RubyLsp::Interface::DocumentSymbol.new(
      name: name,
      kind: LanguageServer::Protocol::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



58
59
60
61
62
63
64
65
# File 'lib/ruby_lsp/ruby_lsp_rspec/document_symbol.rb', line 58

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

    @response_builder.pop
  end
end