Sha256: 76649bbb342297c944adc115e6bbb2939b94bf62e9c8b510bbd907cd79e331ee

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# typed: false
# frozen_string_literal: true

require "open3"

module RubyLsp
  module RubyLspRubyfmt
    class Addon < RubyLsp::Addon
      def name
        "Ruby Fmt Formatter"
      end

      def activate
        # The first argument is an identifier users can pick to select this formatter. To use this formatter, users must
        # have rubyLsp.formatter configured to "rubyfmt"
        # The second argument is a singleton instance that implements the `FormatterRunner` interface (see below)
        RubyLsp::Requests::Formatting.register_formatter("rubyfmt", RubyFmtFormatterRunner.instance)
      end

      def deactivate
      end
    end

    # Custom formatting runner
    class RubyFmtFormatterRunner
      # Make it a singleton class
      include Singleton
      # If using Sorbet to develop the addon, then include this interface to make sure the class is properly implemented
      include RubyLsp::Requests::Support::FormatterRunner

      # Use the initialize method to perform any sort of ahead of time work.
      # For example, reading configurations for yourformatter since they are unlikely to change between requests
      def initialize
      end

      # The main part of the interface is implementing the run method.
      # It receives the URI and the document being formatted.
      # IMPORTANT: This method must return the formatted document source without mutating the original one in document
      def run(uri, document)
        output, _status = Open3.capture2("rubyfmt", stdin_data: document.source)
        output
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-lsp-rubyfmt-0.1.0 lib/ruby_lsp/ruby-lsp-rubyfmt/addon.rb