use crate::{core::scope_tree::common::Scope, server::LspServer, utils::from_uri_to_escape_path_string};
use tower_lsp::lsp_types::*;

pub fn document_symbol(
    server: &LspServer,
    params: &DocumentSymbolParams
) -> Option<DocumentSymbolResponse> {
    let uri = &params.text_document.uri;

    let path_string = from_uri_to_escape_path_string(uri).unwrap();

    // 等待解析完成
    server.srcs.wait_parse_ready(&path_string, false);
    let source = server.srcs.get_source(&path_string)?;
    let source = source.read().ok()?;
    
    let scope_tree = server.srcs.scope_tree.read().ok()?;

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