diff --git a/lib/ruby_lsp/standard/addon.rb b/lib/ruby_lsp/standard/addon.rb index 92883b8a..a4976417 100644 --- a/lib/ruby_lsp/standard/addon.rb +++ b/lib/ruby_lsp/standard/addon.rb @@ -4,21 +4,18 @@ module RubyLsp module Standard class Addon < ::RubyLsp::Addon - def initializer - @wraps_built_in_lsp_standardizer = nil - end - def name "Standard Ruby" end def activate(global_state, message_queue) - warn "Activating Standard Ruby LSP addon v#{::Standard::VERSION}" + @logger = ::Standard::Lsp::Logger.new(prefix: "[Standard Ruby]") + @logger.puts "Activating Standard Ruby LSP addon v#{::Standard::VERSION}" RuboCop::LSP.enable @wraps_built_in_lsp_standardizer = WrapsBuiltinLspStandardizer.new global_state.register_formatter("standard", @wraps_built_in_lsp_standardizer) register_additional_file_watchers(global_state, message_queue) - warn "Initialized Standard Ruby LSP addon #{::Standard::VERSION}" + @logger.puts "Initialized Standard Ruby LSP addon #{::Standard::VERSION}" end def deactivate @@ -53,7 +50,7 @@ def register_additional_file_watchers(global_state, message_queue) def workspace_did_change_watched_files(changes) if changes.any? { |change| change[:uri].end_with?(".standard.yml") } @wraps_built_in_lsp_standardizer.init! - warn "Re-initialized Standard Ruby LSP addon #{::Standard::VERSION} due to .standard.yml file change" + @logger.puts "Re-initialized Standard Ruby LSP addon #{::Standard::VERSION} due to .standard.yml file change" end end end diff --git a/lib/ruby_lsp/standard/wraps_built_in_lsp_standardizer.rb b/lib/ruby_lsp/standard/wraps_built_in_lsp_standardizer.rb index 075d922b..f38fbe1f 100644 --- a/lib/ruby_lsp/standard/wraps_built_in_lsp_standardizer.rb +++ b/lib/ruby_lsp/standard/wraps_built_in_lsp_standardizer.rb @@ -7,11 +7,8 @@ def initialize end def init! - @config = ::Standard::BuildsConfig.new.call([]) - @standardizer = ::Standard::Lsp::Standardizer.new( - @config, - ::Standard::Lsp::Logger.new + ::Standard::BuildsConfig.new.call([]) ) end diff --git a/lib/standard/lsp/logger.rb b/lib/standard/lsp/logger.rb index 24d3b2da..16b58bd0 100644 --- a/lib/standard/lsp/logger.rb +++ b/lib/standard/lsp/logger.rb @@ -1,12 +1,13 @@ module Standard module Lsp class Logger - def initialize + def initialize(prefix: "[server]") + @prefix = prefix @puts_onces = [] end def puts(message) - warn("[server] #{message}") + warn [@prefix, message].compact.join(" ") end def puts_once(message) diff --git a/lib/standard/lsp/server.rb b/lib/standard/lsp/server.rb index 828f00c5..0b1439dd 100644 --- a/lib/standard/lsp/server.rb +++ b/lib/standard/lsp/server.rb @@ -13,7 +13,7 @@ def initialize(config) @writer = Proto::Transport::Io::Writer.new($stdout) @reader = Proto::Transport::Io::Reader.new($stdin) @logger = Logger.new - @standardizer = Standard::Lsp::Standardizer.new(config, @logger) + @standardizer = Standard::Lsp::Standardizer.new(config) @routes = Routes.new(@writer, @logger, @standardizer) end diff --git a/lib/standard/lsp/standardizer.rb b/lib/standard/lsp/standardizer.rb index 30e1713a..3962de39 100644 --- a/lib/standard/lsp/standardizer.rb +++ b/lib/standard/lsp/standardizer.rb @@ -4,13 +4,11 @@ module Standard module Lsp class Standardizer - def initialize(config, logger) + def initialize(config) @diagnostic_runner = ::Standard::Lsp::StdinRubocopRunner.new(config) @format_runner = ::Standard::Lsp::StdinRubocopRunner.new(config.dup.tap { |c| c.rubocop_options[:autocorrect] = true }) - - @logger = logger # TODO: delete if no longer needed for anything. @cop_registry = RuboCop::Cop::Registry.global.to_h end diff --git a/test/ruby_lsp_addon_test.rb b/test/ruby_lsp_addon_test.rb index 557fcd4f..93b61f4c 100644 --- a/test/ruby_lsp_addon_test.rb +++ b/test/ruby_lsp_addon_test.rb @@ -33,30 +33,32 @@ def test_diagnostic s = 'hello' puts s RUBY - with_server(source, "simple.rb") do |server, uri| - server.process_message( - id: 2, - method: "textDocument/diagnostic", - params: { - textDocument: { - uri: uri + do_with_fake_io do + with_server(source, "simple.rb") do |server, uri| + server.process_message( + id: 2, + method: "textDocument/diagnostic", + params: { + textDocument: { + uri: uri + } } - } - ) + ) - result = server.pop_response + result = server.pop_response - assert_instance_of(RubyLsp::Result, result) - assert_equal "full", result.response.kind - assert_equal 1, result.response.items.size - item = result.response.items.first - assert_equal({line: 0, character: 4}, item.range.start.to_hash) - assert_equal({line: 0, character: 11}, item.range.end.to_hash) - assert_equal RubyLsp::Constant::DiagnosticSeverity::INFORMATION, item.severity - assert_equal "Style/StringLiterals", item.code - assert_equal "https://docs.rubocop.org/rubocop/cops_style.html#stylestringliterals", item.code_description.href - assert_equal "Standard Ruby", item.source - assert_equal "Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.", item.message + assert_instance_of(RubyLsp::Result, result) + assert_equal "full", result.response.kind + assert_equal 1, result.response.items.size + item = result.response.items.first + assert_equal({line: 0, character: 4}, item.range.start.to_hash) + assert_equal({line: 0, character: 11}, item.range.end.to_hash) + assert_equal RubyLsp::Constant::DiagnosticSeverity::INFORMATION, item.severity + assert_equal "Style/StringLiterals", item.code + assert_equal "https://docs.rubocop.org/rubocop/cops_style.html#stylestringliterals", item.code_description.href + assert_equal "Standard Ruby", item.source + assert_equal "Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.", item.message + end end end @@ -65,21 +67,23 @@ def test_format s = 'hello' puts s RUBY - with_server(source, "simple.rb") do |server, uri| - server.process_message( - id: 2, - method: "textDocument/formatting", - params: {textDocument: {uri: uri}, position: {line: 0, character: 0}} - ) + do_with_fake_io do + with_server(source, "simple.rb") do |server, uri| + server.process_message( + id: 2, + method: "textDocument/formatting", + params: {textDocument: {uri: uri}, position: {line: 0, character: 0}} + ) - result = server.pop_response + result = server.pop_response - assert_instance_of(RubyLsp::Result, result) - assert 1, result.response.size - assert_equal <<~RUBY, result.response.first.new_text - s = "hello" - puts s - RUBY + assert_instance_of(RubyLsp::Result, result) + assert 1, result.response.size + assert_equal <<~RUBY, result.response.first.new_text + s = "hello" + puts s + RUBY + end end end diff --git a/test/standard/runners/lsp_test.rb b/test/standard/runners/lsp_test.rb index 9b01002f..e6adf3aa 100644 --- a/test/standard/runners/lsp_test.rb +++ b/test/standard/runners/lsp_test.rb @@ -482,7 +482,7 @@ def test_execute_command_with_unsupported_command end def test_did_open_on_ignored_path - msgs, err = run_server_on_requests({ + msgs, _err = run_server_on_requests({ method: "textDocument/didOpen", jsonrpc: "2.0", params: { @@ -508,7 +508,7 @@ def test_did_open_on_ignored_path end def test_formatting_via_execute_command_on_ignored_path - msgs, err = run_server_on_requests( + msgs, _err = run_server_on_requests( { method: "textDocument/didOpen", jsonrpc: "2.0", @@ -549,7 +549,7 @@ def test_formatting_via_execute_command_on_ignored_path end def test_formatting_via_formatting_path_on_ignored_path - msgs, err = run_server_on_requests( + msgs, _err = run_server_on_requests( { method: "textDocument/didOpen", jsonrpc: "2.0",