Skip to content

Commit 70d9575

Browse files
authored
Add documentation link to RuboCop diagnostics (#1111)
* add documentation link to rubocop diagnostics * use real lsp protocl, not the vscode version * test fixtures not being picked up? * load rubocop cops into constant
1 parent 5806f09 commit 70d9575

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

CONTRIBUTING.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ The test suite can be executed by running
2424
bin/test
2525
# or the spec reporter
2626
SPEC_REPORTER=1 bin/test
27-
# Warning are turned off by default. If you wish to see warnings use
27+
# Warnings are turned off by default. If you wish to see warnings use
2828
VERBOSE=1 bin/test
29+
# Run a single test like this: "bin/test my_test.rb test_name_regex", e.g.
30+
bin/test test/requests/diagnostics_expectations_test.rb test_diagnostics__def_bad_formatting
2931
```
3032

3133
## Expectation testing
@@ -46,7 +48,7 @@ We define expectations as `.exp` files, of which there are two variants:
4648
To add a new test scenario
4749

4850
1. Add a new fixture `my_fixture.rb` file under `test/fixtures`
49-
2. For applicable requests, add expectation files related to this fixutre. For example,
51+
2. For applicable requests, add expectation files related to this fixture. For example,
5052
`test/expectations/semantic_highlighting/my_fixture.exp.json`
5153

5254
To add a new expectations test runner for a new request handler:

lib/ruby_lsp/requests/support/rubocop_diagnostic.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ class RuboCopDiagnostic
1919
T::Hash[Symbol, Integer],
2020
)
2121

22+
# Cache cops to attach URLs to diagnostics. Only built-in cops for now.
23+
COP_TO_DOC_URL = T.let(
24+
RuboCop::Cop::Registry.global.to_h,
25+
T::Hash[String, [T.class_of(RuboCop::Cop::Base)]],
26+
)
27+
2228
sig { params(offense: RuboCop::Cop::Offense, uri: URI::Generic).void }
2329
def initialize(offense, uri)
2430
@offense = offense
@@ -52,10 +58,16 @@ def to_lsp_diagnostic
5258

5359
message += "\n\nThis offense is not auto-correctable.\n" unless @offense.correctable?
5460

61+
cop = COP_TO_DOC_URL[@offense.cop_name]&.first
62+
if cop&.documentation_url
63+
code_description = { href: cop.documentation_url }
64+
end
65+
5566
Interface::Diagnostic.new(
5667
message: message,
5768
source: "RuboCop",
5869
code: @offense.cop_name,
70+
code_description: code_description,
5971
severity: severity,
6072
range: Interface::Range.new(
6173
start: Interface::Position.new(

test/expectations/code_actions/def_bad_formatting.exp.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
}
6363
},
6464
"code": "Layout/EmptyLines",
65+
"codeDescription": {
66+
"href": "https://docs.rubocop.org/rubocop/cops_layout.html#layoutemptylines"
67+
},
6568
"severity": 3,
6669
"source": "RuboCop"
6770
},
@@ -111,6 +114,9 @@
111114
}
112115
},
113116
"code": "Layout/IndentationWidth",
117+
"codeDescription": {
118+
"href": "https://docs.rubocop.org/rubocop/cops_layout.html#layoutindentationwidth"
119+
},
114120
"severity": 3,
115121
"source": "RuboCop"
116122
}

test/expectations/diagnostics/def_bad_formatting.exp.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
},
6363
"severity": 3,
6464
"code": "Style/FrozenStringLiteralComment",
65+
"codeDescription": {
66+
"href": "https://docs.rubocop.org/rubocop/cops_style.html#stylefrozenstringliteralcomment"
67+
},
6568
"source": "RuboCop",
6669
"message": "Style/FrozenStringLiteralComment: Missing frozen string literal comment.",
6770
"data": {
@@ -111,6 +114,9 @@
111114
},
112115
"severity": 3,
113116
"code": "Layout/EmptyLinesAroundMethodBody",
117+
"codeDescription": {
118+
"href": "https://docs.rubocop.org/rubocop/cops_layout.html#layoutemptylinesaroundmethodbody"
119+
},
114120
"source": "RuboCop",
115121
"message": "Layout/EmptyLinesAroundMethodBody: Extra empty line detected at method body beginning.",
116122
"data": {
@@ -160,6 +166,9 @@
160166
},
161167
"severity": 3,
162168
"code": "Layout/EmptyLines",
169+
"codeDescription": {
170+
"href": "https://docs.rubocop.org/rubocop/cops_layout.html#layoutemptylines"
171+
},
163172
"source": "RuboCop",
164173
"message": "Layout/EmptyLines: Extra blank line detected.",
165174
"data": {
@@ -209,6 +218,9 @@
209218
},
210219
"severity": 3,
211220
"code": "Layout/IndentationWidth",
221+
"codeDescription": {
222+
"href": "https://docs.rubocop.org/rubocop/cops_layout.html#layoutindentationwidth"
223+
},
212224
"source": "RuboCop",
213225
"message": "Layout/IndentationWidth: Use 2 (not 0) spaces for indentation.",
214226
"data": {

test/expectations/diagnostics/if_inside_else.exp.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
},
4949
"severity": 3,
5050
"code": "Style/IfInsideElse",
51+
"codeDescription": {
52+
"href": "https://docs.rubocop.org/rubocop/cops_style.html#styleifinsideelse"
53+
},
5154
"source": "RuboCop",
5255
"message": "Style/IfInsideElse: Convert `if` nested inside `else` to `elsif`.",
5356
"data": {

test/requests/diagnostics_expectations_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def map_diagnostics(diagnostics)
5252
source: "RuboCop",
5353
code: diagnostic["code"],
5454
severity: diagnostic["severity"],
55+
code_description: diagnostic["codeDescription"],
5556
range: LanguageServer::Protocol::Interface::Range.new(
5657
start: LanguageServer::Protocol::Interface::Position.new(
5758
line: diagnostic["range"]["start"]["line"],

0 commit comments

Comments
 (0)