Skip to content

Commit

Permalink
Refactoring vault api
Browse files Browse the repository at this point in the history
  • Loading branch information
Feel-ix-343 committed Dec 6, 2023
1 parent 5abdc14 commit 382d16e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/gotodef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::vault::Vault;

pub fn goto_definition(vault: &Vault, cursor_position: Position, path: &Path) -> Option<Location> {
// First, find the link that the cursor is in. Get a links for the file and match the cursor position up to one of them
let links = vault.select_links_in_file(&path)?;
let links = vault.select_links(Some(&path))?;
let cursors_link = links.iter().find(|&l|
l.range.start.line <= cursor_position.line &&
l.range.end.line >= cursor_position.line &&
Expand Down
10 changes: 7 additions & 3 deletions src/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ impl Linkable<'_> {

impl Vault {
/// Select all links ([[Link]]) in a file
pub fn select_links_in_file(&self, path: &Path) -> Option<&Vec<Link>> {
self.files.get(path).map(|md| &md.links)
}
pub fn select_links(&self, path: Option<&Path>) -> Option<Vec<&Link>> {
match path {
Some(path) => self.files.get(path).map(|md| &md.links).map(|vec| vec.iter().collect()),
None => Some(self.files.values().flat_map(|md| &md.links).collect())
}

} // TODO: less cloning?

/// Select all linkable positions in the vault
pub fn select_linkable_nodes<'a>(&'a self) -> Vec<Linkable<'a>> {
Expand Down

0 comments on commit 382d16e

Please sign in to comment.