Sha256: 2c455801f4d6000b021c6ad06e7f265a70c89c05065855484b8e0bc29fdc39b1

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

# typed: strict
# frozen_string_literal: true

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

require "singleton"

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

        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: Document).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

        sig do
          override.params(
            uri: URI::Generic,
            document: Document,
          ).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
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-lsp-0.16.3 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.16.2 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.16.1 lib/ruby_lsp/requests/support/rubocop_formatter.rb
ruby-lsp-0.16.0 lib/ruby_lsp/requests/support/rubocop_formatter.rb