|
| 1 | +module PuppetLanguageServer |
| 2 | + module DefinitionProvider |
| 3 | + def self.find_definition(content, line_num, char_num) |
| 4 | + result = PuppetLanguageServer::PuppetParserHelper.object_under_cursor(content, line_num, char_num, false, [Puppet::Pops::Model::BlockExpression]) |
| 5 | + |
| 6 | + return nil if result.nil? |
| 7 | + |
| 8 | + path = result[:path] |
| 9 | + item = result[:model] |
| 10 | + |
| 11 | + response = [] |
| 12 | + case item.class.to_s |
| 13 | + when 'Puppet::Pops::Model::CallNamedFunctionExpression' |
| 14 | + func_name = item.functor_expr.value |
| 15 | + response << function_name(resource_name) |
| 16 | + |
| 17 | + when 'Puppet::Pops::Model::LiteralString' |
| 18 | + # LiteralString could be anything. Context is the key here |
| 19 | + parent = path.last |
| 20 | + |
| 21 | + # What if it's a resource name. Then the Literal String must be the same as the Resource Title |
| 22 | + # e.g. |
| 23 | + # class { 'testclass': <--- testclass would be the LiteralString inside a ResourceBody |
| 24 | + # } |
| 25 | + if !parent.nil? && |
| 26 | + parent.class.to_s == 'Puppet::Pops::Model::ResourceBody' && |
| 27 | + parent.title.value == item.value |
| 28 | + resource_name = item.value |
| 29 | + response << type_or_class(resource_name) |
| 30 | + end |
| 31 | + |
| 32 | + when 'Puppet::Pops::Model::QualifiedName' |
| 33 | + # Qualified names could be anything. Context is the key here |
| 34 | + parent = path.last |
| 35 | + |
| 36 | + # What if it's a function name. Then the Qualified name must be the same as the function name |
| 37 | + if !parent.nil? && |
| 38 | + parent.class.to_s == 'Puppet::Pops::Model::CallNamedFunctionExpression' && |
| 39 | + parent.functor_expr.value == item.value |
| 40 | + func_name = item.value |
| 41 | + response << function_name(func_name) |
| 42 | + end |
| 43 | + # What if it's an "include <class>" call |
| 44 | + if !parent.nil? && parent.class.to_s == 'Puppet::Pops::Model::CallNamedFunctionExpression' && parent.functor_expr.value == 'include' |
| 45 | + resource_name = item.value |
| 46 | + response << type_or_class(resource_name) |
| 47 | + end |
| 48 | + # What if it's the name of a resource type or class |
| 49 | + if !parent.nil? && parent.class.to_s == 'Puppet::Pops::Model::ResourceExpression' |
| 50 | + resource_name = item.value |
| 51 | + response << type_or_class(resource_name) |
| 52 | + end |
| 53 | + |
| 54 | + when 'Puppet::Pops::Model::ResourceExpression' |
| 55 | + resource_name = item.type_name.value |
| 56 | + response << type_or_class(resource_name) |
| 57 | + |
| 58 | + else |
| 59 | + raise "Unable to generate Defintion information for object of type #{item.class}" |
| 60 | + end |
| 61 | + |
| 62 | + response.compact |
| 63 | + end |
| 64 | + |
| 65 | + private |
| 66 | + def self.type_or_class(resource_name) |
| 67 | + # Strip the leading double-colons for root resource names |
| 68 | + resource_name = resource_name.slice(2, resource_name.length - 2) if resource_name.start_with?('::') |
| 69 | + location = PuppetLanguageServer::PuppetHelper.type_load_info(resource_name) |
| 70 | + location = PuppetLanguageServer::PuppetHelper.class_load_info(resource_name) if location.nil? |
| 71 | + unless location.nil? |
| 72 | + return LanguageServer::Location.create({ |
| 73 | + 'uri' => 'file:///' + location['source'], |
| 74 | + 'fromline' => location['line'], |
| 75 | + 'fromchar' => 0, |
| 76 | + 'toline' => location['line'], |
| 77 | + 'tochar' => 1024, |
| 78 | + }) |
| 79 | + end |
| 80 | + nil |
| 81 | + end |
| 82 | + |
| 83 | + def self.function_name(func_name) |
| 84 | + location = PuppetLanguageServer::PuppetHelper.function_load_info(func_name) |
| 85 | + unless location.nil? |
| 86 | + return LanguageServer::Location.create({ |
| 87 | + 'uri' => 'file:///' + location['source'], |
| 88 | + 'fromline' => location['line'], |
| 89 | + 'fromchar' => 0, |
| 90 | + 'toline' => location['line'], |
| 91 | + 'tochar' => 1024, |
| 92 | + }) |
| 93 | + end |
| 94 | + nil |
| 95 | + end |
| 96 | + |
| 97 | + end |
| 98 | +end |
0 commit comments