From 302f2079515cfed4e15bcf6850b70424423831f3 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 27 Sep 2024 02:19:01 +0800 Subject: [PATCH] fix(repo-map): do not report errors when encountering unsupported languages (#652) --- crates/avante-repo-map/src/lib.rs | 47 ++++++++++++++++++++++--------- lua/avante/repo_map.lua | 8 ++++++ 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/crates/avante-repo-map/src/lib.rs b/crates/avante-repo-map/src/lib.rs index 86dd4749..ced43e30 100644 --- a/crates/avante-repo-map/src/lib.rs +++ b/crates/avante-repo-map/src/lib.rs @@ -40,18 +40,18 @@ pub enum Definition { Variable(Variable), } -fn get_ts_language(language: &str) -> Result { +fn get_ts_language(language: &str) -> Option { match language { - "rust" => Ok(tree_sitter_rust::LANGUAGE), - "python" => Ok(tree_sitter_python::LANGUAGE), - "javascript" => Ok(tree_sitter_javascript::LANGUAGE), - "typescript" => Ok(tree_sitter_typescript::LANGUAGE_TSX), - "go" => Ok(tree_sitter_go::LANGUAGE), - "c" => Ok(tree_sitter_c::LANGUAGE), - "cpp" => Ok(tree_sitter_cpp::LANGUAGE), - "lua" => Ok(tree_sitter_lua::LANGUAGE), - "ruby" => Ok(tree_sitter_ruby::LANGUAGE), - _ => Err(format!("Unsupported language: {language}")), + "rust" => Some(tree_sitter_rust::LANGUAGE), + "python" => Some(tree_sitter_python::LANGUAGE), + "javascript" => Some(tree_sitter_javascript::LANGUAGE), + "typescript" => Some(tree_sitter_typescript::LANGUAGE_TSX), + "go" => Some(tree_sitter_go::LANGUAGE), + "c" => Some(tree_sitter_c::LANGUAGE), + "cpp" => Some(tree_sitter_cpp::LANGUAGE), + "lua" => Some(tree_sitter_lua::LANGUAGE), + "ruby" => Some(tree_sitter_ruby::LANGUAGE), + _ => None, } } @@ -66,7 +66,11 @@ const TYPESCRIPT_QUERY: &str = include_str!("../queries/tree-sitter-typescript-d const RUBY_QUERY: &str = include_str!("../queries/tree-sitter-ruby-defs.scm"); fn get_definitions_query(language: &str) -> Result { - let ts_language = get_ts_language(language)?; + let ts_language = get_ts_language(language); + if ts_language.is_none() { + return Err(format!("Unsupported language: {language}")); + } + let ts_language = ts_language.unwrap(); let contents = match language { "c" => C_QUERY, "cpp" => CPP_QUERY, @@ -148,7 +152,13 @@ fn is_first_letter_uppercase(name: &str) -> bool { // Given a language, parse the given source code and return exported definitions fn extract_definitions(language: &str, source: &str) -> Result, String> { - let ts_language = get_ts_language(language)?; + let ts_language = get_ts_language(language); + + if ts_language.is_none() { + return Ok(vec![]); + } + + let ts_language = ts_language.unwrap(); let mut definitions = Vec::new(); let mut parser = Parser::new(); @@ -891,4 +901,15 @@ mod tests { let expected = "var test_var;func test_func(a, b) -> void;"; assert_eq!(stringified, expected); } + + #[test] + fn test_unsupported_language() { + let source = "print('Hello, world!')"; + let definitions = extract_definitions("unknown", source).unwrap(); + + let stringified = stringify_definitions(&definitions); + println!("{stringified}"); + let expected = ""; + assert_eq!(stringified, expected); + } } diff --git a/lua/avante/repo_map.lua b/lua/avante/repo_map.lua index f07dc731..9e072e3b 100644 --- a/lua/avante/repo_map.lua +++ b/lua/avante/repo_map.lua @@ -53,6 +53,14 @@ end local cache = {} function RepoMap.get_repo_map(file_ext) + local repo_map = RepoMap._get_repo_map(file_ext) or {} + if not repo_map or next(repo_map) == nil then + Utils.warn("The repo map is empty. Maybe do not support this language: " .. file_ext) + end + return repo_map +end + +function RepoMap._get_repo_map(file_ext) file_ext = file_ext or vim.fn.expand("%:e") local project_root = Utils.root.get() local cache_key = project_root .. "." .. file_ext