Module: PuppetLanguageServer::Manifest::DefinitionProvider

Defined in:
lib/puppet-languageserver/manifest/definition_provider.rb

Class Method Summary collapse

Class Method Details

.find_definition(session_state, content, line_num, char_num, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/puppet-languageserver/manifest/definition_provider.rb', line 6

def self.find_definition(session_state, content, line_num, char_num, options = {})
  options = {
    tasks_mode: false
  }.merge(options)
  result = PuppetLanguageServer::PuppetParserHelper.object_under_cursor(content, line_num, char_num,
                                                                        disallowed_classes: [Puppet::Pops::Model::BlockExpression],
                                                                        tasks_mode: options[:tasks_mode])
  return nil if result.nil?

  path = result[:path]
  item = result[:model]

  response = []
  case item.class.to_s
  when 'Puppet::Pops::Model::CallNamedFunctionExpression'
    func_name = item.functor_expr.value
    response << function_name(session_state, func_name)

  when 'Puppet::Pops::Model::LiteralString'
    # LiteralString could be anything.  Context is the key here
    parent = path.last

    # What if it's a resource name.  Then the Literal String must be the same as the Resource Title
    # e.g.
    # class { 'testclass':    <--- testclass would be the LiteralString inside a ResourceBody
    # }
    if !parent.nil? &&
       parent.instance_of?(::Puppet::Pops::Model::ResourceBody) &&
       parent.title.value == item.value
      resource_name = item.value
      response << type_or_class(session_state, resource_name)
    end

  when 'Puppet::Pops::Model::QualifiedName'
    # Qualified names could be anything.  Context is the key here
    parent = path.last

    # What if it's a function name.  Then the Qualified name must be the same as the function name
    if !parent.nil? &&
       parent.instance_of?(::Puppet::Pops::Model::CallNamedFunctionExpression) &&
       parent.functor_expr.value == item.value
      func_name = item.value
      response << function_name(session_state, func_name)
    end
    # What if it's an "include <class>" call
    if !parent.nil? && parent.instance_of?(::Puppet::Pops::Model::CallNamedFunctionExpression) && parent.functor_expr.value == 'include'
      resource_name = item.value
      response << type_or_class(session_state, resource_name)
    end
    # What if it's the name of a resource type or class
    if !parent.nil? && parent.instance_of?(::Puppet::Pops::Model::ResourceExpression)
      resource_name = item.value
      response << type_or_class(session_state, resource_name)
    end

  when 'Puppet::Pops::Model::ResourceExpression'
    resource_name = item.type_name.value
    response << type_or_class(session_state, resource_name)

  else
    raise "Unable to generate Defintion information for object of type #{item.class}"
  end

  response.compact
end