Skip to content

Commit

Permalink
this appears to actually work…?
Browse files Browse the repository at this point in the history
  • Loading branch information
searls committed Feb 11, 2023
1 parent aa0b078 commit 6165e5f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 18 deletions.
3 changes: 1 addition & 2 deletions lib/standard/lsp/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ def handle_method_missing(request)
private

def uri_to_path(uri)
# TODO: make this real
uri.sub(%r{^file:///}, "")
uri.sub(%r{^file://}, "")
end

def format_file(file_uri)
Expand Down
8 changes: 2 additions & 6 deletions lib/standard/lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ module Lsp
SEV = Proto::Constant::DiagnosticSeverity

class Server
def self.start(standardizer)
new(standardizer).start
end

def initialize(standardizer)
@standardizer = standardizer
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)
@routes = Routes.new(@writer, @logger, @standardizer)
end

Expand Down
30 changes: 22 additions & 8 deletions lib/standard/lsp/standardizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
module Standard
module Lsp
class Standardizer
def initialize(config)
def initialize(config, logger)
@config = config
@logger = logger
@rubocop_runner = Standard::Runners::Rubocop.new
end

Expand All @@ -26,20 +27,33 @@ def format(path, text)
end

def offenses(path, text)
results = capture_rubocop_stdout(fork_config(path, text, format: false))
JSON.parse(results, symbolize_names: true).dig(:files, 0, :offenses)
results = JSON.parse(
capture_rubocop_stdout(fork_config(path, text, format: false)),
symbolize_names: true
)
if results[:files].empty?
@logger.puts "Ignoring file, per configuration: #{path}"
[]
else
results.dig(:files, 0, :offenses)
end
end

private

# Can't make frozen versions of this hash because RuboCop mutates it
BASE_OPTIONS = {
force_exclusion: true,
parallel: false,
todo_file: nil,
todo_ignore_files: []
}
def fork_config(path, text, format:)
o = if format
{stdin: text, autocorrect: true, formatters: [["Standard::Formatter", nil]], parallel: false, todo_file: nil, todo_ignore_files: [], safe_autocorrect: true}
options = if format
{stdin: text, autocorrect: true, formatters: [], safe_autocorrect: true}
else
{stdin: text, autocorrect: false, formatters: [["json"]], parallel: false, todo_file: nil, todo_ignore_files: [], format: "json"}
{stdin: text, autocorrect: false, formatters: [["json"]], format: "json"}
end
Standard::Config.new(@config.runner, [path], o, @config.rubocop_config_store)
Standard::Config.new(@config.runner, [path], BASE_OPTIONS.merge(options), @config.rubocop_config_store)
end

def capture_rubocop_stdout(config)
Expand Down
3 changes: 1 addition & 2 deletions lib/standard/runners/lsp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module Standard
module Runners
class Lsp
def call(config)
standardizer = Standard::Lsp::Standardizer.new(config)
Standard::Lsp::Server.start(standardizer)
Standard::Lsp::Server.new(config).start
end
end
end
Expand Down
118 changes: 118 additions & 0 deletions test/standard/runners/lsp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,124 @@ def test_execute_command_with_unsupported_command
}, msgs.last)
end

def test_did_open_on_ignored_path
msgs, err = run_server_on_requests({
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: {
textDocument: {
languageId: "ruby",
text: "puts 'neat'",
# Depends on this project's .standard.yml ignoring `tmp/**/*`
uri: "file://#{Dir.pwd}/tmp/foo/bar.rb",
version: 0
}
}
})

assert_equal 1, msgs.count
assert_equal({
method: "textDocument/publishDiagnostics",
params: {
diagnostics: [],
uri: "file://#{Dir.pwd}/tmp/foo/bar.rb"
},
jsonrpc: "2.0"
}, msgs.first)
assert_equal "[server] Ignoring file, per configuration: #{Dir.pwd}/tmp/foo/bar.rb", err.string.chomp
end

def test_formatting_via_execute_command_on_ignored_path
msgs, err = run_server_on_requests(
{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: {
textDocument: {
languageId: "ruby",
text: "puts 'hi'",
# Depends on this project's .standard.yml ignoring `tmp/**/*`
uri: "file://#{Dir.pwd}/tmp/baz.rb",
version: 0
}
}
},
{
method: "workspace/executeCommand",
id: 99,
jsonrpc: "2.0",
params: {
command: "standardRuby.formatAutoFixes",
arguments: [{uri: "file://#{Dir.pwd}/tmp/baz.rb"}]
}
}
)

assert_equal({
id: 99,
method: "workspace/applyEdit",
params: {
label: "Format with Standard Ruby auto-fixes",
edit: {
changes: {
"file://#{Dir.pwd}/tmp/baz.rb": []
}
}
},
jsonrpc: "2.0"
}, msgs.last)
assert_equal "[server] Ignoring file, per configuration: #{Dir.pwd}/tmp/baz.rb", err.string.chomp
end

def test_formatting_via_formatting_path_on_ignored_path
msgs, err = run_server_on_requests(
{
method: "textDocument/didOpen",
jsonrpc: "2.0",
params: {
textDocument: {
languageId: "ruby",
text: "puts 'hi'",
# Depends on this project's .standard.yml ignoring `tmp/**/*`
uri: "file://#{Dir.pwd}/tmp/zzz.rb",
version: 0
}
}
},
{
method: "textDocument/didChange",
jsonrpc: "2.0",
params: {
contentChanges: [{text: "puts 'bye'"}],
textDocument: {
uri: "file://#{Dir.pwd}/tmp/zzz.rb",
version: 10
}
}
},
{
method: "textDocument/formatting",
id: 20,
jsonrpc: "2.0",
params: {
options: {insertSpaces: true, tabSize: 2},
textDocument: {uri: "file://#{Dir.pwd}/tmp/zzz.rb"}
}
}
)

format_result = msgs.last
assert_equal(
{
id: 20,
result: [],
jsonrpc: "2.0"
},
format_result
)
assert_match "[server] Ignoring file, per configuration: #{Dir.pwd}/tmp/zzz.rb", err.string
end

private

def run_server_on_requests(*requests)
Expand Down

0 comments on commit 6165e5f

Please sign in to comment.