Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,51 @@ pub enum Case {
Respect,
}

impl Default for Case {
fn default() -> Self {
Case::Smart
}
}

#[derive(Clone, Debug, Deserialize)]
pub enum EmbeddedBlockTransclusionLength {
Partial(usize),
Full,
}

impl Default for EmbeddedBlockTransclusionLength {
fn default() -> Self {
EmbeddedBlockTransclusionLength::Full
}
}

impl Default for Settings {
fn default() -> Self {
Settings {
dailynote: "%Y-%m-%d".into(),
new_file_folder_path: "".into(),
daily_notes_folder: "".into(),
heading_completions: true,
title_headings: true,
unresolved_diagnostics: true,
semantic_tokens: true,
tags_in_codeblocks: false,
references_in_codeblocks: false,
include_md_extension_md_link: false,
include_md_extension_wikilink: false,
hover: true,
case_matching: Case::default(),
inlay_hints: true,
block_transclusion: true,
block_transclusion_length: EmbeddedBlockTransclusionLength::default(),
link_filenames_only: false,
excluded_folders: vec![],
heading_slug: false,
callout_completions: true,
}
}
}

impl Settings {
pub fn new(root_dir: &Path, capabilities: &ClientCapabilities) -> anyhow::Result<Settings> {
let obsidian_daily_note_config = obsidian_daily_note_config(root_dir).unwrap_or_default();
Expand Down
51 changes: 48 additions & 3 deletions src/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,18 @@ pub trait Rangeable {
&& self_range.end.character >= other_range.end.character))
}

fn overlaps(&self, other: &impl Rangeable) -> bool {
fn before_or_equal(left: Position, right: Position) -> bool {
left.line < right.line || (left.line == right.line && left.character <= right.character)
}

let self_range = self.range();
let other_range = other.range();

before_or_equal(self_range.start, other_range.end)
&& before_or_equal(other_range.start, self_range.end)
}
Comment on lines +604 to +614

fn includes_position(&self, position: Position) -> bool {
let range = self.range();
(range.start.line < position.line
Expand Down Expand Up @@ -672,7 +684,7 @@ impl MDFile {
references_in_codeblocks: false,
..
} => Reference::new(text, file_name)
.filter(|it| !code_blocks.iter().any(|codeblock| codeblock.includes(it)))
.filter(|it| !code_blocks.iter().any(|codeblock| codeblock.overlaps(it)))
.collect_vec(),
_ => Reference::new(text, file_name).collect_vec(),
};
Expand Down Expand Up @@ -1793,17 +1805,25 @@ fn matches_path_or_file(file_ref_text: &str, refname: Option<Refname>) -> bool {
// tests
#[cfg(test)]
mod vault_tests {
use std::path::Path;
use std::path::{Path, PathBuf};

use itertools::Itertools;
use tower_lsp::lsp_types::{Position, Range};

use crate::vault::{HeadingLevel, MyRange, ReferenceData};
use crate::config::Settings;
use crate::vault::{HeadingLevel, ReferenceData};
use crate::vault::{MDLinkReferenceDefinition, Refname};

use super::Reference::*;
use super::{MDFile, MDFootnote, MDHeading, MDIndexedBlock, MDTag, Reference, Referenceable};

fn test_settings() -> Settings {
Settings {
references_in_codeblocks: false,
..Settings::default()
}
}
Comment on lines +1820 to +1825

#[test]
fn wiki_link_parsing() {
let text = "This is a [[link]] [[link 2]]\n[[link 3]]";
Expand Down Expand Up @@ -1860,6 +1880,31 @@ mod vault_tests {
assert_eq!(parsed, expected)
}

#[test]
fn wikilink_markers_in_inline_code_are_not_references() {
let text = "* DO NOT use the square bracket `[[` and `]]` markers\nA real [[Note]]";

let parsed = MDFile::new(&test_settings(), text, PathBuf::from("test.md")).references;

let expected = vec![WikiFileLink(ReferenceData {
reference_text: "Note".into(),
range: tower_lsp::lsp_types::Range {
start: tower_lsp::lsp_types::Position {
line: 1,
character: 7,
},
end: tower_lsp::lsp_types::Position {
line: 1,
character: 15,
},
}
.into(),
..ReferenceData::default()
})];

assert_eq!(parsed, expected)
}

#[test]
fn wiki_link_heading_parsing() {
let text = "This is a [[link#heading]]";
Expand Down