Skip to content

Commit

Permalink
feat: advanced md link completions
Browse files Browse the repository at this point in the history
  • Loading branch information
Feel-ix-343 committed Mar 2, 2024
1 parent 7ea9b1f commit 139ce57
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
70 changes: 48 additions & 22 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use regex::Regex;
use tower_lsp::lsp_types::{
Command, CompletionItem, CompletionItemKind, CompletionItemLabelDetails, CompletionList,
CompletionParams, CompletionResponse, CompletionTextEdit, Documentation, MarkupContent,
MarkupKind, Position, Range, TextEdit, Url,
MarkupKind, Position, Range, TextEdit, Url, InsertTextFormat,
};

use crate::{
Expand Down Expand Up @@ -71,6 +71,7 @@ fn get_completable_mdlink (line: &Vec<char> , cursor_character: usize) -> Option
let full_range = match reference_under_cursor {
Some(reference @ (Reference::MDFileLink(..) | Reference::MDHeadingLink(..) | Reference::MDIndexedBlockLink(..))) =>
reference.range.start.character as usize .. reference.range.end.character as usize,
None if line.get(cursor_character) == Some(&')') => full.range().start .. full.range().end + 1,
_ => full.range()
};

Expand Down Expand Up @@ -279,44 +280,79 @@ pub fn get_completions(
} else if let Some(partialmdlink) = get_completable_mdlink(&selected_line, character) {

match partialmdlink {
CompletableMDLink {path, infile_ref, ..} => {
CompletableMDLink {path, infile_ref, full_range, display, partial} => {
let inputted_refname = format!("{}{}", path.0, infile_ref.clone().map(|(string, _)| format!("#{}", string)).unwrap_or("".to_string()));

let all_links = MatchableReferenceable::from_vault(vault);

let matches = fuzzy_match(&inputted_refname, all_links);


return Some(CompletionResponse::List(CompletionList {
is_incomplete: true,
items: matches
.into_iter()
.take(30)
.take(50)
.filter(|(MatchableReferenceable(_, name), _)| {
*name != inputted_refname
})
.filter_map(|(MatchableReferenceable(referenceable, _), rank)| {
.flat_map(|(MatchableReferenceable(referenceable, _), rank)| {
default_completion_item(
vault,
&referenceable,
Some(CompletionTextEdit::Edit(TextEdit {
range: Range {
start: Position {
line: line as u32,
character: path.1.start as u32,
character: full_range.start as u32,
},
end: Position {
line: line as u32,
character: infile_ref.as_ref().map(|(_, range)| range.end as u32).unwrap_or(path.1.end as u32),
character: full_range.end as u32,
},
},
new_text: referenceable.get_refname(vault.root_dir())?.to_string(),
}))
).map(|item| {
CompletionItem {
new_text: format!(
"[${{1:{}}}]({}{}{}{})",
match (display.0.as_str(), referenceable.get_refname(vault.root_dir())?.infile_ref) {
("", Some(infile_ref_text)) => infile_ref_text.clone(),
("", None) => {
match referenceable {
Referenceable::File(_, mdfile) => {
match mdfile.headings.first() {
Some(heading) => heading.heading_text.clone(),
None => "".to_string()
}
}

_ => "".to_string()
}
},
(display_text, _) => display_text.to_string(),
},
if referenceable.get_refname(vault.root_dir())?.path?.contains(" ") {
"<"
} else {
""
},
referenceable.get_refname(vault.root_dir())?.path?.to_string(),
match referenceable.get_refname(vault.root_dir())?.infile_ref {
Some(string) => format!("#{}", string),
None => "".to_string(),
},
if referenceable.get_refname(vault.root_dir())?.path?.contains(" ") {
">"
} else {
""
},
)
})),
).and_then(|item| {
Some(CompletionItem {
sort_text: Some(rank.to_string()),
insert_text_format: Some(InsertTextFormat::SNIPPET),
filter_text: Some(format!("[{}]({}", display.0, referenceable.get_refname(vault.root_dir())?.to_string())),
..item
}

})
})
})
.collect::<Vec<_>>(),
Expand Down Expand Up @@ -418,16 +454,6 @@ fn default_completion_item(
text_edit,
documentation: preview_referenceable(vault, referenceable)
.map(Documentation::MarkupContent),
filter_text: match referenceable {
Referenceable::IndexedBlock(_, _) => vault
.select_referenceable_preview(referenceable)
.and_then(|preview| match preview {
Preview::Text(string) => Some(string),
Preview::Empty => None,
})
.map(|text| format!("{}{}", *refname, &text)),
_ => None,
},
..Default::default()
};

Expand Down
14 changes: 7 additions & 7 deletions src/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,13 @@ fn range_to_position(rope: &Rope, range: Range<usize>) -> MyRange {

#[derive(Debug, PartialEq, Eq, Default, Hash, Clone)]
pub struct MDFile {
references: Vec<Reference>,
headings: Vec<MDHeading>,
indexed_blocks: Vec<MDIndexedBlock>,
tags: Vec<MDTag>,
footnotes: Vec<MDFootnote>,
path: PathBuf,
link_reference_definitions: Vec<MDLinkReferenceDefinition>
pub references: Vec<Reference>,
pub headings: Vec<MDHeading>,
pub indexed_blocks: Vec<MDIndexedBlock>,
pub tags: Vec<MDTag>,
pub footnotes: Vec<MDFootnote>,
pub path: PathBuf,
pub link_reference_definitions: Vec<MDLinkReferenceDefinition>
}

impl MDFile {
Expand Down

0 comments on commit 139ce57

Please sign in to comment.