Skip to content

Commit

Permalink
fix(repo-map): do not report errors when encountering unsupported lan…
Browse files Browse the repository at this point in the history
…guages (#652)
  • Loading branch information
yetone authored Sep 26, 2024
1 parent 131d800 commit 302f207
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
47 changes: 34 additions & 13 deletions crates/avante-repo-map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ pub enum Definition {
Variable(Variable),
}

fn get_ts_language(language: &str) -> Result<LanguageFn, String> {
fn get_ts_language(language: &str) -> Option<LanguageFn> {
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,

This comment has been minimized.

Copy link
@aarnphm

aarnphm Sep 27, 2024

Collaborator

I think it is better to use mlua API to surface this error instead of this

}
}

Expand All @@ -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<Query, String> {
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,
Expand Down Expand Up @@ -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<Vec<Definition>, 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();
Expand Down Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions lua/avante/repo_map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 302f207

Please sign in to comment.