Skip to content

Commit

Permalink
get LSP side passing
Browse files Browse the repository at this point in the history
  • Loading branch information
searls committed Jun 24, 2024
1 parent 20816c6 commit 3a27dcc
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 54 deletions.
11 changes: 4 additions & 7 deletions lib/ruby_lsp/standard/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions lib/ruby_lsp/standard/wraps_built_in_lsp_standardizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions lib/standard/lsp/logger.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/standard/lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions lib/standard/lsp/standardizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
72 changes: 38 additions & 34 deletions test/ruby_lsp_addon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions test/standard/runners/lsp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 3a27dcc

Please sign in to comment.