实现 sync fast

This commit is contained in:
锦恢 2024-11-17 16:25:27 +08:00
parent 83df59917f
commit 230ef10f59
2 changed files with 64 additions and 2 deletions

View File

@ -4,6 +4,7 @@ use request::{
test::CustomParamRequest, test::CustomParamRequest,
test::CustomRequest, test::CustomRequest,
fast::DoFastApi, fast::DoFastApi,
fast::SyncFastApi,
config::UpdateConfigurationApi, config::UpdateConfigurationApi,
primitives::DoPrimitivesJudgeApi primitives::DoPrimitivesJudgeApi
}; };
@ -54,7 +55,8 @@ async fn main() {
.custom_method("custom/paramRequest", CustomParamRequest) // for test .custom_method("custom/paramRequest", CustomParamRequest) // for test
.custom_method("api/fast", DoFastApi) .custom_method("api/fast", DoFastApi)
.custom_method("api/do-primitives-judge", DoPrimitivesJudgeApi) .custom_method("api/do-primitives-judge", DoPrimitivesJudgeApi)
.custom_method("api/update-fast", UpdateConfigurationApi) .custom_method("api/update-configuration", UpdateConfigurationApi)
.custom_method("api/sync-fast", SyncFastApi)
.finish(); .finish();
Server::new(stdin, stdout, socket) Server::new(stdin, stdout, socket)

View File

@ -33,7 +33,6 @@ pub struct DoFastApi;
impl <'a>tower_lsp::jsonrpc::Method<&'a Arc<Backend>, (DoFastApiRequestParams, ), Result<FastHdlparam>> for DoFastApi { impl <'a>tower_lsp::jsonrpc::Method<&'a Arc<Backend>, (DoFastApiRequestParams, ), Result<FastHdlparam>> for DoFastApi {
type Future = future::Ready<Result<FastHdlparam>>; type Future = future::Ready<Result<FastHdlparam>>;
fn invoke(&self, _server: &'a Arc<Backend>, _params: (DoFastApiRequestParams, )) -> Self::Future { fn invoke(&self, _server: &'a Arc<Backend>, _params: (DoFastApiRequestParams, )) -> Self::Future {
let request_param = _params.0; let request_param = _params.0;
let path = request_param.path; let path = request_param.path;
@ -44,6 +43,30 @@ impl <'a>tower_lsp::jsonrpc::Method<&'a Arc<Backend>, (DoFastApiRequestParams, )
} }
} }
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SyncFastApiRequestParams {
path: String,
file_type: String,
tool_chain: String
}
#[derive(Clone)]
pub struct SyncFastApi;
impl <'a>tower_lsp::jsonrpc::Method<&'a Arc<Backend>, (SyncFastApiRequestParams, ), Result<FastHdlparam>> for SyncFastApi {
type Future = future::Ready<Result<FastHdlparam>>;
fn invoke(&self, _server: &'a Arc<Backend>, _params: (SyncFastApiRequestParams, )) -> Self::Future {
let request_param = _params.0;
let path = request_param.path;
let file_type = request_param.file_type;
let tool_chain = request_param.tool_chain;
let hdlparam = sync_fast(path, file_type, tool_chain, _server);
future::ready(hdlparam)
}
}
fn make_textdocumenitem_from_path(path_buf: &PathBuf) -> Option<TextDocumentItem> { fn make_textdocumenitem_from_path(path_buf: &PathBuf) -> Option<TextDocumentItem> {
if let Ok(url) = Url::from_file_path(path_buf) { if let Ok(url) = Url::from_file_path(path_buf) {
if let Ok(text) = fs::read_to_string(path_buf) { if let Ok(text) = fs::read_to_string(path_buf) {
@ -141,6 +164,7 @@ fn do_sv_fast(
) -> Result<FastHdlparam> { ) -> Result<FastHdlparam> {
let path_buf = PathBuf::from(&path); let path_buf = PathBuf::from(&path);
// 从 pathbuf 中读取文本并作为 TextDocumentItem 打开
let doc = match make_textdocumenitem_from_path(&path_buf) { let doc = match make_textdocumenitem_from_path(&path_buf) {
Some(doc) => doc, Some(doc) => doc,
None => { None => {
@ -269,3 +293,39 @@ fn do_vhdl_fast(
data: None data: None
}) })
} }
pub fn sync_fast(
path: String,
file_type: String,
tool_chain: String,
backend: &Arc<Backend>
) -> Result<FastHdlparam> {
// 根据 file_type 和 tool_chain 计算正确的 path
let path = {
if file_type == "ip" && tool_chain == "xilinx" {
let pathbuf = PathBuf::from_str(&path).unwrap();
let basename = pathbuf.file_name().unwrap().to_str().unwrap();
format!("{}/synth/{}.vhd", path, basename)
} else {
path
}
};
{
let uri = Url::from_file_path(path.to_string()).unwrap();
let file_id = backend.server.srcs.get_id(&uri);
if let Some(file) = backend.server.srcs.get_file(file_id) {
let _ = file.read().unwrap();
}
}
let hdl_param = backend.server.srcs.hdl_param.clone();
let path_to_hdl_file = hdl_param.path_to_hdl_file.read().unwrap();
if let Some(hdl_file) = path_to_hdl_file.get(&path) {
let fast = hdl_file.fast.clone();
Ok(fast)
} else {
do_fast(path, file_type, tool_chain, backend)
}
}