ytil_noxi/quickfix.rs
1//! Utilities for managing and displaying Nvim quickfix lists.
2
3use core::fmt::Debug;
4
5use color_eyre::eyre::Context;
6use nvim_oxi::Array;
7use nvim_oxi::api::opts::CmdOpts;
8use nvim_oxi::api::types::CmdInfosBuilder;
9
10use crate::dict;
11
12/// Opens the quickfix window with the provided file and line number entries.
13///
14/// Populates the quickfix list with the given entries and opens the quickfix window
15/// for user navigation. Each entry consists of a filename and line number.
16/// If no entries are provided, returns early without opening the quickfix window.
17///
18/// # Errors
19/// - Fails if `setqflist` Neovim function call encounters an error.
20/// - Fails if `copen` command execution encounters an error.
21///
22/// # Rationale
23/// Uses Nvim's built-in quickfix functionality to avoid custom UI implementations.
24pub fn open<'a>(entries: impl IntoIterator<Item = (&'a str, i64)> + Debug) -> color_eyre::Result<()> {
25 let mut qflist = vec![];
26 for (filename, lnum) in entries {
27 qflist.push(dict! {
28 "filename": filename.to_string(),
29 "lnum": lnum
30 });
31 }
32
33 if qflist.is_empty() {
34 return Ok(());
35 }
36
37 nvim_oxi::api::call_function::<_, i64>("setqflist", (Array::from_iter(qflist),))
38 .wrap_err("error executing setqflist function")?;
39 nvim_oxi::api::cmd(&CmdInfosBuilder::default().cmd("copen").build(), &CmdOpts::default())
40 .wrap_err("error executing copen cmd")?;
41
42 Ok(())
43}