Skip to content

Commit

Permalink
Use the built-in diagnostics (results in a lot more fun), nix the mes…
Browse files Browse the repository at this point in the history
…sage we can no longer log
  • Loading branch information
searls committed Jun 24, 2024
1 parent e4fe4d1 commit 20816c6
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 56 deletions.
35 changes: 1 addition & 34 deletions lib/standard/lsp/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,45 +154,12 @@ def format_file(file_uri)

def diagnostic(file_uri, text)
@text_cache[file_uri] = text
offenses = @standardizer.offenses(uri_to_path(file_uri), text)

lsp_diagnostics = offenses.map { |o|
code = o[:cop_name]

msg = o[:message].delete_prefix(code)
loc = o[:location]

severity = case o[:severity]
when "error", "fatal"
SEV::ERROR
when "warning"
SEV::WARNING
when "convention"
SEV::INFORMATION
when "refactor", "info"
SEV::HINT
else # the above cases fully cover what RuboCop sends at this time
logger.puts "Unknown severity: #{severity.inspect}"
SEV::HINT
end

{
code: code,
message: msg,
range: {
start: {character: loc[:start_column] - 1, line: loc[:start_line] - 1},
end: {character: loc[:last_column], line: loc[:last_line] - 1}
},
severity: severity,
source: "standard"
}
}

{
method: "textDocument/publishDiagnostics",
params: {
uri: file_uri,
diagnostics: lsp_diagnostics
diagnostics: @standardizer.offenses(uri_to_path(file_uri), text)
}
}
end
Expand Down
196 changes: 174 additions & 22 deletions test/standard/runners/lsp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,185 @@ def test_did_open

assert_equal "", err.string
assert_equal 1, msgs.count
assert_equal({

expected = {
method: "textDocument/publishDiagnostics",
params: {
uri: "file:///path/to/file.rb",
diagnostics: [
{code: "Layout/ArrayAlignment",
message: "Use one level of indentation for elements following the first line of a multi-line array.",
range: {start: {character: 3, line: 2}, end: {character: 4, line: 2}},
severity: 3,
source: "standard"},
{code: "Layout/ExtraSpacing",
message: "Unnecessary spacing detected.",
range: {start: {character: 4, line: 2}, end: {character: 5, line: 2}},
severity: 3,
source: "standard"},
{code: "Layout/SpaceInsideArrayLiteralBrackets",
message: "Do not use space inside array brackets.",
range: {start: {character: 4, line: 2}, end: {character: 6, line: 2}},
severity: 3,
source: "standard"}
],
uri: "file:///path/to/file.rb"
{
range: {
start: {line: 2, character: 3},
end: {line: 2, character: 4}
},
severity: 3,
code: "Layout/ArrayAlignment",
codeDescription: {href: "https://docs.rubocop.org/rubocop/cops_layout.html#layoutarrayalignment"},
source: "Standard Ruby",
message: "Layout/ArrayAlignment: Use one level of indentation for elements following the first line of a multi-line array.",
data: {
correctable: true,
code_actions: [
{
title: "Autocorrect Layout/ArrayAlignment",
kind: "quickfix",
isPreferred: true,
edit: {
documentChanges: [
{
textDocument: {uri: "/path/to/file.rb", version: nil},
edits: [
{
range: {
start: {line: 2, character: 3},
end: {line: 2, character: 3}
}, newText: " "
}
]
}
]
}
},
{
title: "Disable Layout/ArrayAlignment for this line",
kind: "quickfix",
edit: {
documentChanges: [
{
textDocument: {uri: "/path/to/file.rb", version: nil},
edits: [
{
range: {
start: {line: 2, character: 7},
end: {line: 2, character: 7}
}, newText: " # standard:disable Layout/ArrayAlignment"
}
]
}
]
}
}
]
}
},
{
range: {
start: {line: 2, character: 4},
end: {line: 2, character: 5}
},
severity: 3,
code: "Layout/ExtraSpacing",
codeDescription: {href: "https://docs.rubocop.org/rubocop/cops_layout.html#layoutextraspacing"},
source: "Standard Ruby",
message: "Layout/ExtraSpacing: Unnecessary spacing detected.",
data: {
correctable: true,
code_actions: [
{
title: "Autocorrect Layout/ExtraSpacing",
kind: "quickfix",
isPreferred: true,
edit: {
documentChanges: [
{
textDocument: {uri: "/path/to/file.rb", version: nil},
edits: [
{
range: {
start: {line: 2, character: 4},
end: {line: 2, character: 5}
}, newText: ""
}
]
}
]
}
},
{
title: "Disable Layout/ExtraSpacing for this line",
kind: "quickfix",
edit: {
documentChanges: [
{
textDocument: {uri: "/path/to/file.rb", version: nil},
edits: [
{
range: {
start: {line: 2, character: 7},
end: {line: 2, character: 7}
},
newText: " # standard:disable Layout/ExtraSpacing"
}
]
}
]
}
}
]
}
},
{
range: {
start: {line: 2, character: 4},
end: {line: 2, character: 6}
},
severity: 3,
code: "Layout/SpaceInsideArrayLiteralBrackets",
codeDescription: {href: "https://docs.rubocop.org/rubocop/cops_layout.html#layoutspaceinsidearrayliteralbrackets"},
source: "Standard Ruby",
message: "Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.",
data: {
correctable: true,
code_actions: [
{
title: "Autocorrect Layout/SpaceInsideArrayLiteralBrackets",
kind: "quickfix",
isPreferred: true,
edit: {
documentChanges: [
{
textDocument: {uri: "/path/to/file.rb", version: nil},
edits: [
{
range: {
start: {line: 2, character: 4},
end: {line: 2, character: 6}
},
newText: ""
}
]
}
]
}
},
{
title: "Disable Layout/SpaceInsideArrayLiteralBrackets for this line",
kind: "quickfix",
edit: {
documentChanges: [
{
textDocument: {uri: "/path/to/file.rb", version: nil},
edits: [
{
range: {
start: {line: 2, character: 7},
end: {line: 2, character: 7}
},
newText: " # standard:disable Layout/SpaceInsideArrayLiteralBrackets"
}
]
}
]
}
}
]
}
}
]
},
jsonrpc: "2.0"
}, msgs.first)
}
assert_equal expected, msgs.first
end

def test_format
Expand Down Expand Up @@ -350,7 +505,6 @@ def test_did_open_on_ignored_path
},
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
Expand Down Expand Up @@ -392,7 +546,6 @@ def test_formatting_via_execute_command_on_ignored_path
},
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
Expand Down Expand Up @@ -441,7 +594,6 @@ def test_formatting_via_formatting_path_on_ignored_path
},
format_result
)
assert_equal "[server] Ignoring file, per configuration: #{Dir.pwd}/tmp/zzz.rb", err.string.chomp
end

private
Expand Down

0 comments on commit 20816c6

Please sign in to comment.