From 2a2ec99d7ee0f263d58658fdf65fb875ebe39ad3 Mon Sep 17 00:00:00 2001 From: jordanbreen28 Date: Wed, 24 Apr 2024 11:55:04 +0100 Subject: [PATCH 1/3] CAT-1670 - Fix undefined variable json_rpc_handler Prior to this commit, we would see in the language server logs that we were referencing an undefined variable `json_rpc_handler`. This had knock on effects, such as registering the onTypeformatting provider, meaning this setting would not work when enabled. We now update the json_rpc_handler send message event to be consistent with the rest of the language server, and to fix the undefined variable error. The dynamically registered providers, like onTypeFormatting, will now work as expected. --- lib/puppet-languageserver/message_handler.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/puppet-languageserver/message_handler.rb b/lib/puppet-languageserver/message_handler.rb index 0b753974..6265630d 100644 --- a/lib/puppet-languageserver/message_handler.rb +++ b/lib/puppet-languageserver/message_handler.rb @@ -300,9 +300,12 @@ def notification_initialized(_, _json_rpc_message) # Raise a warning if the Puppet version is mismatched server_options = protocol.connection.server.server_options unless server_options[:puppet_version].nil? || server_options[:puppet_version] == Puppet.version - json_rpc_handler.send_show_message_notification( - LSP::MessageType::WARNING, - "Unable to use Puppet version '#{server_options[:puppet_version]}' as it is not available. Using version '#{Puppet.version}' instead." + protocol.encode_and_send( + ::PuppetEditorServices::Protocol::JsonRPCMessages.new_notification( + 'window/showMessage', + 'type' => LSP::MessageType::WARNING, + 'message' => "Unable to use Puppet version '#{server_options[:puppet_version]}' as it is not available. Using version '#{Puppet.version}' instead." + ) ) end From 0a26044490607dd910a798c3abb09e9a983dc6f4 Mon Sep 17 00:00:00 2001 From: jordanbreen28 Date: Wed, 24 Apr 2024 12:02:16 +0100 Subject: [PATCH 2/3] (maint) - Increase max Metrics/ClassLength --- .rubocop_todo.yml | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 71127cb1..6db88aaa 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2024-01-23 14:26:06 UTC using RuboCop version 1.50.2. +# on 2024-04-24 11:01:50 UTC using RuboCop version 1.50.2. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -50,7 +50,7 @@ Metrics/BlockLength: # Offense count: 12 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 330 + Max: 333 # Offense count: 53 # Configuration parameters: AllowedMethods, AllowedPatterns. @@ -103,15 +103,6 @@ Security/IoMethods: Exclude: - 'lib/puppet-debugserver/debug_session/break_points.rb' -# Offense count: 5 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowedMethods, AllowedPatterns. -# AllowedMethods: ==, equal?, eql? -# Style/ClassEqualityComparison: -# Exclude: -# - 'lib/puppet-languageserver/manifest/definition_provider.rb' -# - 'lib/puppet-languageserver/manifest/signature_provider.rb' - # Offense count: 104 # Configuration parameters: AllowedConstants. Style/Documentation: From 2126f5e2a7fae94a7208b2e9be4973b6477cdf6c Mon Sep 17 00:00:00 2001 From: jordanbreen28 Date: Wed, 24 Apr 2024 13:59:48 +0100 Subject: [PATCH 3/3] (CAT-1670) - Add spec test for protocol.encode_and_send --- .../message_handler_spec.rb | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/spec/languageserver/unit/puppet-languageserver/message_handler_spec.rb b/spec/languageserver/unit/puppet-languageserver/message_handler_spec.rb index 6c2f9570..52a1bcfd 100644 --- a/spec/languageserver/unit/puppet-languageserver/message_handler_spec.rb +++ b/spec/languageserver/unit/puppet-languageserver/message_handler_spec.rb @@ -881,6 +881,40 @@ def client_cap_hash(client_cap_string, dynamic_reg) ) end + describe 'PuppetLanguageServer::MessageHandler' do + let(:server_options) { { puppet_version: '5.5.1' } } + let(:server) { double('PuppetEditorServices::Server') } + let(:connection) { double('PuppetEditorServices::Protocol::Connection', id: 'some_id') } + let(:protocol) { double('PuppetEditorServices::Protocol::JsonRPC') } + let(:handler_object) { PuppetLanguageServer::MessageHandler.new(protocol) } + + before do + allow(server).to receive(:server_options).and_return(server_options) + allow(connection).to receive(:server).and_return(server) + allow(protocol).to receive(:connection).and_return(connection) + end + + context 'When receiving a notification' do + context '.notification_initialized' do + it 'sends a warning message when Puppet versions are mismatched' do + allow(Puppet).to receive(:version).and_return('6.0.0') + + expected_message = { + 'type' => LSP::MessageType::WARNING, + 'message' => "Unable to use Puppet version '#{server_options[:puppet_version]}' as it is not available. Using version '#{Puppet.version}' instead." + } + + expect(protocol).to receive(:encode_and_send) do |arg| + expect(arg).to be_a(PuppetEditorServices::Protocol::JsonRPCMessages::NotificationMessage) + expect(arg.params).to eq(expected_message) + end + + handler_object.notification_initialized(nil, nil) + end + end + end + end + # initialized - https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md#initialized describe '.notification_initialized' do let(:notification_method) { 'initialized' }