solargraph icon indicating copy to clipboard operation
solargraph copied to clipboard

Question: Change formatter with plugin?

Open will opened this issue 4 years ago • 3 comments

Hi, I'm looking through https://solargraph.org/guides/plugins and I see I can modify the reporters, which is cool. I'm looking to also change the formatter, but don’t see it explicitly called out as an option. Is it possible? I mean I imagine I could monkey patch anything to work, but more like asking if it's supported.

will avatar Jul 26 '21 21:07 will

I've gone the monkey patching route for now, posting the solution here just in case it's useful to find what spots would be good for hooks:

#!/usr/bin/env ruby

ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
require "bundler/setup"

require "solargraph"
require "standard"

class Solargraph::LanguageServer::Message::TextDocument::Formatting
  def process
    file_uri = params["textDocument"]["uri"]
    config = config_for(file_uri)
    original = host.read_text(file_uri)
    args = cli_args(file_uri, config)

    # require_rubocop(config['version'])
    # options, paths = RuboCop::Options.new.parse(args)
    std = Standard::BuildsConfig.new.call(args)
    options = std.rubocop_options
    options[:stdin] = original
    corrections = redirect_stdout do
      # RuboCop::Runner.new(options, RuboCop::ConfigStore.new).run(paths)
      RuboCop::Runner.new(options, std.rubocop_config_store).run(std.paths)
    end
    result = options[:stdin]

    log_corrections(corrections)

    format original, result
  rescue RuboCop::ValidationError, RuboCop::ConfigNotFoundError => e
    set_error(Solargraph::LanguageServer::ErrorCodes::INTERNAL_ERROR, "[#{e.class}] #{e.message}")
  end

  def formatter_class(_config)
    Standard::Formatter
  end
end

Solargraph::Shell.start(ARGV)

Then I have nvim start this as the lsp instead of the one provided by the gem.

will avatar Jul 27 '21 02:07 will

Setting formatters from plugins is on the roadmap. Thanks for the example implementation. This feature is long overdue, so I'll try to get it into the next minor release.

castwide avatar Jul 27 '21 14:07 castwide

@will using that as a binstub worked great for me — thanks!

sheck avatar Dec 07 '21 17:12 sheck