Sha256: 0fae776ac4cacc1f5b3e1f14243f17d5ba083e82e436ea67466db9e445a36416

Contents?: true

Size: 1.77 KB

Versions: 20

Compression:

Stored size: 1.77 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
          LSP::Interface::Diagnostic.new(
            message: diagnostic.full_message,
            code: diagnostic.diagnostic_code,
            severity: severity,
            range: diagnostic.location.as_lsp_range
          ).to_hash
        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

20 entries across 20 versions & 1 rubygems

Version Path
steep-1.2.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.2.0 lib/steep/diagnostic/lsp_formatter.rb
steep-1.2.0.pre.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.1.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.1.0 lib/steep/diagnostic/lsp_formatter.rb
steep-1.1.0.pre.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.0.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.0.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.0.0 lib/steep/diagnostic/lsp_formatter.rb
steep-0.52.2 lib/steep/diagnostic/lsp_formatter.rb
steep-0.52.1 lib/steep/diagnostic/lsp_formatter.rb
steep-0.52.0 lib/steep/diagnostic/lsp_formatter.rb
steep-0.51.0 lib/steep/diagnostic/lsp_formatter.rb
steep-0.50.0 lib/steep/diagnostic/lsp_formatter.rb
steep-0.49.1 lib/steep/diagnostic/lsp_formatter.rb
steep-0.49.0 lib/steep/diagnostic/lsp_formatter.rb
steep-0.48.0 lib/steep/diagnostic/lsp_formatter.rb
steep-0.47.1 lib/steep/diagnostic/lsp_formatter.rb
steep-0.47.0 lib/steep/diagnostic/lsp_formatter.rb
steep-0.46.0 lib/steep/diagnostic/lsp_formatter.rb