Skip to content

Commit 2aea8d8

Browse files
committed
(GH-166) Detect definition for Resources bodies
Previously definition detection would fail on resource bodies as the text comes in as a LiteralString e.g. class { 'testclass': <--- testclass would be the LiteralString inside } a ResourceBody This commit updates the definition provider to detect these instances and return a defintion location. This commit also adds tests for this scenario.
1 parent 1359d66 commit 2aea8d8

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

server/lib/puppet-languageserver/definition_provider.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ def self.find_definition(content, line_num, char_num)
1414
func_name = item.functor_expr.value
1515
response << function_name(resource_name)
1616

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+
1732
when 'Puppet::Pops::Model::QualifiedName'
1833
# Qualified names could be anything. Context is the key here
1934
parent = path.last
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class testclasses () {
2+
$param1 = 'value2'
3+
}

server/spec/languageserver/integration/puppet-languageserver/definition_provider_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,51 @@ class Test::NoParams {
5858
it_should_behave_like "a single definition result", /user\.rb/
5959
end
6060

61+
context 'When cursor is on a puppet class' do
62+
let(:content) { <<-EOT
63+
class Test::NoParams {
64+
class { 'testclasses':
65+
ensure => 'present',
66+
}
67+
}
68+
EOT
69+
}
70+
let(:line_num) { 1 }
71+
let(:char_num) { 13 }
72+
73+
it_should_behave_like "a single definition result", /init\.pp/
74+
end
75+
76+
context 'When cursor is on a root puppet class' do
77+
let(:content) { <<-EOT
78+
class Test::NoParams {
79+
class { '::testclasses':
80+
ensure => 'present',
81+
}
82+
}
83+
EOT
84+
}
85+
let(:line_num) { 1 }
86+
let(:char_num) { 13 }
87+
88+
it_should_behave_like "a single definition result", /init\.pp/
89+
end
90+
91+
context 'When cursor is on a fully qualified puppet class' do
92+
let(:content) { <<-EOT
93+
class Test::NoParams {
94+
class { 'testclasses::nestedclass':
95+
ensure => 'present',
96+
}
97+
}
98+
EOT
99+
}
100+
let(:line_num) { 1 }
101+
let(:char_num) { 13 }
102+
103+
it_should_behave_like "a single definition result", /nestedclass\.pp/
104+
end
105+
61106
context 'When cursor is on a defined type' do
62107
let(:content) { <<-EOT
63108
class Test::NoParams {

0 commit comments

Comments
 (0)