1use nvim_oxi::Dictionary;
2
3use crate::buffer::token_under_cursor;
4use crate::buffer::token_under_cursor::TokenUnderCursor;
5
6pub fn dict() -> Dictionary {
7 dict! {
8 "open_token_under_cursor": fn_from!(open_token_under_cursor),
9 "copy_enclosing_function": fn_from!(copy_enclosing_function),
10 }
11}
12
13fn copy_enclosing_function(_: ()) -> Option<()> {
14 let file_path = ytil_noxi::buffer::get_absolute_path(Some(&nvim_oxi::api::get_current_buf()))?;
15 let enclosing_fn = ytil_noxi::tree_sitter::get_enclosing_fn_name_of_position(&file_path)?;
16 ytil_sys::file::cp_to_system_clipboard(&mut enclosing_fn.as_bytes())
17 .inspect_err(|err| {
18 ytil_noxi::notify::error(format!(
19 "error copying content to system clipboard | content={enclosing_fn:?} error={err:#?}"
20 ));
21 })
22 .ok()?;
23 nvim_oxi::print!("Enclosing fn name copied to clipboard: {enclosing_fn}");
24 Some(())
25}
26
27fn open_token_under_cursor(_: ()) -> Option<()> {
28 let token_under_cursor = token_under_cursor::get(())?;
29
30 match token_under_cursor {
31 TokenUnderCursor::BinaryFile(_) | TokenUnderCursor::MaybeTextFile { .. } => None,
32 TokenUnderCursor::TextFile { path, lnum, col } => {
33 let open_path_cmd = format!(
34 "edit {path} | call cursor({}, {})",
35 lnum.unwrap_or_default(),
36 col.unwrap_or_default()
37 );
38
39 let vim_script = if let Some(win_num) =
40 ytil_noxi::window::find_with_buffer("").and_then(|(win, _)| ytil_noxi::window::get_number(&win))
41 {
42 format!("{win_num} wincmd w | {open_path_cmd}")
43 } else {
44 let width = crate::layout::compute_width(70)?;
45 format!("vsplit | vertical resize {width} | {open_path_cmd}")
46 };
47
48 ytil_noxi::common::exec_vim_script(&vim_script, None);
49
50 Some(())
51 }
52 TokenUnderCursor::Url(arg) | TokenUnderCursor::Directory(arg) => ytil_sys::open(&arg)
53 .inspect_err(|err| {
54 ytil_noxi::notify::error(format!("{err:?}"));
55 })
56 .ok(),
57 }
58}