Skip to content

Commit

Permalink
change file hover and references position
Browse files Browse the repository at this point in the history
  • Loading branch information
Feel-ix-343 committed Mar 8, 2024
1 parent fb7c51a commit ea7742a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 45 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ vim.api.nvim_exec_autocmds('User', { pattern = 'LspAttached' })
*Test it out! Go to definitions, get references, and more!*

> [!NOTE]
> To get references on files, you must place your cursor/pointer on the first character of the first line of the file, and then get references. (In VSCode, you can also use the references code lens)
> To get references on files, you can have your cursor anywhere on the markdown file where there is not another referenceable (heading, tag, ...)
## Note on Linking Syntax

The linking syntax is that of Obsidian's and can be found here https://help.obsidian.md/Linking+notes+and+files/Internal+links

Generally, this is `[[relativeFilePath(#heading)?(|display text)?]]` e.g. [[articles/markdown oxide#Features|Markdown Oxide Features]] to link to a heading in `Markdown Oxide.md` file in the `articles` folder or [[Obsidian]] for the `Obsidian.md` file in the root folder.
Generally, this is `[[relativeFilePath(#heading)?(|display text)?]]` e.g. [[articles/markdown oxide#Features|Markdown Oxide Features]] to link to a heading in `Markdown Oxide.md` file in the `articles` folder or [[Obsidian]] for the `Obsidian.md` file in the root folder. Markdown oxide also support markdown links

## Features

Expand All @@ -103,7 +103,7 @@ Generally, this is `[[relativeFilePath(#heading)?(|display text)?]]` e.g. [[arti
- [X] Footnotes: "paraphrased text[^footnoteindex]"
- [ ] Metadata tag
- Get references
- [X] For File when the cursor is on the **first character of the first line** of the file. This will produce references not only to the file but also to headings and blocks in the file
- [X] For File when the cursor is anywhere where there is not another referenceable. This will produce references not only to the file but also to headings and blocks in the file
- [X] For block when the cursor is on the block's index "...text *^index*"
- [X] For tag when the cursor is on the tags declaration. Unlike go-to-definition for tags, this will produce all references to the tag and to the tag with subtags
- [X] Footnotes when the cursor is on the declaration line of the footnote; *[^1]: description...*
Expand Down
19 changes: 16 additions & 3 deletions src/codelens.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::{CodeLens, CodeLensParams, Command, Location, Position, Url};

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

use serde::Serialize;

Expand Down Expand Up @@ -47,14 +47,27 @@ pub fn code_lens(vault: &Vault, path: &Path, _params: &CodeLensParams) -> Option
})
.collect_vec();

let range = match referenceable {
Referenceable::File(..) => tower_lsp::lsp_types::Range {
start: Position{
line: 0,
character: 0
}, end: Position {
line: 0,
character: 1
}
},
_ => *referenceable.get_range()?
};

Some(CodeLens {
range: *referenceable.get_range()?,
range,
command: Some(Command {
title,
command: "moxide.findReferences".into(),
arguments: Some(vec![serde_json::to_value(FindReferencesData {
uri: Url::from_file_path(path).ok()?,
position: referenceable.get_range()?.start,
position: range.start,
locations,
})
.ok()?]),
Expand Down
19 changes: 17 additions & 2 deletions src/gotodef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::Path;

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

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

pub fn goto_definition(
vault: &Vault,
Expand All @@ -20,9 +20,24 @@ pub fn goto_definition(
referenceables
.into_iter()
.filter_map(|linkable| {

let range = match linkable {
Referenceable::File(..) => tower_lsp::lsp_types::Range {
start: Position{
line: 0,
character: 0
}, end: Position {
line: 0,
character: 1
}
},
_ => *linkable.get_range()?
};


Some(Location {
uri: Url::from_file_path(linkable.get_path().to_str()?).unwrap(),
range: *linkable.get_range()?,
range,
})
})
.collect(),
Expand Down
2 changes: 1 addition & 1 deletion src/hover.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::{Path, PathBuf};

use tower_lsp::lsp_types::{Hover, HoverContents, HoverParams};

Expand Down
17 changes: 16 additions & 1 deletion src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ pub fn workspace_symbol(
let symbol_informations = referenceables
.into_iter()
.flat_map(|referenceable| {

let range = match referenceable {
Referenceable::File(..) => tower_lsp::lsp_types::Range {
start: tower_lsp::lsp_types::Position{
line: 0,
character: 0
}, end: tower_lsp::lsp_types::Position {
line: 0,
character: 1
}
},
_ => *referenceable.get_range()?
};


Some(SymbolInformation {
name: referenceable.get_refname(vault.root_dir())?.to_string(),
kind: match referenceable {
Expand All @@ -25,7 +40,7 @@ pub fn workspace_symbol(
},
location: Location {
uri: Url::from_file_path(referenceable.get_path()).ok()?,
range: *referenceable.get_range()?,
range,
},
container_name: None,
tags: None,
Expand Down
26 changes: 16 additions & 10 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use tower_lsp::lsp_types::{MarkupContent, MarkupKind};

use crate::vault::{get_obsidian_ref_path, Preview, Reference, Referenceable, Vault};

fn referenceable_string(vault: &Vault, referenceable: &Referenceable) -> Option<String> {
fn referenceable_string(vault: &Vault, referenceables: &[Referenceable]) -> Option<String> {

let referenceable = referenceables.get(0)?;

let preview = vault.select_referenceable_preview(referenceable);

let written_text_preview = match preview {
Expand All @@ -20,8 +23,13 @@ fn referenceable_string(vault: &Vault, referenceable: &Referenceable) -> Option<
None => "No Preview".into(),
};

let backlinks_preview = match vault.select_references_for_referenceable(referenceable) {
Some(references) if references.len() > 0 => references
let backlinks_preview = match referenceables
.iter()
.flat_map(|i| Some(vault.select_references_for_referenceable(i)?))
.flatten()
.collect_vec()
{
references if references.len() > 0 => references
.into_iter()
.take(20)
.flat_map(|(path, reference)| {
Expand All @@ -47,7 +55,7 @@ pub fn preview_referenceable(
vault: &Vault,
referenceable: &Referenceable,
) -> Option<MarkupContent> {
let display = referenceable_string(vault, referenceable)?;
let display = referenceable_string(vault, &[referenceable.clone()])?;

Some(MarkupContent {
kind: MarkupKind::Markdown,
Expand All @@ -59,7 +67,7 @@ use Reference::*;

pub fn preview_reference(
vault: &Vault,
_reference_path: &Path,
reference_path: &Path,
reference: &Reference,
) -> Option<MarkupContent> {
match reference {
Expand All @@ -71,12 +79,10 @@ pub fn preview_reference(
| MDHeadingLink(..)
| MDIndexedBlockLink(..)
| LinkRef(..) => {
let positions = vault.select_referenceable_nodes(None);
let referenceable = positions
.iter()
.find(|i| reference.references(vault.root_dir(), i.get_path(), i))?;

let display = referenceable_string(vault, referenceable)?;
let referenceables_for_reference = vault.select_referenceables_for_reference(reference, reference_path);

let display = referenceable_string(vault, &referenceables_for_reference)?;

Some(MarkupContent {
kind: MarkupKind::Markdown,
Expand Down
49 changes: 24 additions & 25 deletions src/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,35 @@ impl Vault {
.collect(),
),
}
} // TODO: less cloning?
}

pub fn select_referenceable_at_position<'a>(
&'a self,
path: &'a Path,
position: Position,
) -> Option<Referenceable<'a>> {
let linkable_nodes = self.select_referenceable_nodes(Some(path));
let linkable = linkable_nodes.into_iter().find(|l| {
let Some(range) = l.get_range() else {
return false;
};
range.start.line <= position.line

// If no other referenceables are under the cursor, the file should be returned.

let referenceable_nodes = self.select_referenceable_nodes(Some(path));


let referenceable = referenceable_nodes.into_iter()
.flat_map(|referenceable| {
Some((referenceable.clone(), referenceable.get_range()?))
})
.find(|(_, range)| {
range.start.line <= position.line
&& range.end.line >= position.line
&& range.start.character <= position.character
&& range.end.character >= position.character
})?;
})
.map(|tupl| tupl.0);

Some(linkable)
match referenceable {
None => self.md_files.iter().find(|(iterpath, _)| *iterpath == path).map(|(pathbuf, mdfile)| Referenceable::File(pathbuf, mdfile)),
_ => referenceable
}
}

pub fn select_reference_at_position<'a>(
Expand Down Expand Up @@ -221,6 +231,7 @@ impl Vault {
.iter()
.flat_map(|resolved| {
resolved.get_refname(self.root_dir()).and_then(|refname| vec![refname.to_string(), format!("{}{}", refname.link_file_key()?, refname.infile_ref.map(|refe| format!("#{}", refe)).unwrap_or("".to_string()))].into())

})
.flatten()
.collect();
Expand Down Expand Up @@ -378,9 +389,8 @@ impl Vault {
.map(Into::into)
}
Referenceable::File(_, _) => {
let range = referenceable.get_range()?;
Some(
(range.start.line..=range.end.line + 13)
(0..=13)
.filter_map(|ln| self.select_line(referenceable.get_path(), ln as isize)) // flatten those options!
.map(String::from_iter)
.join("")
Expand Down Expand Up @@ -712,7 +722,8 @@ impl Reference {
match referenceable {
&Referenceable::Tag(_, _) => {
match self {
Tag(..) => referenceable.get_refname(root_dir) == Some(text.to_string().into()),
Tag(..) => referenceable.get_refname(root_dir).map(|thing| thing.to_string()) == Some(format!("#{}", text.to_string())),

WikiFileLink(_) => false,
WikiHeadingLink(_, _, _) => false,
WikiIndexedBlockLink(_, _, _) => false,
Expand Down Expand Up @@ -1336,19 +1347,7 @@ impl Referenceable<'_> {

pub fn get_range(&self) -> Option<MyRange> {
match self {
Referenceable::File(_, _) => Some(
tower_lsp::lsp_types::Range {
start: Position {
line: 0,
character: 0,
},
end: Position {
line: 0,
character: 1,
},
}
.into(),
),
Referenceable::File(_, _) => None,
Referenceable::Heading(_, heading) => Some(heading.range),
Referenceable::IndexedBlock(_, indexed_block) => Some(indexed_block.range),
Referenceable::Tag(_, tag) => Some(tag.range),
Expand Down

0 comments on commit ea7742a

Please sign in to comment.