diff --git a/TestFiles/Another Test.md b/TestFiles/Another Test.md index d4ee6803..59b894e7 100644 --- a/TestFiles/Another Test.md +++ b/TestFiles/Another Test.md @@ -19,5 +19,5 @@ This file is popppinnng, 2 links to it. [[Test#Heading 1 2]] [[Test#Heading 1 2]] -#tag +#tag/test diff --git a/src/references.rs b/src/references.rs index e00f06d7..7b689e94 100644 --- a/src/references.rs +++ b/src/references.rs @@ -1,5 +1,6 @@ use std::path::Path; +use itertools::Itertools; use tower_lsp::lsp_types::{Position, Location, Url}; use crate::vault::{Vault, Referenceable}; @@ -19,24 +20,24 @@ pub fn references(vault: &Vault, cursor_position: Position, path: &Path) -> Opti let references = vault.select_references(None)?; - let locations = |reference_text| references.iter() - .filter(move |r| r.1.reference_text == reference_text) + let locations = |referenceable: &Referenceable| references.iter() + .filter(|&r| referenceable.is_reference(&vault.root_dir(), &r.1.reference_text)) .map(|link| Url::from_file_path(link.0).map(|good| Location {uri: good, range: link.1.range})) .flat_map(|l| match l.is_ok() { true => Some(l), false => None }) - .flatten(); + .flatten() + .collect_vec(); return match linkable { Referenceable::File(_, _) => { return Some(linkable_nodes.iter() .filter(|&referenceable| !matches!(referenceable, &Referenceable::Tag(_, _))) - .filter_map(|linkable| linkable.get_refname(vault.root_dir())) - .map(|refname| locations(refname)) + .map(|referenceable| locations(referenceable)) .flatten() .collect()) } - _ => linkable.get_refname(vault.root_dir()).and_then(|r| Some(locations(r).collect())) + _ => Some(locations(linkable)) } } diff --git a/src/vault.rs b/src/vault.rs index 129e3f07..aacf35ba 100644 --- a/src/vault.rs +++ b/src/vault.rs @@ -164,7 +164,7 @@ pub struct Vault { root_dir: PathBuf, } -#[derive(Debug)] +#[derive(Debug, Clone)] /// Linkable algebreic type that easily allows for new linkable nodes to be added if necessary and everything in it should live the same amount because it is all from vault /// These will also use the current obsidian syntax to come up with reference names for the linkables. These are the things that are using in links ([[Refname]]) pub enum Referenceable<'a> { @@ -188,6 +188,13 @@ impl Referenceable<'_> { } } + pub fn is_reference(&self, root_dir: &Path, reference: &str) -> bool { + match self { + &Referenceable::Tag(_, tag) => self.get_refname(root_dir).is_some_and(|refname| reference.starts_with(&refname)), + _ => self.get_refname(root_dir) == Some(reference.to_string()) + } + } + pub fn get_path(&self) -> &PathBuf { match self { &Referenceable::File(path, _) => path,