use crate::{server::LspServer, utils::{from_uri_to_escape_path_string, get_definition_token, get_language_id_by_uri}};
use tower_lsp::lsp_types::*;

mod sv;
mod vhdl;

impl LspServer {
    pub fn document_highlight(
        &self,
        params: DocumentHighlightParams,
    ) -> Option<Vec<DocumentHighlight>> {
        let uri = &params.text_document_position_params.text_document.uri;
        let pos = params.text_document_position_params.position;

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

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

        let line_text = source.text.line(pos.line as usize);
        let token = get_definition_token(&line_text, pos);

        let language_id = get_language_id_by_uri(&uri);
        
        match language_id.as_str() {
            // "vhdl" => vhdl::document_highlight(
            //     self,
            //     &token,
            //     &file,
            //     pos,
            //     &uri
            // ),
            
            "verilog" | "systemverilog" => sv::document_highlight(
                self,
                &token,
                &source,
                pos,
                &uri
            ),
            _ => None
        }
    }
}