Skip to content

Commit deebeda

Browse files
authored
Merge pull request #75 from austb/hover_resource_expr_facts
Hover fixes for resources and facts
2 parents 73c6700 + 40ccdcf commit deebeda

File tree

3 files changed

+74
-20
lines changed

3 files changed

+74
-20
lines changed

server/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,13 @@ Usage: puppet-languageserver.rb [options]
112112
--debug=DEBUG Output debug information. Either specify a filename or 'STDOUT'. Default is no debug output
113113
-s, --slow-start Delay starting the TCP Server until Puppet initialisation has completed. Default is to start fast
114114
-h, --help Prints this help
115-
-v, --version Prints the Langauge Server version```
115+
-v, --version Prints the Langauge Server version
116+
```
116117

117118
## Why are there vendored gems and why only native ruby
118119

119120
When used by VSCode this language server will be running using the Ruby runtime provided by Puppet Agent. That means no native extensions and no bundler. Also, only the gems provided by Puppet Agent would be available by default. To work around this limitation all runtime dependencies should be re-vendored and then the load path modified appropriately.
121+
122+
## Known Issues
123+
124+
* [PUP-7668](https://tickets.puppetlabs.com/browse/PUP-7668) Due to incorrect offsets, hover documentation can be displayed when the user is not actually hovering over the resource that the documentation is for.

server/lib/puppet-languageserver/hover_provider.rb

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@ def self.resolve(content, line_num, char_num)
66

77
content = nil
88
case item.class.to_s
9+
when 'Puppet::Pops::Model::ResourceExpression'
10+
content = get_resource_expression_content(item)
11+
when 'Puppet::Pops::Model::LiteralString'
12+
if item.eContainer.class == Puppet::Pops::Model::AccessExpression
13+
expr = item.eContainer.left_expr.expr.value
14+
15+
content = get_hover_content_for_access_expression(item, expr)
16+
elsif item.eContainer.class == Puppet::Pops::Model::ResourceBody
17+
# We are hovering over the resource name
18+
content = get_resource_expression_content(item.eContainer.eContainer)
19+
end
920
when 'Puppet::Pops::Model::VariableExpression'
1021
expr = item.expr.value
1122

12-
if expr == 'facts'
13-
# We are dealing with the facts variable
14-
# Just get the first part of the array and display that
15-
if item.eContainer.eContents.length > 1
16-
factname = item.eContainer.eContents[1].value
17-
content = get_fact_content(factname)
18-
end
19-
elsif expr.start_with?('::') && expr.rindex(':') == 1
20-
# We are dealing with a top local scope variable - Possible fact name
21-
factname = expr.slice(2, expr.length - 2)
22-
content = get_fact_content(factname)
23-
else
24-
# Could be a flatout fact name. May not *shrugs*. That method of access is deprecated
25-
content = get_fact_content(expr)
26-
end
27-
23+
content = get_hover_content_for_access_expression(item, expr)
2824
when 'Puppet::Pops::Model::QualifiedName'
2925
if !item.eContainer.nil? && item.eContainer.class.to_s == 'Puppet::Pops::Model::ResourceExpression'
3026
content = get_resource_expression_content(item.eContainer)
@@ -62,6 +58,27 @@ def self.resolve(content, line_num, char_num)
6258
end
6359
end
6460

61+
def self.get_hover_content_for_access_expression(item, expr)
62+
if expr == 'facts'
63+
# We are dealing with the facts variable
64+
# Just get the first part of the array and display that
65+
if item.eContainer.eContents.length > 1
66+
factname = item.eContainer.eContents[1].value
67+
content = get_fact_content(factname)
68+
end
69+
elsif expr.start_with?('::') && expr.rindex(':') == 1
70+
# We are dealing with a top local scope variable - Possible fact name
71+
factname = expr.slice(2, expr.length - 2)
72+
content = get_fact_content(factname)
73+
else
74+
# Could be a flatout fact name. May not *shrugs*. That method of access is deprecated
75+
content = get_fact_content(expr)
76+
end
77+
78+
content
79+
end
80+
81+
6582
# Content generation functions
6683
def self.get_fact_content(factname)
6784
return nil unless PuppetLanguageServer::FacterHelper.facts.key?(factname)

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
let(:char_num) { 10 }
6666

6767
it 'should return resource description' do
68-
pending('Not implemented')
6968
result = subject.resolve(content, line_num, char_num)
7069

7170
expect(result['contents']).to start_with("**user** Resource\n")
@@ -77,7 +76,6 @@
7776
let(:char_num) { 1 }
7877

7978
it 'should return resource description' do
80-
pending('Not implemented')
8179
result = subject.resolve(content, line_num, char_num)
8280

8381
expect(result['contents']).to start_with("**user** Resource\n")
@@ -175,7 +173,6 @@
175173
let(:char_num) { 22 }
176174

177175
it 'should return fact information' do
178-
pending('Not implemented')
179176
result = subject.resolve(content, line_num, char_num)
180177

181178
expect(result['contents']).to start_with("**operatingsystem** Fact\n")
@@ -201,5 +198,40 @@
201198
end
202199
end
203200
end
201+
202+
context "Given a resource in an else block" do
203+
let(:content) { <<-EOF
204+
class firewall {
205+
if(true) {
206+
} else {
207+
service { 'service':
208+
ensure => running
209+
}
210+
}
211+
}
212+
EOF
213+
}
214+
215+
describe 'when cursor is hovering on else branch' do
216+
let(:line_num) { 2 }
217+
let(:char_num) { 6 }
218+
it 'should not complete to service resource' do
219+
pending("(PUP-7668) parser is assigning an incorrect offset")
220+
221+
result = subject.resolve(content, line_num, char_num)
222+
expect(result['contents']).not_to start_with("**service** Resource\n")
223+
end
224+
end
225+
226+
describe 'when cursor is hovering on service resource' do
227+
let(:line_num) { 3 }
228+
let(:char_num) { 6 }
229+
it 'should complete to service resource documentation' do
230+
result = subject.resolve(content, line_num, char_num)
231+
232+
expect(result['contents']).to start_with("**service** Resource\n")
233+
end
234+
end
235+
end
204236
end
205237
end

0 commit comments

Comments
 (0)