Skip to content

Commit

Permalink
Support safe or unsafe autocorrect config for LSP
Browse files Browse the repository at this point in the history
This PR supports safe or unsafe autocorrect config for LSP.

The autocorrection is still safe by default (`rubocop -a`).

LSP client can switch to unsafe autocorrection (`rubocop -A`) by passing the following
`safeAutocorrect` parameter in the `initialize` request.

```json
{
  "jsonrpc": "2.0",
  "id": 42,
  "method": "initialize",
  "params": {
    "initializationOptions": {
      "safeAutocorrect": false
    }
  }
}
```

Some users may want to set unsafe autocorrection as the default for the LSP,
understanding the risks involved.

However, considering that the default should be safe, unsafe is an option.
  • Loading branch information
koic authored and bbatsov committed Jun 30, 2023
1 parent 94fac02 commit 7e8c47d
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/new_support_safe_autocorrect_config_for_lsp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12000](https://github.com/rubocop/rubocop/pull/12000): Support safe or unsafe autocorrect config for LSP. ([@koic][])
21 changes: 20 additions & 1 deletion docs/modules/ROOT/pages/usage/lsp.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,26 @@ See Eglot's official documentation for more information.

== Autocorrection

The language server supports `textDocument/formatting` method and is autocorrectable. The autocorrection is safe.
The language server supports `textDocument/formatting` method and is autocorrectable. The autocorrection is safe by default (`rubocop -a`).

LSP client can switch to unsafe autocorrection (`rubocop -A`) by passing the following `safeAutocorrect` parameter in the `initialize` request.

```json
{
"jsonrpc": "2.0",
"id": 42,
"method": "initialize",
"params": {
"initializationOptions": {
"safeAutocorrect": false
}
}
}
```

For detailed instructions on setting the parameter, please refer to the configuration methods of your LSP client.

NOTE: The `safeAutocorrect` parameter was introduced in RuboCop 1.54.

== Run as a Language Server

Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/lsp/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def for(name)
end

handle 'initialize' do |request|
@server.configure(safe_autocorrect: safe_autocorrect?(request))

@server.write(
id: request[:id],
result: LanguageServer::Protocol::Interface::InitializeResult.new(
Expand Down Expand Up @@ -162,6 +164,12 @@ def handle_method_missing(request)

private

def safe_autocorrect?(request)
safe_autocorrect = request.dig(:params, :initializationOptions, :safeAutocorrect)

safe_autocorrect.nil? || safe_autocorrect == true
end

def format_file(file_uri)
unless (text = @text_cache[file_uri])
Logger.log("Format request arrived before text synchronized; skipping: `#{file_uri}'")
Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/lsp/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ module Lsp
# Runtime for Language Server Protocol of RuboCop.
# @api private
class Runtime
attr_writer :safe_autocorrect

def initialize(config_store)
@config_store = config_store
@logged_paths = []
@safe_autocorrect = true
end

# This abuses the `--stdin` option of rubocop and reads the formatted text
Expand All @@ -32,7 +35,7 @@ def initialize(config_store)
# https://github.com/rubocop/rubocop/blob/v1.52.0/lib/rubocop/runner.rb#L72
def format(path, text)
formatting_options = {
stdin: text, force_exclusion: true, autocorrect: true, safe_autocorrect: true
stdin: text, force_exclusion: true, autocorrect: true, safe_autocorrect: @safe_autocorrect
}

redirect_stdout { run_rubocop(formatting_options, path) }
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def offenses(path, text)
@runtime.offenses(path, text)
end

def configure(safe_autocorrect: true)
@runtime.safe_autocorrect = safe_autocorrect
end

def stop(&block)
at_exit(&block) if block
exit
Expand Down
128 changes: 127 additions & 1 deletion spec/rubocop/lsp/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
end
end

describe 'format' do
describe 'format by default (safe autocorrect)' do
let(:requests) do
[
{
Expand Down Expand Up @@ -191,6 +191,132 @@
end
end

describe 'format with `safeAutocorrect: true`' do
let(:requests) do
[
{
jsonrpc: '2.0',
id: 2,
method: 'initialize',
params: {
probably: "Don't need real params for this test?",
initializationOptions: {
safeAutocorrect: true
}
}
},
{
jsonrpc: '2.0',
method: 'textDocument/didOpen',
params: {
textDocument: {
languageId: 'ruby',
text: "puts 'hi'",
uri: 'file:///path/to/file.rb',
version: 0
}
}
}, {
jsonrpc: '2.0',
method: 'textDocument/didChange',
params: {
contentChanges: [{ text: "puts 'bye'" }],
textDocument: {
uri: 'file:///path/to/file.rb',
version: 10
}
}
}, {
jsonrpc: '2.0',
id: 20,
method: 'textDocument/formatting',
params: {
options: { insertSpaces: true, tabSize: 2 },
textDocument: { uri: 'file:///path/to/file.rb' }
}
}
]
end

it 'handles requests' do
expect(stderr).to eq('')
format_result = messages.last
expect(format_result).to eq(
jsonrpc: '2.0',
id: 20,
result: [
newText: "puts 'bye'\n",
range: {
start: { line: 0, character: 0 }, end: { line: 1, character: 0 }
}
]
)
end
end

describe 'format with `safeAutocorrect: false`' do
let(:requests) do
[
{
jsonrpc: '2.0',
id: 2,
method: 'initialize',
params: {
probably: "Don't need real params for this test?",
initializationOptions: {
safeAutocorrect: false
}
}
},
{
jsonrpc: '2.0',
method: 'textDocument/didOpen',
params: {
textDocument: {
languageId: 'ruby',
text: "puts 'hi'",
uri: 'file:///path/to/file.rb',
version: 0
}
}
}, {
jsonrpc: '2.0',
method: 'textDocument/didChange',
params: {
contentChanges: [{ text: "puts 'bye'" }],
textDocument: {
uri: 'file:///path/to/file.rb',
version: 10
}
}
}, {
jsonrpc: '2.0',
id: 20,
method: 'textDocument/formatting',
params: {
options: { insertSpaces: true, tabSize: 2 },
textDocument: { uri: 'file:///path/to/file.rb' }
}
}
]
end

it 'handles requests' do
expect(stderr).to eq('')
format_result = messages.last
expect(format_result).to eq(
jsonrpc: '2.0',
id: 20,
result: [
newText: "# frozen_string_literal: true\n\nputs 'bye'\n",
range: {
start: { line: 0, character: 0 }, end: { line: 1, character: 0 }
}
]
)
end
end

describe 'no op commands' do
let(:requests) do
[
Expand Down

0 comments on commit 7e8c47d

Please sign in to comment.