Skip to main content

nvrim/plugins/
opener.rs

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        "reveal_in_finder": fn_from!(reveal_in_finder),
11    }
12}
13
14fn copy_enclosing_function(_: ()) -> Option<()> {
15    let file_path = ytil_noxi::buffer::get_absolute_path(Some(&nvim_oxi::api::get_current_buf()))?;
16    let enclosing_fn = ytil_noxi::tree_sitter::get_enclosing_fn_name_of_position(&file_path)?;
17    ytil_sys::file::cp_to_system_clipboard(&mut enclosing_fn.as_bytes())
18        .inspect_err(|err| {
19            ytil_noxi::notify::error(format!(
20                "error copying content to system clipboard | content={enclosing_fn:?} error={err:#?}"
21            ));
22        })
23        .ok()?;
24    nvim_oxi::print!("Enclosing fn name copied to clipboard: {enclosing_fn}");
25    Some(())
26}
27
28fn open_token_under_cursor(_: ()) -> Option<()> {
29    let token_under_cursor = token_under_cursor::get(())?;
30
31    match token_under_cursor {
32        TokenUnderCursor::BinaryFile(_) | TokenUnderCursor::MaybeTextFile { .. } => None,
33        TokenUnderCursor::TextFile { path, lnum, col } => {
34            let open_path_cmd = format!(
35                "edit {path} | call cursor({}, {})",
36                lnum.unwrap_or_default(),
37                col.unwrap_or_default()
38            );
39
40            let vim_script = if let Some(win_num) =
41                ytil_noxi::window::find_with_buffer("").and_then(|(win, _)| ytil_noxi::window::get_number(&win))
42            {
43                format!("{win_num} wincmd w | {open_path_cmd}")
44            } else {
45                let width = crate::layout::compute_width(70)?;
46                format!("vsplit | vertical resize {width} | {open_path_cmd}")
47            };
48
49            ytil_noxi::common::exec_vim_script(&vim_script, None);
50
51            Some(())
52        }
53        TokenUnderCursor::Url(arg) | TokenUnderCursor::Directory(arg) => ytil_sys::open(&arg)
54            .inspect_err(|err| {
55                ytil_noxi::notify::error(format!("{err:?}"));
56            })
57            .ok(),
58    }
59}
60
61fn reveal_in_finder(_: ()) -> Option<()> {
62    let file_path = ytil_noxi::buffer::get_absolute_path(Some(&nvim_oxi::api::get_current_buf()))?;
63    let Some(parent) = file_path.parent() else {
64        ytil_noxi::notify::error(format!(
65            "error no parent for current buffer file path | file_path={}",
66            file_path.display()
67        ));
68        return None;
69    };
70    let Some(parent_str) = parent.to_str() else {
71        ytil_noxi::notify::error(format!(
72            "error parent path is not valid UTF-8 | path={}",
73            parent.display()
74        ));
75        return None;
76    };
77    ytil_sys::open(parent_str)
78        .inspect_err(|err| {
79            ytil_noxi::notify::error(format!("error opening path | path={} error={err:#?}", parent.display()));
80        })
81        .ok()?;
82    Some(())
83}