Improve search and show save errors

- Show search results in the right notebook or All Notes list
- Scroll to the selected note and highlight matching text
- Keep unsaved edits safe and ignore old or cancelled searches
- Show save errors with a Retry button
- Run graph and vault statistics scans in the background
- Fix folder checks when counting vault files
- Keep desktop window-close code out of mobile builds
This commit is contained in:
Yuri Karamian committed 2026-09-28 21:06:28 +02:00
1 parent 640b5e3d10
commit 923cb9571c
21 files changed
+1224 -117

No files matched your search

+1 -1
View File
@@ -1776,7 +1776,7 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "helixnotes"
version = "1.3.5"
version = "1.3.6"
dependencies = [
"aho-corasick",
"arboard",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "helixnotes"
version = "1.3.5"
version = "1.3.6"
description = "Local markdown note-taking app"
authors = ["HelixNotes"]
license = "AGPL-3.0-or-later"
+194 -19
View File
@@ -366,21 +366,19 @@ pub fn get_pending_open_file(state: State<'_, AppState>) -> Result<Option<String
Ok(pending.take())
}
/// Finish a close request only after the frontend has flushed its editor.
/// Finish a desktop close request only after the frontend has flushed its editor.
#[cfg(desktop)]
#[tauri::command]
pub fn complete_window_close<R: Runtime>(
window: tauri::Window<R>,
state: State<'_, AppState>,
) -> Result<(), String> {
#[cfg(desktop)]
if state.should_hide_to_tray(
window.label(),
window.app_handle().tray_by_id("helixnotes").is_some(),
)? {
return window.hide().map_err(|error| error.to_string());
}
#[cfg(mobile)]
let _ = state;
window.destroy().map_err(|error| error.to_string())
}
@@ -996,14 +994,19 @@ pub async fn get_note_switcher_titles(
// ── Graph ──
#[tauri::command]
pub fn get_graph_data(
pub async fn get_graph_data(
state: State<'_, AppState>,
vault_path: String,
) -> Result<crate::types::GraphData, String> {
state.validate_vault(&vault_path)?;
tauri::async_runtime::spawn_blocking(move || scan_graph_data(&vault_path))
.await
.map_err(|error| error.to_string())?
}
fn scan_graph_data(vault_path: &str) -> Result<crate::types::GraphData, String> {
use std::collections::{HashMap, HashSet};
state.validate_vault(&vault_path)?;
let vault_path = vault_path.as_str();
let vault = std::path::Path::new(vault_path);
// Pass 1: collect ALL notes as nodes (no title deduplication - every note must appear)
@@ -2114,22 +2117,50 @@ pub async fn reorder_quick_access(
}
#[tauri::command]
pub fn get_vault_stats(state: State<'_, AppState>) -> Result<VaultStats, String> {
use rayon::prelude::*;
let config = state.config.lock().map_err(|e| e.to_string())?;
let vault_path = config.active_vault.as_ref().ok_or("No active vault")?;
pub async fn get_vault_stats(state: State<'_, AppState>) -> Result<VaultStats, String> {
let vault_path = state
.config
.lock()
.map_err(|error| error.to_string())?
.active_vault
.clone()
.ok_or("No active vault")?;
tauri::async_runtime::spawn_blocking(move || scan_vault_stats(&vault_path))
.await
.map_err(|error| error.to_string())?
}
let paths: Vec<std::path::PathBuf> = walkdir::WalkDir::new(vault_path)
fn scan_vault_stats(vault_path: &str) -> Result<VaultStats, String> {
use rayon::prelude::*;
let vault = Path::new(vault_path);
let paths: Vec<PathBuf> = walkdir::WalkDir::new(vault)
.into_iter()
.filter_entry(|entry| {
if entry.depth() == 0 {
return true;
}
let Ok(relative) = entry.path().strip_prefix(vault) else {
return false;
};
let is_directory = entry.file_type().is_dir();
if is_directory && entry.file_name() == ".trash" {
return false;
}
// Prune metadata children except the attachments directory. Inspect
// only vault-relative components, never names in the vault's parent.
if relative
.parent()
.and_then(Path::file_name)
.is_some_and(|name| name == ".helixnotes")
{
return is_directory && entry.file_name() == "attachments";
}
true
})
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.map(|e| e.into_path())
.filter(|p| {
// attachments under .helixnotes count; nothing else in .helixnotes or .trash does
let s = p.to_string_lossy();
(!s.contains("/.helixnotes/") || s.contains("/.helixnotes/attachments/"))
&& !s.contains("/.trash/")
})
.collect();
// Stat files in parallel; each metadata() is a FUSE round-trip on Android.
@@ -2157,6 +2188,150 @@ pub fn get_vault_stats(state: State<'_, AppState>) -> Result<VaultStats, String>
})
}
#[cfg(test)]
mod vault_stats_tests {
use super::scan_vault_stats;
use std::path::Path;
fn write_fixture(vault: &Path, files: &[(&[&str], &[u8])]) {
for (components, contents) in files {
let mut path = vault.to_path_buf();
for component in *components {
path.push(component);
}
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, contents).unwrap();
}
}
fn assert_stats(vault: &Path, expected: (u64, u64, u64, u64, u64)) {
let stats = scan_vault_stats(vault.to_str().unwrap()).unwrap();
assert_eq!(
(
stats.total_notes,
stats.total_attachments,
stats.notes_size,
stats.attachments_size,
stats.total_size,
),
expected,
"vault: {}",
vault.display()
);
}
#[test]
fn vault_stats_preserve_file_classification_and_byte_totals() {
let vault = tempfile::tempdir().unwrap();
write_fixture(
vault.path(),
&[
(&["Note.md"], b"note"),
(&["Notebook", "Note.md"], "café".as_bytes()),
(&["Empty.md"], b""),
(&["Upper.MD"], b"upper"),
(&["Notebook", "data.bin"], b"\0\x01\xff"),
(&["README"], b"plain"),
],
);
assert_stats(vault.path(), (3, 3, 9, 13, 22));
}
#[test]
fn vault_stats_include_only_attachment_subtrees_inside_metadata() {
let vault = tempfile::tempdir().unwrap();
write_fixture(
vault.path(),
&[
(&[".helixnotes", "attachments", "image.png"], b"123"),
(&[".helixnotes", "attachments", "Manual.md"], b"1234"),
(
&[".helixnotes", "attachments", "Nested", "icon.svg"],
b"12345",
),
(
&[".helixnotes", "attachments", ".trash-backup", "backup.bin"],
b"123456",
),
(&[".helixnotes", "settings.json"], b"excluded"),
(&[".helixnotes", "history", "Snapshot.md"], b"excluded"),
(&[".helixnotes", "trash", "Deleted.md"], b"excluded"),
(&[".helixnotes", "attachments-old", "old.png"], b"excluded"),
(
&[".helixnotes", "attachments", ".trash", "deleted.png"],
b"excluded",
),
],
);
assert_stats(vault.path(), (1, 3, 4, 14, 18));
}
#[test]
fn vault_stats_match_exact_directory_boundaries() {
let vault = tempfile::tempdir().unwrap();
write_fixture(
vault.path(),
&[
(&[".trash-backup", "Note.md"], b"1"),
(&[".helixnotes-backup", "Note.md"], b"12"),
(&["attachments-old", "Note.md"], b"123"),
(&["Work.notebook", "attachments-old", "Note.md"], b"1234"),
(&["Work.notebook", ".trash-backup", "picture.png"], b"12345"),
(&["Files", ".trash"], b"123456"),
(&["Files", ".helixnotes"], b"1234567"),
(
&["Work.notebook", ".helixnotes", "attachments", "photo.png"],
b"12345678",
),
(&[".trash", "Deleted.md"], b"excluded"),
(&["Work.notebook", ".trash", "deleted.png"], b"excluded"),
(
&["Work.notebook", ".helixnotes", "history", "Snapshot.md"],
b"excluded",
),
(
&["Work.notebook", ".helixnotes", "attachments-old", "old.png"],
b"excluded",
),
],
);
assert_stats(vault.path(), (4, 4, 10, 26, 36));
}
#[test]
fn vault_stats_ignore_excluded_names_outside_the_vault() {
let locations: &[&[&str]] = &[
&[".trash", "vault"],
&[".helixnotes", "history", "vault"],
&[".helixnotes", "attachments-old", "vault"],
&[".trash"],
&[".helixnotes"],
];
for components in locations {
let root = tempfile::tempdir().unwrap();
let mut vault = root.path().to_path_buf();
for component in *components {
vault.push(component);
}
write_fixture(
&vault,
&[
(&["Note.md"], b"hello"),
(&["Notebook", "photo.png"], b"123"),
(&[".helixnotes", "attachments", "photo.png"], b"12"),
(&[".helixnotes", "history", "Snapshot.md"], b"excluded"),
(&[".trash", "Deleted.md"], b"excluded"),
],
);
assert_stats(&vault, (1, 2, 5, 5, 10));
}
}
}
#[tauri::command]
pub fn import_obsidian(app: AppHandle) -> Result<(), String> {
let vault_path = {
@@ -3840,7 +4015,7 @@ mod mutation_order_tests {
.collect::<Vec<_>>(),
["Alpha"]
);
let graph = get_graph_data(app.state(), a.clone()).unwrap();
let graph = get_graph_data(app.state(), a.clone()).await.unwrap();
assert_eq!(
graph
.nodes
+1
View File
@@ -162,6 +162,7 @@ pub fn run() {
commands::restore_external_vault,
commands::remove_vault,
commands::get_app_config,
#[cfg(desktop)]
commands::complete_window_close,
commands::set_theme,
commands::set_system_themes,
+215 -7
View File
@@ -1,4 +1,4 @@
use crate::types::{SearchMatch, SearchPage, SearchResult};
use crate::types::{SearchMatch, SearchPage, SearchResult, SearchTarget, SearchTargetField};
use crate::vault::frontmatter;
use crate::vault::operations::helixnotes_dir;
use parking_lot::{Condvar, Mutex};
@@ -29,6 +29,7 @@ const INDEX_BATCH_DELAY: Duration = Duration::from_millis(300);
const MAX_PAGE_SIZE: usize = 100;
const SNIPPET_CHARS: usize = 240;
const SNIPPET_LEADING_CHARS: usize = 80;
const TARGET_CONTEXT_CHARS: usize = 32;
/// Per-vault search index dir in local app-data, kept out of the (possibly synced) vault.
#[cfg(desktop)]
@@ -198,7 +199,7 @@ fn utf16_matches(text: &str, mut matches: Vec<SearchMatch>, prefix: usize) -> Ve
matches
}
fn title_matches(
fn title_byte_matches(
title: &str,
terms: &[SearchTerm],
analyzer: &mut TextAnalyzer,
@@ -211,14 +212,69 @@ fn title_matches(
push_match(&mut matches, token.offset_from, token.offset_to);
}
}
utf16_matches(title, matches, 0)
matches
}
fn first_tag_match(
tags: &str,
terms: &[SearchTerm],
analyzer: &mut TextAnalyzer,
) -> Option<SearchMatch> {
let mut first: Option<SearchMatch> = None;
let mut stream = analyzer.token_stream(tags);
while stream.advance() {
let token = stream.token();
if first
.as_ref()
.is_some_and(|range| token.offset_from > range.end)
{
break;
}
if terms.iter().any(|term| term.matches(&token.text)) {
if let Some(range) = &mut first {
range.end = range.end.max(token.offset_to);
} else {
first = Some(SearchMatch {
start: token.offset_from,
end: token.offset_to,
});
}
}
}
first
}
fn match_target(text: &str, field: SearchTargetField, start: usize, end: usize) -> SearchTarget {
let end = text[start..end]
.char_indices()
.nth(SNIPPET_CHARS)
.map_or(end, |(offset, _)| start + offset);
let before = text[..start]
.char_indices()
.rev()
.nth(TARGET_CONTEXT_CHARS - 1)
.map_or(0, |(offset, _)| offset);
let after = text[end..]
.char_indices()
.nth(TARGET_CONTEXT_CHARS)
.map_or(text.len(), |(offset, _)| end + offset);
let matched = &text[start..end];
let utf16_start = text[..start].encode_utf16().count();
SearchTarget {
field,
start: utf16_start,
end: utf16_start + matched.encode_utf16().count(),
text: matched.to_owned(),
before: text[before..start].to_owned(),
after: text[end..after].to_owned(),
}
}
fn body_snippet(
body: &str,
terms: &[SearchTerm],
analyzer: &mut TextAnalyzer,
) -> (String, Vec<SearchMatch>) {
) -> (String, Vec<SearchMatch>, Option<SearchTarget>) {
let mut stream = analyzer.token_stream(body);
let mut start = 0;
let mut end = body
@@ -253,6 +309,14 @@ fn body_snippet(
token.offset_to.min(end) - start,
);
}
let target = matches.first().map(|range| {
match_target(
body,
SearchTargetField::Body,
start + range.start,
start + range.end,
)
});
let context = &body[start..end];
let prefix = usize::from(start > 0);
let matches = utf16_matches(context, matches, prefix);
@@ -264,7 +328,7 @@ fn body_snippet(
if end < body.len() {
snippet.push('…');
}
(snippet, matches)
(snippet, matches, target)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -886,8 +950,21 @@ impl SearchIndex {
.get_first(self.body_field)
.and_then(|v| v.as_str())
.unwrap_or("");
let title_matches = title_matches(title, &terms, &mut analyzer);
let (snippet, snippet_matches) = body_snippet(body, &terms, &mut analyzer);
let title_ranges = title_byte_matches(title, &terms, &mut analyzer);
let (snippet, snippet_matches, body_target) = body_snippet(body, &terms, &mut analyzer);
let match_target = body_target
.or_else(|| {
title_ranges.first().map(|range| {
match_target(title, SearchTargetField::Title, range.start, range.end)
})
})
.or_else(|| {
let tags = doc.get_first(self.tags_field).and_then(|v| v.as_str())?;
first_tag_match(tags, &terms, &mut analyzer).map(|range| {
match_target(tags, SearchTargetField::Tags, range.start, range.end)
})
});
let title_matches = utf16_matches(title, title_ranges, 0);
results.push(SearchResult {
path: path.to_owned(),
title: title.to_owned(),
@@ -895,6 +972,7 @@ impl SearchIndex {
score,
title_matches,
snippet_matches,
match_target,
});
}
Ok(SearchPage { results, total })
@@ -967,6 +1045,36 @@ mod tests {
.collect()
}
fn assert_target<'a>(
result: &'a SearchResult,
source: &str,
field: SearchTargetField,
matched: &str,
) -> &'a SearchTarget {
let target = result.match_target.as_ref().unwrap();
assert_eq!(target.field, field);
assert_eq!(target.text, matched);
assert!(target.text.chars().count() <= SNIPPET_CHARS);
let utf16: Vec<_> = source.encode_utf16().collect();
assert_eq!(
String::from_utf16(&utf16[target.start..target.end]).unwrap(),
matched
);
let before = String::from_utf16(&utf16[..target.start]).unwrap();
let after = String::from_utf16(&utf16[target.end..]).unwrap();
assert!(before.ends_with(&target.before));
assert!(after.starts_with(&target.after));
assert_eq!(
target.before.chars().count(),
before.chars().count().min(TARGET_CONTEXT_CHARS)
);
assert_eq!(
target.after.chars().count(),
after.chars().count().min(TARGET_CONTEXT_CHARS)
);
target
}
#[test]
fn exact_and_title_matches_outrank_prefix_and_typo_body_matches() {
let vault = TestVault::new();
@@ -1126,6 +1234,11 @@ mod tests {
vec!["Café", "東京"]
);
assert_eq!(result.title_matches[0], SearchMatch { start: 3, end: 7 });
let target = assert_target(&result, &body, SearchTargetField::Body, "Café");
assert_eq!(
target.start,
body[..body.find("Café").unwrap()].encode_utf16().count()
);
// Prefixes highlight the actual matched indexed token, not an invented
// exact substring; overlapping CJK uni/bigrams become one range.
let prefix = index.search("straw", 20, 0).unwrap().results.remove(0);
@@ -1133,11 +1246,15 @@ mod tests {
highlighted(&prefix.snippet, &prefix.snippet_matches),
vec!["strawberry"]
);
assert_target(&prefix, &body, SearchTargetField::Body, "strawberry");
let fuzzy = index.search("strawberyr", 20, 0).unwrap().results.remove(0);
assert_target(&fuzzy, &body, SearchTargetField::Body, "strawberry");
let cjk = index.search("東京大学", 20, 0).unwrap().results.remove(0);
assert_eq!(
highlighted(&cjk.snippet, &cjk.snippet_matches),
vec!["東京大学"]
);
assert_target(&cjk, &body, SearchTargetField::Body, "東京大学");
}
#[test]
@@ -1163,20 +1280,99 @@ mod tests {
unicode.title_matches,
vec![SearchMatch { start: 3, end: 11 }]
);
let target = assert_target(&unicode, "🦊 İSTANBUL", SearchTargetField::Body, "İSTANBUL");
assert_eq!((target.start, target.end), (3, 11));
let long = index.search("prefix", 20, 0).unwrap().results.remove(0);
assert_eq!(long.snippet.chars().count(), SNIPPET_CHARS + 1);
assert_eq!(
highlighted(&long.snippet, &long.snippet_matches),
vec![long_word[..SNIPPET_CHARS].to_owned()]
);
assert_target(
&long,
&long_word,
SearchTargetField::Body,
&long_word[..SNIPPET_CHARS],
);
for query in ["heading", "watermelon"] {
let result = index.search(query, 20, 0).unwrap().results.remove(0);
assert_eq!(result.path, tagged.to_str().unwrap());
assert_eq!(result.snippet, "Plain body without the requested token.");
assert!(result.snippet_matches.is_empty());
let (field, text) = if query == "heading" {
(SearchTargetField::Title, "Heading")
} else {
(SearchTargetField::Tags, "watermelon")
};
assert_target(&result, text, field, text);
}
}
#[test]
fn targets_prefer_first_body_then_title_then_stored_tags() {
let vault = TestVault::new();
let title = "🦊 Watermelon Strawberry";
let body = "quiet 🦊 STRAWBERRY strawberry";
let tags = "watermelon pineapple strawberry 東京大学";
fs::write(
vault.0.join("priority.md"),
format!(
"---\ntitle: '{title}'\ntags: [watermelon, pineapple, strawberry, 東京大学]\n---\n{body}"
),
)
.unwrap();
let index = vault.index();
index.rebuild(vault.0.to_str().unwrap()).unwrap();
let body_hit = index.search("straw", 20, 0).unwrap().results.remove(0);
let target = assert_target(&body_hit, body, SearchTargetField::Body, "STRAWBERRY");
assert_eq!((target.start, target.end), (9, 19));
let title_hit = index.search("watermelno", 20, 0).unwrap().results.remove(0);
let target = assert_target(&title_hit, title, SearchTargetField::Title, "Watermelon");
assert_eq!((target.start, target.end), (3, 13));
let tags_hit = index.search("pineap", 20, 0).unwrap().results.remove(0);
let target = assert_target(&tags_hit, tags, SearchTargetField::Tags, "pineapple");
assert_eq!((target.start, target.end), (11, 20));
let cjk_tags = index.search("東京大学", 20, 0).unwrap().results.remove(0);
assert_target(&cjk_tags, tags, SearchTargetField::Tags, "東京大学");
}
#[test]
fn title_and_tag_targets_bound_unicode_text_and_context() {
let vault = TestVault::new();
let long_title = format!("heading{}", "é".repeat(300));
let long_tag = format!("category{}", "é".repeat(300));
let title = format!("{} {long_title} {}", "🦊".repeat(40), "🌊".repeat(40));
let tags = format!(
"{} {long_tag} {} pineapple {}",
"🦊".repeat(40),
"🌊".repeat(40),
"🚀".repeat(40)
);
fs::write(
vault.0.join("bounded.md"),
format!("---\ntitle: '{title}'\ntags: ['{tags}']\n---\nPlain content."),
)
.unwrap();
let index = vault.index();
index.rebuild(vault.0.to_str().unwrap()).unwrap();
for (query, source, field, word) in [
("head", &title, SearchTargetField::Title, &long_title),
("categ", &tags, SearchTargetField::Tags, &long_tag),
] {
let result = index.search(query, 20, 0).unwrap().results.remove(0);
let matched: String = word.chars().take(SNIPPET_CHARS).collect();
let target = assert_target(&result, source, field, &matched);
assert_eq!(target.start, 81);
assert_eq!(target.end, 81 + SNIPPET_CHARS);
assert_eq!(target.before, format!("{} ", "🦊".repeat(31)));
assert_eq!(target.after, "é".repeat(TARGET_CONTEXT_CHARS));
}
let result = index.search("pineap", 20, 0).unwrap().results.remove(0);
let target = assert_target(&result, &tags, SearchTargetField::Tags, "pineapple");
assert_eq!(target.before, format!("{} ", "🌊".repeat(31)));
assert_eq!(target.after, format!(" {}", "🚀".repeat(31)));
}
#[test]
fn stored_snippets_survive_reopen_and_refresh_without_reading_result_files() {
let vault = TestVault::new();
@@ -1197,6 +1393,12 @@ mod tests {
highlighted(&cached.snippet, &cached.snippet_matches),
vec!["pineapple"]
);
assert_target(
&cached,
"🦊 pineapple context",
SearchTargetField::Body,
"pineapple",
);
write_search_note(
&vault,
"cached.md",
@@ -1211,6 +1413,12 @@ mod tests {
highlighted(&refreshed.snippet, &refreshed.snippet_matches),
vec!["watermelon"]
);
assert_target(
&refreshed,
"🦊 watermelon changed context",
SearchTargetField::Body,
"watermelon",
);
index.refresh_vault(vault.0.to_str().unwrap()).unwrap();
assert_eq!(index.search("watermelno", 20, 0).unwrap().total, 1);
}
+22
View File
@@ -457,6 +457,27 @@ pub struct SearchMatch {
pub end: usize,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum SearchTargetField {
Body,
Title,
Tags,
}
/// Half-open UTF-16 offsets into the indexed field, with exact source context.
/// Body excludes frontmatter; tags use the indexed space-joined values.
/// Text is capped at 240 Unicode scalars and each context at 32 scalars.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SearchTarget {
pub field: SearchTargetField,
pub start: usize,
pub end: usize,
pub text: String,
pub before: String,
pub after: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
pub path: String,
@@ -465,6 +486,7 @@ pub struct SearchResult {
pub score: f32,
pub title_matches: Vec<SearchMatch>,
pub snippet_matches: Vec<SearchMatch>,
pub match_target: Option<SearchTarget>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
+1 -1
View File
@@ -1,7 +1,7 @@
{
"$schema": "../node_modules/@tauri-apps/cli/config.schema.json",
"productName": "HelixNotes",
"version": "1.3.5",
"version": "1.3.6",
"identifier": "com.helixnotes.app",
"build": {
"frontendDist": "../build",