class Yoda::Server::LifecycleHandler

Handle

Attributes

notifier[R]

@return [Notifier]

session[R]

@return [Session, nil]

Public Class Methods

new(root_handler) click to toggle source
# File lib/yoda/server/lifecycle_handler.rb, line 13
def initialize(root_handler)
  @root_handler = root_handler
  @notifier = root_handler.notifier
end

Public Instance Methods

handle(method:, params:) click to toggle source

@param method [Symbol] @param params [Object]

# File lib/yoda/server/lifecycle_handler.rb, line 26
def handle(method:, params:)
  lifecycle_handlers[method].call(params)
end
handle?(method) click to toggle source

@param method [Symbol] @return [true, false]

# File lib/yoda/server/lifecycle_handler.rb, line 20
def handle?(method)
  lifecycle_handlers.key?(method)
end

Private Instance Methods

core_import_warnings(core_import_errors) click to toggle source

@param gem_import_errors [Array<GemImportError>] @return [String, nil]

# File lib/yoda/server/lifecycle_handler.rb, line 134
      def core_import_warnings(core_import_errors)
        return if core_import_errors.empty?

        <<~EOS
        Failed to import some core libraries (Ruby version: #{RUBY_VERSION}).
        Please execute `yoda setup` with Ruby version #{RUBY_VERSION}.
        EOS
      end
gem_import_warnings(gem_import_errors) click to toggle source

@param gem_import_errors [Array<GemImportError>] @return [String, nil]

# File lib/yoda/server/lifecycle_handler.rb, line 121
      def gem_import_warnings(gem_import_errors)
        return if gem_import_errors.empty?
        warnings = gem_import_errors.map { |error| "- #{error.name} (#{error.version})" }

        <<~EOS
        Failed to import some gems.
        Please check these gems are installed for Ruby version #{RUBY_VERSION}.
        #{warnings.join("\n")}
        EOS
      end
handle_cancel(params) click to toggle source
# File lib/yoda/server/lifecycle_handler.rb, line 82
def handle_cancel(params)
  @root_handler.cancel_request(params[:id])

  NO_RESPONSE
end
handle_exit(_params) click to toggle source
# File lib/yoda/server/lifecycle_handler.rb, line 88
def handle_exit(_params)
  NO_RESPONSE
end
handle_initialize(params) click to toggle source
# File lib/yoda/server/lifecycle_handler.rb, line 42
def handle_initialize(params)
  Instrument.instance.hear(initialization_progress: method(:notify_initialization_progress)) do
    @session = Session.new(params[:root_uri])
    send_warnings(@session.setup || [])

    LanguageServer::Protocol::Interface::InitializeResult.new(
      capabilities: LanguageServer::Protocol::Interface::ServerCapabilities.new(
        text_document_sync: LanguageServer::Protocol::Interface::TextDocumentSyncOptions.new(
          change: LanguageServer::Protocol::Constant::TextDocumentSyncKind::FULL,
          save: LanguageServer::Protocol::Interface::SaveOptions.new(
            include_text: true,
          ),
        ),
        completion_provider: LanguageServer::Protocol::Interface::CompletionOptions.new(
          resolve_provider: false,
          trigger_characters: ['.', '@', '[', ':', '!', '<'],
        ),
        hover_provider: true,
        definition_provider: true,
        signature_help_provider: LanguageServer::Protocol::Interface::SignatureHelpOptions.new(
          trigger_characters: ['(', ','],
        ),
      ),
    )
  end
rescue => e
  LanguageServer::Protocol::Interface::ResponseError.new(
    message: "Failed to initialize yoda: #{e.class} #{e.message}",
    code: LanguageServer::Protocol::Constant::ErrorCodes::SERVER_ERROR_START,
    data: LanguageServer::Protocol::Interface::InitializeError.new(retry: false),
  )
end
handle_initialized(_params) click to toggle source
# File lib/yoda/server/lifecycle_handler.rb, line 75
def handle_initialized(_params)
  NO_RESPONSE
end
handle_shutdown(_params) click to toggle source
# File lib/yoda/server/lifecycle_handler.rb, line 79
def handle_shutdown(_params)
end
lifecycle_handlers() click to toggle source
# File lib/yoda/server/lifecycle_handler.rb, line 32
def lifecycle_handlers
  @lifecycle_handlers ||= {
    initialize: method(:handle_initialize),
    initialized: method(:handle_initialized),
    shutdown: method(:handle_shutdown),
    exit: method(:handle_exit),
    '$/cancelRequest': method(:handle_cancel),
  }
end
notify_initialization_progress(phase: nil, message: nil, **params) click to toggle source
# File lib/yoda/server/lifecycle_handler.rb, line 143
def notify_initialization_progress(phase: nil, message: nil, **params)
  notifier.event(type: :initialization, phase: phase, message: message)
end
send_warnings(errors) click to toggle source

@param errors [Array<BaseError>] @return [Array<Object>]

# File lib/yoda/server/lifecycle_handler.rb, line 94
def send_warnings(errors)
  return [] if errors.empty?
  gem_import_errors = errors.select { |error| error.is_a?(GemImportError) }
  core_import_errors = errors.select { |error| error.is_a?(CoreImportError) }

  notifier.show_message(
    type: :warning,
    message: "Failed to load some libraries (Please check console for details)",
  )

  if gem_message = gem_import_warnings(gem_import_errors)
    notifier.log_message(
      type: :warning,
      message: gem_message,
    )
  end

  if core_message = core_import_warnings(core_import_errors)
    notifier.log_message(
      type: :warning,
      message: core_message,
    )
  end
end