Skip to content

Commit

Permalink
Refactoring and adding support for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Feel-ix-343 committed Dec 7, 2023
1 parent 4a9134d commit 3b99281
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 76 deletions.
3 changes: 3 additions & 0 deletions TestFiles/Another Test.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ This file is popppinnng, 2 links to it.
[[folder/File#Heading]]
[[Test#Heading 1 2]]
[[Test#Heading 1 2]]

#tag

4 changes: 4 additions & 0 deletions TestFiles/Test.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
# Heading 1 2

[[Another Test]]


#tag

2 changes: 2 additions & 0 deletions TestFiles/folder/File.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[[Another Test]]

# Heading

#tag
9 changes: 4 additions & 5 deletions src/gotodef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use tower_lsp::lsp_types::{Position, Url, Location};

use crate::vault::Vault;

pub fn goto_definition(vault: &Vault, cursor_position: Position, path: &Path) -> Option<Location> {
pub fn goto_definition(vault: &Vault, cursor_position: Position, path: &Path) -> Option<Vec<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(Some(&path))?;
let links = vault.select_references(Some(&path))?;
let cursors_link = links.iter().find(|&l|
l.1.range.start.line <= cursor_position.line &&
l.1.range.end.line >= cursor_position.line &&
Expand All @@ -19,8 +19,7 @@ pub fn goto_definition(vault: &Vault, cursor_position: Position, path: &Path) ->
// Lets get all of the linkable nodes

let positions = vault.select_linkable_nodes();
let referenced_linkable = positions.iter().find(|i| i.get_refname(&vault.root_dir()).as_ref() == Some(&reference_text))?;
let path_as_str = referenced_linkable.get_path().to_str()?;
let referenced_linkables = positions.iter().filter(|i| i.get_refname(&vault.root_dir()).as_ref() == Some(&reference_text));

return Some(Location { uri: Url::from_file_path(path_as_str).unwrap(), range: referenced_linkable.get_range() })
return Some(referenced_linkables.filter_map(|linkable| Some(Location{uri: Url::from_file_path(linkable.get_path().to_str()?).unwrap(), range: linkable.get_range()})).collect());
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl LanguageServer for Backend {
let result = goto_definition(&vault, position, &path);


return Ok(result.map(|l| GotoDefinitionResponse::Scalar(l)))
return Ok(result.map(|l| GotoDefinitionResponse::Array(l)))
}


Expand Down
7 changes: 4 additions & 3 deletions src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;
use itertools::Itertools;
use tower_lsp::lsp_types::{Position, Location, Url};

use crate::vault::{Vault, Linkable};
use crate::vault::{Vault, Referenceable};

pub fn references(vault: &Vault, cursor_position: Position, path: &Path) -> Option<Vec<Location>> {
// First we need to get the linkable node under the cursor
Expand All @@ -19,7 +19,7 @@ pub fn references(vault: &Vault, cursor_position: Position, path: &Path) -> Opti
)?;


let references = vault.select_links(None)?;
let references = vault.select_references(None)?;
let locations = |reference_text| references.iter()
.filter(move |r| r.1.reference_text == reference_text)
.map(|link| Url::from_file_path(link.0).map(|good| Location {uri: good, range: link.1.range}))
Expand All @@ -30,8 +30,9 @@ pub fn references(vault: &Vault, cursor_position: Position, path: &Path) -> Opti
.flatten();

return match linkable {
Linkable::MDFile(path, md) => {
Referenceable::File(path, md) => {
return Some(linkable_nodes.iter()
.filter(|&referenceable| !matches!(referenceable, &Referenceable::Tag(_, _)))
.filter_map(|linkable| linkable.get_refname(vault.root_dir()))
.map(|refname| locations(refname))
.flatten()
Expand Down
Loading

0 comments on commit 3b99281

Please sign in to comment.