Sha256: 76b05e57a2870d8061f75e031c8193b5a3d966e589090909e1227d00af04cdfa

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 KB

Contents

require_relative "../runners/rubocop"
require "tempfile"

module Standard
  module LSP
    class Standardizer
      def initialize(config)
        @template_options = config
        @runner = Standard::Runners::Rubocop.new
      end

      def format(text)
        run_standard(text, format: true)
      end

      def offenses(text)
        results = run_standard(text, format: false)
        JSON.parse(results, symbolize_names: true).dig(:files, 0, :offenses)
      end

      private

      BASENAME = ["source", ".rb"].freeze
      def run_standard(text, format:)
        Tempfile.open(BASENAME) do |temp|
          temp.write(text)
          temp.flush
          stdout = capture_rubocop_stdout(make_config(temp.path, format))
          format ? File.read(temp.path) : stdout
        end
      end

      def make_config(file, format)
        # Can't make frozen versions of this hash because RuboCop mutates it
        o = if format
          {autocorrect: true, formatters: [["Standard::Formatter", nil]], parallel: true, todo_file: nil, todo_ignore_files: [], safe_autocorrect: true}
        else
          {autocorrect: false, formatters: [["json"]], parallel: true, todo_file: nil, todo_ignore_files: [], format: "json"}
        end
        Standard::Config.new(@template_options.runner, [file], o, @template_options.rubocop_config_store)
      end

      def capture_rubocop_stdout(config)
        redir = StringIO.new
        $stdout = redir
        @runner.call(config)
        redir.string
      ensure
        $stdout = STDOUT
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
standard-1.23.0 lib/standard/lsp/standardizer.rb
standard-1.22.1 lib/standard/lsp/standardizer.rb
standard-1.22.0 lib/standard/lsp/standardizer.rb
standard-1.21.1 lib/standard/lsp/standardizer.rb
standard-1.21.0 lib/standard/lsp/standardizer.rb
standard-1.20.0 lib/standard/lsp/standardizer.rb
standard-1.19.1 lib/standard/lsp/standardizer.rb
standard-1.19.0 lib/standard/lsp/standardizer.rb