Skip to content

Commit

Permalink
Merge pull request #23 from r7kamura/feature/debug
Browse files Browse the repository at this point in the history
Write given and sent messages to stderr when debug mode is enabled
  • Loading branch information
r7kamura committed Sep 11, 2022
2 parents 6988efe + 973ff03 commit 0582d53
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 99 deletions.
5 changes: 3 additions & 2 deletions lib/rucoa/cli.rb
Expand Up @@ -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
Expand Down
65 changes: 49 additions & 16 deletions lib/rucoa/configuration.rb
Expand Up @@ -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]
Expand All @@ -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] ||= {}
Expand All @@ -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<String>]
# @param default [Object]
# @return [Object]
def fetch(*keys, default:)
value = @settings.dig(*keys)
if value.nil?
true
default
else
value
end
Expand Down
54 changes: 42 additions & 12 deletions lib/rucoa/server.rb
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require 'logger'
require 'stringio'

module Rucoa
class Server
# @return [Hash{String => Class}]
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -115,18 +136,27 @@ 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

# @param message [Hash]
# @return [void]
def write_server_response(message)
debug do
{
kind: :write,
message: message
}
end
@writer.write(message)
end
end
Expand Down
7 changes: 1 addition & 6 deletions 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
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions 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
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions 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
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions 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
Expand All @@ -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
Expand Down
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'stringio'

RSpec.describe Rucoa::Handlers::TextDocumentCodeActionHandler do
describe '.call' do
subject do
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/rucoa/handlers/text_document_did_change_handler_spec.rb
Expand Up @@ -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

Expand Down
6 changes: 1 addition & 5 deletions 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
Expand Down Expand Up @@ -41,10 +40,7 @@
end

let(:server) do
Rucoa::Server.new(
input: StringIO.new,
output: StringIO.new
)
Rucoa::Server.new
end

let(:content) do
Expand Down
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'stringio'
require 'tmpdir'

RSpec.describe Rucoa::Handlers::TextDocumentDocumentSymbolHandler do
Expand Down Expand Up @@ -29,10 +28,7 @@
end

let(:server) do
Rucoa::Server.new(
input: StringIO.new,
output: StringIO.new
)
Rucoa::Server.new
end

let(:content) do
Expand Down

0 comments on commit 0582d53

Please sign in to comment.