Skip to content

Commit 13b8645

Browse files
committed
(GH-139) Add more intelligence to completion request
Previously the completion provider blindly moved the cursor one place back which worked fine if completions were triggered by a character instead of manual. Now that the LSP has provisions for the context of the completion request we can intelligently figure out when to remove the trigger character. This commit updates the mesasge handler to pass through the CompletionContext object and the completion provider can then determine how to use the context accordingly. This commit also updates the tests for this behaviour.
1 parent 64d5d1e commit 13b8645

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

lib/puppet-languageserver/manifest/completion_provider.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ module Manifest
55
module CompletionProvider
66
def self.complete(content, line_num, char_num, options = {})
77
options = {
8-
:tasks_mode => false
8+
:tasks_mode => false,
9+
:context => nil # LSP::CompletionContext object
910
}.merge(options)
1011
items = []
1112
incomplete = false
13+
is_trigger_char = !options[:context].nil? && options[:context].triggerKind == LSP::CompletionTriggerKind::TRIGGERCHARACTER
1214

1315
result = PuppetLanguageServer::PuppetParserHelper.object_under_cursor(content, line_num, char_num,
14-
:multiple_attempts => true,
15-
:disallowed_classes => [Puppet::Pops::Model::QualifiedName, Puppet::Pops::Model::BlockExpression],
16-
:tasks_mode => options[:tasks_mode])
16+
:multiple_attempts => true,
17+
:disallowed_classes => [Puppet::Pops::Model::QualifiedName, Puppet::Pops::Model::BlockExpression],
18+
:tasks_mode => options[:tasks_mode],
19+
:remove_trigger_char => is_trigger_char)
1720
if result.nil?
1821
# We are in the root of the document.
1922

lib/puppet-languageserver/message_handler.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ def request_textdocument_completion(_, json_rpc_message)
105105
line_num = json_rpc_message.params['position']['line']
106106
char_num = json_rpc_message.params['position']['character']
107107
content = documents.document(file_uri)
108+
context = json_rpc_message.params['context'].nil? ? nil : LSP::CompletionContext.new(json_rpc_message.params['context'])
108109

109110
case documents.document_type(file_uri)
110111
when :manifest
111-
return PuppetLanguageServer::Manifest::CompletionProvider.complete(content, line_num, char_num, :tasks_mode => PuppetLanguageServer::DocumentStore.plan_file?(file_uri))
112+
return PuppetLanguageServer::Manifest::CompletionProvider.complete(content, line_num, char_num, :context => context, :tasks_mode => PuppetLanguageServer::DocumentStore.plan_file?(file_uri))
112113
else
113114
raise "Unable to provide completion on #{file_uri}"
114115
end

spec/languageserver/integration/puppet-languageserver/manifest/completion_provider_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def create_ensurable_property
116116

117117
context "Given a simple valid manifest" do
118118
let(:content) { <<-EOT
119+
119120
class Alice {
120121
121122
user { 'Bob':
@@ -143,7 +144,7 @@ class Alice {
143144
let(:char_num) { 0 }
144145
let(:expected_types) { ['keyword','resource_type','function','resource_class'] }
145146

146-
[0, 8].each do |line_num|
147+
[0, 9].each do |line_num|
147148
it "should return a list of keyword, resource_type, function, resource_class regardless of cursor location (Testing line #{line_num})" do
148149
result = subject.complete(content, line_num, char_num)
149150

@@ -160,7 +161,7 @@ class Alice {
160161

161162
[
162163
{ :name => 'class', :line_num => 1 },
163-
{ :name => 'defined type', :line_num => 18 },
164+
{ :name => 'defined type', :line_num => 19 },
164165
].each do |testcase|
165166
describe "When inside the root of a #{testcase[:name]}" do
166167
let(:char_num) { 0 }
@@ -181,7 +182,7 @@ class Alice {
181182
end
182183

183184
describe "When inside the root of a resource" do
184-
let(:line_num) { 11 }
185+
let(:line_num) { 12 }
185186
let(:char_num) { 0 }
186187
let(:expected_types) { ['resource_parameter','resource_property'] }
187188

spec/languageserver/unit/puppet-languageserver/message_handler_spec.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,16 +488,36 @@
488488
let(:file_uri) { MANIFEST_FILENAME }
489489

490490
it 'should call complete method on the Completion Provider' do
491-
expect(PuppetLanguageServer::Manifest::CompletionProvider).to receive(:complete).with(Object,line_num,char_num,{:tasks_mode=>false}).and_return('something')
491+
expect(PuppetLanguageServer::Manifest::CompletionProvider).to receive(:complete).with(Object, line_num, char_num, { :tasks_mode => false, :context => nil }).and_return('something')
492492
subject.request_textdocument_completion(connection_id, request_message)
493493
end
494494

495495
it 'should set tasks_mode option if the file is Puppet plan file' do
496-
expect(PuppetLanguageServer::Manifest::CompletionProvider).to receive(:complete).with(Object,line_num,char_num,{:tasks_mode=>true}).and_return('something')
496+
expect(PuppetLanguageServer::Manifest::CompletionProvider).to receive(:complete).with(Object, line_num, char_num, { :tasks_mode => true, :context => nil }).and_return('something')
497497
allow(PuppetLanguageServer::DocumentStore).to receive(:plan_file?).and_return true
498498
subject.request_textdocument_completion(connection_id, request_message)
499499
end
500500

501+
context 'with a completion context' do
502+
let(:request_params) {{
503+
'textDocument' => {
504+
'uri' => file_uri
505+
},
506+
'position' => {
507+
'line' => line_num,
508+
'character' => char_num,
509+
},
510+
'context' => {
511+
'triggerKind' => 1
512+
}
513+
}}
514+
515+
it 'should pass the context' do
516+
expect(PuppetLanguageServer::Manifest::CompletionProvider).to receive(:complete).with(Object, line_num, char_num, { :tasks_mode => false, :context => LSP::CompletionContext}).and_return('something')
517+
subject.request_textdocument_completion(connection_id, request_message)
518+
end
519+
end
520+
501521
context 'and an error occurs during completion' do
502522
before(:each) do
503523
expect(PuppetLanguageServer::Manifest::CompletionProvider).to receive(:complete).and_raise('MockError')

0 commit comments

Comments
 (0)