Skip to content

Commit

Permalink
(GH-139) Add more intelligence to completion request
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
glennsarti committed Nov 27, 2019
1 parent 64d5d1e commit 3bcdb03
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
11 changes: 7 additions & 4 deletions lib/puppet-languageserver/manifest/completion_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ module Manifest
module CompletionProvider
def self.complete(content, line_num, char_num, options = {})
options = {
:tasks_mode => false
:tasks_mode => false,
:context => nil # LSP::CompletionContext object
}.merge(options)
items = []
incomplete = false
is_trigger_char = !options[:context].nil? && options[:context].triggerKind == LSP::CompletionTriggerKind::TRIGGERCHARACTER

result = PuppetLanguageServer::PuppetParserHelper.object_under_cursor(content, line_num, char_num,
:multiple_attempts => true,
:disallowed_classes => [Puppet::Pops::Model::QualifiedName, Puppet::Pops::Model::BlockExpression],
:tasks_mode => options[:tasks_mode])
:multiple_attempts => true,
:disallowed_classes => [Puppet::Pops::Model::QualifiedName, Puppet::Pops::Model::BlockExpression],
:tasks_mode => options[:tasks_mode],
:remove_trigger_char => is_trigger_char)
if result.nil?
# We are in the root of the document.

Expand Down
3 changes: 2 additions & 1 deletion lib/puppet-languageserver/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ def request_textdocument_completion(_, json_rpc_message)
line_num = json_rpc_message.params['position']['line']
char_num = json_rpc_message.params['position']['character']
content = documents.document(file_uri)
context = json_rpc_message.params['context'].nil? ? nil : LSP::CompletionContext.new(json_rpc_message.params['context'])

case documents.document_type(file_uri)
when :manifest
return PuppetLanguageServer::Manifest::CompletionProvider.complete(content, line_num, char_num, :tasks_mode => PuppetLanguageServer::DocumentStore.plan_file?(file_uri))
return PuppetLanguageServer::Manifest::CompletionProvider.complete(content, line_num, char_num, :context => context, :tasks_mode => PuppetLanguageServer::DocumentStore.plan_file?(file_uri))
else
raise "Unable to provide completion on #{file_uri}"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def create_ensurable_property

context "Given a simple valid manifest" do
let(:content) { <<-EOT
class Alice {
user { 'Bob':
Expand Down Expand Up @@ -143,7 +144,7 @@ class Alice {
let(:char_num) { 0 }
let(:expected_types) { ['keyword','resource_type','function','resource_class'] }

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

Expand All @@ -160,7 +161,7 @@ class Alice {

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

describe "When inside the root of a resource" do
let(:line_num) { 11 }
let(:line_num) { 12 }
let(:char_num) { 0 }
let(:expected_types) { ['resource_parameter','resource_property'] }

Expand Down

0 comments on commit 3bcdb03

Please sign in to comment.