Sha256: f3ea73206218122530a2348de4f0a2b10668144168d77992c1884d5cd127af29

Contents?: true

Size: 1.85 KB

Versions: 30

Compression:

Stored size: 1.85 KB

Contents

module Steep
  module Diagnostic
    class LSPFormatter
      LSP = LanguageServer::Protocol

      attr_reader :config
      attr_reader :default_severity

      ERROR = :error
      WARNING = :warning
      INFORMATION = :information
      HINT = :hint

      def initialize(config = {}, default_severity: ERROR)
        @config = config
        @default_severity = default_severity

        config.each do |klass, severity|
          validate_severity(klass, severity)
          validate_class(klass)
        end
        validate_severity(:default, default_severity)
      end

      def validate_class(klass)
        unless klass < Diagnostic::Ruby::Base
          raise "Unexpected diagnostics class `#{klass}` given"
        end
      end

      def validate_severity(klass, severity)
        case severity
        when ERROR, WARNING, INFORMATION, HINT, nil
          # ok
        else
          raise "Unexpected severity `#{severity}` is specified for #{klass}"
        end
      end

      def format(diagnostic)
        severity = severity_for(diagnostic)

        if severity
          range = diagnostic.location&.as_lsp_range || raise("#{diagnostic.class} object (#{diagnostic.full_message}) instance must have `#location`")

          {
            message: diagnostic.full_message,
            code: diagnostic.diagnostic_code,
            severity: severity,
            range: range
          }
        end
      end

      def severity_for(diagnostic)
        case config.fetch(diagnostic.class, default_severity)
        when ERROR
          LSP::Constant::DiagnosticSeverity::ERROR
        when WARNING
          LSP::Constant::DiagnosticSeverity::WARNING
        when INFORMATION
          LSP::Constant::DiagnosticSeverity::INFORMATION
        when HINT
          LSP::Constant::DiagnosticSeverity::HINT
        when nil
          nil
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
steep-1.9.0.dev.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.9.0.dev.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.8.3 lib/steep/diagnostic/lsp_formatter.rb
steep-1.8.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.8.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.8.0 lib/steep/diagnostic/lsp_formatter.rb
steep-1.8.0.pre.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.8.0.pre.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.8.0.dev.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.8.0.dev.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.7.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.7.0 lib/steep/diagnostic/lsp_formatter.rb
steep-1.7.0.dev.3 lib/steep/diagnostic/lsp_formatter.rb
steep-1.7.0.dev.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.7.0.dev.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.6.0 lib/steep/diagnostic/lsp_formatter.rb
steep-1.6.0.pre.4 lib/steep/diagnostic/lsp_formatter.rb
steep-1.6.0.pre.3 lib/steep/diagnostic/lsp_formatter.rb
steep-1.6.0.pre.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.6.0.pre.1 lib/steep/diagnostic/lsp_formatter.rb