Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

feat(sidebar): remote file search, right-edge hover reveal and rendered Markdown previews

RoversX2da8600077de
diff --git a/Cargo.lock b/Cargo.lock
index d5899474..53aaed62 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -9676,7 +9676,6 @@ dependencies = [
"frecency",
"futures",
"hdrhistogram",
- "ignore",
"image",
"k9",
"lazy_static",
@@ -9719,6 +9718,7 @@ dependencies = [
"termwiz-funcs",
"textwrap",
"thinkterm-core",
+ "thinkterm-file-index",
"thinkterm-i18n",
"thinkterm-proto",
"thinkterm-render",
diff --git a/thinkterm-i18n/i18n/de-DE.ftl b/thinkterm-i18n/i18n/de-DE.ftl
index ce31ecb9..c409031c 100644
--- a/thinkterm-i18n/i18n/de-DE.ftl
+++ b/thinkterm-i18n/i18n/de-DE.ftl
@@ -342,6 +342,7 @@ right-rename = Umbenennen…
right-move-trash = In den Papierkorb legen
right-open-with = Öffnen mit { $app }
right-open-with-other = Öffnen mit anderer App…
+right-open-with-menu = Öffnen mit
right-create-vault = Vault erstellen
right-create = Erstellen
right-choose-vault = Vault auswählen
@@ -388,7 +389,6 @@ right-connect = Verbinden
right-connecting = Verbinden…
right-connection-failed = Verbindung fehlgeschlagen
right-retry = Erneut versuchen
-right-remote-files = Remote-Dateien
right-loading-remote-directory = Remote-Verzeichnis wird geladen…
right-more-entries = Weitere Einträge werden nicht angezeigt
right-more-transfer = 1 weitere Übertragung
@@ -397,6 +397,7 @@ right-filter-files = Dateien filtern
right-indexing-files = Dateien werden indexiert…
right-searching-files = Dateien werden durchsucht…
right-no-matching-files = Keine passenden Dateien
+right-remote-search-needs-update = Für die Suche auf diesem Host braucht es dort ein neueres ThinkTerm. Aktualisieren Sie es dort und laden Sie dann neu.
right-new-snippet = Neues Snippet
right-search = Suchen
right-no-snippets = Keine Snippets
diff --git a/thinkterm-i18n/i18n/en-US.ftl b/thinkterm-i18n/i18n/en-US.ftl
index a916d2b4..43870d78 100644
--- a/thinkterm-i18n/i18n/en-US.ftl
+++ b/thinkterm-i18n/i18n/en-US.ftl
@@ -342,6 +342,7 @@ right-rename = Rename…
right-move-trash = Move to Trash
right-open-with = Open With { $app }
right-open-with-other = Open With Other…
+right-open-with-menu = Open With
right-create-vault = Create Vault
right-create = Create
right-choose-vault = Choose Vault
@@ -388,7 +389,6 @@ right-connect = Connect
right-connecting = Connecting…
right-connection-failed = Connection failed
right-retry = Retry
-right-remote-files = Remote Files
right-loading-remote-directory = Loading remote directory…
right-more-entries = More entries are not shown
right-more-transfer = 1 more transfer
@@ -397,6 +397,7 @@ right-filter-files = Filter files
right-indexing-files = Indexing files…
right-searching-files = Searching files…
right-no-matching-files = No matching files
+right-remote-search-needs-update = Searching this host needs a newer ThinkTerm there. Update it, then refresh.
right-new-snippet = New Snippet
right-search = Search
right-no-snippets = No snippets
diff --git a/thinkterm-i18n/i18n/fr-FR.ftl b/thinkterm-i18n/i18n/fr-FR.ftl
index f18ae996..ecae4cd2 100644
--- a/thinkterm-i18n/i18n/fr-FR.ftl
+++ b/thinkterm-i18n/i18n/fr-FR.ftl
@@ -342,6 +342,7 @@ right-rename = Renommer…
right-move-trash = Placer dans la corbeille
right-open-with = Ouvrir avec { $app }
right-open-with-other = Ouvrir avec une autre application…
+right-open-with-menu = Ouvrir avec
right-create-vault = Créer un coffre
right-create = Créer
right-choose-vault = Choisir un coffre
@@ -388,7 +389,6 @@ right-connect = Connecter
right-connecting = Connexion…
right-connection-failed = Échec de la connexion
right-retry = Réessayer
-right-remote-files = Fichiers distants
right-loading-remote-directory = Chargement du dossier distant…
right-more-entries = D’autres éléments ne sont pas affichés
right-more-transfer = 1 transfert supplémentaire
@@ -397,6 +397,7 @@ right-filter-files = Filtrer les fichiers
right-indexing-files = Indexation des fichiers…
right-searching-files = Recherche de fichiers…
right-no-matching-files = Aucun fichier correspondant
+right-remote-search-needs-update = La recherche sur cet hôte demande un ThinkTerm plus récent là-bas. Mettez-le à jour, puis actualisez.
right-new-snippet = Nouvel extrait
right-search = Rechercher
right-no-snippets = Aucun extrait
diff --git a/thinkterm-i18n/i18n/ja-JP.ftl b/thinkterm-i18n/i18n/ja-JP.ftl
index 7ef145c3..9d37a1b8 100644
--- a/thinkterm-i18n/i18n/ja-JP.ftl
+++ b/thinkterm-i18n/i18n/ja-JP.ftl
@@ -333,6 +333,7 @@ right-rename = 名前を変更…
right-move-trash = ゴミ箱に入れる
right-open-with = { $app } で開く
right-open-with-other = 別のアプリで開く…
+right-open-with-menu = アプリで開く
right-create-vault = 保管庫を作成
right-create = 作成
right-choose-vault = 保管庫を選択
@@ -379,7 +380,6 @@ right-connect = 接続
right-connecting = 接続中…
right-connection-failed = 接続に失敗しました
right-retry = 再試行
-right-remote-files = リモートファイル
right-loading-remote-directory = リモートディレクトリを読み込み中…
right-more-entries = その他の項目は表示されません
right-more-transfer = 他 1 件の転送
@@ -388,6 +388,7 @@ right-filter-files = ファイルを絞り込む
right-indexing-files = ファイルを索引中…
right-searching-files = ファイルを検索中…
right-no-matching-files = 一致するファイルはありません
+right-remote-search-needs-update = このホストで検索するには、そちらの ThinkTerm を新しくする必要があります。更新してから再読み込みしてください。
right-new-snippet = 新しいスニペット
right-search = 検索
right-no-snippets = スニペットはありません
diff --git a/thinkterm-i18n/i18n/zh-CN.ftl b/thinkterm-i18n/i18n/zh-CN.ftl
index 19b52a6f..98ad46f9 100644
--- a/thinkterm-i18n/i18n/zh-CN.ftl
+++ b/thinkterm-i18n/i18n/zh-CN.ftl
@@ -333,6 +333,7 @@ right-rename = 重命名…
right-move-trash = 移到废纸篓
right-open-with = 使用 { $app } 打开
right-open-with-other = 使用其他应用打开…
+right-open-with-menu = 打开方式
right-create-vault = 创建库
right-create = 创建
right-choose-vault = 选择库
@@ -379,7 +380,6 @@ right-connect = 连接
right-connecting = 正在连接…
right-connection-failed = 连接失败
right-retry = 重试
-right-remote-files = 远程文件
right-loading-remote-directory = 正在加载远程目录…
right-more-entries = 更多条目未显示
right-more-transfer = 还有 1 项传输
@@ -388,6 +388,7 @@ right-filter-files = 筛选文件
right-indexing-files = 正在索引文件…
right-searching-files = 正在搜索文件…
right-no-matching-files = 没有匹配的文件
+right-remote-search-needs-update = 在这台主机上搜索需要更新那里的 ThinkTerm。更新后点刷新。
right-new-snippet = 新建片段
right-search = 搜索
right-no-snippets = 没有片段
diff --git a/wezterm-gui/Cargo.toml b/wezterm-gui/Cargo.toml
index c25a375e..b081e9ad 100644
--- a/wezterm-gui/Cargo.toml
+++ b/wezterm-gui/Cargo.toml
@@ -68,7 +68,7 @@ thinkterm-i18n.workspace = true
frecency.workspace = true
futures.workspace = true
hdrhistogram.workspace = true
-ignore.workspace = true
+thinkterm-file-index.workspace = true
image.workspace = true
lazy_static.workspace = true
lfucache.workspace = true
diff --git a/wezterm-gui/src/markdown_editor/host.rs b/wezterm-gui/src/markdown_editor/host.rs
index 8c8f8406..eda6691e 100644
--- a/wezterm-gui/src/markdown_editor/host.rs
+++ b/wezterm-gui/src/markdown_editor/host.rs
@@ -126,6 +126,10 @@ pub(crate) struct NoteHostState {
/// link had no cached resolution; a background parse round trip owes us
/// the authoritative vault lookup.
links_resolution_pending: bool,
+ /// A resolution was asked for while a parse was already running: that
+ /// parse cannot have used what prompted it, so its result must not clear
+ /// `links_resolution_pending`.
+ links_requested_during_parse: bool,
/// Memoized `caret_reveal_start` for the current projection, keyed by
/// (revision, caret). `active_syntax` is a linear scan over every syntax
/// node and refresh runs once per paint; a pure scroll frame must not pay
@@ -207,6 +211,7 @@ impl Default for NoteHostState {
parse_requested_revision: None,
parse_in_flight_revision: None,
links_resolution_pending: false,
+ links_requested_during_parse: false,
reveal_key_cache: None,
background_wrap_preferred: false,
wrap_work_estimate: 0,
@@ -263,6 +268,7 @@ impl NoteHostState {
self.parse_requested_revision = None;
self.parse_in_flight_revision = None;
self.links_resolution_pending = false;
+ self.links_requested_during_parse = false;
self.reveal_key_cache = None;
self.reset_wrap_work_metrics();
self.background_wrap_requested_key = None;
@@ -322,6 +328,7 @@ impl NoteHostState {
self.parse_requested_revision = None;
self.parse_in_flight_revision = None;
self.links_resolution_pending = false;
+ self.links_requested_during_parse = false;
self.reveal_key_cache = None;
self.reset_wrap_work_metrics();
self.background_wrap_requested_key = None;
@@ -496,7 +503,7 @@ impl NoteHostState {
}
self.visual_key = None;
}
- let caret = self.view.selection.focus.byte;
+ let caret = self.reveal_caret();
let active_start = match self.reveal_key_cache {
Some((cached_revision, cached_caret, result))
if cached_revision == revision && cached_caret == caret =>
@@ -520,7 +527,7 @@ impl NoteHostState {
source,
&self.projection,
self.view.mode,
- self.view.selection.focus.byte,
+ self.reveal_caret(),
));
self.refresh_wrap_work_metrics(source.len());
self.visual_key = Some(key);
@@ -531,6 +538,16 @@ impl NoteHostState {
}
}
+ /// Where Live Preview reveals the Markdown under the caret. A read-only
+ /// document is a reading view: selecting in it reveals nothing.
+ fn reveal_caret(&self) -> usize {
+ if self.view.mode == super::EditorMode::ReadOnly {
+ usize::MAX
+ } else {
+ self.view.selection.focus.byte
+ }
+ }
+
pub(crate) fn background_parse_request(
&mut self,
) -> Option<(
@@ -559,7 +576,7 @@ impl NoteHostState {
snapshot.revision,
snapshot.source,
self.view.mode,
- self.view.selection.focus.byte,
+ self.reveal_caret(),
document,
))
}
@@ -579,7 +596,7 @@ impl NoteHostState {
let display_revision = self.display_snapshot().map(|snapshot| snapshot.revision);
if display_revision != Some(revision)
|| self.view.mode != mode
- || self.view.selection.focus.byte != caret
+ || self.reveal_caret() != caret
{
return false;
}
@@ -590,8 +607,9 @@ impl NoteHostState {
let active_start = projection.caret_reveal_start(caret);
self.projection = projection;
self.reveal_key_cache = Some((revision, caret, active_start));
- // The worker performed the authoritative vault-link resolution.
- self.links_resolution_pending = false;
+ // The worker performed the authoritative vault-link resolution, unless
+ // more was asked for after it started.
+ self.links_resolution_pending = std::mem::take(&mut self.links_requested_during_parse);
self.visual = Arc::new(visual);
self.refresh_wrap_work_metrics(display_source_len);
self.projection_revision = Some(revision);
@@ -707,6 +725,15 @@ impl NoteHostState {
true
}
+ /// Ask the next background parse to resolve wiki links again, as when
+ /// the files they could name have just become known.
+ pub(crate) fn request_link_resolution(&mut self) {
+ self.links_resolution_pending = true;
+ if self.parse_in_flight_revision.is_some() {
+ self.links_requested_during_parse = true;
+ }
+ }
+
pub(crate) fn background_parse_pending(&self) -> bool {
if self.links_resolution_pending {
return self.session.is_some();
@@ -1207,6 +1234,37 @@ mod state_tests {
);
}
+ #[test]
+ fn read_only_documents_reveal_nothing_under_the_caret() {
+ let source = "A **bold** word\n\n## Heading\n";
+ let mut host = host_with_source(source);
+ host.view.mode = super::super::EditorMode::ReadOnly;
+ host.refresh_projection();
+ let key = host.visual_key;
+ let text = |host: &NoteHostState| {
+ host.visual
+ .lines
+ .iter()
+ .map(crate::markdown_editor::surface::VisualLine::text)
+ .collect::<Vec<_>>()
+ .join("\n")
+ };
+ let rendered = text(&host);
+ for caret in [
+ source.find("bold").unwrap(),
+ source.find("Heading").unwrap(),
+ ] {
+ host.view.selection = SourceSelection::caret(caret);
+ host.refresh_projection();
+ assert_eq!(
+ host.visual_key, key,
+ "a caret at {caret} must not re-lay out"
+ );
+ assert_eq!(text(&host), rendered);
+ }
+ assert!(!rendered.contains("**") && !rendered.contains("## "));
+ }
+
#[test]
fn small_note_wiki_links_resolve_via_background_parse_not_ui_thread() {
let source = "See [[Other Note]] for details.";
@@ -1228,6 +1286,28 @@ mod state_tests {
assert!(host.background_parse_request().is_none());
}
+ #[test]
+ fn a_link_resolution_asked_for_mid_parse_outlives_that_parse() {
+ let source = "See [[Other Note]] for details.";
+ let mut host = host_with_source(source);
+ let (revision, _, mode, caret, _) =
+ host.background_parse_request().expect("resolution request");
+ // The files the links may name arrive while that parse runs.
+ host.request_link_resolution();
+ let resolved = MarkdownProjection::parse(source);
+ let visual = build_visual_document(source, &resolved, mode, caret);
+ assert!(host.apply_background_parse(revision, mode, caret, resolved, visual));
+ assert!(
+ host.background_parse_pending(),
+ "the parse that just landed never saw the new files"
+ );
+ let (revision, _, mode, caret, _) = host.background_parse_request().expect("another round");
+ let resolved = MarkdownProjection::parse(source);
+ let visual = build_visual_document(source, &resolved, mode, caret);
+ assert!(host.apply_background_parse(revision, mode, caret, resolved, visual));
+ assert!(!host.background_parse_pending());
+ }
+
#[test]
fn edited_duplicate_embeds_cannot_amplify_cached_payload_or_lose_first_content() {
for payload_bytes in [16, 128 * 1024] {
diff --git a/wezterm-gui/src/markdown_editor/projection.rs b/wezterm-gui/src/markdown_editor/projection.rs
index 88a76123..0a5c89d7 100644
--- a/wezterm-gui/src/markdown_editor/projection.rs
+++ b/wezterm-gui/src/markdown_editor/projection.rs
@@ -190,6 +190,9 @@ pub(crate) struct MarkdownProjection {
pub objects: Vec<ProjectedObject>,
pub blocks: Vec<ProjectionBlock>,
pub syntax: Vec<MarkdownSyntaxNode>,
+ /// Where each soft line break's `\n` sits, ascending: a line ending
+ /// inside a paragraph that CommonMark renders as a space.
+ pub soft_breaks: Vec<usize>,
}
#[derive(Debug, Clone, Default)]
@@ -253,6 +256,7 @@ impl MarkdownProjection {
let mut table: Option<TableBuilder> = None;
for (event, range) in Parser::new_ext(source, options).into_offset_iter() {
+ let soft_break = matches!(event, Event::SoftBreak);
match event {
Event::Start(tag) => {
if let Some(kind) = syntax_kind_for_tag(&tag) {
@@ -476,6 +480,13 @@ impl MarkdownProjection {
});
}
Event::SoftBreak | Event::HardBreak => {
+ if soft_break {
+ if let Some(newline) =
+ source.get(range.clone()).and_then(|text| text.find('\n'))
+ {
+ projection.soft_breaks.push(range.start + newline);
+ }
+ }
projection.text.push(ProjectedText {
source: range,
text: "\n".to_string(),
@@ -596,12 +607,14 @@ impl MarkdownProjection {
})
}
- pub(crate) fn resolve_vault_links(&mut self, vault_root: &Path, current_note: &str) {
- if !self
- .objects
+ pub(crate) fn has_wiki_links(&self) -> bool {
+ self.objects
.iter()
.any(|object| matches!(object, ProjectedObject::WikiLink { .. }))
- {
+ }
+
+ pub(crate) fn resolve_vault_links(&mut self, vault_root: &Path, current_note: &str) {
+ if !self.has_wiki_links() {
return;
}
let paths = match crate::markdown_editor::vault_file_paths(vault_root) {
@@ -611,6 +624,19 @@ impl MarkdownProjection {
return;
}
};
+ self.resolve_links_among(Some(vault_root), paths, current_note);
+ }
+
+ /// Resolve wiki links against `paths`, files relative to one root with
+ /// `/` separators, as `resolve_vault_links` does against every file of a
+ /// Notebook. Embeds are rendered from `vault_root`; without one (files
+ /// that are not on this machine) they stay unrendered.
+ pub(crate) fn resolve_links_among(
+ &mut self,
+ vault_root: Option<&Path>,
+ paths: Vec<String>,
+ current_note: &str,
+ ) {
let index = VaultLinkIndex::new(paths);
let mut embed_budget = EmbedBudget::default();
let current_directory = Path::new(current_note)
@@ -634,7 +660,7 @@ impl MarkdownProjection {
*ambiguous_paths = resolution.ambiguous_paths;
rendered_lines.clear();
*embed_truncation = None;
- if *embed {
+ if let (true, Some(vault_root)) = (*embed, vault_root) {
if let Some(path) = resolved_path.as_deref() {
let mut visiting = HashSet::new();
let preview = render_embedded_note(
diff --git a/wezterm-gui/src/markdown_editor/surface.rs b/wezterm-gui/src/markdown_editor/surface.rs
index 7a62f8b0..82c4cc66 100644
--- a/wezterm-gui/src/markdown_editor/surface.rs
+++ b/wezterm-gui/src/markdown_editor/surface.rs
@@ -944,6 +944,21 @@ pub(crate) fn build_visual_document(
}
if source.as_bytes()[idx] == b'\n' {
+ // Read-only renders a document as its reader would see it: a
+ // line ending inside a paragraph is a space, not a new line.
+ if mode == EditorMode::ReadOnly && projection.soft_breaks.binary_search(&idx).is_ok() {
+ append_run(
+ &mut line,
+ VisualRun {
+ source: idx..idx + 1,
+ text: Arc::new(" ".to_string()),
+ style: InlineStyle::default(),
+ atomic: true,
+ },
+ );
+ idx += 1;
+ continue;
+ }
let next = idx + 1;
flush_line(&mut lines, &mut line, next);
idx = next;
@@ -1256,6 +1271,44 @@ mod tests {
);
}
+ #[test]
+ fn read_only_joins_soft_line_breaks_into_one_line() {
+ let source = "One line\nwraps here.\n\n- item that\n continues\n\n> quoted\n> more\n\nhard \nbreak\n";
+ let projection = MarkdownProjection::parse(source);
+ let texts = |mode| {
+ build_visual_document(source, &projection, mode, 0)
+ .lines
+ .iter()
+ .map(VisualLine::text)
+ .filter(|text| !text.trim().is_empty())
+ .collect::<Vec<_>>()
+ };
+ let read_only = texts(EditorMode::ReadOnly);
+ assert!(
+ read_only.contains(&"One line wraps here.".to_string()),
+ "{:?}",
+ read_only
+ );
+ assert!(
+ read_only
+ .iter()
+ .any(|line| line.ends_with("item that continues")),
+ "{:?}",
+ read_only
+ );
+ assert!(
+ read_only.iter().any(|line| line.contains("quoted more")),
+ "{:?}",
+ read_only
+ );
+ // A hard break stays a break.
+ assert!(read_only.iter().any(|line| line.starts_with("hard")));
+ assert!(read_only.contains(&"break".to_string()), "{:?}", read_only);
+ // Live Preview keeps the source's lines, as the Note always has.
+ let live = texts(EditorMode::LivePreview);
+ assert!(live.contains(&"One line".to_string()), "{:?}", live);
+ }
+
#[test]
fn live_preview_projects_table_and_task_object() {
let source = "- [x] done\n\n| A | B |\n| - | - |\n| 1 | 2 |\n";
diff --git a/wezterm-gui/src/native_settings.rs b/wezterm-gui/src/native_settings.rs
index 66909ac8..1289dc04 100644
--- a/wezterm-gui/src/native_settings.rs
+++ b/wezterm-gui/src/native_settings.rs
@@ -448,6 +448,9 @@ pub(crate) struct NativeChromeSettings {
pub(crate) workspace_sidebar_width: Option<usize>,
/// Hover-reveal of the collapsed left sidebar; `None` means enabled.
pub(crate) workspace_sidebar_hover_reveal: Option<bool>,
+ /// Whether the file preview shows a Markdown file rendered (the default)
+ /// or as source; the last choice is kept.
+ pub(crate) right_sidebar_markdown_preview_rendered: Option<bool>,
pub(crate) right_sidebar_width: Option<usize>,
pub(crate) right_sidebar_file_preview_width: Option<usize>,
pub(crate) right_sidebar_note_pane_width: Option<usize>,
@@ -1420,6 +1423,19 @@ pub(crate) fn save_right_sidebar_file_preview_width(width: usize) -> anyhow::Res
save(&settings)
}
+pub(crate) fn right_sidebar_markdown_preview_rendered() -> bool {
+ load_shared()
+ .chrome
+ .right_sidebar_markdown_preview_rendered
+ .unwrap_or(true)
+}
+
+pub(crate) fn save_right_sidebar_markdown_preview_rendered(rendered: bool) -> anyhow::Result<()> {
+ let mut settings = load();
+ settings.chrome.right_sidebar_markdown_preview_rendered = Some(rendered);
+ save(&settings)
+}
+
pub(crate) fn save_right_sidebar_note_pane_width(width: usize) -> anyhow::Result<()> {
let mut settings = load();
settings.chrome.right_sidebar_note_pane_width = Some(width);
diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs
index 7ab4b1cb..34d70983 100644
--- a/wezterm-gui/src/termwindow/mod.rs
+++ b/wezterm-gui/src/termwindow/mod.rs
@@ -626,6 +626,11 @@ pub(crate) enum NoteEditorCommand {
#[derive(Clone, Debug)]
pub(crate) enum ContextMenuApplicationAction {
Note(NoteEditorCommand),
+ /// Open this local file in the file preview: one of the files an
+ /// ambiguous wiki link in rendered Markdown could mean.
+ OpenFilePreview(PathBuf),
+ /// Open this remote file in the file preview, for the same reason.
+ OpenRemoteFilePreview(remote_files::RemotePath),
EditRecordingOverlay,
ClearRecordingOverlay,
/// One entry of the Remote Hosts page's card menu, carrying the host it
@@ -815,6 +820,13 @@ pub enum UIItemType {
RightSidebarNoteCodeToggle(usize),
RightSidebarNoteCodeCopy(usize),
RightSidebarNoteBody,
+ /// The file preview's rendered Markdown and its code block controls: the
+ /// Note's items, for the preview's own surface.
+ RightSidebarFilePreviewMarkdownBody,
+ RightSidebarFilePreviewMarkdownCodeToggle(usize),
+ RightSidebarFilePreviewMarkdownCodeCopy(usize),
+ /// Switches a Markdown file preview between rendered and source.
+ RightSidebarFilePreviewMarkdownToggle,
RightSidebarNotePaneToggle,
RightSidebarNotePaneResize,
RightSidebarFilePreviewScrollTrack,
@@ -936,6 +948,32 @@ pub(crate) struct RightSidebarFileIndexEntry {
#[derive(Clone, Debug)]
pub(crate) struct RightSidebarFileIndex {
pub entries: Vec<RightSidebarFileIndexEntry>,
+ /// The files' display paths, for resolving a preview's wiki links; built
+ /// once per index on first use.
+ file_paths: std::sync::OnceLock<Arc<Vec<String>>>,
+}
+
+impl RightSidebarFileIndex {
+ pub(crate) fn new(entries: Vec<RightSidebarFileIndexEntry>) -> Self {
+ Self {
+ entries,
+ file_paths: std::sync::OnceLock::new(),
+ }
+ }
+
+ /// Every file's path relative to the root (entry 0 is the root itself).
+ pub(crate) fn file_paths(&self) -> Arc<Vec<String>> {
+ Arc::clone(self.file_paths.get_or_init(|| {
+ Arc::new(
+ self.entries
+ .iter()
+ .skip(1)
+ .filter(|entry| !entry.is_dir)
+ .map(|entry| entry.display_path.clone())
+ .collect(),
+ )
+ }))
+ }
}
/// One child of a directory we actually read. Deliberately smaller than
@@ -2687,6 +2725,12 @@ pub struct TermWindow {
/// touches `workspace_sidebar_collapsed` or `workspace_sidebar_width`:
/// the terminal must not reflow for a hover.
workspace_sidebar_hover: sidebar_hover::SidebarHoverReveal,
+ /// Hover-reveal of the collapsed right sidebar: the same machine as the
+ /// left one's, mirrored onto the right edge.
+ right_sidebar_hover: sidebar_hover::SidebarHoverReveal,
+ /// Whether the right sidebar was on screen by hover at the last step, so
+ /// arriving and leaving can open and release what the panel shows.
+ right_sidebar_hover_was_presented: bool,
/// The pointer is over the native macOS titlebar sidebar button (which
/// lives outside our view, so `current_mouse_event` cannot see it).
/// Feeds the hover-reveal as a hot zone.
@@ -2756,6 +2800,12 @@ pub struct TermWindow {
right_sidebar_note_image_failures: HashMap<RightSidebarNoteImageSource, Instant>,
right_sidebar_note_code_highlight: ui::right_sidebar::NoteCodeHighlightState,
right_sidebar_note_paint_cache: ui::right_sidebar::NotePaintCache,
+ /// Which surface's state is in the `right_sidebar_note*` fields right now;
+ /// the Note's, except inside `with_note_surface`.
+ note_surface_installed: ui::right_sidebar::NoteSurface,
+ /// The installed surface's instance; 0 for the Note.
+ note_surface_instance: u64,
+ right_sidebar_markdown_preview: Option<Box<ui::right_sidebar::MarkdownPreviewSurface>>,
right_sidebar_note_prewarm: Option<ui::right_sidebar::NotePrewarmState>,
right_sidebar_note_memory_release_token: u64,
right_sidebar_file_view: RightSidebarFileView,
@@ -2814,6 +2864,9 @@ pub struct TermWindow {
/// The preview text before display sanitization (tab expansion, control
/// stripping): the display lines are wrong for the clipboard.
right_sidebar_file_preview_raw_text: Option<String>,
+ /// Bumped whenever the preview's text changes, so rendered Markdown knows
+ /// to rebuild.
+ right_sidebar_file_preview_text_generation: u64,
right_sidebar_file_preview_max_columns: usize,
right_sidebar_file_preview_image: Option<RightSidebarFilePreviewImage>,
right_sidebar_file_preview_message: Option<String>,
@@ -2872,6 +2925,7 @@ pub struct TermWindow {
/// worded without borrowing the queued plan.
pending_local_copy_conflict_count: usize,
right_sidebar_remote_file_tree_scroll_offset: f32,
+ right_sidebar_remote_file_search: ui::right_sidebar::RemoteFileSearchState,
// Bumped to invalidate a pending periodic-rescan timer tick.
right_sidebar_file_rescan_token: u64,
// True while a keep-showing refresh build is in flight (drives the Refresh
@@ -3478,6 +3532,7 @@ impl TermWindow {
self.content_view_fade.is_some()
|| self.active_content_view_index().is_some()
|| self.workspace_sidebar_hover.needs_frames()
+ || self.right_sidebar_hover.needs_frames()
|| self.overlay_scrollbar_owes_frames()
}
@@ -3717,6 +3772,7 @@ impl TermWindow {
self.recording_overlay.cancel_interaction();
self.workspace_sidebar_swipe.cancel_immediately();
self.workspace_sidebar_hover.cancel_immediately();
+ self.right_sidebar_hover.cancel_immediately();
self.clear_workspace_space_swipe_frame_transition();
self.right_sidebar_note.native_text_input_snapshot_key = None;
window.set_native_text_input_snapshot(None);
@@ -4222,6 +4278,8 @@ impl TermWindow {
workspace_space_swipe_needs_settle_start: false,
workspace_sidebar_scrollbar_visible_until: None,
workspace_sidebar_hover: sidebar_hover::SidebarHoverReveal::default(),
+ right_sidebar_hover: sidebar_hover::SidebarHoverReveal::default(),
+ right_sidebar_hover_was_presented: false,
titlebar_sidebar_button_hovered: false,
thread_ref_groups_collapsed: std::collections::HashSet::new(),
workspace_sidebar_show_archived: false,
@@ -4262,6 +4320,9 @@ impl TermWindow {
right_sidebar_note_image_failures: HashMap::new(),
right_sidebar_note_code_highlight: ui::right_sidebar::NoteCodeHighlightState::default(),
right_sidebar_note_paint_cache: ui::right_sidebar::NotePaintCache::default(),
+ note_surface_installed: ui::right_sidebar::NoteSurface::Note,
+ note_surface_instance: 0,
+ right_sidebar_markdown_preview: None,
right_sidebar_note_prewarm: None,
right_sidebar_note_memory_release_token: 0,
right_sidebar_file_view: RightSidebarFileView::Tree,
@@ -4304,6 +4365,7 @@ impl TermWindow {
right_sidebar_file_preview_highlight_cancel: Arc::new(AtomicUsize::new(0)),
right_sidebar_file_preview_lines: Vec::new(),
right_sidebar_file_preview_raw_text: None,
+ right_sidebar_file_preview_text_generation: 0,
right_sidebar_file_preview_max_columns: 0,
right_sidebar_file_preview_image: None,
right_sidebar_file_preview_message: None,
@@ -4331,6 +4393,7 @@ impl TermWindow {
local_copy_generation: 0,
pending_local_copy_conflict_count: 0,
right_sidebar_remote_file_tree_scroll_offset: 0.0,
+ right_sidebar_remote_file_search: Default::default(),
right_sidebar_file_rescan_token: 0,
right_sidebar_file_refreshing: false,
right_sidebar_open_with_generation: 0,
@@ -4739,6 +4802,7 @@ impl TermWindow {
// settled overlay survives a resize cleanly, a mid-flight one
// would jump.
self.workspace_sidebar_hover.settle_immediately();
+ self.right_sidebar_hover.settle_immediately();
self.resize(dimensions, window_state, window, live_resizing);
Ok(true)
}
diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs
index 54fdbec6..eee349ef 100644
--- a/wezterm-gui/src/termwindow/mouseevent.rs
+++ b/wezterm-gui/src/termwindow/mouseevent.rs
@@ -940,19 +940,31 @@ impl super::TermWindow {
}
}
+ /// The right sidebar's counterpart of `update_workspace_sidebar_hover`.
+ fn update_right_sidebar_hover(&mut self, context: &dyn WindowOps) {
+ if self.step_right_sidebar_hover(std::time::Instant::now())
+ != crate::termwindow::sidebar_hover::HoverFrame::None
+ {
+ context.invalidate();
+ }
+ }
+
pub(crate) fn workspace_sidebar_hover_input(
&self,
) -> crate::termwindow::sidebar_hover::HoverInput {
use crate::termwindow::sidebar_hover::{HoverInput, PointerZone};
+ // A menu, modal or rename opened from the revealed panel holds it
+ // out; one open while it is away keeps it from revealing.
+ let overlay_open = self.modal.borrow().is_some()
+ || self.context_menu.is_some()
+ || self.native_context_menu_open
+ || self.inline_tab_rename.is_some();
let eligible = crate::native_settings::workspace_sidebar_hover_reveal_enabled()
&& self.workspace_sidebar_collapsed
&& !self.content_view_foreground()
&& !self.content_view_transition_running()
&& !self.frontend_surface_blocked()
- && self.modal.borrow().is_none()
- && self.context_menu.is_none()
- && !self.native_context_menu_open
- && self.inline_tab_rename.is_none();
+ && (!overlay_open || self.workspace_sidebar_hover.is_presented());
// Panel is checked before the hot zone on purpose: once revealed,
// the strip is inside the panel.
let pointer = match &self.current_mouse_event {
@@ -1018,6 +1030,7 @@ impl super::TermWindow {
|| self.sidebar_row_drag.is_some()
|| self.right_sidebar_file_drag.is_some()
|| self.pane_tab_drag.is_some()
+ || overlay_open
// A Space swipe on the revealed panel pins it open: retreating
// mid-gesture would slide the ground out from under the pages.
|| self.workspace_sidebar_swipe.is_active();
@@ -1381,6 +1394,48 @@ impl super::TermWindow {
changed
}
+ /// Scroll the file preview's rendered Markdown as the Note body scrolls:
+ /// vertically, and sideways (or with Shift) across a table or code block.
+ fn mouse_wheel_right_sidebar_markdown_preview(
+ &mut self,
+ event: &MouseEvent,
+ context: &dyn WindowOps,
+ ) -> bool {
+ use crate::termwindow::ui::right_sidebar::NoteSurface;
+ let shift_vertical = matches!(event.kind, WMEK::VertWheel(_))
+ && event.modifiers.contains(::window::Modifiers::SHIFT);
+ let (sideways, delta) = match event.kind {
+ WMEK::HorzWheel(_) => (true, self.sidebar_horizontal_scroll_delta(event)),
+ WMEK::VertWheel(_) if shift_vertical => {
+ (true, self.sidebar_vertical_scroll_delta(event))
+ }
+ WMEK::VertWheel(_) => (false, self.sidebar_vertical_scroll_delta(event)),
+ _ => return false,
+ };
+ let delta = delta.unwrap_or(0.0);
+ if delta.abs() <= f32::EPSILON {
+ return true;
+ }
+ let (x, y) = (event.coords.x as f32, event.coords.y as f32);
+ let changed = self
+ .with_note_surface(NoteSurface::FilePreview, |term_window| {
+ if sideways {
+ term_window.scroll_right_sidebar_note_table_at(x, y, delta)
+ || term_window
+ .right_sidebar_note
+ .scroll_code_block_at(x, y, delta)
+ } else {
+ term_window.right_sidebar_note.reveal_caret = false;
+ term_window.right_sidebar_note.scroll_by(delta)
+ }
+ })
+ .unwrap_or(false);
+ if changed {
+ context.invalidate();
+ }
+ true
+ }
+
fn mouse_wheel_right_sidebar(&mut self, event: &MouseEvent, context: &dyn WindowOps) -> bool {
if self.right_sidebar_mode == super::RightSidebarMode::Chat {
if let Some(rect) = self.right_sidebar_file_preview_rect() {
@@ -1391,6 +1446,9 @@ impl super::TermWindow {
&& y >= rect.y as isize
&& y < rect.y.saturating_add(rect.height) as isize
{
+ if self.right_sidebar_markdown_preview_rendering() {
+ return self.mouse_wheel_right_sidebar_markdown_preview(event, context);
+ }
let vert_delta = self.sidebar_vertical_scroll_delta(event).unwrap_or(0.0);
let changed = match event.kind {
WMEK::VertWheel(_) => self.scroll_right_sidebar_file_preview_by(vert_delta),
@@ -1586,6 +1644,10 @@ impl super::TermWindow {
| UIItemType::RightSidebarFilePreviewHorizontalScrollTrack
| UIItemType::RightSidebarFilePreviewHorizontalScrollThumb
| UIItemType::RightSidebarFilePreviewText
+ | UIItemType::RightSidebarFilePreviewMarkdownBody
+ | UIItemType::RightSidebarFilePreviewMarkdownCodeToggle(_)
+ | UIItemType::RightSidebarFilePreviewMarkdownCodeCopy(_)
+ | UIItemType::RightSidebarFilePreviewMarkdownToggle
| UIItemType::RightSidebarFileFilter
| UIItemType::RightSidebarFileRefresh
| UIItemType::RightSidebarFileRow(_)
@@ -1707,6 +1769,10 @@ impl super::TermWindow {
| UIItemType::RightSidebarFilePreviewHorizontalScrollTrack
| UIItemType::RightSidebarFilePreviewHorizontalScrollThumb
| UIItemType::RightSidebarFilePreviewText
+ | UIItemType::RightSidebarFilePreviewMarkdownBody
+ | UIItemType::RightSidebarFilePreviewMarkdownCodeToggle(_)
+ | UIItemType::RightSidebarFilePreviewMarkdownCodeCopy(_)
+ | UIItemType::RightSidebarFilePreviewMarkdownToggle
| UIItemType::RightSidebarFileFilter
| UIItemType::RightSidebarFileRefresh
| UIItemType::RightSidebarFileRow(_)
@@ -1940,6 +2006,7 @@ impl super::TermWindow {
// hover-reveal machinery would slide the sidebar out underneath it.
if self.command_palette.is_none() {
self.update_workspace_sidebar_hover(context);
+ self.update_right_sidebar_hover(context);
}
if self.consume_context_menu_suppressed_release(&event) {
@@ -2100,6 +2167,18 @@ impl super::TermWindow {
);
context.invalidate();
}
+ if completed_drag.as_ref().is_some_and(|(item, _)| {
+ item.item_type == UIItemType::RightSidebarFilePreviewMarkdownBody
+ }) {
+ self.with_note_surface(
+ crate::termwindow::ui::right_sidebar::NoteSurface::FilePreview,
+ |term_window| {
+ term_window.right_sidebar_note.drag_selection_active = false;
+ term_window.right_sidebar_note.drag_selection_base = None;
+ },
+ );
+ context.invalidate();
+ }
if completed_drag
.as_ref()
.is_some_and(|(item, _)| item.item_type == UIItemType::RightSidebarNoteBody)
@@ -2402,6 +2481,7 @@ impl super::TermWindow {
self.current_mouse_event = None;
// The machine reads `Away` now: cancels an Arming, starts a grace.
self.update_workspace_sidebar_hover(context);
+ self.update_right_sidebar_hover(context);
self.update_title();
if !preserve_cursor {
context.set_cursor(Some(MouseCursor::Arrow));
@@ -2802,6 +2882,14 @@ impl super::TermWindow {
UIItemType::RightSidebarNoteBody => {
self.drag_right_sidebar_note_selection(item, start_event, event, context);
}
+ UIItemType::RightSidebarFilePreviewMarkdownBody => {
+ self.drag_right_sidebar_markdown_preview_selection(
+ item,
+ start_event,
+ event,
+ context,
+ );
+ }
UIItemType::RightSidebarFileFilter
| UIItemType::RightSidebarSnippetSearch
| UIItemType::RightSidebarSnippetTitle => {
@@ -3776,6 +3864,12 @@ impl super::TermWindow {
UIItemType::RightSidebarFilePreviewText => {
self.mouse_event_right_sidebar_file_preview_text(item, event, context);
}
+ UIItemType::RightSidebarFilePreviewMarkdownBody
+ | UIItemType::RightSidebarFilePreviewMarkdownCodeToggle(_)
+ | UIItemType::RightSidebarFilePreviewMarkdownCodeCopy(_)
+ | UIItemType::RightSidebarFilePreviewMarkdownToggle => {
+ self.mouse_event_right_sidebar_markdown_preview(&item, &event, context);
+ }
UIItemType::RightSidebarSnippetNew
| UIItemType::RightSidebarSnippetBack
| UIItemType::RightSidebarSnippetSave
@@ -4343,6 +4437,12 @@ impl super::TermWindow {
UIItemType::RightSidebarFilePreviewText => {
self.mouse_event_right_sidebar_file_preview_text(item, event, context);
}
+ UIItemType::RightSidebarFilePreviewMarkdownBody
+ | UIItemType::RightSidebarFilePreviewMarkdownCodeToggle(_)
+ | UIItemType::RightSidebarFilePreviewMarkdownCodeCopy(_)
+ | UIItemType::RightSidebarFilePreviewMarkdownToggle => {
+ self.mouse_event_right_sidebar_markdown_preview(&item, &event, context);
+ }
UIItemType::RightSidebarSnippetNew
| UIItemType::RightSidebarSnippetBack
| UIItemType::RightSidebarSnippetSave
@@ -4644,6 +4744,179 @@ impl super::TermWindow {
}
}
+ /// The rendered Markdown in the file preview: the rendered/source switch,
+ /// and the code block controls, which act on the preview's own surface
+ /// exactly as the Note's act on the Note.
+ fn mouse_event_right_sidebar_markdown_preview(
+ &mut self,
+ item: &UIItem,
+ event: &MouseEvent,
+ context: &dyn WindowOps,
+ ) {
+ use crate::termwindow::ui::right_sidebar::NoteSurface;
+ let pressed = event.kind == WMEK::Press(MousePress::Left);
+ match item.item_type {
+ UIItemType::RightSidebarFilePreviewMarkdownToggle => {
+ context.set_cursor(Some(MouseCursor::Hand));
+ if pressed {
+ self.toggle_right_sidebar_markdown_preview_rendered();
+ context.invalidate();
+ }
+ }
+ UIItemType::RightSidebarFilePreviewMarkdownCodeToggle(source_start) => {
+ context.set_cursor(Some(MouseCursor::Hand));
+ if pressed {
+ self.with_note_surface(NoteSurface::FilePreview, |term_window| {
+ term_window
+ .right_sidebar_note
+ .toggle_code_block(source_start);
+ term_window.right_sidebar_note.refresh_projection();
+ });
+ context.invalidate();
+ }
+ }
+ UIItemType::RightSidebarFilePreviewMarkdownCodeCopy(source_start) => {
+ context.set_cursor(Some(MouseCursor::Hand));
+ if pressed {
+ let text = self
+ .with_note_surface(NoteSurface::FilePreview, |term_window| {
+ term_window
+ .right_sidebar_note
+ .projection
+ .objects
+ .iter()
+ .find_map(|object| match object {
+ crate::markdown_editor::ProjectedObject::CodeBlock(code)
+ if code.source.start == source_start =>
+ {
+ Some(code.text.clone())
+ }
+ _ => None,
+ })
+ })
+ .flatten();
+ if let Some(text) = text {
+ self.copy_to_clipboard(ClipboardCopyDestination::Clipboard, text);
+ }
+ context.invalidate();
+ }
+ }
+ UIItemType::RightSidebarFilePreviewMarkdownBody => {
+ // As the Note body, read-only: links open, text selects.
+ let (x, y) = (event.coords.x as f32, event.coords.y as f32);
+ let external_link = self
+ .with_note_surface(NoteSurface::FilePreview, |term_window| {
+ term_window
+ .right_sidebar_note
+ .external_link_target_for_point(x, y)
+ })
+ .flatten();
+ let wiki_link = self.right_sidebar_markdown_preview_wiki_link_at(x, y);
+ let over_link = external_link.is_some()
+ || wiki_link.as_ref().is_some_and(|(resolved, ambiguous)| {
+ resolved.is_some() || ambiguous.len() > 1
+ });
+ context.set_cursor(Some(if over_link {
+ MouseCursor::Hand
+ } else {
+ MouseCursor::Text
+ }));
+ if event.kind == WMEK::Press(MousePress::Right) {
+ // The same menu the source view offers.
+ if self.right_sidebar_remote_files.selected.is_some() {
+ self.show_right_sidebar_remote_file_preview_context_menu(
+ context,
+ event.coords,
+ );
+ } else {
+ self.show_right_sidebar_file_preview_context_menu(context, event.coords);
+ }
+ return;
+ }
+ if !pressed {
+ return;
+ }
+ let click_streak = self
+ .last_mouse_click
+ .as_ref()
+ .map(|click| click.streak)
+ .unwrap_or(1);
+ let extend = event.modifiers.contains(::window::Modifiers::SHIFT);
+ if click_streak == 1 && !extend {
+ if let Some(target) = external_link {
+ wezterm_open_url::open_url(&target);
+ context.invalidate();
+ return;
+ }
+ if let Some((resolved, ambiguous)) = wiki_link {
+ self.follow_right_sidebar_markdown_preview_wiki_link(
+ context,
+ event.coords,
+ resolved,
+ ambiguous,
+ );
+ context.invalidate();
+ return;
+ }
+ }
+ self.with_note_surface(NoteSurface::FilePreview, |term_window| {
+ let host = &mut term_window.right_sidebar_note;
+ let position = host.source_position_for_point(x, y);
+ let granularity = if click_streak >= 3 {
+ crate::markdown_editor::SelectionGranularity::MarkdownBlock
+ } else if click_streak == 2 {
+ crate::markdown_editor::SelectionGranularity::Word
+ } else {
+ crate::markdown_editor::SelectionGranularity::Character
+ };
+ let atomic_range = host.atomic_source_for_point(x, y);
+ match atomic_range {
+ Some(range) if click_streak == 2 && !extend => {
+ host.selection_granularity = granularity;
+ host.view.selection = crate::markdown_editor::SourceSelection {
+ anchor: crate::markdown_editor::SourcePosition::new(range.start),
+ focus: crate::markdown_editor::SourcePosition::new(range.end),
+ };
+ host.drag_selection_base = Some(range);
+ }
+ _ => host.begin_selection(position, granularity, extend),
+ }
+ // A preview has no caret to chase: selecting never scrolls.
+ host.reveal_caret = false;
+ host.drag_selection_active = false;
+ host.refresh_projection();
+ });
+ self.dragging.replace((item.clone(), event.clone()));
+ context.invalidate();
+ }
+ _ => {}
+ }
+ }
+
+ /// Extend the rendered Markdown preview's selection to where the pointer
+ /// is dragged, as the Note body does.
+ fn drag_right_sidebar_markdown_preview_selection(
+ &mut self,
+ item: UIItem,
+ start_event: MouseEvent,
+ event: MouseEvent,
+ context: &dyn WindowOps,
+ ) {
+ use crate::termwindow::ui::right_sidebar::NoteSurface;
+ let (x, y) = (event.coords.x as f32, event.coords.y as f32);
+ self.with_note_surface(NoteSurface::FilePreview, |term_window| {
+ let host = &mut term_window.right_sidebar_note;
+ host.drag_selection_active = true;
+ let position = host.source_position_for_point(x, y);
+ host.extend_selection_to(position);
+ host.reveal_caret = false;
+ host.refresh_projection();
+ });
+ context.set_cursor(Some(MouseCursor::Text));
+ context.invalidate();
+ self.dragging.replace((item, start_event));
+ }
+
pub fn mouse_event_right_sidebar_file_preview_text(
&mut self,
item: UIItem,
@@ -4668,7 +4941,7 @@ impl super::TermWindow {
self.show_right_sidebar_remote_file_preview_context_menu(context, event.coords);
}
WMEK::Press(MousePress::Right) => {
- self.show_right_sidebar_file_open_with_menu(context, event.coords);
+ self.show_right_sidebar_file_preview_context_menu(context, event.coords);
}
_ => {}
}
diff --git a/wezterm-gui/src/termwindow/remote_files.rs b/wezterm-gui/src/termwindow/remote_files.rs
index cd4d8d86..75831a7d 100644
--- a/wezterm-gui/src/termwindow/remote_files.rs
+++ b/wezterm-gui/src/termwindow/remote_files.rs
@@ -741,6 +741,206 @@ pub(crate) trait RemoteFileBackend: Send + Sync {
let _ = remote;
Box::pin(async { Err("This connection cannot create directories".to_string()) })
}
+
+ /// Every file under `root` for search, listed on the remote host by
+ /// `thinkterm list-files` under the rules the local index uses.
+ fn list_project_files(
+ &self,
+ root: RemotePath,
+ respect_gitignore: bool,
+ ) -> RemoteFuture<RemoteProjectListing> {
+ let _ = (root, respect_gitignore);
+ Box::pin(async { Ok(RemoteProjectListing::NeedsUpdate) })
+ }
+}
+
+/// What listing a remote project for search came back with.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub(crate) enum RemoteProjectListing {
+ Listed(thinkterm_file_index::Listing),
+ /// The remote host has no `thinkterm` that can list files: none at all, or
+ /// one from before `list-files`.
+ NeedsUpdate,
+}
+
+/// The most a remote listing may send: the entry limit times a generous path
+/// length. A listing past this is refused rather than held.
+const REMOTE_LISTING_MAX_BYTES: u64 = 64 * 1024 * 1024;
+/// How long the remote walk may run before it stops and says it is partial.
+const REMOTE_LISTING_TIME_LIMIT_SECS: u64 = 20;
+/// How much longer than that the desktop waits for the listing to arrive.
+const REMOTE_LISTING_GRACE_SECS: u64 = 15;
+/// A listing given up on. The remote may ignore the hangup sent after it, in
+/// which case its reader stays blocked until the connection goes, so this is
+/// not retried as the query changes.
+pub(crate) const REMOTE_LISTING_TIMED_OUT: &str = "The remote file listing timed out";
+const REMOTE_LISTING_EXEC_FAILED: &str = "Unable to run the remote file listing";
+const REMOTE_LISTING_READ_FAILED: &str = "Reading the remote file listing failed";
+
+/// Whether a listing failure came from the connection itself rather than
+/// from what the remote printed. Only those may retire the pooled connection:
+/// the remote's own words, a stray "broken pipe" among them, must not.
+pub(crate) fn remote_listing_failure_is_transport(message: &str) -> bool {
+ message.starts_with(REMOTE_LISTING_EXEC_FAILED)
+ || message.starts_with(REMOTE_LISTING_READ_FAILED)
+}
+
+/// Quote `value` for a POSIX shell: the command line an ssh exec runs goes
+/// through the remote user's shell.
+fn shell_quote(value: &str) -> String {
+ format!("'{}'", value.replace('\'', "'\\''"))
+}
+
+/// The command that lists `root` on the remote host: the host's configured
+/// `remote_wezterm_path` when there is one, as the mux proxy runs it; else
+/// `thinkterm` from where the proxy and the mobile probe look (an exec's shell
+/// does not read the startup files that put it on PATH). The search runs
+/// under `sh`, whatever the login shell is, and exits 127 when it finds none.
+fn list_files_command(
+ root: &RemotePath,
+ respect_gitignore: bool,
+ remote_command: Option<&str>,
+) -> String {
+ let mut args = format!("list-files --time-limit {REMOTE_LISTING_TIME_LIMIT_SECS}");
+ if respect_gitignore {
+ args.push_str(" --respect-gitignore");
+ }
+ let root = shell_quote(root.as_str());
+ if let Some(command) = remote_command {
+ return format!("{command} {args} -- {root}");
+ }
+ // No single quotes or `!` inside: it is single-quoted for every shell.
+ let search = format!(
+ "for p in \"$HOME/.local/bin/thinkterm\" \"$(command -v thinkterm)\" \
+ /usr/local/bin/thinkterm /opt/homebrew/bin/thinkterm \
+ /Applications/ThinkTerm.app/Contents/MacOS/thinkterm; do \
+ [ -n \"$p\" ] && [ -x \"$p\" ] && exec \"$p\" {args} -- \"$1\"; done; exit 127"
+ );
+ format!("sh -c '{search}' sh {root}")
+}
+
+/// Make sense of what `list_files_command` produced. A missing `thinkterm`
+/// (exit 127) or one that does not know `list-files` means the host needs
+/// updating; anything else that is not a whole listing is a failure.
+fn interpret_remote_listing(
+ stdout: &[u8],
+ stderr: &str,
+ exit_code: Option<u32>,
+) -> Result<RemoteProjectListing, String> {
+ if let Ok(listing) = thinkterm_file_index::parse_command_output(stdout) {
+ return Ok(RemoteProjectListing::Listed(listing));
+ }
+ if exit_code == Some(127)
+ || stderr.contains("unrecognized subcommand")
+ || stderr.contains("list-files")
+ && (stderr.contains("unexpected") || stderr.contains("invalid"))
+ {
+ return Ok(RemoteProjectListing::NeedsUpdate);
+ }
+ let detail = stderr.trim();
+ Err(if detail.is_empty() {
+ "The remote host did not return a file listing".to_string()
+ } else {
+ format!("The remote host could not list files: {detail}")
+ })
+}
+
+/// Run the listing on an exec channel of `session` and read it back. Blocking:
+/// call it off the UI thread. stderr drains on a thread of its own, so a
+/// remote that writes a lot there cannot stall the listing.
+fn run_remote_listing(exec: wezterm_ssh::ExecResult) -> Result<RemoteProjectListing, String> {
+ use portable_pty::{Child as _, ChildKiller as _};
+ use std::io::Read;
+ let wezterm_ssh::ExecResult {
+ stdin,
+ stdout,
+ mut stderr,
+ mut child,
+ } = exec;
+ drop(stdin);
+ let stderr = std::thread::spawn(move || {
+ let mut err = Vec::new();
+ let _ = (&mut stderr).take(16 * 1024).read_to_end(&mut err);
+ let _ = std::io::copy(&mut stderr, &mut std::io::sink());
+ err
+ });
+ let mut out = Vec::new();
+ let read = stdout
+ .take(REMOTE_LISTING_MAX_BYTES + 1)
+ .read_to_end(&mut out)
+ .map_err(|err| format!("{REMOTE_LISTING_READ_FAILED}: {err}"));
+ if read.is_err() || out.len() as u64 > REMOTE_LISTING_MAX_BYTES {
+ let _ = child.kill();
+ read?;
+ return Err("The remote file listing is too large".to_string());
+ }
+ let err = stderr.join().unwrap_or_default();
+ let exit_code = child.wait().ok().map(|status| status.exit_code());
+ interpret_remote_listing(&out, &String::from_utf8_lossy(&err), exit_code)
+}
+
+#[cfg(test)]
+mod listing_tests {
+ use super::*;
+
+ #[test]
+ fn roots_are_quoted_for_the_shell() {
+ assert_eq!(shell_quote("/srv/app"), "'/srv/app'");
+ assert_eq!(shell_quote("/srv/it's here"), "'/srv/it'\\''s here'");
+ let root = RemotePath::from_server_absolute("/srv/a b;rm -rf ~").unwrap();
+ let command = list_files_command(&root, true, None);
+ assert!(command.starts_with("sh -c '"));
+ assert!(command.ends_with("' sh '/srv/a b;rm -rf ~'"));
+ assert!(command.contains("--respect-gitignore -- \"$1\""));
+ assert!(!list_files_command(&root, false, None).contains("--respect-gitignore"));
+ // The script is one single-quoted word for any shell.
+ let script = &command["sh -c '".len()..command.len() - "' sh '/srv/a b;rm -rf ~'".len()];
+ assert!(!script.contains('\'') && !script.contains('!'));
+ assert_eq!(
+ list_files_command(&root, false, Some("/opt/tt/thinkterm")),
+ "/opt/tt/thinkterm list-files --time-limit 20 -- '/srv/a b;rm -rf ~'"
+ );
+ }
+
+ #[test]
+ fn listings_and_missing_commands_are_told_apart() {
+ let mut listed = vec![];
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("a.txt"), "").unwrap();
+ thinkterm_file_index::write_listing(
+ dir.path(),
+ true,
+ 10,
+ Instant::now() + Duration::from_secs(60),
+ &mut listed,
+ )
+ .unwrap();
+ match interpret_remote_listing(&listed, "", Some(0)).unwrap() {
+ RemoteProjectListing::Listed(listing) => assert_eq!(listing.entries.len(), 1),
+ other => panic!("{:?}", other),
+ }
+ let mut greeted = b"hello from .bashrc\n".to_vec();
+ greeted.extend_from_slice(&listed);
+ assert!(matches!(
+ interpret_remote_listing(&greeted, "", Some(0)).unwrap(),
+ RemoteProjectListing::Listed(_)
+ ));
+ assert_eq!(
+ interpret_remote_listing(b"", "", Some(127)).unwrap(),
+ RemoteProjectListing::NeedsUpdate
+ );
+ assert_eq!(
+ interpret_remote_listing(b"", "error: unrecognized subcommand 'list-files'", Some(2))
+ .unwrap(),
+ RemoteProjectListing::NeedsUpdate
+ );
+ assert!(
+ interpret_remote_listing(b"", "listing /x: not a directory", Some(1))
+ .unwrap_err()
+ .contains("not a directory")
+ );
+ assert!(interpret_remote_listing(&listed[..listed.len() - 2], "", Some(0)).is_err());
+ }
}
pub(crate) trait RemoteFileConnector: Send + Sync {
@@ -907,9 +1107,41 @@ struct SftpRemoteFileBackend {
// the lifetime of this independent file connection.
_session: Session,
sftp: wezterm_ssh::Sftp,
+ /// The host's `remote_wezterm_path`, for `list-files`.
+ remote_command: Option<String>,
}
impl RemoteFileBackend for SftpRemoteFileBackend {
+ fn list_project_files(
+ &self,
+ root: RemotePath,
+ respect_gitignore: bool,
+ ) -> RemoteFuture<RemoteProjectListing> {
+ // An exec channel on the session this backend already holds: no second
+ // login, and it closes when the listing is read.
+ let session = self._session.clone();
+ let command = list_files_command(&root, respect_gitignore, self.remote_command.as_deref());
+ Box::pin(async move {
+ let exec = session
+ .exec(&command, None)
+ .await
+ .map_err(|err| format!("{REMOTE_LISTING_EXEC_FAILED}: {err:#}"))?;
+ // The remote walk stops itself at its time limit, but only between
+ // entries; one stuck on a dead mount must not hold the search.
+ let mut killer = portable_pty::ChildKiller::clone_killer(&exec.child);
+ let listing = smol::unblock(move || run_remote_listing(exec));
+ let timeout = async {
+ smol::Timer::after(Duration::from_secs(
+ REMOTE_LISTING_TIME_LIMIT_SECS + REMOTE_LISTING_GRACE_SECS,
+ ))
+ .await;
+ let _ = killer.kill();
+ Err(REMOTE_LISTING_TIMED_OUT.to_string())
+ };
+ smol::future::or(listing, timeout).await
+ })
+ }
+
fn resolve_root(&self, requested: String) -> RemoteFuture<RemotePath> {
let sftp = self.sftp.clone();
Box::pin(async move {
@@ -1511,6 +1743,7 @@ impl RemoteFileConnector for SshRemoteFileConnector {
let backend: Arc<dyn RemoteFileBackend> = Arc::new(SftpRemoteFileBackend {
sftp: session.sftp(),
_session: session,
+ remote_command: config.remote_wezterm_path.clone(),
});
return Ok(backend);
}
diff --git a/wezterm-gui/src/termwindow/render/paint.rs b/wezterm-gui/src/termwindow/render/paint.rs
index 870466ac..cda030d3 100644
--- a/wezterm-gui/src/termwindow/render/paint.rs
+++ b/wezterm-gui/src/termwindow/render/paint.rs
@@ -3483,6 +3483,122 @@ impl crate::TermWindow {
}
}
+ /// The right sidebar's counterpart of `advance_workspace_sidebar_hover`.
+ fn advance_right_sidebar_hover(&mut self, now: Instant) {
+ match self.step_right_sidebar_hover(now) {
+ crate::termwindow::sidebar_hover::HoverFrame::None => {}
+ crate::termwindow::sidebar_hover::HoverFrame::Now => {
+ if let Some(window) = self.window.as_ref() {
+ window.invalidate();
+ }
+ }
+ crate::termwindow::sidebar_hover::HoverFrame::At(due) => {
+ self.update_next_frame_time(Some(due));
+ }
+ }
+ }
+
+ /// The left sidebar's hover shadow, mirrored: it falls off the revealed
+ /// right panel's LEFT edge, onto the terminal it floats over.
+ fn paint_right_sidebar_hover_shadow(
+ &mut self,
+ layers: &mut TripleLayerQuadAllocator,
+ ) -> anyhow::Result<()> {
+ let Some(rect) = self.right_sidebar_rect() else {
+ return Ok(());
+ };
+ let edge = rect.x as f32;
+ let top = rect.y as f32;
+ let height = rect.height as f32;
+ let spread = self.ui_f32(16.0);
+ const STEPS: usize = 8;
+ const BASE_ALPHA: f32 = 0.22;
+ let step_width = spread / STEPS as f32;
+ for i in 0..STEPS {
+ let t = (i as f32 + 0.5) / STEPS as f32;
+ let alpha = BASE_ALPHA * (1.0 - t) * (1.0 - t);
+ self.filled_rectangle(
+ layers,
+ 0,
+ euclid::rect(edge - (i + 1) as f32 * step_width, top, step_width, height),
+ LinearRgba::with_components(0.0, 0.0, 0.0, alpha),
+ )
+ .context("right sidebar hover shadow strip")?;
+ }
+ Ok(())
+ }
+
+ /// Record the hover-revealed right sidebar off-screen, as the left one is,
+ /// holding back its hit targets until after the tab bar has laid out its
+ /// own. Returns the recording, how far the panel still has to travel
+ /// (0 = arrived) and the items to register once it is safe to.
+ fn record_right_sidebar_hover_overlay(
+ &mut self,
+ now: Instant,
+ ) -> anyhow::Result<Option<(HeapQuadAllocator, f32, Vec<UIItem>)>> {
+ if !self.right_sidebar_collapsed {
+ return Ok(None);
+ }
+ let Some(progress) = self.right_sidebar_hover.progress(now) else {
+ return Ok(None);
+ };
+ let ui_items_before = self.ui_items.len();
+ let mut panel_frame = HeapQuadAllocator::default();
+ let result = {
+ let mut panel_layers = TripleLayerQuadAllocator::Heap(&mut panel_frame);
+ self.paint_right_sidebar(&mut panel_layers)
+ .and_then(|_| self.paint_right_sidebar_hover_shadow(&mut panel_layers))
+ };
+ result.context("record hover-revealed right sidebar")?;
+ let mut items: Vec<UIItem> = self.ui_items.drain(ui_items_before..).collect();
+ if !self.right_sidebar_hover.is_fully_presented(now) {
+ // While the panel moves no target inside it is where it looks to
+ // be: one inert item over the part already on screen keeps clicks
+ // off the terminal underneath.
+ items.clear();
+ if let Some(rect) = self.right_sidebar_rect() {
+ let revealed = (rect.width as f32 * progress).round() as usize;
+ if revealed > 0 {
+ let right_edge = rect.x.saturating_add(rect.width);
+ items.push(UIItem {
+ x: right_edge.saturating_sub(revealed),
+ y: 0,
+ width: revealed,
+ height: rect.y + rect.height,
+ item_type: UIItemType::RightSidebarBackground,
+ });
+ }
+ }
+ }
+ Ok(Some((panel_frame, 1.0 - progress, items)))
+ }
+
+ /// Composite the recorded right sidebar above the tab bar, pushed right by
+ /// the part of it still to travel; the window crops the overhang.
+ fn paint_right_sidebar_hover_overlay(
+ &self,
+ overlay: &HeapQuadAllocator,
+ hidden: f32,
+ ) -> anyhow::Result<()> {
+ let Some(rect) = self.right_sidebar_rect() else {
+ return Ok(());
+ };
+ let gl_state = self.render_state.as_ref().unwrap();
+ let layer = gl_state
+ .layer_for_zindex(0)
+ .context("hover right sidebar layer")?;
+ let mut layers = layer.quad_allocator();
+ let offset_x = hidden * rect.width as f32;
+ let clip = crate::quad::QuadClipRect::from_top_left_pixels(
+ 0.0,
+ 0.0,
+ self.dimensions.pixel_width as f32,
+ self.dimensions.pixel_height as f32,
+ &self.dimensions,
+ );
+ overlay.apply_to_single_layer(&mut layers, 2, offset_x, clip)
+ }
+
/// A soft shadow falling off the hover-revealed panel's right edge, so
/// the overlay reads as a layer floating above the terminal rather than
/// a slab butted against it. Only the hover overlay gets this: the
@@ -3899,6 +4015,7 @@ impl crate::TermWindow {
let frame_now = Instant::now();
self.advance_workspace_space_swipe_push(frame_now);
self.advance_workspace_sidebar_hover(frame_now);
+ self.advance_right_sidebar_hover(frame_now);
self.advance_content_view_fade(frame_now);
// Card texture work is queued per pass; a retried pass re-queues it.
self.pending_card_renders.borrow_mut().clear();
@@ -4172,6 +4289,7 @@ impl crate::TermWindow {
// bar, so both its pixels and its hit targets land above the
// chrome that would otherwise cover its top edge.
let hover_overlay = self.record_workspace_sidebar_hover_overlay(frame_now)?;
+ let right_hover_overlay = self.record_right_sidebar_hover_overlay(frame_now)?;
let render_space_push = self.workspace_space_swipe_push_active
&& self.workspace_space_swipe_source_frame.is_some();
@@ -4376,8 +4494,12 @@ impl crate::TermWindow {
}
let mut chrome_layers = layer.quad_allocator();
- self.paint_right_sidebar(&mut chrome_layers)
- .context("paint_right_sidebar")?;
+ // A hover-revealed right sidebar was recorded above and is
+ // composited after the tab bar instead.
+ if !self.right_sidebar_collapsed {
+ self.paint_right_sidebar(&mut chrome_layers)
+ .context("paint_right_sidebar")?;
+ }
if self.show_tab_bar {
self.paint_tab_bar(&mut chrome_layers)
@@ -4413,6 +4535,33 @@ impl crate::TermWindow {
.context("paint sidebar hover arming hint")?;
}
}
+
+ // The right sidebar's reveal, composited after the tab bar for the
+ // same reasons as the left one's.
+ if let Some((overlay, hidden, items)) = right_hover_overlay {
+ self.paint_right_sidebar_hover_overlay(&overlay, hidden)
+ .context("paint hover-revealed right sidebar")?;
+ self.ui_items.extend(items);
+ } else if self.right_sidebar_hover.is_arming() {
+ if let Some((zx, zy, zw, zh)) = self.right_sidebar_hover_hot_zone() {
+ let hint_width =
+ self.ui_px(crate::termwindow::ui::tokens::SIDEBAR_HOVER_HINT_WIDTH) as f32;
+ let palette = self.chrome();
+ let mut hint_layers = layer.quad_allocator();
+ self.filled_rectangle(
+ &mut hint_layers,
+ 2,
+ euclid::rect(
+ (zx + zw) as f32 - hint_width,
+ zy as f32,
+ hint_width,
+ zh as f32,
+ ),
+ palette.sidebar_row_active_border.mul_alpha(0.7),
+ )
+ .context("paint right sidebar hover arming hint")?;
+ }
+ }
}
if fading_content_view {
diff --git a/wezterm-gui/src/termwindow/ui/context_menu.rs b/wezterm-gui/src/termwindow/ui/context_menu.rs
index c6e01e10..90a60edd 100644
--- a/wezterm-gui/src/termwindow/ui/context_menu.rs
+++ b/wezterm-gui/src/termwindow/ui/context_menu.rs
@@ -236,6 +236,18 @@ impl crate::TermWindow {
crate::termwindow::ContextMenuApplicationAction::Note(command) => {
self.perform_right_sidebar_note_command(command);
}
+ crate::termwindow::ContextMenuApplicationAction::OpenFilePreview(path) => {
+ self.open_right_sidebar_file_path(path);
+ if let Some(window) = &self.window {
+ window.invalidate();
+ }
+ }
+ crate::termwindow::ContextMenuApplicationAction::OpenRemoteFilePreview(path) => {
+ self.open_right_sidebar_remote_file_preview(path);
+ if let Some(window) = &self.window {
+ window.invalidate();
+ }
+ }
crate::termwindow::ContextMenuApplicationAction::SetFrontendAccessMode(mode) => {
self.request_frontend_access_mode(mode);
}
diff --git a/wezterm-gui/src/termwindow/ui/right_sidebar.rs b/wezterm-gui/src/termwindow/ui/right_sidebar.rs
index 38842c96..5b5b21a0 100644
--- a/wezterm-gui/src/termwindow/ui/right_sidebar.rs
+++ b/wezterm-gui/src/termwindow/ui/right_sidebar.rs
@@ -11,9 +11,10 @@ use crate::termwindow::remote_files::{
download_name_candidates, invalidate_remote_connection, invalidate_remote_connection_if_dead,
local_path_is_occupied, remote_connection_key, remote_connection_manager,
reserve_download_directory, reserve_download_path, RemoteAcquireError, RemoteFileBytes,
- RemoteFileKind, RemoteFileRow, RemoteFilesEffect, RemoteFilesEvent, RemoteFilesPhase,
- RemoteOperationOrigin, RemotePath, RemoteTransfer, RemoteTransferKind, RemoteTransferProgress,
- RemoteTransferSource, RemoteTransferStatus, TransferFailure, REMOTE_TRANSFER_CANCELED,
+ RemoteFileEntry, RemoteFileKind, RemoteFileRow, RemoteFilesEffect, RemoteFilesEvent,
+ RemoteFilesPhase, RemoteOperationOrigin, RemotePath, RemoteProjectListing, RemoteTransfer,
+ RemoteTransferKind, RemoteTransferProgress, RemoteTransferSource, RemoteTransferStatus,
+ TransferFailure, REMOTE_TRANSFER_CANCELED,
};
use crate::termwindow::remote_walk::{
plan_remote_walk, RemoteWalkEntry, RemoteWalkMode, RemoteWalkPlan,
@@ -51,7 +52,6 @@ use crate::workspace_threads;
use anyhow::Context;
use config::keyassignment::{ClipboardCopyDestination, ClipboardPasteSource, KeyAssignment};
use fluent_bundle::FluentArgs;
-use ignore::{DirEntry as IgnoreDirEntry, WalkBuilder};
use mux::pane::{Pane, PaneId};
use mux::Mux;
use std::borrow::Cow;
@@ -167,6 +167,8 @@ const SNIPPET_CARET_WIDTH: f32 = 3.0;
const NOTE_TOOLBAR_HEIGHT: usize = 54;
const NOTE_BODY_TOP_GAP: usize = 12;
const NOTE_BODY_PADDING: usize = 24;
+/// The widest a rendered Markdown file preview's text runs.
+const MARKDOWN_PREVIEW_READING_MAX_WIDTH: usize = 1500;
const NOTE_LINE_GAP: usize = 5;
const NOTE_CARET_WIDTH: f32 = 2.0;
const NOTE_TABLE_CELL_HORIZONTAL_PADDING: usize = 10;
@@ -329,7 +331,7 @@ const FILE_PREVIEW_FULL_LINE_SHAPE_MAX_COLS: usize = 4096;
const FILE_PREVIEW_SCROLLBAR_THICKNESS: usize = 4;
const FILE_PREVIEW_SCROLLBAR_HIT_SLOP: usize = 6;
const FILE_TREE_ROW_LIMIT: usize = 2000;
-const FILE_INDEX_ENTRY_LIMIT: usize = 100_000;
+const FILE_INDEX_ENTRY_LIMIT: usize = thinkterm_file_index::ENTRY_LIMIT;
// How long the file panel must stay closed/idle before its in-memory index and
// buffers are released. Reopening within this window keeps everything resident.
const FILE_INDEX_IDLE_RELEASE_SECS: u64 = 30;
@@ -828,6 +830,58 @@ fn classify_vault_failure(vault_root: &Path, err: &anyhow::Error) -> NoteVaultFa
}
}
+/// Where the note painter draws. The Note is the right sidebar's editor; the
+/// file preview renders a Markdown file read-only with the same painter.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum NoteSurface {
+ Note,
+ FilePreview,
+}
+
+/// One particular surface: which kind, and which instance of it. A file
+/// preview is rebuilt for every file it shows, so background work records the
+/// instance it started on and a result for an earlier one is dropped.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) struct NoteSurfaceTicket {
+ pub(crate) surface: NoteSurface,
+ instance: u64,
+}
+
+static NEXT_MARKDOWN_PREVIEW_INSTANCE: std::sync::atomic::AtomicU64 =
+ std::sync::atomic::AtomicU64::new(1);
+
+/// Everything the note painter draws a surface from. The Note's lives in the
+/// `right_sidebar_note*` fields the painter and its helpers use; another
+/// surface keeps its own here and is installed into those fields only for the
+/// span of `TermWindow::with_note_surface`.
+#[derive(Default)]
+pub(crate) struct NoteSurfaceState {
+ host: crate::markdown_editor::NoteHostState,
+ table_horizontal_offsets: std::collections::BTreeMap<usize, f32>,
+ table_layouts: Vec<RightSidebarNoteTableLayout>,
+ code_highlight: NoteCodeHighlightState,
+ paint_cache: NotePaintCache,
+}
+
+/// The file preview's rendered Markdown: which preview it renders and the
+/// read-only surface it renders into. Dropped when the preview closes, the
+/// file changes, the preview switches to source, or the panel's memory is
+/// released.
+pub(crate) struct MarkdownPreviewSurface {
+ key: MarkdownPreviewKey,
+ /// Unique per surface built; never 0, which is the Note's.
+ instance: u64,
+ state: NoteSurfaceState,
+}
+
+#[derive(Clone, Debug, PartialEq, Eq)]
+struct MarkdownPreviewKey {
+ /// The previewed file, local or remote.
+ path: String,
+ /// Bumped by every preview load, so a reload of the same path re-renders.
+ generation: u64,
+}
+
/// Immutable component geometry for one visual/layout revision. Vertical
/// scrolling only clones these Arcs; it must not clone the full Markdown
/// projection or remeasure every table/code cell on every frame.
@@ -1366,7 +1420,7 @@ fn file_preview_close_requires_reflow(
impl crate::TermWindow {
fn right_sidebar_file_preview_active(&self) -> bool {
- !self.right_sidebar_collapsed
+ self.right_sidebar_presented()
&& self.right_sidebar_mode == RightSidebarMode::Chat
&& self.right_sidebar_file_view == RightSidebarFileView::Preview
&& (self.right_sidebar_file_selected.is_some()
@@ -1407,7 +1461,7 @@ impl crate::TermWindow {
}
fn right_sidebar_file_preview_width(&self) -> Option<usize> {
- if self.right_sidebar_collapsed
+ if !self.right_sidebar_presented()
|| self.right_sidebar_mode != RightSidebarMode::Chat
|| self.right_sidebar_file_view != RightSidebarFileView::Preview
|| (self.right_sidebar_file_selected.is_none()
@@ -1434,7 +1488,7 @@ impl crate::TermWindow {
}
pub(crate) fn right_sidebar_note_pane_active(&self) -> bool {
- !self.right_sidebar_collapsed
+ self.right_sidebar_presented()
&& self.right_sidebar_mode == RightSidebarMode::Tasks
&& self.right_sidebar_note_pane_expanded
// Without a vault there is no editor to expand; the sidebar shows
@@ -1477,26 +1531,53 @@ impl crate::TermWindow {
RightSidebarMode::any_panel_enabled()
}
+ /// The width the TERMINAL is laid out against. Zero whenever the sidebar
+ /// is collapsed, hover reveal or not: a hover must never reflow a PTY.
+ /// Drawing and hit-testing ask `right_sidebar_presented_width`.
pub fn right_sidebar_width(&self) -> usize {
- if self.right_sidebar_collapsed || !self.right_sidebar_has_panels() {
+ if self.right_sidebar_collapsed {
0
} else {
- // The file preview pane and the Note pane are mutually exclusive
- // (different sidebar modes); at most one is non-zero.
- let pane_width = self
- .right_sidebar_file_preview_width()
- .or_else(|| self.right_sidebar_note_pane_width())
- .unwrap_or(0);
- self.right_sidebar_tree_width()
- .saturating_add(pane_width)
- .min(if pane_width > 0 {
- self.right_sidebar_pane_total_max_width()
- } else {
- self.right_sidebar_available_width()
- })
+ self.right_sidebar_docked_width()
+ }
+ }
+
+ /// The width the panel has when it is on screen, docked or not.
+ fn right_sidebar_docked_width(&self) -> usize {
+ if !self.right_sidebar_has_panels() {
+ return 0;
+ }
+ // The file preview pane and the Note pane are mutually exclusive
+ // (different sidebar modes); at most one is non-zero.
+ let pane_width = self
+ .right_sidebar_file_preview_width()
+ .or_else(|| self.right_sidebar_note_pane_width())
+ .unwrap_or(0);
+ self.right_sidebar_tree_width()
+ .saturating_add(pane_width)
+ .min(if pane_width > 0 {
+ self.right_sidebar_pane_total_max_width()
+ } else {
+ self.right_sidebar_available_width()
+ })
+ }
+
+ /// The width the panel is DRAWN and HIT-TESTED at: the docked width while
+ /// it is docked or hover-revealed, zero otherwise.
+ pub(crate) fn right_sidebar_presented_width(&self) -> usize {
+ if self.right_sidebar_presented() {
+ self.right_sidebar_docked_width()
+ } else {
+ 0
}
}
+ /// Whether the panel is on screen at all, docked or by a hover reveal.
+ /// What the panel shows follows this; the terminal's layout does not.
+ pub(crate) fn right_sidebar_presented(&self) -> bool {
+ !self.right_sidebar_collapsed || self.right_sidebar_hover.is_presented()
+ }
+
pub fn right_sidebar_max_width(&self) -> usize {
let available_width = self.right_sidebar_available_width();
let proportional_max = (available_width * 2 / 3).max(self.ui_px(RIGHT_SIDEBAR_MIN_WIDTH));
@@ -1604,6 +1685,10 @@ impl crate::TermWindow {
}
pub fn toggle_right_sidebar(&mut self) {
+ // Collapsing must not instantly re-reveal under a pointer still on the
+ // button, and docking makes a reveal moot.
+ self.right_sidebar_hover.suppress_until_pointer_leaves();
+ self.right_sidebar_hover_was_presented = false;
self.right_sidebar_collapsed = !self.right_sidebar_collapsed;
if self.right_sidebar_collapsed {
if self.right_sidebar_mode == RightSidebarMode::Tasks {
@@ -1623,6 +1708,8 @@ impl crate::TermWindow {
}
pub fn expand_right_sidebar(&mut self) {
+ self.right_sidebar_hover.suppress_until_pointer_leaves();
+ self.right_sidebar_hover_was_presented = false;
self.right_sidebar_collapsed = false;
self.kick_right_sidebar_file_rescan_cycle();
self.request_right_sidebar_remote_files_connect(false);
@@ -1632,10 +1719,132 @@ impl crate::TermWindow {
}
}
+ /// The right-edge strip that arms a hover reveal, as (x, y, w, h) in
+ /// window pixels. Computed against the docked rect so it exists while the
+ /// panel does not; like the left one it leaves out the tab bar row, where
+ /// the pointer is on its way to the tabs or the sidebar button.
+ pub(crate) fn right_sidebar_hover_hot_zone(&self) -> Option<(usize, usize, usize, usize)> {
+ let rect = self.right_sidebar_rect_for_width(self.right_sidebar_docked_width())?;
+ let top_tab_bar_height = if self.show_tab_bar && !self.config.tab_bar_at_bottom {
+ self.tab_bar_pixel_height().unwrap_or(0.0).ceil() as usize
+ } else {
+ 0
+ };
+ let width = self.ui_px(crate::termwindow::ui::tokens::SIDEBAR_HOVER_HOT_ZONE_WIDTH);
+ let right_edge = rect.x.saturating_add(rect.width);
+ crate::termwindow::sidebar_hover::hot_zone(
+ right_edge.saturating_sub(width),
+ rect.y,
+ rect.height,
+ width,
+ top_tab_bar_height,
+ )
+ }
+
+ /// What the right sidebar's hover machine sees: the left one's rules,
+ /// mirrored. The panel also holds on while one of its text fields has the
+ /// keyboard -- the pointer drifting off must not take a note away mid-word.
+ pub(crate) fn right_sidebar_hover_input(&self) -> crate::termwindow::sidebar_hover::HoverInput {
+ use crate::termwindow::sidebar_hover::{HoverInput, PointerZone};
+ // A menu, modal or rename opened from the revealed panel holds it
+ // out; one open while it is away keeps it from revealing.
+ let overlay_open = self.modal.borrow().is_some()
+ || self.context_menu.is_some()
+ || self.native_context_menu_open
+ || self.inline_tab_rename.is_some();
+ let eligible = self.right_sidebar_collapsed
+ && self.right_sidebar_has_panels()
+ && !self.content_view_foreground()
+ && !self.content_view_transition_running()
+ && !self.frontend_surface_blocked()
+ && (!overlay_open || self.right_sidebar_hover.is_presented());
+ let pointer = match &self.current_mouse_event {
+ None => PointerZone::Away,
+ Some(event) => match right_sidebar_hover_zone(
+ event.coords.x,
+ event.coords.y,
+ self.right_sidebar_rect(),
+ self.right_sidebar_hover_hot_zone(),
+ self.ui_px(crate::termwindow::ui::tokens::SIDEBAR_HOVER_STICKY_ZONE_WIDTH),
+ self.ui_items.iter().any(|item| {
+ matches!(item.item_type, UIItemType::RightSidebarToggle)
+ && item.hit_test(event.coords.x, event.coords.y)
+ }),
+ ) {
+ // The rightmost pane's scrollbar runs down this edge too; a
+ // pointer on its thumb is after the scrollbar, not the panel.
+ // Only the thumb: the track spans the whole edge.
+ PointerZone::HotZone | PointerZone::NearHotZone
+ if self.ui_items.iter().any(|item| {
+ matches!(item.item_type, UIItemType::ScrollThumb(_))
+ && item.hit_test(event.coords.x, event.coords.y)
+ }) =>
+ {
+ PointerZone::Away
+ }
+ zone => zone,
+ },
+ };
+ let pinned = !self.current_mouse_buttons.is_empty()
+ || self.current_mouse_capture.is_some()
+ || self.dragging.is_some()
+ || self.sidebar_row_drag.is_some()
+ || self.right_sidebar_file_drag.is_some()
+ || self.pane_tab_drag.is_some()
+ || overlay_open
+ // Focus only holds a panel that is out: a field left focused when
+ // the docked panel collapsed must not stop the next reveal.
+ || (self.right_sidebar_hover.is_presented()
+ && (self.right_sidebar_focused_input().is_some()
+ || self.right_sidebar_note.view.focused));
+ HoverInput {
+ eligible,
+ pointer,
+ pinned,
+ }
+ }
+
+ /// Step the right sidebar's hover machine and keep what the panel shows in
+ /// step with it being on screen.
+ pub(crate) fn step_right_sidebar_hover(
+ &mut self,
+ now: Instant,
+ ) -> crate::termwindow::sidebar_hover::HoverFrame {
+ let input = self.right_sidebar_hover_input();
+ let frame = self.right_sidebar_hover.step(input, now);
+ self.sync_right_sidebar_hover_presence();
+ frame
+ }
+
+ /// A hover reveal opens the panel's content the way docking does, without
+ /// docking it; the panel leaving releases it the way collapsing does.
+ fn sync_right_sidebar_hover_presence(&mut self) {
+ let presented = self.right_sidebar_collapsed && self.right_sidebar_hover.is_presented();
+ if presented == self.right_sidebar_hover_was_presented {
+ return;
+ }
+ self.right_sidebar_hover_was_presented = presented;
+ if presented {
+ self.kick_right_sidebar_file_rescan_cycle();
+ self.request_right_sidebar_remote_files_connect(false);
+ if self.right_sidebar_mode == RightSidebarMode::Tasks {
+ self.right_sidebar_note_memory_release_token =
+ self.right_sidebar_note_memory_release_token.wrapping_add(1);
+ }
+ } else {
+ // A reveal comes and goes with the pointer, so the remote
+ // connection and its preview wait for the idle release below
+ // rather than going the moment the panel slides away.
+ self.clear_right_sidebar_text_focus();
+ self.schedule_right_sidebar_file_memory_release();
+ self.schedule_right_sidebar_note_memory_release();
+ }
+ }
+
/// The file panel (and its in-memory index) is only relevant while the right
/// sidebar is open and in `Chat`/File mode.
pub(crate) fn right_sidebar_file_view_active(&self) -> bool {
- !self.right_sidebar_collapsed && self.right_sidebar_mode == RightSidebarMode::Chat
+ self.right_sidebar_presented() && self.right_sidebar_mode == RightSidebarMode::Chat
}
/// Schedule a delayed check that frees the file index + buffers if the panel
@@ -1695,10 +1904,11 @@ impl crate::TermWindow {
// `close_right_sidebar_file_preview` only `.clear()`s the preview lines,
// which keeps the (potentially large) capacity; drop it outright.
self.right_sidebar_file_preview_lines = Vec::new();
- self.right_sidebar_file_preview_raw_text = None;
+ self.set_right_sidebar_file_preview_raw_text(None);
self.right_sidebar_file_index = None;
self.right_sidebar_file_index_status = RightSidebarFileIndexStatus::Empty;
+ self.right_sidebar_remote_file_search.release();
self.right_sidebar_file_browse_rows = Vec::new();
self.right_sidebar_file_browse_cache_key = None;
self.reset_right_sidebar_file_dir_cache();
@@ -1706,7 +1916,10 @@ impl crate::TermWindow {
// status can no longer signal that, since it may never leave `Empty`.
self.right_sidebar_file_view_needs_restore = true;
// Remote trees survive a panel toggle so switching back is instant, but
- // a panel left hidden this long should give them back too.
+ // a panel left hidden this long should give them back too. One that
+ // went away with a hover reveal still holds its lease: hiding stashes
+ // its tree first.
+ self.release_right_sidebar_remote_files_if_hidden();
self.right_sidebar_remote_files.release_cached_trees();
self.ui_shape_caches.borrow_mut().clear_file_preview();
self.publish_ui_shape_cache_diagnostics();
@@ -1777,6 +1990,194 @@ impl crate::TermWindow {
self.publish_ui_shape_cache_diagnostics();
}
+ fn set_right_sidebar_file_preview_raw_text(&mut self, text: Option<String>) {
+ self.right_sidebar_file_preview_raw_text = text;
+ self.right_sidebar_file_preview_text_generation = self
+ .right_sidebar_file_preview_text_generation
+ .wrapping_add(1);
+ // Rendered Markdown is only ever of the text it was built from.
+ self.right_sidebar_markdown_preview = None;
+ }
+
+ /// The previewed file when it is Markdown, as the key its rendering is
+ /// kept under. A remote selection is the one shown when there is one.
+ fn right_sidebar_previewed_markdown_path(&self) -> Option<String> {
+ if let Some(path) = self.right_sidebar_remote_files.selected.as_ref() {
+ return path
+ .extension()
+ .is_some_and(is_markdown_extension)
+ .then(|| path.as_str().to_string());
+ }
+ let path = self.right_sidebar_file_selected.as_ref()?;
+ path.extension()
+ .and_then(|extension| extension.to_str())
+ .is_some_and(is_markdown_extension)
+ .then(|| path.to_string_lossy().into_owned())
+ }
+
+ /// A Markdown file whose text the preview holds, so it can be rendered.
+ fn right_sidebar_markdown_preview_available(&self) -> bool {
+ self.right_sidebar_file_preview_message.is_none()
+ && self.right_sidebar_file_preview_image.is_none()
+ && self.right_sidebar_file_preview_raw_text.is_some()
+ && self.right_sidebar_previewed_markdown_path().is_some()
+ }
+
+ /// Whether the file preview shows rendered Markdown right now.
+ pub(crate) fn right_sidebar_markdown_preview_rendering(&self) -> bool {
+ self.right_sidebar_file_preview_active()
+ && self.right_sidebar_markdown_preview_available()
+ && crate::native_settings::right_sidebar_markdown_preview_rendered()
+ }
+
+ pub(crate) fn toggle_right_sidebar_markdown_preview_rendered(&mut self) {
+ let rendered = !crate::native_settings::right_sidebar_markdown_preview_rendered();
+ if let Err(err) =
+ crate::native_settings::save_right_sidebar_markdown_preview_rendered(rendered)
+ {
+ log::warn!("failed to save the Markdown preview mode: {err:#}");
+ }
+ if !rendered {
+ // Source keeps nothing of the rendering.
+ self.right_sidebar_markdown_preview = None;
+ }
+ }
+
+ /// Build the rendered surface for the previewed text, unless the one
+ /// there already renders it.
+ fn ensure_right_sidebar_markdown_preview(&mut self) -> bool {
+ let Some(path) = self.right_sidebar_previewed_markdown_path() else {
+ return false;
+ };
+ if self.right_sidebar_file_preview_raw_text.is_none() {
+ return false;
+ }
+ let key = MarkdownPreviewKey {
+ path,
+ generation: self.right_sidebar_file_preview_text_generation,
+ };
+ if self
+ .right_sidebar_markdown_preview
+ .as_ref()
+ .is_some_and(|preview| preview.key == key)
+ {
+ return true;
+ }
+ // Copied only to build: this runs every frame.
+ let Some(text) = self.right_sidebar_file_preview_raw_text.clone() else {
+ return false;
+ };
+ let document = self.markdown_preview_document(&key.path, text);
+ let mut state = NoteSurfaceState::default();
+ state.host.bind_document(document);
+ state.host.view.mode = EditorMode::ReadOnly;
+ self.right_sidebar_markdown_preview = Some(Box::new(MarkdownPreviewSurface {
+ key,
+ instance: NEXT_MARKDOWN_PREVIEW_INSTANCE
+ .fetch_add(1, std::sync::atomic::Ordering::Relaxed),
+ state,
+ }));
+ true
+ }
+
+ /// The document a previewed Markdown file renders as: a session of its
+ /// own, never registered with the Notebook's and never saved. A local
+ /// file's images resolve beside it and cannot leave the project; a remote
+ /// file has no local folder, so it shows none from relative paths.
+ fn markdown_preview_document(
+ &self,
+ path: &str,
+ text: String,
+ ) -> crate::markdown_editor::VaultDocument {
+ let remote_relative = self
+ .right_sidebar_remote_files
+ .selected
+ .as_ref()
+ .map(|selected| {
+ self.right_sidebar_remote_files
+ .root
+ .as_ref()
+ .and_then(|root| remote_relative_path(root, selected))
+ .unwrap_or_else(|| selected.file_name().to_string())
+ });
+ markdown_preview_document(
+ path,
+ text,
+ remote_relative,
+ self.right_sidebar_file_index_root.as_deref(),
+ )
+ }
+
+ /// Paint the previewed Markdown rendered, with the Note's painter on the
+ /// preview's own read-only surface.
+ fn paint_right_sidebar_markdown_preview(
+ &mut self,
+ layers: &mut TripleLayerQuadAllocator,
+ ui_font: &Rc<LoadedFont>,
+ ui_metrics: RenderMetrics,
+ chrome: UiPalette,
+ foreground: LinearRgba,
+ muted_fg: LinearRgba,
+ x: usize,
+ top: usize,
+ width: usize,
+ bottom: usize,
+ ) -> anyhow::Result<()> {
+ let font_size = self.right_sidebar_file_preview_font_size();
+ self.with_note_surface(NoteSurface::FilePreview, |term_window| {
+ term_window.paint_note_editor_area(
+ layers,
+ ui_font,
+ ui_metrics,
+ chrome,
+ foreground,
+ muted_fg,
+ x,
+ top,
+ width,
+ bottom,
+ font_size,
+ false,
+ SvgIcon::FolderTree,
+ )
+ })
+ .unwrap_or(Ok(()))
+ }
+
+ /// The rendered/source switch beside a Markdown preview's close button.
+ /// Returns the width it takes, gap included.
+ fn paint_markdown_preview_toggle(
+ &mut self,
+ layers: &mut TripleLayerQuadAllocator,
+ chrome: UiPalette,
+ foreground: LinearRgba,
+ muted_fg: LinearRgba,
+ x: usize,
+ y: usize,
+ size: usize,
+ ) -> anyhow::Result<usize> {
+ if !self.right_sidebar_markdown_preview_available() {
+ return Ok(0);
+ }
+ let icon = if crate::native_settings::right_sidebar_markdown_preview_rendered() {
+ SvgIcon::CodeXml
+ } else {
+ SvgIcon::Eye
+ };
+ self.paint_files_preview_header_icon_button(
+ layers,
+ chrome,
+ foreground,
+ muted_fg,
+ x,
+ y,
+ size,
+ icon,
+ UIItemType::RightSidebarFilePreviewMarkdownToggle,
+ )?;
+ Ok(size + self.ui_px(PREVIEW_HEADER_ACTION_GAP))
+ }
+
fn right_sidebar_file_view_state_key(&self) -> Option<(PathBuf, String)> {
self.right_sidebar_file_index_root
.clone()
@@ -2027,7 +2428,7 @@ impl crate::TermWindow {
}
pub(crate) fn scroll_right_sidebar_snippets(&mut self, amount: i16) -> bool {
- if self.right_sidebar_collapsed
+ if !self.right_sidebar_presented()
|| self.right_sidebar_mode != RightSidebarMode::Snippets
|| self.right_sidebar_snippet_view != RightSidebarSnippetView::List
|| amount == 0
@@ -2070,7 +2471,7 @@ impl crate::TermWindow {
}
pub(crate) fn scroll_right_sidebar_file_preview_horizontal(&mut self, amount: i16) -> bool {
- if self.right_sidebar_collapsed
+ if !self.right_sidebar_presented()
|| self.right_sidebar_mode != RightSidebarMode::Chat
|| self.right_sidebar_file_view != RightSidebarFileView::Preview
|| (self.right_sidebar_file_selected.is_none()
@@ -2146,7 +2547,7 @@ impl crate::TermWindow {
self.right_sidebar_file_preview_highlight_cancel = Arc::new(AtomicUsize::new(0));
let highlight_cancel = Arc::clone(&self.right_sidebar_file_preview_highlight_cancel);
self.right_sidebar_file_preview_lines.clear();
- self.right_sidebar_file_preview_raw_text = None;
+ self.set_right_sidebar_file_preview_raw_text(None);
self.right_sidebar_file_preview_max_columns = 0;
self.clear_right_sidebar_file_preview_slice_cache();
self.right_sidebar_file_preview_image = None;
@@ -2246,7 +2647,7 @@ impl crate::TermWindow {
.store(1, AtomicOrdering::Relaxed);
self.right_sidebar_file_preview_highlight_cancel = Arc::new(AtomicUsize::new(0));
self.right_sidebar_file_preview_lines.clear();
- self.right_sidebar_file_preview_raw_text = None;
+ self.set_right_sidebar_file_preview_raw_text(None);
self.right_sidebar_file_preview_max_columns = 0;
self.clear_right_sidebar_file_preview_slice_cache();
self.right_sidebar_file_preview_image = None;
@@ -2300,16 +2701,19 @@ impl crate::TermWindow {
{
if let Some(window) = self.window.as_ref().cloned() {
let code = code.clone();
+ let ticket = self.note_surface_ticket();
promise::spawn::spawn(async move {
smol::Timer::after(Duration::from_millis(NOTE_CODE_HIGHLIGHT_DEBOUNCE_MS))
.await;
window.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
- term_window.start_note_code_highlight(
- code.source.start,
- key,
- generation,
- code,
- );
+ term_window.with_note_surface_ticket(ticket, move |term_window| {
+ term_window.start_note_code_highlight(
+ code.source.start,
+ key,
+ generation,
+ code,
+ );
+ });
})));
})
.detach();
@@ -2383,12 +2787,15 @@ impl crate::TermWindow {
.in_flight
.insert(block_start, (key, Arc::clone(&cancellation)));
let source: Arc<str> = Arc::from(code.text.as_str());
+ let ticket = self.note_surface_ticket();
syntax_highlight_pool().spawn(move || {
let stage = crate::input_diagnostics::StageTimer::begin("note_highlight");
let result = note_code_highlight_pair(&code, Some(cancellation.as_ref()));
stage.finish(result.is_some());
window.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
- term_window.apply_note_code_highlight_result(block_start, key, source, result);
+ term_window.with_note_surface_ticket(ticket, move |term_window| {
+ term_window.apply_note_code_highlight_result(block_start, key, source, result);
+ });
})));
});
}
@@ -2437,13 +2844,20 @@ impl crate::TermWindow {
return;
};
self.right_sidebar_note_code_highlight.repaint_scheduled = true;
+ let ticket = self.note_surface_ticket();
+ let surface = ticket.surface;
promise::spawn::spawn(async move {
smol::Timer::after(Duration::from_millis(NOTE_CODE_HIGHLIGHT_REPAINT_MS)).await;
window.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
- term_window
- .right_sidebar_note_code_highlight
- .repaint_scheduled = false;
- if term_window.right_sidebar_note_visible() {
+ let visible = term_window
+ .with_note_surface_ticket(ticket, |term_window| {
+ term_window
+ .right_sidebar_note_code_highlight
+ .repaint_scheduled = false;
+ surface != NoteSurface::Note || term_window.right_sidebar_note_visible()
+ })
+ .unwrap_or(false);
+ if visible {
term_window.invalidate_window();
}
})));
@@ -2539,7 +2953,7 @@ impl crate::TermWindow {
}
self.right_sidebar_file_preview_lines = result.lines;
- self.right_sidebar_file_preview_raw_text = result.raw_text;
+ self.set_right_sidebar_file_preview_raw_text(result.raw_text);
self.right_sidebar_file_preview_max_columns = self
.right_sidebar_file_preview_lines
.iter()
@@ -2590,7 +3004,62 @@ impl crate::TermWindow {
};
let key = right_sidebar_open_with_cache_key(&path);
self.start_right_sidebar_open_with_load_if_needed(&key, &path);
- let items = self.right_sidebar_open_with_menu_items(&key, &path);
+ let items = self.right_sidebar_open_with_menu_items(&key, &path, false);
+ self.show_term_context_menu(context, anchor, items);
+ }
+
+ /// Right-clicking a local file's preview: what the text offers first, as
+ /// the remote preview's menu does, then the file, with every Open With
+ /// app in a submenu.
+ pub(crate) fn show_right_sidebar_file_preview_context_menu(
+ &mut self,
+ context: &dyn WindowOps,
+ anchor: window::Point,
+ ) {
+ let Some(path) = self.right_sidebar_file_selected.clone() else {
+ return;
+ };
+ let key = right_sidebar_open_with_cache_key(&path);
+ self.start_right_sidebar_open_with_load_if_needed(&key, &path);
+ let has_selection = self.right_sidebar_file_preview_selected_text().is_some();
+ let has_text = self.right_sidebar_file_preview_image.is_none()
+ && !self.right_sidebar_file_preview_lines.is_empty();
+ let path_string = path.to_string_lossy().to_string();
+ let open_with = self.right_sidebar_open_with_menu_items(&key, &path, true);
+
+ self.begin_context_menu_application_actions();
+ let copy_selection = self.context_menu_application_item_with_icon(
+ crate::i18n::tr("menu-copy"),
+ ContextMenuIcon::Copy,
+ crate::termwindow::ContextMenuApplicationAction::CopyRemotePreviewSelection,
+ has_selection,
+ );
+ let copy_all = self.context_menu_application_item_with_icon(
+ crate::i18n::tr("right-copy-all"),
+ ContextMenuIcon::Copy,
+ crate::termwindow::ContextMenuApplicationAction::CopyRemotePreviewAll,
+ has_text,
+ );
+ let items = vec![
+ copy_selection,
+ copy_all,
+ ContextMenuItem::item_with_icon(
+ crate::i18n::tr("right-copy-path"),
+ ContextMenuIcon::Copy,
+ KeyAssignment::CopyFilePathToClipboard(path_string.clone()),
+ ),
+ ContextMenuItem::Separator,
+ ContextMenuItem::item_with_icon(
+ super::context_menu::reveal_in_folder_label(),
+ ContextMenuIcon::Folder,
+ KeyAssignment::RevealFileInFolder(path_string),
+ ),
+ ContextMenuItem::submenu_with_icon(
+ crate::i18n::tr("right-open-with-menu"),
+ ContextMenuIcon::Application,
+ open_with,
+ ),
+ ];
self.show_term_context_menu(context, anchor, items);
}
@@ -2711,7 +3180,15 @@ impl crate::TermWindow {
self.invalidate_window();
}
- fn right_sidebar_open_with_menu_items(&self, key: &str, path: &Path) -> Vec<ContextMenuItem> {
+ /// The Open With apps for `path`. The toolbar's list leaves out the app
+ /// its button already opens with; a submenu (`in_submenu`) lists every app
+ /// by its bare name, under a parent that says "Open With".
+ fn right_sidebar_open_with_menu_items(
+ &self,
+ key: &str,
+ path: &Path,
+ in_submenu: bool,
+ ) -> Vec<ContextMenuItem> {
let path_string = path.to_string_lossy().to_string();
let current_id = self
.current_right_sidebar_open_with_app(key)
@@ -2755,10 +3232,16 @@ impl crate::TermWindow {
let mut items: Vec<ContextMenuItem> =
sorted_open_with_candidates(candidates, current_id.as_deref())
.into_iter()
- .filter(|candidate| current_id.as_deref() != Some(candidate.id.as_str()))
+ .filter(|candidate| {
+ in_submenu || current_id.as_deref() != Some(candidate.id.as_str())
+ })
.map(|candidate| {
ContextMenuItem::item_with_icon(
- right_sidebar_arg("right-open-with", "app", candidate.label.clone()),
+ if in_submenu {
+ candidate.label.clone()
+ } else {
+ right_sidebar_arg("right-open-with", "app", candidate.label.clone())
+ },
ContextMenuIcon::Application,
KeyAssignment::OpenFileWith {
path: path_string.clone(),
@@ -2882,6 +3365,16 @@ impl crate::TermWindow {
if !self.right_sidebar_file_preview_active() {
return None;
}
+ if self.right_sidebar_markdown_preview_rendering() {
+ // The Markdown source of what is selected, as the Note copies.
+ let host = &self.right_sidebar_markdown_preview.as_ref()?.state.host;
+ let range = host.view.selection.range();
+ if range.is_empty() {
+ return None;
+ }
+ let session = host.session.as_ref()?;
+ return session.lock().source().get(range).map(str::to_string);
+ }
let (start, end) = self.right_sidebar_file_preview_selection_range()?;
let mut text = String::new();
for line_idx in start.line..=end.line {
@@ -4006,6 +4499,97 @@ impl crate::TermWindow {
self.show_term_context_menu(context, anchor, items);
}
+ /// The wiki link under a point of the rendered Markdown preview: what it
+ /// resolved to and every file it could mean.
+ pub(crate) fn right_sidebar_markdown_preview_wiki_link_at(
+ &mut self,
+ x: f32,
+ y: f32,
+ ) -> Option<(Option<String>, Vec<String>)> {
+ self.with_note_surface(NoteSurface::FilePreview, |term_window| {
+ let host = &term_window.right_sidebar_note;
+ let range = host.atomic_source_for_point(x, y)?;
+ host.projection
+ .objects
+ .iter()
+ .find_map(|object| match object {
+ crate::markdown_editor::ProjectedObject::WikiLink {
+ source,
+ resolved_path,
+ ambiguous_paths,
+ ..
+ } if *source == range => Some((resolved_path.clone(), ambiguous_paths.clone())),
+ _ => None,
+ })
+ })
+ .flatten()
+ }
+
+ /// The file a preview's wiki link resolved to, from its project-relative
+ /// path: under the local project root, or under the remote one for a
+ /// remote file. Either way it cannot leave the root.
+ fn right_sidebar_markdown_preview_link_action(
+ &self,
+ relative: &str,
+ ) -> Option<crate::termwindow::ContextMenuApplicationAction> {
+ if self.right_sidebar_remote_files.selected.is_some() {
+ let root = self.right_sidebar_remote_files.root.as_ref()?;
+ return remote_path_under(root, relative)
+ .map(crate::termwindow::ContextMenuApplicationAction::OpenRemoteFilePreview);
+ }
+ let preview = self.right_sidebar_markdown_preview.as_ref()?;
+ let raw_root = &preview.state.host.document.as_ref()?.vault_root;
+ let root = raw_root.canonicalize().ok()?;
+ let path = root.join(relative).canonicalize().ok()?;
+ // Checked in canonical form, opened in the root's own spelling, so the
+ // file keeps its place in the project (its tree row, its links).
+ let inside = path.strip_prefix(&root).ok()?;
+ Some(
+ crate::termwindow::ContextMenuApplicationAction::OpenFilePreview(raw_root.join(inside)),
+ )
+ }
+
+ /// Follow a rendered preview's wiki link: open the file it means in the
+ /// preview, or offer the files it could mean. An unresolved link (or one
+ /// in a remote file) does nothing -- a preview never creates notes.
+ pub(crate) fn follow_right_sidebar_markdown_preview_wiki_link(
+ &mut self,
+ context: &dyn WindowOps,
+ anchor: window::Point,
+ resolved: Option<String>,
+ ambiguous: Vec<String>,
+ ) {
+ if ambiguous.len() > 1 {
+ self.begin_context_menu_application_actions();
+ let items = ambiguous
+ .into_iter()
+ .filter_map(|relative| {
+ let action = self.right_sidebar_markdown_preview_link_action(&relative)?;
+ Some(self.context_menu_application_item_with_icon(
+ relative,
+ ContextMenuIcon::File,
+ action,
+ true,
+ ))
+ })
+ .collect();
+ self.show_term_context_menu(context, anchor, items);
+ return;
+ }
+ match resolved
+ .as_deref()
+ .and_then(|relative| self.right_sidebar_markdown_preview_link_action(relative))
+ {
+ Some(crate::termwindow::ContextMenuApplicationAction::OpenFilePreview(path)) => {
+ self.open_right_sidebar_file_path(path);
+ }
+ Some(crate::termwindow::ContextMenuApplicationAction::OpenRemoteFilePreview(path)) => {
+ self.open_right_sidebar_remote_file_preview(path);
+ }
+ _ => {}
+ }
+ }
+
pub(crate) fn show_right_sidebar_note_menu(
&mut self,
context: &dyn WindowOps,
@@ -4838,6 +5422,95 @@ impl crate::TermWindow {
}
}
+ /// Run `f` with `surface`'s state in the `right_sidebar_note*` fields,
+ /// then put the Note's back. The painter and the note helpers only ever
+ /// read those fields, so this is how they draw or update a surface other
+ /// than the Note without a line of them changing. Background work started
+ /// inside records `note_surface_installed` and comes back through here,
+ /// so a result can only land on the surface that asked for it; `None`
+ /// when that surface is gone by then.
+ pub(crate) fn with_note_surface<R>(
+ &mut self,
+ surface: NoteSurface,
+ f: impl FnOnce(&mut Self) -> R,
+ ) -> Option<R> {
+ if surface == self.note_surface_installed {
+ return Some(f(self));
+ }
+ // Only ever entered from the Note's state: a nested switch would put
+ // one surface's state into another's slot when unwinding.
+ if self.note_surface_installed != NoteSurface::Note {
+ log::error!(
+ "note surface {surface:?} requested inside {:?}",
+ self.note_surface_installed
+ );
+ return None;
+ }
+ let mut preview = match surface {
+ NoteSurface::Note => unreachable!("handled above"),
+ NoteSurface::FilePreview => self.right_sidebar_markdown_preview.take()?,
+ };
+ self.swap_note_surface_state(&mut preview.state);
+ self.note_surface_installed = surface;
+ self.note_surface_instance = preview.instance;
+ let result = f(self);
+ self.note_surface_installed = NoteSurface::Note;
+ self.note_surface_instance = 0;
+ self.swap_note_surface_state(&mut preview.state);
+ // `f` sees no preview while it runs, so it cannot have replaced it.
+ self.right_sidebar_markdown_preview = Some(preview);
+ Some(result)
+ }
+
+ /// The surface installed right now, instance included, for background
+ /// work to come back to through `with_note_surface_ticket`.
+ pub(crate) fn note_surface_ticket(&self) -> NoteSurfaceTicket {
+ NoteSurfaceTicket {
+ surface: self.note_surface_installed,
+ instance: self.note_surface_instance,
+ }
+ }
+
+ /// `with_note_surface` for the exact surface `ticket` names: `None` when
+ /// that surface is gone, even if another of its kind has replaced it.
+ pub(crate) fn with_note_surface_ticket<R>(
+ &mut self,
+ ticket: NoteSurfaceTicket,
+ f: impl FnOnce(&mut Self) -> R,
+ ) -> Option<R> {
+ let current = match ticket.surface {
+ NoteSurface::Note => 0,
+ NoteSurface::FilePreview if self.note_surface_installed == NoteSurface::FilePreview => {
+ self.note_surface_instance
+ }
+ NoteSurface::FilePreview => self.right_sidebar_markdown_preview.as_ref()?.instance,
+ };
+ if current != ticket.instance {
+ return None;
+ }
+ self.with_note_surface(ticket.surface, f)
+ }
+
+ fn swap_note_surface_state(&mut self, state: &mut NoteSurfaceState) {
+ std::mem::swap(&mut self.right_sidebar_note, &mut state.host);
+ std::mem::swap(
+ &mut self.right_sidebar_note_table_horizontal_offsets,
+ &mut state.table_horizontal_offsets,
+ );
+ std::mem::swap(
+ &mut self.right_sidebar_note_table_layouts,
+ &mut state.table_layouts,
+ );
+ std::mem::swap(
+ &mut self.right_sidebar_note_code_highlight,
+ &mut state.code_highlight,
+ );
+ std::mem::swap(
+ &mut self.right_sidebar_note_paint_cache,
+ &mut state.paint_cache,
+ );
+ }
+
fn schedule_right_sidebar_note_parse(&mut self) {
let Some((revision, source, mode, caret, document)) =
self.right_sidebar_note.background_parse_request()
@@ -4848,12 +5521,33 @@ impl crate::TermWindow {
self.right_sidebar_note.parse_in_flight_revision = None;
return;
};
+ let ticket = self.note_surface_ticket();
+ let surface = ticket.surface;
+ // A preview's wiki links resolve among its project's files; nothing is
+ // listed for a file without any.
+ let link_files = match &document {
+ Some((root, _)) if surface == NoteSurface::FilePreview && source.contains("[[") => {
+ self.preview_link_files(root)
+ }
+ _ => None,
+ };
promise::spawn::spawn(async move {
let result = promise::spawn::spawn_into_new_thread(move || {
let stage = crate::input_diagnostics::StageTimer::begin("note_projection");
let mut projection = crate::markdown_editor::MarkdownProjection::parse(&source);
if let Some((vault_root, relative_path)) = document {
- projection.resolve_vault_links(&vault_root, &relative_path);
+ if surface == NoteSurface::Note {
+ projection.resolve_vault_links(&vault_root, &relative_path);
+ } else if projection.has_wiki_links() {
+ if let Some(files) = link_files {
+ resolve_preview_wiki_links(
+ &mut projection,
+ &vault_root,
+ &relative_path,
+ files,
+ );
+ }
+ }
}
let visual = build_visual_document(&source, &projection, mode, caret);
stage.finish(true);
@@ -4861,25 +5555,28 @@ impl crate::TermWindow {
})
.await;
window.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
- match result {
- Ok((projection, visual)) => {
- let applied = term_window
- .right_sidebar_note
- .apply_background_parse(revision, mode, caret, projection, visual);
- if applied {
- term_window
- .schedule_right_sidebar_note_spellcheck(Duration::from_millis(50));
- term_window.invalidate_window();
+ term_window.with_note_surface_ticket(ticket, move |term_window| {
+ match result {
+ Ok((projection, visual)) => {
+ let applied = term_window
+ .right_sidebar_note
+ .apply_background_parse(revision, mode, caret, projection, visual);
+ if applied {
+ term_window.schedule_right_sidebar_note_spellcheck(
+ Duration::from_millis(50),
+ );
+ term_window.invalidate_window();
+ }
+ }
+ Err(err) => {
+ term_window.right_sidebar_note.parse_in_flight_revision = None;
+ log::error!("failed to parse Note in background: {err:#}");
}
}
- Err(err) => {
- term_window.right_sidebar_note.parse_in_flight_revision = None;
- log::error!("failed to parse Note in background: {err:#}");
+ if term_window.right_sidebar_note.background_parse_pending() {
+ term_window.schedule_right_sidebar_note_parse();
}
- }
- if term_window.right_sidebar_note.background_parse_pending() {
- term_window.schedule_right_sidebar_note_parse();
- }
+ });
})));
})
.detach();
@@ -4905,6 +5602,7 @@ impl crate::TermWindow {
self.right_sidebar_note.wrap_cache = wrap_cache;
return;
};
+ let ticket = self.note_surface_ticket();
promise::spawn::spawn(async move {
let result = promise::spawn::spawn_into_new_thread(move || {
// Worker-side wrap: slow is acceptable here. Decisions about
@@ -4931,7 +5629,7 @@ impl crate::TermWindow {
})
.await;
window.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
- match result {
+ term_window.with_note_surface_ticket(ticket, move |term_window| match result {
Ok((wrapped, wrap_cache)) => {
term_window
.right_sidebar_note
@@ -4944,7 +5642,7 @@ impl crate::TermWindow {
}
log::error!("failed to wrap Note in background: {err:#}");
}
- }
+ });
term_window.invalidate_window();
})));
})
@@ -4986,6 +5684,10 @@ impl crate::TermWindow {
visual: &Arc<VisualDocument>,
fonts: NotePrewarmFonts,
) {
+ // Prewarming serves the Note's typing latency; a preview does not type.
+ if self.note_surface_installed != NoteSurface::Note {
+ return;
+ }
let same_target = self
.right_sidebar_note_prewarm
.as_ref()
@@ -5100,6 +5802,10 @@ impl crate::TermWindow {
}
fn schedule_right_sidebar_note_spellcheck(&mut self, delay: Duration) {
+ // A read-only surface other than the Note is never spell-checked.
+ if self.note_surface_installed != NoteSurface::Note {
+ return;
+ }
let Some(window) = self.window.as_ref().cloned() else {
return;
};
@@ -5268,6 +5974,10 @@ impl crate::TermWindow {
}
fn sync_right_sidebar_note_native_text_input_snapshot(&mut self, wrap_key: usize) {
+ // Only the Note takes text input.
+ if self.note_surface_installed != NoteSurface::Note {
+ return;
+ }
let active = self.right_sidebar_note.view.focused
&& self.right_sidebar_note.view.mode != EditorMode::ReadOnly;
let Some(window) = self.window.as_ref().cloned() else {
@@ -5411,7 +6121,7 @@ impl crate::TermWindow {
}
fn right_sidebar_note_visible(&self) -> bool {
- !self.right_sidebar_collapsed && self.right_sidebar_mode == RightSidebarMode::Tasks
+ self.right_sidebar_presented() && self.right_sidebar_mode == RightSidebarMode::Tasks
}
fn apply_right_sidebar_note_published_revision(
@@ -5608,14 +6318,37 @@ impl crate::TermWindow {
}
}
+ /// The panel as presented -- a hover reveal included. Terminal geometry
+ /// wants `right_sidebar_width` instead.
pub fn right_sidebar_rect(&self) -> Option<RightSidebarRect> {
+ let mut rect = self.right_sidebar_rect_for_width(self.right_sidebar_presented_width())?;
+ // Off macOS the panel has no toggle of its own and the tab bar keeps
+ // the window buttons at its right end; a hover-revealed panel stays
+ // below the tab bar so both remain reachable.
+ if !cfg!(target_os = "macos")
+ && self.right_sidebar_collapsed
+ && self.show_tab_bar
+ && !self.config.tab_bar_at_bottom
+ {
+ let tab_bar = self.tab_bar_pixel_height().unwrap_or(0.0).ceil() as usize;
+ let top = tab_bar.max(rect.y);
+ rect.height = rect.height.saturating_sub(top - rect.y);
+ rect.y = top;
+ if rect.height == 0 {
+ return None;
+ }
+ }
+ Some(rect)
+ }
+
+ fn right_sidebar_rect_for_width(&self, width: usize) -> Option<RightSidebarRect> {
let border = self.get_os_border();
let bottom_tab_bar_height = if self.config.tab_bar_at_bottom && self.show_tab_bar {
self.tab_bar_pixel_height().unwrap_or(0.0).ceil() as usize
} else {
0
};
- let width = self.right_sidebar_width().min(
+ let width = width.min(
self.dimensions
.pixel_width
.saturating_sub((border.left + border.right).get() as usize),
@@ -6076,7 +6809,7 @@ impl crate::TermWindow {
let Some(rect) = self.right_sidebar_rect() else {
return None;
};
- if self.right_sidebar_collapsed
+ if !self.right_sidebar_presented()
|| self.right_sidebar_mode != RightSidebarMode::Snippets
|| self.right_sidebar_snippet_view != RightSidebarSnippetView::List
{
@@ -7620,8 +8353,14 @@ impl crate::TermWindow {
self.schedule_right_sidebar_note_parse();
self.schedule_right_sidebar_note_spellcheck(Duration::from_millis(50));
- let toolbar_height = self.ui_px(NOTE_TOOLBAR_HEIGHT);
- let menu_size = toolbar_height;
+ // The Note has its toolbar; another surface brings its own header.
+ let is_note = self.note_surface_installed == NoteSurface::Note;
+ let toolbar_height = if is_note {
+ self.ui_px(NOTE_TOOLBAR_HEIGHT)
+ } else {
+ 0
+ };
+ let menu_size = self.ui_px(NOTE_TOOLBAR_HEIGHT);
let body_y = content_top + toolbar_height + self.ui_px(NOTE_BODY_TOP_GAP);
let body_bottom = content_bottom.saturating_sub(self.ui_px(SIDEBAR_INSET));
@@ -7635,7 +8374,11 @@ impl crate::TermWindow {
y: body_y,
width: content_width,
height: body_height,
- item_type: UIItemType::RightSidebarNoteBody,
+ item_type: if is_note {
+ UIItemType::RightSidebarNoteBody
+ } else {
+ UIItemType::RightSidebarFilePreviewMarkdownBody
+ },
});
let settings = crate::native_settings::load();
@@ -7706,8 +8449,14 @@ impl crate::TermWindow {
let preedit = self.right_sidebar_note.view.preedit.clone();
let padding = self.ui_px(NOTE_BODY_PADDING) as f32;
let available_reading_width = (content_width as f32 - padding * 2.0).max(1.0);
- let reading_width = available_reading_width
- .min(self.ui_f32(self.config.note_reading_max_width.max(320) as f32));
+ // A rendered file preview reads wider than a note: it is someone
+ // else's document, often a README laid out for a browser.
+ let reading_max_width = if self.note_surface_installed == NoteSurface::FilePreview {
+ MARKDOWN_PREVIEW_READING_MAX_WIDTH
+ } else {
+ self.config.note_reading_max_width.max(320)
+ };
+ let reading_width = available_reading_width.min(self.ui_f32(reading_max_width as f32));
let text_left = content_x as f32 + (content_width as f32 - reading_width) / 2.0;
let clip_left = text_left;
let clip_right = text_left + reading_width;
@@ -7862,18 +8611,20 @@ impl crate::TermWindow {
self.right_sidebar_note.code_block_layouts.clear();
self.right_sidebar_note.viewport_height = body_height as f32;
self.right_sidebar_note.content_height = body_height as f32;
- self.paint_note_editor_toolbar(
- layers,
- chrome,
- foreground,
- muted_fg,
- content_x,
- content_top,
- content_width,
- menu_size,
- show_tree_button,
- tree_button_icon,
- )?;
+ if is_note {
+ self.paint_note_editor_toolbar(
+ layers,
+ chrome,
+ foreground,
+ muted_fg,
+ content_x,
+ content_top,
+ content_width,
+ menu_size,
+ show_tree_button,
+ tree_button_icon,
+ )?;
+ }
note_layout_stage.finish(true);
if let Some(before) = note_stats_before {
let after = self
@@ -8125,10 +8876,20 @@ impl crate::TermWindow {
let mut image_sources = HashMap::new();
if let Some(document) = active_document.as_ref() {
+ // Embeds resolve to canonical paths; the root may not be one
+ // (a preview's project root is spelled as the user opened it).
+ let canonical_root = if document.vault_root.as_os_str().is_empty() {
+ None
+ } else {
+ document.vault_root.canonicalize().ok()
+ };
for object in &projected_objects {
let resolved = match object {
ProjectedObject::Image { source, target, .. } => {
+ // A previewed file is not the user's own note: it
+ // never makes the desktop fetch anything.
if self.config.note_remote_images_enabled
+ && self.note_surface_installed == NoteSurface::Note
&& (target.starts_with("https://") || target.starts_with("http://"))
{
Some((
@@ -8153,9 +8914,16 @@ impl crate::TermWindow {
resolved_path: Some(target),
..
} => {
- let candidate = document.vault_root.join(target).canonicalize().ok();
- candidate
- .filter(|candidate| candidate.starts_with(&document.vault_root))
+ // A remote preview has no local root: an empty one
+ // would resolve against the process's own folder.
+ canonical_root
+ .as_ref()
+ .and_then(|root| {
+ root.join(target)
+ .canonicalize()
+ .ok()
+ .filter(|candidate| candidate.starts_with(root))
+ })
.map(|path| {
(source.start, RightSidebarNoteImageSource::Local(path))
})
@@ -8445,7 +9213,13 @@ impl crate::TermWindow {
y: header_y,
width: wrap_width as usize,
height: header_height,
- item_type: UIItemType::RightSidebarNoteCodeToggle(code_row.block_start),
+ item_type: if is_note {
+ UIItemType::RightSidebarNoteCodeToggle(code_row.block_start)
+ } else {
+ UIItemType::RightSidebarFilePreviewMarkdownCodeToggle(
+ code_row.block_start,
+ )
+ },
});
let control_size = self.ui_px(NOTE_CODE_CONTROL_SIZE);
let control_y = header_y + header_height.saturating_sub(control_size) / 2;
@@ -8463,7 +9237,13 @@ impl crate::TermWindow {
} else {
SvgIcon::ChevronDown
},
- UIItemType::RightSidebarNoteCodeToggle(code_row.block_start),
+ if is_note {
+ UIItemType::RightSidebarNoteCodeToggle(code_row.block_start)
+ } else {
+ UIItemType::RightSidebarFilePreviewMarkdownCodeToggle(
+ code_row.block_start,
+ )
+ },
)?;
let copy_x = clip_right.max(text_left) as usize - control_size - self.ui_px(4);
self.paint_snippet_icon_button(
@@ -8475,7 +9255,13 @@ impl crate::TermWindow {
control_y,
control_size,
SvgIcon::Copy,
- UIItemType::RightSidebarNoteCodeCopy(code_row.block_start),
+ if is_note {
+ UIItemType::RightSidebarNoteCodeCopy(code_row.block_start)
+ } else {
+ UIItemType::RightSidebarFilePreviewMarkdownCodeCopy(
+ code_row.block_start,
+ )
+ },
)?;
let language_x = leading_x + control_size + self.ui_px(4);
self.paint_sidebar_text(
@@ -9087,18 +9873,20 @@ impl crate::TermWindow {
self.ui_px(FILE_SCROLL_FADE_HEIGHT).min(body_height),
)?;
}
- self.paint_note_editor_toolbar(
- layers,
- chrome,
- foreground,
- muted_fg,
- content_x,
- content_top,
- content_width,
- menu_size,
- show_tree_button,
- tree_button_icon,
- )?;
+ if is_note {
+ self.paint_note_editor_toolbar(
+ layers,
+ chrome,
+ foreground,
+ muted_fg,
+ content_x,
+ content_top,
+ content_width,
+ menu_size,
+ show_tree_button,
+ tree_button_icon,
+ )?;
+ }
if self.right_sidebar_note.content_height > self.right_sidebar_note.viewport_height {
let track_height = body_height as f32;
@@ -10126,26 +10914,28 @@ impl crate::TermWindow {
let refresh_x = content_x + content_width.saturating_sub(refresh_size);
let refresh_y =
content_top + self.ui_px(FILE_FILTER_HEIGHT).saturating_sub(refresh_size) / 2;
- let label_width = content_width.saturating_sub(refresh_size + self.ui_px(SIDEBAR_INSET));
- let label = self
- .right_sidebar_remote_files
- .target
- .as_ref()
- .map(|target| target.project_name.clone())
- .unwrap_or_else(|| crate::i18n::tr("right-remote-files"));
- self.paint_sidebar_text(
+ // The same filter box as a local project; the project's name heads
+ // the tree below it.
+ let filter_width = content_width.saturating_sub(refresh_size + self.ui_px(SIDEBAR_INSET));
+ let filter_label = crate::i18n::tr("right-filter-files");
+ let filter_input = self.right_sidebar_file_filter.clone();
+ self.paint_snippet_text_box(
layers,
+ 1,
ui_font,
ui_metrics,
- &label,
+ chrome,
+ muted_fg,
content_x,
- content_top
- + self
- .ui_px(FILE_FILTER_HEIGHT)
- .saturating_sub(ui_metrics.cell_size.height as usize)
- / 2,
- label_width,
- foreground,
+ content_top,
+ filter_width,
+ self.ui_px(FILE_FILTER_HEIGHT),
+ Some(SvgIcon::Search),
+ &filter_label,
+ &filter_input,
+ self.right_sidebar_file_focus == Some(RightSidebarFileField::Filter),
+ UIItemType::RightSidebarFileFilter,
+ false,
)?;
self.paint_files_preview_header_icon_button(
layers,
@@ -10176,8 +10966,27 @@ impl crate::TermWindow {
)?;
tree_top += ui_metrics.cell_size.height as usize + self.ui_px(8);
}
- let rows = self.right_sidebar_remote_files.rows();
+ let query = self.right_sidebar_file_filter_for_tree().trim().to_string();
+ self.update_remote_file_search(&query);
+ let searching = !query.is_empty();
+ let rows = if searching {
+ Arc::clone(&self.right_sidebar_remote_file_search.rows)
+ } else {
+ Arc::new(self.right_sidebar_remote_files.rows())
+ };
if rows.is_empty() {
+ let search = &self.right_sidebar_remote_file_search;
+ let message = match (&search.status, search.index.is_some()) {
+ _ if !searching => crate::i18n::tr("right-loading-remote-directory"),
+ (RemoteFileSearchStatus::NeedsUpdate, _) => {
+ crate::i18n::tr("right-remote-search-needs-update")
+ }
+ (RemoteFileSearchStatus::Failed(message), false) => message.clone(),
+ (_, false) => crate::i18n::tr("right-indexing-files"),
+ // Still searching: say nothing rather than "no matches".
+ (_, true) if search.search_cancel.is_some() => String::new(),
+ (_, true) => crate::i18n::tr("right-no-matching-files"),
+ };
return self.paint_files_message(
layers,
ui_font,
@@ -10189,16 +10998,17 @@ impl crate::TermWindow {
content_width,
content_bottom,
self.ui_px(22),
- &crate::i18n::tr("right-loading-remote-directory"),
+ &message,
);
}
let row_metrics = right_sidebar_file_row_metrics(ui_metrics);
- let truncated_height = if self.right_sidebar_remote_files.has_truncated_directory() {
- row_metrics.row_height
- } else {
- 0
- };
+ let truncated_height =
+ if !searching && self.right_sidebar_remote_files.has_truncated_directory() {
+ row_metrics.row_height
+ } else {
+ 0
+ };
let strip_rows = self.transfer_strip_rows(
content_bottom
.saturating_sub(tree_top)
@@ -10263,19 +11073,23 @@ impl crate::TermWindow {
content_width,
tree_top.saturating_sub(content_top),
)?;
- self.paint_sidebar_text(
+ self.paint_snippet_text_box(
layers,
+ 2,
ui_font,
ui_metrics,
- &label,
+ chrome,
+ muted_fg,
content_x,
- content_top
- + self
- .ui_px(FILE_FILTER_HEIGHT)
- .saturating_sub(ui_metrics.cell_size.height as usize)
- / 2,
- label_width,
- foreground,
+ content_top,
+ filter_width,
+ self.ui_px(FILE_FILTER_HEIGHT),
+ Some(SvgIcon::Search),
+ &filter_label,
+ &filter_input,
+ self.right_sidebar_file_focus == Some(RightSidebarFileField::Filter),
+ UIItemType::RightSidebarFileFilter,
+ false,
)?;
self.paint_files_preview_header_icon_button(
layers,
@@ -12131,7 +12945,12 @@ impl crate::TermWindow {
anchor: Point,
path: RemotePath,
) {
- let Some(kind) = self.right_sidebar_remote_files.kind_for_path(&path) else {
+ // A search result can sit in a folder the tree never loaded.
+ let Some(kind) = self
+ .right_sidebar_remote_files
+ .kind_for_path(&path)
+ .or_else(|| self.right_sidebar_remote_file_search.kind_for_path(&path))
+ else {
return;
};
let Some(origin) = self.current_remote_operation_origin() else {
@@ -13766,40 +14585,291 @@ impl crate::TermWindow {
pub(crate) fn refresh_right_sidebar_remote_files(&mut self) {
self.close_right_sidebar_file_preview();
+ self.right_sidebar_remote_file_search.release();
let effects = self
.right_sidebar_remote_files
- .transition(RemoteFilesEvent::Refresh);
- self.apply_right_sidebar_remote_files_effects(effects);
+ .transition(RemoteFilesEvent::Refresh);
+ self.apply_right_sidebar_remote_files_effects(effects);
+ }
+
+ pub(crate) fn open_right_sidebar_remote_file(&mut self, path: RemotePath) {
+ // A search result can sit in a folder the tree never loaded; its kind
+ // comes from the listing then.
+ let kind = self
+ .right_sidebar_remote_files
+ .kind_for_path(&path)
+ .or_else(|| self.right_sidebar_remote_file_search.kind_for_path(&path));
+ match kind {
+ Some(RemoteFileKind::Directory) => {
+ let effects = self
+ .right_sidebar_remote_files
+ .transition(RemoteFilesEvent::ToggleDirectory(path));
+ self.apply_right_sidebar_remote_files_effects(effects);
+ }
+ Some(RemoteFileKind::File) => self.open_right_sidebar_remote_file_preview(path),
+ Some(RemoteFileKind::Symlink) | Some(RemoteFileKind::Other) | None => {}
+ }
+ }
+
+ /// Preview the remote file at `path`, known to be a file.
+ pub(crate) fn open_right_sidebar_remote_file_preview(&mut self, path: RemotePath) {
+ if !self.right_sidebar_file_preview_active() {
+ let max_tree_for_preview = self
+ .right_sidebar_pane_total_max_width()
+ .saturating_sub(self.ui_px(FILE_PREVIEW_PANE_MIN_WIDTH))
+ .max(self.ui_px(RIGHT_SIDEBAR_MIN_WIDTH));
+ self.right_sidebar_file_tree_width = self
+ .right_sidebar_width
+ .clamp(self.ui_px(RIGHT_SIDEBAR_MIN_WIDTH), max_tree_for_preview);
+ }
+ self.close_right_sidebar_file_preview();
+ self.right_sidebar_file_view = RightSidebarFileView::Preview;
+ self.right_sidebar_file_preview_message =
+ Some("Loading remote file preview...".to_string());
+ let effects = self
+ .right_sidebar_remote_files
+ .transition(RemoteFilesEvent::SelectFile(path));
+ self.apply_right_sidebar_remote_files_effects(effects);
+ }
+
+ fn remote_file_search_key(&self) -> Option<RemoteFileSearchKey> {
+ Some(RemoteFileSearchKey {
+ source_key: self.right_sidebar_remote_files.current_source_key()?,
+ root: self.right_sidebar_remote_files.root.clone()?,
+ respect_gitignore: self.config.right_sidebar_search_respects_gitignore,
+ })
+ }
+
+ /// Answer `query` for the remote project, listing the project on the
+ /// remote host first when there is no index for it yet or it has gone
+ /// stale. The listing is one short `thinkterm list-files` on the existing
+ /// connection; typing only searches what it returned.
+ fn update_remote_file_search(&mut self, query: &str) {
+ if query.is_empty() {
+ let search = &mut self.right_sidebar_remote_file_search;
+ search.cancel_search();
+ search.query.clear();
+ search.rows = Arc::default();
+ return;
+ }
+ let Some(key) = self.remote_file_search_key() else {
+ return;
+ };
+ if self.right_sidebar_remote_file_search.key.as_ref() != Some(&key) {
+ self.right_sidebar_remote_file_search.release();
+ self.right_sidebar_remote_file_search.key = Some(key.clone());
+ }
+ let search = &self.right_sidebar_remote_file_search;
+ let query_changed = search.query != query;
+ let stale = search
+ .built_at
+ .is_some_and(|at| at.elapsed() >= Duration::from_secs(FILE_INDEX_RESCAN_SECS));
+ let needs_listing = match &search.status {
+ RemoteFileSearchStatus::Idle => true,
+ RemoteFileSearchStatus::Indexing | RemoteFileSearchStatus::NeedsUpdate => false,
+ RemoteFileSearchStatus::Ready => stale && query_changed,
+ // Tried again when the query changes, not on every frame; a
+ // timeout only by a refresh or reconnect.
+ RemoteFileSearchStatus::Failed(message) => {
+ query_changed
+ && message != crate::termwindow::remote_files::REMOTE_LISTING_TIMED_OUT
+ }
+ };
+ if needs_listing {
+ self.start_remote_file_search_listing(key.clone());
+ }
+ if query_changed {
+ self.right_sidebar_remote_file_search.query = query.to_string();
+ self.spawn_remote_file_search_rows(key.root);
+ self.right_sidebar_remote_file_tree_scroll_offset = 0.0;
+ }
+ }
+
+ /// Search the remote index for the current query off the UI thread, as
+ /// the local search is; the rows answer an earlier query until it lands.
+ /// A newer query cancels it.
+ fn spawn_remote_file_search_rows(&mut self, root: RemotePath) {
+ let search = &mut self.right_sidebar_remote_file_search;
+ search.cancel_search();
+ let Some(index) = search.index.clone() else {
+ search.rows = Arc::default();
+ return;
+ };
+ if search.query.is_empty() {
+ search.rows = Arc::default();
+ return;
+ }
+ let Some(window) = self.window.as_ref().cloned() else {
+ return;
+ };
+ let cancel = Arc::new(AtomicBool::new(false));
+ search.search_cancel = Some(Arc::clone(&cancel));
+ let generation = search.generation;
+ let query = search.query.clone();
+ promise::spawn::spawn(async move {
+ let (rows, query) = promise::spawn::spawn_into_new_thread(move || {
+ let rows = remote_file_search_rows(&index, &root, &query, &cancel);
+ Ok::<_, anyhow::Error>((
+ (!cancel.load(AtomicOrdering::Relaxed)).then_some(rows),
+ query,
+ ))
+ })
+ .await?;
+ let Some(rows) = rows else {
+ return Ok(());
+ };
+ window.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
+ let search = &mut term_window.right_sidebar_remote_file_search;
+ if search.generation != generation || search.query != query {
+ return;
+ }
+ search.search_cancel = None;
+ search.rows = Arc::new(rows);
+ term_window.invalidate_window();
+ })));
+ Ok::<(), anyhow::Error>(())
+ })
+ .detach();
+ }
+
+ /// The remote project's files, for a remote preview's wiki links: the
+ /// listing search keeps when there is one for this root; otherwise it is
+ /// requested, and the preview resolves its links again when it lands.
+ fn remote_file_paths_for_preview_links(&mut self) -> Option<Arc<Vec<String>>> {
+ let key = self.remote_file_search_key()?;
+ if self.right_sidebar_remote_file_search.key.as_ref() != Some(&key) {
+ self.right_sidebar_remote_file_search.release();
+ self.right_sidebar_remote_file_search.key = Some(key.clone());
+ }
+ if let Some(index) = self.right_sidebar_remote_file_search.index.as_ref() {
+ return Some(index.file_paths());
+ }
+ if self.right_sidebar_remote_file_search.status == RemoteFileSearchStatus::Idle {
+ self.start_remote_file_search_listing(key);
+ }
+ None
+ }
+
+ /// Where a preview rooted at `root` finds the files its wiki links may
+ /// name: a remote project's listing; the local project's search index when
+ /// there is one, else a walk under the search's rules; and for a file
+ /// outside the project, the files beside it. `None` while a remote
+ /// listing is still on its way.
+ fn preview_link_files(&mut self, root: &Path) -> Option<PreviewLinkFiles> {
+ if root.as_os_str().is_empty() {
+ return self
+ .remote_file_paths_for_preview_links()
+ .map(PreviewLinkFiles::Listed);
+ }
+ if self.right_sidebar_file_index_root.as_deref() != Some(root) {
+ return Some(PreviewLinkFiles::Folder);
+ }
+ Some(match self.right_sidebar_file_index.as_ref() {
+ Some(index) => PreviewLinkFiles::Listed(index.file_paths()),
+ None => PreviewLinkFiles::WalkProject {
+ respect_gitignore: self.config.right_sidebar_search_respects_gitignore,
+ },
+ })
+ }
+
+ fn start_remote_file_search_listing(&mut self, key: RemoteFileSearchKey) {
+ // Without a connection yet this stays Idle and is tried again once the
+ // lease lands.
+ let Some(lease) = self.right_sidebar_remote_files_lease.as_ref() else {
+ return;
+ };
+ if self.current_remote_connection_key().as_deref() != Some(lease.connection_key()) {
+ return;
+ }
+ let Some(operation_lease) = lease.operation_lease() else {
+ return;
+ };
+ let backend = lease.backend();
+ let connection_key = lease.connection_key().to_string();
+ let connection_id = lease.connection_id();
+ let Some(window) = self.window.as_ref().cloned() else {
+ return;
+ };
+ let project_name = self
+ .right_sidebar_remote_files
+ .target
+ .as_ref()
+ .map(|target| target.project_name.clone())
+ .unwrap_or_default();
+ let search = &mut self.right_sidebar_remote_file_search;
+ search.status = RemoteFileSearchStatus::Indexing;
+ let generation = search.generation;
+ promise::spawn::spawn(async move {
+ let listed = backend
+ .list_project_files(key.root.clone(), key.respect_gitignore)
+ .await;
+ drop(operation_lease);
+ if let Err(message) = &listed {
+ if crate::termwindow::remote_files::remote_listing_failure_is_transport(message) {
+ invalidate_remote_connection_if_dead(&connection_key, connection_id, message);
+ }
+ }
+ let result = match listed {
+ Ok(RemoteProjectListing::Listed(listing)) => {
+ let truncated = listing.truncated;
+ promise::spawn::spawn_into_new_thread(move || {
+ Ok(remote_file_index_from_listing(&project_name, &listing))
+ })
+ .await
+ .map(|index| Some((Arc::new(index), truncated)))
+ .map_err(|err| format!("Unable to index remote files: {err}"))
+ }
+ Ok(RemoteProjectListing::NeedsUpdate) => Ok(None),
+ Err(message) => Err(message),
+ };
+ window.notify(TermWindowNotif::Apply(Box::new(move |term_window| {
+ term_window.apply_remote_file_search_listing(generation, key, result);
+ })));
+ })
+ .detach();
}
- pub(crate) fn open_right_sidebar_remote_file(&mut self, path: RemotePath) {
- match self.right_sidebar_remote_files.kind_for_path(&path) {
- Some(RemoteFileKind::Directory) => {
- let effects = self
- .right_sidebar_remote_files
- .transition(RemoteFilesEvent::ToggleDirectory(path));
- self.apply_right_sidebar_remote_files_effects(effects);
- }
- Some(RemoteFileKind::File) => {
- if !self.right_sidebar_file_preview_active() {
- let max_tree_for_preview = self
- .right_sidebar_pane_total_max_width()
- .saturating_sub(self.ui_px(FILE_PREVIEW_PANE_MIN_WIDTH))
- .max(self.ui_px(RIGHT_SIDEBAR_MIN_WIDTH));
- self.right_sidebar_file_tree_width = self
- .right_sidebar_width
- .clamp(self.ui_px(RIGHT_SIDEBAR_MIN_WIDTH), max_tree_for_preview);
+ fn apply_remote_file_search_listing(
+ &mut self,
+ generation: u64,
+ key: RemoteFileSearchKey,
+ result: Result<Option<(Arc<RightSidebarFileIndex>, bool)>, String>,
+ ) {
+ let mut listed = false;
+ let search = &mut self.right_sidebar_remote_file_search;
+ if search.generation != generation || search.key.as_ref() != Some(&key) {
+ return;
+ }
+ match result {
+ Ok(Some((index, truncated))) => {
+ if truncated {
+ log::warn!(
+ "Remote file search stopped at a limit; some files will not be findable by name"
+ );
}
- self.close_right_sidebar_file_preview();
- self.right_sidebar_file_view = RightSidebarFileView::Preview;
- self.right_sidebar_file_preview_message =
- Some("Loading remote file preview...".to_string());
- let effects = self
- .right_sidebar_remote_files
- .transition(RemoteFilesEvent::SelectFile(path));
- self.apply_right_sidebar_remote_files_effects(effects);
+ search.index = Some(index);
+ search.status = RemoteFileSearchStatus::Ready;
+ search.built_at = Some(Instant::now());
+ listed = true;
}
- Some(RemoteFileKind::Symlink) | Some(RemoteFileKind::Other) | None => {}
+ Ok(None) => {
+ search.index = None;
+ search.status = RemoteFileSearchStatus::NeedsUpdate;
+ }
+ // An index from before stays searchable.
+ Err(message) => search.status = RemoteFileSearchStatus::Failed(message),
+ }
+ self.spawn_remote_file_search_rows(key.root.clone());
+ if listed && self.right_sidebar_remote_files.selected.is_some() {
+ // A remote preview may be waiting on these files for its links.
+ self.with_note_surface(NoteSurface::FilePreview, |term_window| {
+ if term_window.right_sidebar_note.projection.has_wiki_links() {
+ term_window.right_sidebar_note.request_link_resolution();
+ }
+ });
+ }
+ if let Some(window) = self.window.as_ref() {
+ window.invalidate();
}
}
@@ -13947,7 +15017,7 @@ impl crate::TermWindow {
return;
}
term_window.right_sidebar_file_preview_lines = result.lines;
- term_window.right_sidebar_file_preview_raw_text = result.raw_text;
+ term_window.set_right_sidebar_file_preview_raw_text(result.raw_text);
term_window.right_sidebar_file_preview_max_columns = term_window
.right_sidebar_file_preview_lines
.iter()
@@ -14840,8 +15910,18 @@ impl crate::TermWindow {
)?;
let action_gap = self.ui_px(PREVIEW_HEADER_ACTION_GAP);
- let available_after_back =
- content_width.saturating_sub(button_size + self.ui_px(SIDEBAR_INSET));
+ let toggle_width = self.paint_markdown_preview_toggle(
+ layers,
+ chrome,
+ foreground,
+ muted_fg,
+ content_x + button_size + action_gap,
+ header_top,
+ button_size,
+ )?;
+ let available_after_back = content_width
+ .saturating_sub(button_size + self.ui_px(SIDEBAR_INSET))
+ .saturating_sub(toggle_width);
// The label is always shown in full. Size the button to fit it, limited
// only by the room left after the two icon buttons, the gaps and a small
// reserved minimum for the filename — no fixed cap, so a wide pane is
@@ -14904,7 +15984,7 @@ impl crate::TermWindow {
action_x += button_size + action_gap;
}
- let title_x = content_x + button_size + self.ui_px(SIDEBAR_INSET);
+ let title_x = content_x + button_size + self.ui_px(SIDEBAR_INSET) + toggle_width;
let title_right = actions_x.saturating_sub(self.ui_px(SIDEBAR_INSET));
let title_width = title_right.saturating_sub(title_x);
let title = file_name_for_path(path);
@@ -14947,6 +16027,15 @@ impl crate::TermWindow {
SvgIcon::X,
UIItemType::RightSidebarRemoteFileBack,
)?;
+ let toggle_width = self.paint_markdown_preview_toggle(
+ layers,
+ chrome,
+ foreground,
+ muted_fg,
+ content_x + button_size + self.ui_px(PREVIEW_HEADER_ACTION_GAP),
+ header_top,
+ button_size,
+ )?;
let can_copy = self.right_sidebar_file_preview_image.is_none()
&& !self.right_sidebar_file_preview_lines.is_empty();
let actions_x = content_x + content_width.saturating_sub(button_size);
@@ -14963,7 +16052,7 @@ impl crate::TermWindow {
UIItemType::RightSidebarRemoteFileCopyText,
)?;
}
- let title_x = content_x + button_size + self.ui_px(SIDEBAR_INSET);
+ let title_x = content_x + button_size + self.ui_px(SIDEBAR_INSET) + toggle_width;
let title_right = if can_copy {
actions_x.saturating_sub(self.ui_px(SIDEBAR_INSET))
} else {
@@ -15166,7 +16255,7 @@ impl crate::TermWindow {
content_x: usize,
content_top: usize,
content_width: usize,
- _content_bottom: usize,
+ content_bottom: usize,
) -> anyhow::Result<()> {
let local_path = self.right_sidebar_file_selected.clone();
let remote_path = self.right_sidebar_remote_files.selected.clone();
@@ -15204,7 +16293,22 @@ impl crate::TermWindow {
let mut show_top_fade = false;
let mut show_scrollbars = false;
- if let Some(message) = self.right_sidebar_file_preview_message.clone() {
+ if self.right_sidebar_markdown_preview_rendering()
+ && self.ensure_right_sidebar_markdown_preview()
+ {
+ self.paint_right_sidebar_markdown_preview(
+ layers,
+ ui_font,
+ ui_metrics,
+ chrome,
+ foreground,
+ muted_fg,
+ metrics.x,
+ metrics.y,
+ metrics.width,
+ content_bottom,
+ )?;
+ } else if let Some(message) = self.right_sidebar_file_preview_message.clone() {
self.paint_sidebar_text(
layers, ui_font, ui_metrics, &message, body_x, body_y, body_width, muted_fg,
)?;
@@ -18865,26 +19969,11 @@ fn build_right_sidebar_file_index_with_cancel(
char_bag: RightSidebarFileCharBag::from_str(project_name),
}];
- // `ignore` is ripgrep's walker: it prunes ignored directories as it goes
- // rather than listing then discarding them, which is what keeps a project
- // with a large vendored subtree from costing hundreds of milliseconds.
- let mut builder = WalkBuilder::new(root);
- builder
- // Dotfiles stay visible — `.github`, `.cargo` and friends are part of
- // the project. Only .gitignore decides what is hidden.
- .hidden(false)
- .follow_links(false)
- .git_ignore(respect_gitignore)
- .git_exclude(respect_gitignore)
- // Only this project's own ignore rules: no global core.excludesFile and
- // no walking up into parent repositories.
- .git_global(false)
- .parents(false)
- // Honour .gitignore even in a directory that is not a git repo yet.
- .require_git(false)
- .filter_entry(should_index_walk_entry);
-
- for entry in builder.build().skip(1) {
+ // The same rules a remote project is indexed with (`thinkterm list-files`).
+ for entry in thinkterm_file_index::project_walker(root, respect_gitignore)
+ .build()
+ .skip(1)
+ {
if entries.len() >= FILE_INDEX_ENTRY_LIMIT {
log::warn!(
"File search index for {} hit the {} entry limit; \
@@ -18922,7 +20011,7 @@ fn build_right_sidebar_file_index_with_cancel(
return Err("File indexing canceled".to_string());
}
- Ok(RightSidebarFileIndex { entries })
+ Ok(RightSidebarFileIndex::new(entries))
}
/// Whether a directory that was just read differs from what the cache holds.
@@ -19111,45 +20200,314 @@ fn search_right_sidebar_file_index(
.collect()
}
-fn should_index_walk_entry(entry: &IgnoreDirEntry) -> bool {
- if entry.depth() == 0 || !entry.file_type().is_some_and(|kind| kind.is_dir()) {
- return true;
+/// What a remote project's search index is for: the connection, the root it
+/// lists and the ignore rule it was listed under.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub(crate) struct RemoteFileSearchKey {
+ source_key: String,
+ root: RemotePath,
+ respect_gitignore: bool,
+}
+
+#[derive(Clone, Debug, Default, PartialEq, Eq)]
+pub(crate) enum RemoteFileSearchStatus {
+ #[default]
+ Idle,
+ Indexing,
+ Ready,
+ /// The remote host has no `thinkterm` that can list files.
+ NeedsUpdate,
+ Failed(String),
+}
+
+/// Search for a remote project: the listing `thinkterm list-files` sends back,
+/// indexed and searched here exactly as a local project's index is. Held only
+/// while it is used; released with the rest of the Files memory.
+#[derive(Default)]
+pub(crate) struct RemoteFileSearchState {
+ key: Option<RemoteFileSearchKey>,
+ status: RemoteFileSearchStatus,
+ index: Option<Arc<RightSidebarFileIndex>>,
+ built_at: Option<Instant>,
+ /// Bumped on every reset, so a listing requested before it is dropped.
+ generation: u64,
+ /// The query `rows` answer, or will once the search running for it
+ /// lands.
+ query: String,
+ /// Shared with each frame's painter rather than copied into it.
+ rows: Arc<Vec<RemoteFileRow>>,
+ /// Set to stop the search running off the UI thread; `Some` while one is.
+ search_cancel: Option<Arc<AtomicBool>>,
+}
+
+impl RemoteFileSearchState {
+ pub(crate) fn release(&mut self) {
+ self.cancel_search();
+ let generation = self.generation.wrapping_add(1);
+ *self = Self {
+ generation,
+ ..Self::default()
+ };
+ }
+
+ fn cancel_search(&mut self) {
+ if let Some(cancel) = self.search_cancel.take() {
+ cancel.store(true, AtomicOrdering::Relaxed);
+ }
+ }
+
+ fn kind_for_path(&self, path: &RemotePath) -> Option<RemoteFileKind> {
+ self.rows
+ .iter()
+ .find(|row| &row.entry.path == path)
+ .map(|row| row.entry.kind)
}
- let name = entry.file_name().to_string_lossy();
- !should_skip_file_index_dir(&name)
}
-fn should_skip_file_index_dir(name: &str) -> bool {
- matches!(
- name,
- ".git"
- | ".hg"
- | ".svn"
- | "target"
- | "node_modules"
- | ".next"
- | ".nuxt"
- | ".turbo"
- | ".cache"
- | "dist"
- | "build"
- | "coverage"
- | "vendor"
- | ".venv"
- | "venv"
- | "__pycache__"
- )
+/// Index a remote listing the way a local project is indexed. Paths stay
+/// relative to the root; entry 0 stands for the root itself, as search skips it.
+fn remote_file_index_from_listing(
+ project_name: &str,
+ listing: &thinkterm_file_index::Listing,
+) -> RightSidebarFileIndex {
+ let mut entries = Vec::with_capacity(listing.entries.len() + 1);
+ entries.push(RightSidebarFileIndexEntry {
+ path: PathBuf::new(),
+ name: project_name.to_string(),
+ display_path: project_name.to_string(),
+ is_dir: true,
+ name_char_bag: RightSidebarFileCharBag::from_str(project_name),
+ char_bag: RightSidebarFileCharBag::from_str(project_name),
+ });
+ for entry in &listing.entries {
+ let name = entry
+ .path
+ .rsplit('/')
+ .next()
+ .unwrap_or(&entry.path)
+ .to_string();
+ entries.push(RightSidebarFileIndexEntry {
+ path: PathBuf::from(&entry.path),
+ name_char_bag: RightSidebarFileCharBag::from_str(&name),
+ name,
+ display_path: entry.path.clone(),
+ is_dir: entry.is_dir,
+ char_bag: RightSidebarFileCharBag::from_str(&entry.path),
+ });
+ }
+ RightSidebarFileIndex::new(entries)
}
-fn path_to_display_string(path: &Path) -> String {
- let mut display = String::new();
- for component in path.components() {
- if !display.is_empty() {
- display.push('/');
+/// `relative` under `root`, one component at a time, so a listing cannot name
+/// anything outside the root (`..`, `.` and empty components are refused).
+fn remote_path_under(root: &RemotePath, relative: &str) -> Option<RemotePath> {
+ let mut path = root.clone();
+ for component in relative.split('/') {
+ path = path.join_name(component).ok()?;
+ }
+ Some(path)
+}
+
+fn remote_file_search_rows(
+ index: &RightSidebarFileIndex,
+ root: &RemotePath,
+ query: &str,
+ cancel: &AtomicBool,
+) -> Vec<RemoteFileRow> {
+ search_right_sidebar_file_index(index, query, cancel)
+ .into_iter()
+ .filter_map(|row| {
+ let path = remote_path_under(root, &row.name)?;
+ Some(RemoteFileRow {
+ entry: RemoteFileEntry {
+ path,
+ name: row.name,
+ kind: if row.is_dir {
+ RemoteFileKind::Directory
+ } else {
+ RemoteFileKind::File
+ },
+ size: None,
+ },
+ depth: 0,
+ expanded: false,
+ })
+ })
+ .collect()
+}
+
+/// Where the pointer is for the right sidebar's hover machine: over the panel
+/// as presented, over the right-edge strip or the sidebar button that arm a
+/// reveal, in the wider band just inside the strip that keeps a running dwell
+/// alive, or away. The panel is checked first: once revealed, the strip is
+/// inside it.
+fn right_sidebar_hover_zone(
+ x: isize,
+ y: isize,
+ panel: Option<RightSidebarRect>,
+ hot_zone: Option<(usize, usize, usize, usize)>,
+ sticky_width: usize,
+ over_toggle: bool,
+) -> crate::termwindow::sidebar_hover::PointerZone {
+ use crate::termwindow::sidebar_hover::PointerZone;
+ let in_panel = panel.is_some_and(|rect| {
+ x >= rect.x as isize
+ && x < rect.x.saturating_add(rect.width) as isize
+ && y >= rect.y as isize
+ && y < rect.y.saturating_add(rect.height) as isize
+ });
+ if in_panel {
+ return PointerZone::Panel;
+ }
+ if over_toggle {
+ return PointerZone::HotZone;
+ }
+ let Some((zx, zy, zw, zh)) = hot_zone else {
+ return PointerZone::Away;
+ };
+ let right = zx.saturating_add(zw) as isize;
+ if y < zy as isize || y >= zy.saturating_add(zh) as isize || x >= right {
+ return PointerZone::Away;
+ }
+ if x >= zx as isize {
+ PointerZone::HotZone
+ } else if x >= right - sticky_width as isize {
+ PointerZone::NearHotZone
+ } else {
+ PointerZone::Away
+ }
+}
+
+/// See `TermWindow::markdown_preview_document`. `project_root` bounds where a
+/// local file's images may come from; a file outside it is bounded by its own
+/// folder.
+fn markdown_preview_document(
+ path: &str,
+ text: String,
+ remote_relative: Option<String>,
+ project_root: Option<&Path>,
+) -> crate::markdown_editor::VaultDocument {
+ // A session stats its path; a remote path means nothing on this machine,
+ // so a remote preview's session has none.
+ let session_path = if remote_relative.is_some() {
+ PathBuf::new()
+ } else {
+ PathBuf::from(path)
+ };
+ let session = Arc::new(parking_lot::Mutex::new(
+ crate::markdown_editor::MarkdownDocumentSession::new(
+ "file-preview".to_string(),
+ session_path,
+ text,
+ ),
+ ));
+ if let Some(relative_path) = remote_relative {
+ // No local root: nothing is read from this machine for it. The path
+ // relative to the remote project lets its wiki links resolve among
+ // the remote files.
+ return crate::markdown_editor::VaultDocument {
+ vault_root: PathBuf::new(),
+ relative_path,
+ document_path: PathBuf::new(),
+ session,
+ };
+ }
+ let document_path = PathBuf::from(path);
+ let root = project_root
+ .filter(|root| document_path.starts_with(root))
+ .map(Path::to_path_buf)
+ .or_else(|| document_path.parent().map(Path::to_path_buf))
+ .unwrap_or_default();
+ let relative_path = document_path
+ .strip_prefix(&root)
+ .map(path_to_display_string)
+ .unwrap_or_default();
+ crate::markdown_editor::VaultDocument {
+ vault_root: root,
+ relative_path,
+ document_path,
+ session,
+ }
+}
+
+/// The files a preview's wiki links resolve among; see
+/// `TermWindow::preview_link_files`.
+pub(crate) enum PreviewLinkFiles {
+ /// Relative to the root, `/`-separated.
+ Listed(Arc<Vec<String>>),
+ WalkProject {
+ respect_gitignore: bool,
+ },
+ /// Only the files directly in the root folder.
+ Folder,
+}
+
+/// A previewed file is not in a Notebook: its wiki links resolve among the
+/// project's files, never by walking everything under the root. A remote file
+/// has no local root, so nothing of this machine's is read for it.
+fn resolve_preview_wiki_links(
+ projection: &mut crate::markdown_editor::MarkdownProjection,
+ root: &Path,
+ relative_path: &str,
+ files: PreviewLinkFiles,
+) {
+ let paths = match files {
+ PreviewLinkFiles::Listed(paths) => {
+ Arc::try_unwrap(paths).unwrap_or_else(|paths| paths.as_ref().clone())
}
- display.push_str(&component.as_os_str().to_string_lossy());
+ PreviewLinkFiles::WalkProject { respect_gitignore } => {
+ let Ok(listing) = thinkterm_file_index::list_project(
+ root,
+ respect_gitignore,
+ thinkterm_file_index::ENTRY_LIMIT,
+ Instant::now() + Duration::from_secs(5),
+ ) else {
+ return;
+ };
+ listing
+ .entries
+ .into_iter()
+ .filter(|entry| !entry.is_dir)
+ .map(|entry| entry.path)
+ .collect()
+ }
+ PreviewLinkFiles::Folder => match fs::read_dir(root) {
+ Ok(entries) => entries
+ .flatten()
+ .filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_file()))
+ .filter_map(|entry| entry.file_name().into_string().ok())
+ .take(thinkterm_file_index::ENTRY_LIMIT)
+ .collect(),
+ Err(_) => return,
+ },
+ };
+ if root.as_os_str().is_empty() {
+ projection.resolve_links_among(None, paths, relative_path);
+ return;
}
- display
+ let Ok(root) = root.canonicalize() else {
+ return;
+ };
+ projection.resolve_links_among(Some(&root), paths, relative_path);
+}
+
+/// `path` relative to `root` with `/` separators, when it lies under it.
+fn remote_relative_path(root: &RemotePath, path: &RemotePath) -> Option<String> {
+ let root = root.as_str().trim_end_matches('/');
+ let rest = path.as_str().strip_prefix(root)?.strip_prefix('/')?;
+ (!rest.is_empty()).then(|| rest.to_string())
+}
+
+fn is_markdown_extension(extension: &str) -> bool {
+ extension.eq_ignore_ascii_case("md") || extension.eq_ignore_ascii_case("markdown")
+}
+
+fn should_skip_file_index_dir(name: &str) -> bool {
+ thinkterm_file_index::should_skip_dir(name)
+}
+
+fn path_to_display_string(path: &Path) -> String {
+ thinkterm_file_index::display_path(path)
}
fn file_index_entry_cmp(
@@ -20673,6 +22031,296 @@ mod tests {
assert_eq!(naturalish_cmp("tab01", "tab1"), std::cmp::Ordering::Greater);
}
+ use super::{
+ remote_file_index_from_listing, remote_file_search_rows, remote_path_under,
+ right_sidebar_hover_zone, RemoteFileSearchState, RemoteFileSearchStatus,
+ };
+
+ #[test]
+ fn markdown_previews_render_read_only_from_their_own_session() {
+ use crate::markdown_editor::{EditorMode, NoteHostState, ProjectedObject};
+ let base = tempfile::tempdir().unwrap();
+ let project_root = base.path().join("project");
+ let docs = project_root.join("docs");
+ std::fs::create_dir_all(&docs).unwrap();
+ std::fs::write(docs.join("shot.png"), b"not really a png").unwrap();
+ let outside_root = base.path().join("other");
+ std::fs::create_dir_all(&outside_root).unwrap();
+ std::fs::write(outside_root.join("secret.png"), b"x").unwrap();
+ let file = docs.join("README.md");
+ let text = "# Title\n\nSome *text*.\n\n```rust\nfn main() {}\n```\n".to_string();
+
+ let document = super::markdown_preview_document(
+ file.to_str().unwrap(),
+ text.clone(),
+ None,
+ Some(&project_root),
+ );
+ assert_eq!(document.vault_root, project_root);
+ assert_eq!(document.relative_path, "docs/README.md");
+ // Images resolve beside the file and cannot leave the project.
+ assert!(crate::markdown_editor::resolve_local_image(
+ &document.vault_root,
+ &document.document_path,
+ "shot.png"
+ )
+ .is_ok());
+ // A real file one folder up from the project: reachable, but outside.
+ assert!(docs.join("../../other/secret.png").exists());
+ assert!(crate::markdown_editor::resolve_local_image(
+ &document.vault_root,
+ &document.document_path,
+ "../../other/secret.png"
+ )
+ .is_err());
+
+ let mut host = NoteHostState::default();
+ host.bind_document(document);
+ host.view.mode = EditorMode::ReadOnly;
+ host.refresh_projection();
+ assert!(host
+ .projection
+ .objects
+ .iter()
+ .any(|object| matches!(object, ProjectedObject::CodeBlock(_))));
+ // Read-only: typing changes nothing.
+ let session = host.session.clone().unwrap();
+ let changed = session.lock().insert_text(&mut host.view, "x");
+ assert!(!changed);
+ assert_eq!(session.lock().source(), text);
+
+ // Outside the project, the file's own folder bounds it.
+ let loose = outside_root.join("notes.md");
+ let document = super::markdown_preview_document(
+ loose.to_str().unwrap(),
+ String::new(),
+ None,
+ Some(&project_root),
+ );
+ assert_eq!(document.vault_root, outside_root);
+ // A remote file has no local folder to take images from.
+ let remote = super::markdown_preview_document(
+ "/srv/app/docs/README.md",
+ text,
+ Some("docs/README.md".to_string()),
+ None,
+ );
+ assert_eq!(remote.relative_path, "docs/README.md");
+ assert!(remote.document_path.as_os_str().is_empty());
+ assert!(crate::markdown_editor::resolve_local_image(
+ &remote.vault_root,
+ &remote.document_path,
+ "shot.png"
+ )
+ .is_err());
+ assert!(super::is_markdown_extension("MD"));
+ assert!(super::is_markdown_extension("markdown"));
+ assert!(!super::is_markdown_extension("mdx"));
+ }
+
+ #[test]
+ fn preview_wiki_links_resolve_among_the_project_files() {
+ use crate::markdown_editor::{MarkdownProjection, ProjectedObject};
+ let project = tempfile::tempdir().unwrap();
+ let root = project.path();
+ std::fs::create_dir_all(root.join("docs")).unwrap();
+ std::fs::create_dir_all(root.join("node_modules/pkg")).unwrap();
+ std::fs::write(root.join("docs/guide.md"), "# Guide").unwrap();
+ std::fs::write(root.join("node_modules/pkg/setup.md"), "").unwrap();
+ let walk = || super::PreviewLinkFiles::WalkProject {
+ respect_gitignore: true,
+ };
+ let resolved_in = |source: &str, root: &Path, current: &str, files| {
+ let mut projection = MarkdownProjection::parse(source);
+ super::resolve_preview_wiki_links(&mut projection, root, current, files);
+ projection
+ .objects
+ .iter()
+ .find_map(|object| match object {
+ ProjectedObject::WikiLink { resolved_path, .. } => Some(resolved_path.clone()),
+ _ => None,
+ })
+ .expect("a wiki link")
+ };
+ let resolved = |source: &str| resolved_in(source, root, "docs/README.md", walk());
+ assert_eq!(
+ resolved("See [[guide]]."),
+ Some("docs/guide.md".to_string())
+ );
+ // Dependency trees are not part of the project for links either.
+ assert_eq!(resolved("See [[setup]]."), None);
+ // The search index's paths serve as well as a walk.
+ let listed = super::PreviewLinkFiles::Listed(Arc::new(vec!["docs/guide.md".to_string()]));
+ assert_eq!(
+ resolved_in("See [[guide]].", root, "docs/README.md", listed),
+ Some("docs/guide.md".to_string())
+ );
+ // A file outside the project sees only the files beside it.
+ assert_eq!(
+ resolved_in(
+ "See [[guide]].",
+ root,
+ "README.md",
+ super::PreviewLinkFiles::Folder
+ ),
+ None
+ );
+ std::fs::write(root.join("notes.md"), "").unwrap();
+ assert_eq!(
+ resolved_in(
+ "See [[notes]].",
+ root,
+ "README.md",
+ super::PreviewLinkFiles::Folder
+ ),
+ Some("notes.md".to_string())
+ );
+ // Without a local root (a remote file) nothing local is walked.
+ let mut projection = MarkdownProjection::parse("See [[guide]].");
+ super::resolve_preview_wiki_links(&mut projection, Path::new(""), "README.md", walk());
+ assert!(projection.objects.iter().all(|object| !matches!(
+ object,
+ ProjectedObject::WikiLink {
+ resolved_path: Some(_),
+ ..
+ }
+ )));
+ }
+
+ #[test]
+ fn remote_preview_links_resolve_among_listed_files_without_reading_any() {
+ use crate::markdown_editor::{MarkdownProjection, ProjectedObject};
+ let root = RemotePath::from_server_absolute("/srv/app").unwrap();
+ let file = RemotePath::from_server_absolute("/srv/app/docs/README.md").unwrap();
+ assert_eq!(
+ super::remote_relative_path(&root, &file).as_deref(),
+ Some("docs/README.md")
+ );
+ let slash = RemotePath::from_server_absolute("/").unwrap();
+ assert_eq!(
+ super::remote_relative_path(&slash, &file).as_deref(),
+ Some("srv/app/docs/README.md")
+ );
+ let elsewhere = RemotePath::from_server_absolute("/srv/other/a.md").unwrap();
+ assert_eq!(super::remote_relative_path(&root, &elsewhere), None);
+ let sibling = RemotePath::from_server_absolute("/srv/application/a.md").unwrap();
+ assert_eq!(super::remote_relative_path(&root, &sibling), None);
+
+ let mut projection = MarkdownProjection::parse("See [[guide]].\n\n![[guide]]\n");
+ projection.resolve_links_among(None, vec!["docs/guide.md".to_string()], "docs/README.md");
+ let links: Vec<_> = projection
+ .objects
+ .iter()
+ .filter_map(|object| match object {
+ ProjectedObject::WikiLink {
+ resolved_path,
+ rendered_lines,
+ ..
+ } => Some((resolved_path.clone(), rendered_lines.is_empty())),
+ _ => None,
+ })
+ .collect();
+ assert_eq!(links.len(), 2);
+ // Resolved, and the embed renders nothing: no local root to read from.
+ assert!(links
+ .iter()
+ .all(|(path, empty)| path.as_deref() == Some("docs/guide.md") && *empty));
+ }
+
+ #[test]
+ fn right_sidebar_hover_zones_mirror_the_left_edge() {
+ use crate::termwindow::sidebar_hover::PointerZone;
+ // A 1000px-wide window: the strip is the last 12px below a 40px tab
+ // bar, the sticky band the 28px ending at the window edge.
+ let hot = crate::termwindow::sidebar_hover::hot_zone(988, 0, 800, 12, 40);
+ assert_eq!(hot, Some((988, 40, 12, 760)));
+ let zone = |x, y, panel, toggle| right_sidebar_hover_zone(x, y, panel, hot, 28, toggle);
+ assert_eq!(zone(995, 400, None, false), PointerZone::HotZone);
+ assert_eq!(zone(988, 400, None, false), PointerZone::HotZone);
+ assert_eq!(zone(980, 400, None, false), PointerZone::NearHotZone);
+ assert_eq!(zone(972, 400, None, false), PointerZone::NearHotZone);
+ assert_eq!(zone(971, 400, None, false), PointerZone::Away);
+ assert_eq!(zone(500, 400, None, false), PointerZone::Away);
+ // Not in the tab bar row, where the pointer heads for the tabs...
+ assert_eq!(zone(995, 20, None, false), PointerZone::Away);
+ // ...unless it is on the sidebar button, which arms on its own.
+ assert_eq!(zone(960, 20, None, true), PointerZone::HotZone);
+ // Once out, the panel is the panel, strip included.
+ let panel = Some(super::RightSidebarRect {
+ x: 700,
+ y: 0,
+ width: 300,
+ height: 800,
+ });
+ assert_eq!(zone(995, 400, panel, false), PointerZone::Panel);
+ assert_eq!(zone(700, 400, panel, false), PointerZone::Panel);
+ assert_eq!(zone(699, 400, panel, false), PointerZone::Away);
+ }
+
+ #[test]
+ fn remote_search_finds_listed_files_under_the_root() {
+ let listing = thinkterm_file_index::Listing {
+ entries: vec![
+ thinkterm_file_index::ListedEntry {
+ path: "src".to_string(),
+ is_dir: true,
+ },
+ thinkterm_file_index::ListedEntry {
+ path: "src/main.rs".to_string(),
+ is_dir: false,
+ },
+ thinkterm_file_index::ListedEntry {
+ path: "docs/main-notes.md".to_string(),
+ is_dir: false,
+ },
+ ],
+ truncated: false,
+ };
+ let index = remote_file_index_from_listing("app", &listing);
+ let root = RemotePath::from_server_absolute("/srv/app").unwrap();
+ let rows = remote_file_search_rows(&index, &root, "main", &AtomicBool::new(false));
+ let found: Vec<_> = rows
+ .iter()
+ .map(|row| (row.entry.path.as_str().to_string(), row.entry.kind))
+ .collect();
+ assert!(found.contains(&("/srv/app/src/main.rs".to_string(), RemoteFileKind::File)));
+ assert!(found.contains(&(
+ "/srv/app/docs/main-notes.md".to_string(),
+ RemoteFileKind::File
+ )));
+ assert_eq!(found.len(), 2);
+ let dirs = remote_file_search_rows(&index, &root, "src", &AtomicBool::new(false));
+ assert_eq!(dirs[0].entry.kind, RemoteFileKind::Directory);
+ // The same matching as a local project: a path query searches paths.
+ assert_eq!(
+ remote_file_search_rows(&index, &root, "src/ma", &AtomicBool::new(false)).len(),
+ 1
+ );
+ assert!(remote_file_search_rows(&index, &root, "", &AtomicBool::new(false)).is_empty());
+ }
+
+ #[test]
+ fn remote_search_results_cannot_leave_the_root() {
+ let root = RemotePath::from_server_absolute("/srv/app").unwrap();
+ assert!(remote_path_under(&root, "../etc/passwd").is_none());
+ assert!(remote_path_under(&root, "a/./b").is_none());
+ assert!(remote_path_under(&root, "a//b").is_none());
+ assert_eq!(
+ remote_path_under(&root, "a/b").unwrap().as_str(),
+ "/srv/app/a/b"
+ );
+ }
+
+ #[test]
+ fn remote_search_state_drops_stale_listings() {
+ let mut state = RemoteFileSearchState::default();
+ let before = state.generation;
+ state.release();
+ assert_ne!(state.generation, before);
+ assert!(state.index.is_none());
+ assert_eq!(state.status, RemoteFileSearchStatus::Idle);
+ }
+
#[test]
fn file_index_search_matches_paths() {
let dir = tempfile::tempdir().unwrap();