Skip to content

Commit

Permalink
Supporting tag references including subtags
Browse files Browse the repository at this point in the history
References for #tag includes references like #tag/subtag
  • Loading branch information
Feel-ix-343 committed Dec 7, 2023
1 parent b1bfd5f commit 6b2cd93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion TestFiles/Another Test.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ This file is popppinnng, 2 links to it.
[[Test#Heading 1 2]]
[[Test#Heading 1 2]]

#tag
#tag/test

13 changes: 7 additions & 6 deletions src/references.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::Path;

use itertools::Itertools;
use tower_lsp::lsp_types::{Position, Location, Url};

use crate::vault::{Vault, Referenceable};
Expand All @@ -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))
}
}
9 changes: 8 additions & 1 deletion src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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,
Expand Down

0 comments on commit 6b2cd93

Please sign in to comment.