Skip to content

Commit

Permalink
LSP mode configurable from external source
Browse files Browse the repository at this point in the history
The original LSP mode determination relied on the `--lsp` command-line argument,
making it hard to set the mode from 3rd party LSP(-like) tools.
This PR refines the API to resolve this issue. Additionally, as a side effect,
it improves the mocking part of spec.

However, the APIs are still experimental and not yet mature.
  • Loading branch information
koic authored and bbatsov committed Jan 24, 2024
1 parent 8a000a3 commit b4080d8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
24 changes: 18 additions & 6 deletions lib/rubocop/cop/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ def self.support_autocorrect?
false
end

### LSP

# This experimental feature has been under consideration for a while.
# @api private
def self.lsp_mode?
!!@lsp_mode
end

# This experimental feature has been under consideration for a while.
def self.enable_lsp_mode
@lsp_mode = true
end

# This experimental feature has been under consideration for a while.
def self.disable_lsp_mode
@lsp_mode = false
end

### Naming

def self.badge
Expand Down Expand Up @@ -481,12 +499,6 @@ def range_for_original(range)
range.end_pos + @current_offset
)
end

# This experimental feature has been under consideration for a while.
# @api private
def lsp_mode?
ARGV.include?('--lsp')
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def on_other_file
private

def add_offense_from_diagnostic(diagnostic, ruby_version)
message = if lsp_mode?
message = if Base.lsp_mode?
diagnostic.message
else
"#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module Lsp
# @api private
class Server
def initialize(config_store)
RuboCop::Cop::Base.enable_lsp_mode

@reader = LanguageServer::Protocol::Transport::Io::Reader.new($stdin)
@writer = LanguageServer::Protocol::Transport::Io::Writer.new($stdout)
@runtime = RuboCop::Lsp::Runtime.new(config_store)
Expand Down
6 changes: 5 additions & 1 deletion lib/rubocop/rspec/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ def source_range(range, buffer: source_buffer)

RSpec.shared_context 'lsp mode' do
before do
allow(cop).to receive(:lsp_mode?).and_return(true)
RuboCop::Cop::Base.enable_lsp_mode
end

after do
RuboCop::Cop::Base.disable_lsp_mode
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/rubocop/lsp/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

subject(:result) { run_server_on_requests(*requests) }

after do
RuboCop::Cop::Base.disable_lsp_mode
end

let(:messages) { result[0] }
let(:stderr) { result[1].string }

Expand Down

0 comments on commit b4080d8

Please sign in to comment.