Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RuboCop requests with separate runners #183

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/ruby_lsp/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module Requests
autoload :FoldingRanges, "ruby_lsp/requests/folding_ranges"
autoload :SelectionRanges, "ruby_lsp/requests/selection_ranges"
autoload :SemanticHighlighting, "ruby_lsp/requests/semantic_highlighting"
autoload :RuboCopRequest, "ruby_lsp/requests/rubocop_request"
autoload :Formatting, "ruby_lsp/requests/formatting"
autoload :Diagnostics, "ruby_lsp/requests/diagnostics"
autoload :CodeActions, "ruby_lsp/requests/code_actions"
Expand All @@ -30,6 +29,8 @@ module Support
autoload :SelectionRange, "ruby_lsp/requests/support/selection_range"
autoload :SemanticTokenEncoder, "ruby_lsp/requests/support/semantic_token_encoder"
autoload :SyntaxErrorDiagnostic, "ruby_lsp/requests/support/syntax_error_diagnostic"
autoload :RuboCopDiagnosticsRunner, "ruby_lsp/requests/support/rubocop_diagnostics_runner"
autoload :RuboCopFormattingRunner, "ruby_lsp/requests/support/rubocop_formatting_runner"
end
end
end
18 changes: 9 additions & 9 deletions lib/ruby_lsp/requests/diagnostics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ module Requests
# puts "Hello" # --> diagnostics: incorrect indentantion
# end
# ```
class Diagnostics < RuboCopRequest
class Diagnostics < BaseRequest
extend T::Sig

sig { params(uri: String, document: Document).void }
def initialize(uri, document)
super(document)

@uri = uri
end

sig do
override.returns(
T.any(
Expand All @@ -30,14 +37,7 @@ class Diagnostics < RuboCopRequest
def run
return syntax_error_diagnostics if @document.syntax_errors?

super

@diagnostics
end

sig { params(_file: String, offenses: T::Array[RuboCop::Cop::Offense]).void }
def file_finished(_file, offenses)
@diagnostics = offenses.map { |offense| Support::RuboCopDiagnostic.new(offense, @uri) }
Support::RuboCopDiagnosticsRunner.instance.run(@uri, @document)
end

private
Expand Down
29 changes: 9 additions & 20 deletions lib/ruby_lsp/requests/formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,33 @@ module Requests
# puts "Hello" # --> formatting: fixes the indentation on save
# end
# ```
class Formatting < RuboCopRequest
class Formatting < BaseRequest
extend T::Sig

RUBOCOP_FLAGS = T.let((COMMON_RUBOCOP_FLAGS + ["--auto-correct"]).freeze, T::Array[String])

sig { params(uri: String, document: Document).void }
def initialize(uri, document)
super
@formatted_text = T.let(nil, T.nilable(String))
super(document)

@uri = uri
end

sig { override.returns(T.nilable(T.all(T::Array[LanguageServer::Protocol::Interface::TextEdit], Object))) }
def run
super
formatted_text = Support::RuboCopFormattingRunner.instance.run(@uri, @document)
return unless formatted_text

@formatted_text = @options[:stdin] # Rubocop applies the corrections on stdin
return unless @formatted_text
size = @document.source.size

[
LanguageServer::Protocol::Interface::TextEdit.new(
range: LanguageServer::Protocol::Interface::Range.new(
start: LanguageServer::Protocol::Interface::Position.new(line: 0, character: 0),
end: LanguageServer::Protocol::Interface::Position.new(
line: text.size,
character: text.size
)
end: LanguageServer::Protocol::Interface::Position.new(line: size, character: size)
),
new_text: @formatted_text
new_text: formatted_text
),
]
end

private

sig { returns(T::Array[String]) }
def rubocop_flags
RUBOCOP_FLAGS
end
end
end
end
61 changes: 0 additions & 61 deletions lib/ruby_lsp/requests/rubocop_request.rb

This file was deleted.

55 changes: 55 additions & 0 deletions lib/ruby_lsp/requests/support/rubocop_diagnostics_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# typed: strict
# frozen_string_literal: true

require "rubocop"
require "cgi"
require "singleton"

module RubyLsp
module Requests
module Support
# :nodoc:
class RuboCopDiagnosticsRunner < RuboCop::Runner
extend T::Sig
include Singleton

sig { void }
def initialize
@options = T.let({}, T::Hash[Symbol, T.untyped])
@uri = T.let(nil, T.nilable(String))
@diagnostics = T.let([], T::Array[Support::RuboCopDiagnostic])

super(
::RuboCop::Options.new.parse([
"--stderr", # Print any output to stderr so that our stdout does not get polluted
"--force-exclusion",
"--format",
"RuboCop::Formatter::BaseFormatter", # Suppress any output by using the base formatter
]).first,
::RuboCop::ConfigStore.new
)
end

sig { params(uri: String, document: Document).returns(T::Array[Support::RuboCopDiagnostic]) }
def run(uri, document)
@uri = uri

file = CGI.unescape(URI.parse(uri).path)
# We communicate with Rubocop via stdin
@options[:stdin] = document.source

# Invoke RuboCop with just this file in `paths`
process_file(file)
@diagnostics
end

private

sig { params(_file: String, offenses: T::Array[RuboCop::Cop::Offense]).void }
def file_finished(_file, offenses)
@diagnostics = offenses.map { |offense| Support::RuboCopDiagnostic.new(offense, T.must(@uri)) }
end
end
end
end
end
45 changes: 45 additions & 0 deletions lib/ruby_lsp/requests/support/rubocop_formatting_runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# typed: strict
# frozen_string_literal: true

require "rubocop"
require "cgi"
require "singleton"

module RubyLsp
module Requests
module Support
# :nodoc:
class RuboCopFormattingRunner < RuboCop::Runner
extend T::Sig
include Singleton

sig { void }
def initialize
@options = T.let({}, T::Hash[Symbol, T.untyped])

super(
::RuboCop::Options.new.parse([
"--stderr", # Print any output to stderr so that our stdout does not get polluted
"--force-exclusion",
"--format",
"RuboCop::Formatter::BaseFormatter", # Suppress any output by using the base formatter
"-a", # --auto-correct
]).first,
::RuboCop::ConfigStore.new
)
end

sig { params(uri: String, document: Document).returns(T.nilable(String)) }
def run(uri, document)
file = CGI.unescape(URI.parse(uri).path)
# We communicate with Rubocop via stdin
@options[:stdin] = document.source

# Invoke RuboCop with just this file in `paths`
process_file(file)
@options[:stdin]
end
end
end
end
end
5 changes: 4 additions & 1 deletion rakelib/check_docs.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ task :check_docs do
require "syntax_tree"
require "logger"
require "ruby_lsp/requests/base_request"
require "ruby_lsp/requests/rubocop_request"
require "ruby_lsp/requests/support/rubocop_diagnostics_runner"
require "ruby_lsp/requests/support/rubocop_formatting_runner"

request_doc_files = Dir["#{Dir.pwd}/lib/ruby_lsp/requests/*.rb"]
request_doc_files << "#{Dir.pwd}/lib/ruby_lsp/requests.rb"
Expand All @@ -22,6 +23,8 @@ task :check_docs do
error_messages = RubyLsp::Requests
.constants # rubocop:disable Sorbet/ConstantsFromStrings
.each_with_object(Hash.new { |h, k| h[k] = [] }) do |request, errors|
next if request == :Support

full_name = "RubyLsp::Requests::#{request}"
docs = YARD::Registry.at(full_name).docstring
next if /:nodoc:/.match?(docs)
Expand Down