use crate::{definition::Scope, server::LSPServer};
use tower_lsp::lsp_types::*;

pub fn document_symbol(
    server: &LSPServer,
    params: &DocumentSymbolParams
) -> Option<DocumentSymbolResponse> {
    let uri = &params.text_document.uri;
    let file_id = server.srcs.get_id(uri).to_owned();
    server.srcs.wait_parse_ready(file_id, false);
    let file = server.srcs.get_file(file_id)?;
    let file = file.read().ok()?;
    let scope_tree = server.srcs.scope_tree.read().ok()?;

    Some(DocumentSymbolResponse::Nested(
        scope_tree.as_ref()?.document_symbols(uri, &file.text),
    ))
}