use lsp_types::TypeHierarchySubtypesRequest;
use lsp_types::{TypeHierarchyItem, TypeHierarchySubtypesParams};

use crate::server::api::traits::{
    BackgroundRequestHandler, RequestHandler, RetriableRequestHandler,
};
use crate::server::api::type_hierarchy::{TypeHierarchyKind, hierarchy_handler};
use crate::session::SessionSnapshot;
use crate::session::client::Client;

/// Handles a `typeHierarchy/subtypes` request.
///
/// Note that this implements the `BackgroundRequestHandler` because the
/// request might be for a symbol in a document that is not open in the current
/// session.
pub(crate) struct TypeHierarchySubtypesRequestHandler;

impl RequestHandler for TypeHierarchySubtypesRequestHandler {
    type RequestType = TypeHierarchySubtypesRequest;
}

impl BackgroundRequestHandler for TypeHierarchySubtypesRequestHandler {
    fn run(
        snapshot: &SessionSnapshot,
        _client: &Client,
        params: TypeHierarchySubtypesParams,
    ) -> crate::server::Result<Option<Vec<TypeHierarchyItem>>> {
        Ok(hierarchy_handler(
            snapshot,
            &params.item,
            TypeHierarchyKind::Subtypes,
        ))
    }
}

impl RetriableRequestHandler for TypeHierarchySubtypesRequestHandler {}
