ytil_noxi/
window.rs

1//! Provides functions for Neovim window operations.
2
3use nvim_oxi::api::Buffer;
4use nvim_oxi::api::Window;
5
6use crate::buffer::BufferExt;
7
8/// Sets the specified window as the current window in Neovim.
9///
10/// On failure, notifies Neovim of the error and returns `None`.
11///
12/// # Errors
13/// - Setting the current window fails.
14pub fn set_current(window: &Window) -> Option<()> {
15    nvim_oxi::api::set_current_win(window)
16        .inspect_err(|err| {
17            crate::notify::error(format!(
18                "error setting current window | window={window:?}, error={err:?}"
19            ));
20        })
21        .ok()?;
22    Some(())
23}
24
25/// Retrieves the buffer associated with the specified window.
26///
27/// On failure, notifies Neovim of the error and returns `None`.
28///
29/// # Errors
30/// - Retrieving the buffer fails.
31pub fn get_buffer(window: &Window) -> Option<Buffer> {
32    window
33        .get_buf()
34        .inspect_err(|err| {
35            crate::notify::error(format!(
36                "error getting window buffer | window={window:?}, error={err:?}"
37            ));
38        })
39        .ok()
40}
41
42pub fn find_with_buffer(buffer_type: &str) -> Option<(Window, Buffer)> {
43    nvim_oxi::api::list_wins().find_map(|win| {
44        if let Some(buffer) = get_buffer(&win)
45            && buffer.get_buf_type().is_some_and(|bt| bt == buffer_type)
46        {
47            Some((win, buffer))
48        } else {
49            None
50        }
51    })
52}
53
54pub fn get_number(win: &Window) -> Option<u32> {
55    win.get_number()
56        .inspect_err(|err| crate::notify::error(format!("error getting window number | window={win:?} error={err:?}")))
57        .ok()
58}