use super::*;
pub struct Note {
pub id: u64,
pub file: String,
pub offset: usize,
pub body: String,
}
impl PairState {
pub(crate) fn notes_in<'a>(&'a self, file: &'a str) -> impl Iterator<Item = &'a Note> {
self.notes.iter().filter(move |n| n.file == file)
}
pub(crate) fn clear_all_notes(&mut self) {
self.notes.clear();
}
pub(crate) fn remove_note(&mut self, id: u64) {
self.notes.retain(|n| n.id != id);
}
pub(crate) fn append_to_note(&mut self, id: u64, line: &str) {
if let Some(n) = self.notes.iter_mut().find(|n| n.id == id) {
n.body.push('\n');
n.body.push_str(line);
}
}
pub(super) fn take_note_id(&mut self) -> u64 {
let id = self.next_note_id;
self.next_note_id += 1;
id
}
}
#[cfg(test)]
pub(crate) fn inject_note(
state: &Mutex<PairState>,
file: &str,
row: usize,
body: &str,
) -> Option<u64> {
state.lock().unwrap().session.request_file(file);
if !wait_live(state, file) {
return None;
}
inject_note_now(state, file, row, body)
}
pub(crate) fn inject_note_now(
state: &Mutex<PairState>,
file: &str,
row: usize,
body: &str,
) -> Option<u64> {
let mut st = lock_briefly(state)?;
if !st.session.is_live(file) {
return None;
}
let lines = st.doc_lines(file)?;
let offset = note_offset(&lines, row);
st.caret_now(file, position(&lines, offset).0, 0);
let id = st.take_note_id();
st.notes.push(Note {
id,
file: file.to_string(),
offset,
body: body.to_string(),
});
Some(id)
}