Sha256: f8b3eb904b56e482997464a2b777b96f875fd1b4506ca7d8e0d86404652c9d49

Contents?: true

Size: 1.8 KB

Versions: 10

Compression:

Stored size: 1.8 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 or raise

          LSP::Interface::Diagnostic.new(
            message: diagnostic.full_message,
            code: diagnostic.diagnostic_code,
            severity: severity,
            range: 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

10 entries across 10 versions & 1 rubygems

Version Path
steep-1.4.0.dev.5 lib/steep/diagnostic/lsp_formatter.rb
steep-1.4.0.dev.4 lib/steep/diagnostic/lsp_formatter.rb
steep-1.4.0.dev.3 lib/steep/diagnostic/lsp_formatter.rb
steep-1.3.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.3.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.4.0.dev.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.4.0.dev.1 lib/steep/diagnostic/lsp_formatter.rb
steep-1.3.0 lib/steep/diagnostic/lsp_formatter.rb
steep-1.3.0.pre.2 lib/steep/diagnostic/lsp_formatter.rb
steep-1.3.0.pre.1 lib/steep/diagnostic/lsp_formatter.rb