Sha256: c3ba37038fd623a4f0b4da4be1ab7abc349361352293bebebeecfcaacbcddf07

Contents?: true

Size: 1.9 KB

Versions: 9

Compression:

Stored size: 1.9 KB

Contents

# typed: strict
# frozen_string_literal: true

return unless defined?(RubyLsp::Requests::Support::RuboCopRunner)

require "ruby_lsp/requests/support/rubocop_diagnostic"

module RubyLsp
  module Requests
    module Support
      class RuboCopFormatter
        extend T::Sig
        include Formatter

        sig { void }
        def initialize
          @diagnostic_runner = T.let(RuboCopRunner.new, RuboCopRunner)
          # -a is for "--auto-correct" (or "--autocorrect" on newer versions of RuboCop)
          @format_runner = T.let(RuboCopRunner.new("-a"), RuboCopRunner)
        end

        sig { override.params(uri: URI::Generic, document: RubyDocument).returns(T.nilable(String)) }
        def run_formatting(uri, document)
          filename = T.must(uri.to_standardized_path || uri.opaque)

          # Invoke RuboCop with just this file in `paths`
          @format_runner.run(filename, document.source)
          @format_runner.formatted_source
        end

        # RuboCop does not support range formatting
        sig { override.params(uri: URI::Generic, source: String, base_indentation: Integer).returns(T.nilable(String)) }
        def run_range_formatting(uri, source, base_indentation)
          nil
        end

        sig do
          override.params(
            uri: URI::Generic,
            document: RubyDocument,
          ).returns(T.nilable(T::Array[Interface::Diagnostic]))
        end
        def run_diagnostic(uri, document)
          filename = T.must(uri.to_standardized_path || uri.opaque)
          # Invoke RuboCop with just this file in `paths`
          @diagnostic_runner.run(filename, document.source)

          @diagnostic_runner.offenses.map do |offense|
            Support::RuboCopDiagnostic.new(
              document,
              offense,
              uri,
            ).to_lsp_diagnostic(@diagnostic_runner.config_for_working_directory)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby-lsp-0.22.1 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.22.0 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.21.3 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.21.2 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.21.1 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.21.0 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.20.1 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.20.0 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.19.1 lib/ruby_lsp/requests/support/rubocop_formatter.rb