From 973ff036d8d085a256251004b1feb8ff597ef813 Mon Sep 17 00:00:00 2001 From: Ryo Nakamura Date: Sat, 10 Sep 2022 11:29:58 +0900 Subject: [PATCH] Write given and sent messages to stderr when debug mode is enabled --- lib/rucoa/cli.rb | 5 +- lib/rucoa/configuration.rb | 65 ++++++++++++++----- lib/rucoa/server.rb | 54 +++++++++++---- spec/rucoa/handlers/exit_handler_spec.rb | 7 +- .../rucoa/handlers/initialize_handler_spec.rb | 7 +- .../handlers/initialized_handler_spec.rb | 7 +- spec/rucoa/handlers/shutdown_handler_spec.rb | 7 +- .../text_document_code_action_handler_spec.rb | 7 +- .../text_document_did_change_handler_spec.rb | 4 +- .../text_document_did_open_handler_spec.rb | 6 +- ...t_document_document_symbol_handler_spec.rb | 6 +- .../text_document_formatting_handler_spec.rb | 6 +- ..._document_range_formatting_handler_spec.rb | 6 +- ...t_document_selection_range_handler_spec.rb | 4 +- ...xt_document_signature_help_handler_spec.rb | 7 +- ...e_did_change_configuration_handler_spec.rb | 7 +- spec/rucoa/server_spec.rb | 31 ++++++++- 17 files changed, 137 insertions(+), 99 deletions(-) diff --git a/lib/rucoa/cli.rb b/lib/rucoa/cli.rb index 6e11890..f199b9f 100644 --- a/lib/rucoa/cli.rb +++ b/lib/rucoa/cli.rb @@ -16,8 +16,9 @@ def initialize(argv) # @return [void] def call Server.new( - input: $stdin, - output: $stdout + io_in: $stdin, + io_log: $stderr, + io_out: $stdout ).start end end diff --git a/lib/rucoa/configuration.rb b/lib/rucoa/configuration.rb index 786c0c2..5c3738d 100644 --- a/lib/rucoa/configuration.rb +++ b/lib/rucoa/configuration.rb @@ -8,62 +8,88 @@ def initialize # @return [void] def disable_code_action - disable('codeAction') + disable_feature('codeAction') end # @return [void] def disable_diagnostics - disable('diagnostics') + disable_feature('diagnostics') end # @return [void] def disable_document_symbol - disable('documentSymbol') + disable_feature('documentSymbol') end # @return [void] def disable_formatting - disable('formatting') + disable_feature('formatting') end # @return [void] def disable_selection_range - disable('selectionRange') + disable_feature('selectionRange') end # @return [void] def disable_signature_help - disable('signatureHelp') + disable_feature('signatureHelp') + end + + # @return [void] + def enable_debug + @settings ||= {} + @settings['base'] ||= {} + @settings['base']['debug'] = true end # @return [Boolean] + # @example returns false if the configuration is empty + # configuration = Rucoa::Configuration.new + # expect(configuration).not_to be_enables_debug + # @example returns true if the configuration enables debug + # configuration = Rucoa::Configuration.new + # configuration.update('base' => { 'debug' => true }) + # expect(configuration).to be_enables_debug + def enables_debug? + fetch('base', 'debug', default: false) + end + + # @return [Boolean] + # @example returns true if the configuration is empty + # configuration = Rucoa::Configuration.new + # expect(configuration).to be_enables_code_action + # @example returns false if the configuration disables code action + # configuration = Rucoa::Configuration.new + # configuration.disable_code_action + # expect(configuration).not_to be_enables_code_action def enables_code_action? - enables?('codeAction') + enables_feature?('codeAction') end # @return [Boolean] def enables_diagnostics? - enables?('diagnostics') + enables_feature?('diagnostics') end # @return [Boolean] def enables_document_symbol? - enables?('documentSymbol') + enables_feature?('documentSymbol') end # @return [Boolean] def enables_formatting? - enables?('formatting') + enables_feature?('formatting') end # @return [Boolean] def enables_selection_range? - enables?('selectionRange') + enables_feature?('selectionRange') end # @return [Boolean] def enables_signature_help? - enables?('signatureHelp') + enables_feature?('signatureHelp') end # @param settings [Hash] @@ -76,7 +102,7 @@ def update(settings) # @param feature [String] # @return [void] - def disable(feature) + def disable_feature(feature) @settings ||= {} @settings['feature'] ||= {} @settings['feature'][feature] ||= {} @@ -85,10 +111,17 @@ def disable(feature) # @param feature [String] # @return [Boolean] - def enables?(feature) - value = @settings.dig('feature', feature, 'enable') + def enables_feature?(feature) + fetch('feature', feature, 'enable', default: true) + end + + # @param keys [Array] + # @param default [Object] + # @return [Object] + def fetch(*keys, default:) + value = @settings.dig(*keys) if value.nil? - true + default else value end diff --git a/lib/rucoa/server.rb b/lib/rucoa/server.rb index a74930f..630a2ef 100644 --- a/lib/rucoa/server.rb +++ b/lib/rucoa/server.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require 'logger' +require 'stringio' + module Rucoa class Server # @return [Hash{String => Class}] @@ -32,11 +35,18 @@ class Server # @return [Rucoa::SourceStore] attr_reader :source_store - # @param input [IO] - # @param output [IO] - def initialize(input:, output:) - @reader = MessageReader.new(input) - @writer = MessageWriter.new(output) + # @param io_log [IO] + # @param io_in [IO] + # @param io_out [IO] + def initialize( + io_log: ::StringIO.new, + io_in: ::StringIO.new, + io_out: ::StringIO.new + ) + @logger = ::Logger.new(io_log) + @logger.level = ::Logger::DEBUG + @reader = MessageReader.new(io_in) + @writer = MessageWriter.new(io_out) @client_response_handlers = {} @configuration = Configuration.new @@ -50,8 +60,14 @@ def initialize(input:, output:) # @return [void] def start - @reader.read do |request| - handle(request) + @reader.read do |message| + debug do + { + kind: :read, + message: message + } + end + handle(message) end end @@ -91,6 +107,11 @@ def handle(request) end end + # @yieldparam log [String] + def debug(&block) + @logger.debug(&block) if configuration.enables_debug? + end + # @param request [Hash] # @return [void] def handle_client_request(request) @@ -115,11 +136,14 @@ def find_client_request_handler(request_method) # @param message [Hash] # @return [void] def write_server_request(message, &block) - @writer.write( - message.merge( - id: @server_request_id - ) - ) + message = message.merge('id' => @server_request_id) + debug do + { + kind: :write, + message: message + } + end + @writer.write(message) @client_response_handlers[@server_request_id] = block @server_request_id += 1 end @@ -127,6 +151,12 @@ def write_server_request(message, &block) # @param message [Hash] # @return [void] def write_server_response(message) + debug do + { + kind: :write, + message: message + } + end @writer.write(message) end end diff --git a/spec/rucoa/handlers/exit_handler_spec.rb b/spec/rucoa/handlers/exit_handler_spec.rb index 5bee834..72f1678 100644 --- a/spec/rucoa/handlers/exit_handler_spec.rb +++ b/spec/rucoa/handlers/exit_handler_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'stringio' - RSpec.describe Rucoa::Handlers::ExitHandler do describe '.call' do subject do @@ -20,10 +18,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end context 'with valid condition' do diff --git a/spec/rucoa/handlers/initialize_handler_spec.rb b/spec/rucoa/handlers/initialize_handler_spec.rb index 1706f5e..63cd826 100644 --- a/spec/rucoa/handlers/initialize_handler_spec.rb +++ b/spec/rucoa/handlers/initialize_handler_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'stringio' - RSpec.describe Rucoa::Handlers::InitializeHandler do describe '.call' do subject do @@ -20,10 +18,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end context 'with valid condition' do diff --git a/spec/rucoa/handlers/initialized_handler_spec.rb b/spec/rucoa/handlers/initialized_handler_spec.rb index bd0e083..01e85bf 100644 --- a/spec/rucoa/handlers/initialized_handler_spec.rb +++ b/spec/rucoa/handlers/initialized_handler_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'stringio' - RSpec.describe Rucoa::Handlers::InitializedHandler do describe '.call' do subject do @@ -16,10 +14,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end context 'with valid condition' do diff --git a/spec/rucoa/handlers/shutdown_handler_spec.rb b/spec/rucoa/handlers/shutdown_handler_spec.rb index c74d4ac..4a5db17 100644 --- a/spec/rucoa/handlers/shutdown_handler_spec.rb +++ b/spec/rucoa/handlers/shutdown_handler_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'stringio' - RSpec.describe Rucoa::Handlers::ShutdownHandler do describe '.call' do subject do @@ -20,10 +18,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end context 'with valid condition' do diff --git a/spec/rucoa/handlers/text_document_code_action_handler_spec.rb b/spec/rucoa/handlers/text_document_code_action_handler_spec.rb index 6652077..dcd8153 100644 --- a/spec/rucoa/handlers/text_document_code_action_handler_spec.rb +++ b/spec/rucoa/handlers/text_document_code_action_handler_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'stringio' - RSpec.describe Rucoa::Handlers::TextDocumentCodeActionHandler do describe '.call' do subject do @@ -47,10 +45,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end let(:file_path) do diff --git a/spec/rucoa/handlers/text_document_did_change_handler_spec.rb b/spec/rucoa/handlers/text_document_did_change_handler_spec.rb index 137f561..d9d76a3 100644 --- a/spec/rucoa/handlers/text_document_did_change_handler_spec.rb +++ b/spec/rucoa/handlers/text_document_did_change_handler_spec.rb @@ -45,8 +45,8 @@ let(:server) do Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new + io_in: StringIO.new, + io_out: StringIO.new ) end diff --git a/spec/rucoa/handlers/text_document_did_open_handler_spec.rb b/spec/rucoa/handlers/text_document_did_open_handler_spec.rb index 9b50f43..6e2ef90 100644 --- a/spec/rucoa/handlers/text_document_did_open_handler_spec.rb +++ b/spec/rucoa/handlers/text_document_did_open_handler_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'fileutils' -require 'stringio' require 'tmpdir' RSpec.describe Rucoa::Handlers::TextDocumentDidOpenHandler do @@ -41,10 +40,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end let(:content) do diff --git a/spec/rucoa/handlers/text_document_document_symbol_handler_spec.rb b/spec/rucoa/handlers/text_document_document_symbol_handler_spec.rb index 1b9d449..bb901e5 100644 --- a/spec/rucoa/handlers/text_document_document_symbol_handler_spec.rb +++ b/spec/rucoa/handlers/text_document_document_symbol_handler_spec.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -require 'stringio' require 'tmpdir' RSpec.describe Rucoa::Handlers::TextDocumentDocumentSymbolHandler do @@ -29,10 +28,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end let(:content) do diff --git a/spec/rucoa/handlers/text_document_formatting_handler_spec.rb b/spec/rucoa/handlers/text_document_formatting_handler_spec.rb index 6ad2051..205ed74 100644 --- a/spec/rucoa/handlers/text_document_formatting_handler_spec.rb +++ b/spec/rucoa/handlers/text_document_formatting_handler_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'fileutils' -require 'stringio' require 'tmpdir' RSpec.describe Rucoa::Handlers::TextDocumentFormattingHandler do @@ -40,10 +39,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end let(:content) do diff --git a/spec/rucoa/handlers/text_document_range_formatting_handler_spec.rb b/spec/rucoa/handlers/text_document_range_formatting_handler_spec.rb index 1e8f0d1..d0a0b89 100644 --- a/spec/rucoa/handlers/text_document_range_formatting_handler_spec.rb +++ b/spec/rucoa/handlers/text_document_range_formatting_handler_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'fileutils' -require 'stringio' require 'tmpdir' RSpec.describe Rucoa::Handlers::TextDocumentRangeFormattingHandler do @@ -50,10 +49,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end let(:content) do diff --git a/spec/rucoa/handlers/text_document_selection_range_handler_spec.rb b/spec/rucoa/handlers/text_document_selection_range_handler_spec.rb index db43999..38b1610 100644 --- a/spec/rucoa/handlers/text_document_selection_range_handler_spec.rb +++ b/spec/rucoa/handlers/text_document_selection_range_handler_spec.rb @@ -32,8 +32,8 @@ let(:server) do Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new + io_in: StringIO.new, + io_out: StringIO.new ) end diff --git a/spec/rucoa/handlers/text_document_signature_help_handler_spec.rb b/spec/rucoa/handlers/text_document_signature_help_handler_spec.rb index 4614acb..6c8864f 100644 --- a/spec/rucoa/handlers/text_document_signature_help_handler_spec.rb +++ b/spec/rucoa/handlers/text_document_signature_help_handler_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'stringio' - RSpec.describe Rucoa::Handlers::TextDocumentSignatureHelpHandler do describe '.call' do subject do @@ -29,10 +27,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end let(:uri) do diff --git a/spec/rucoa/handlers/workspace_did_change_configuration_handler_spec.rb b/spec/rucoa/handlers/workspace_did_change_configuration_handler_spec.rb index 600ef56..91c6068 100644 --- a/spec/rucoa/handlers/workspace_did_change_configuration_handler_spec.rb +++ b/spec/rucoa/handlers/workspace_did_change_configuration_handler_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'stringio' - RSpec.describe Rucoa::Handlers::WorkspaceDidChangeConfigurationHandler do describe '.call' do subject do @@ -16,10 +14,7 @@ end let(:server) do - Rucoa::Server.new( - input: StringIO.new, - output: StringIO.new - ) + Rucoa::Server.new end context 'with valid condition' do diff --git a/spec/rucoa/server_spec.rb b/spec/rucoa/server_spec.rb index 0d5d26a..5ddd3ca 100644 --- a/spec/rucoa/server_spec.rb +++ b/spec/rucoa/server_spec.rb @@ -10,15 +10,19 @@ let(:instance) do described_class.new( - input: input, - output: StringIO.new + io_in: io_input, + io_log: io_log ) end - let(:input) do + let(:io_input) do StringIO.new(raw_input) end + let(:io_log) do + StringIO.new + end + context 'when responsible request is sent' do let(:raw_input) do Rucoa::MessageWriter.pack( @@ -37,6 +41,27 @@ ) ] ) + expect(io_log.string).to eq('') + end + end + + context 'when debug mode is enabled' do + before do + instance.configuration.enable_debug + end + + let(:raw_input) do + Rucoa::MessageWriter.pack( + id: 1, + method: 'initialize', + params: {} + ) + end + + it 'writes debug log' do + subject + expect(io_log.string).to match(':kind=>:read') + expect(io_log.string).to match(':kind=>:write') end end