Skip to content

feat(desktop): reveal the active file in the sidebar file tree (#528) - #5654

Merged
Jocs merged 1 commit into
developfrom
feat/sidebar-auto-reveal-528
Oct 8, 2026
Merged

Jocs merged 1 commit into
developfrom
feat/sidebar-auto-reveal-528

Conversation

@Jocs

@Jocs Jocs commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Closes #528

Summary

Opening a document that lives in a deep folder, collapsing that folder and then switching back to the tab left no way to find the file: the title bar shows the path but is not navigable. Atom, IntelliJ and VS Code all reveal the active file in their project tree, so the side bar now does the same, following VS Code's explorer.autoReveal:

  • expand only the folders leading to the file; never collapse anything;
  • scroll only when the row is not already fully visible, then centre it, using the same formula as ListWidget.reveal(index, 0.5);
  • leave the caret, the tree selection and activeItem alone, so F2 and Delete keep acting on what the user picked;
  • do nothing while the file list is off screen, and reveal again as soon as it is visible.

A single watcher on currentFile drives it, so every existing way of switching files — tab click, cycle, quick open, side bar click, startup restore — gets the behaviour without touching those call sites. It builds on #5632 (merged): keeping the folder collapse state on the tree node is what lets the store expand ancestors before the render instead of racing freshly mounted folder components.

The reveal also has manual entry points: Show in Side Bar in a tab's context menu and a file.reveal-in-sidebar command. Both ignore the preference, bring the file list into view and expand the collapsed Directories section. A new autoRevealInSidebar preference (Boolean, default true) turns the automatic behaviour off, wired through static/preference.json, the main-process schema, the renderer store, the General → Sidebar settings pane, all 12 locales and the preferences doc.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added:

    • test/unit/specs/reveal-tree-row.spec.ts — centring above/below the viewport, clamping at both ends, relativeTop 0/1, zero-height viewport, rows taller than the viewport, already-visible rows.
    • test/unit/specs/sidebar-reveal-path.spec.ts — expands only the ancestor chain, keeps sibling folders collapsed, is idempotent, returns null for project-external/unknown paths, and covers the store's REVEAL_PATH.
    • test/e2e/sidebar-auto-reveal-528.spec.ts — real build against a generated project with 25 files in one folder so the list overflows: switching to a tab re-expands its folders, marks the row .current and scrolls it into view; a row that is already visible leaves scrollTop at 0; with autoRevealInSidebar: false switching tabs does not touch the tree; and Show in Side Bar re-opens a collapsed side bar and reveals the file anyway.
    • test/e2e/helpers.ts gains openProjectFolder, which both side bar e2e specs use to open a fixture folder in the running window.
  • Manually tested on: macOS

    Also run locally: pnpm run typecheck, pnpm run lint (0 errors), the unit suite, and the full desktop Playwright suite — 346 passed / 3 failed / 5 skipped, the 3 failures (find-replace regex, i18n-shell, parity-cursor-lang) reproducing identically on develop.

Notes for reviewers

  • Revealing deliberately does not set activeItem, so it cannot retarget F2/Delete; the file tree already marks the active row with its .current style.
  • Because collapse state now lives on the tree node (fix(desktop): persist sidebar folder collapse state across view switches (#5631) #5632), an expansion also survives switching to Search/TOC and back.
  • pnpm run minify-locales must run before production builds, as usual for locale changes.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Website preview ready: https://pr-5654-marktext-website.ransixi.workers.dev

Built from 4ef39d4931eac8fc9c3601ad9b58e1a0276b7d9f · Worker: marktext-website · Version: ada332c9-5393-42ee-859b-d339f48c2b32 · Alias: pr-5654

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Build artifacts for PR #5654:

Run: https://github.com/marktext/marktext/actions/runs/37749895486

Artifact Size Link
marktext-macos-x64 296.0 MB Download
marktext-windows-arm64 278.2 MB Download
marktext-macos-arm64 285.7 MB Download
marktext-linux 635.1 MB Download
marktext-windows-x64 286.4 MB Download

@Jocs
Jocs force-pushed the feat/sidebar-auto-reveal-528 branch 3 times, most recently from 1637099 to 365b97f Compare October 8, 2026 07:44
@Jocs
Jocs requested a balanced review from Copilot October 8, 2026 07:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unknown targets mutate collapse state, and reveals are not retried when the active row arrives asynchronously.

2 open findings
What changed in this PR

Adds automatic and manual sidebar reveal for the active file, including preferences, localization, documentation, and tests.

Changes:

  • Expands ancestor folders and scrolls the active file into view.
  • Adds “Show in Side Bar,” a command, and an autoRevealInSidebar preference.
  • Adds unit and end-to-end coverage.
File Description
packages/​website/​content/​docs/​end-user/​PREFERENCES.md Documents the preference.
packages/​desktop/​test/​unit/​specs/​sidebar-reveal-path.spec.ts Tests ancestor expansion.
packages/​desktop/​test/​unit/​specs/​reveal-tree-row.spec.ts Tests scroll calculations.
packages/​desktop/​test/​e2e/​sidebar-auto-reveal-528.spec.ts Tests reveal workflows.
packages/​desktop/​test/​e2e/​helpers.ts Adds project-opening helper.
packages/​desktop/​static/​preference.json Adds the default preference.
packages/​desktop/​static/​locales/​zh-TW.json Adds Traditional Chinese strings.
packages/​desktop/​static/​locales/​zh-CN.json Adds Simplified Chinese strings.
packages/​desktop/​static/​locales/​tr.json Adds Turkish strings.
packages/​desktop/​static/​locales/​ru.json Adds Russian strings.
packages/​desktop/​static/​locales/​pt.json Adds Portuguese strings.
packages/​desktop/​static/​locales/​nl.json Adds Dutch strings.
packages/​desktop/​static/​locales/​ko.json Adds Korean strings.
packages/​desktop/​static/​locales/​ja.json Adds Japanese strings.
packages/​desktop/​static/​locales/​fr.json Adds French strings.
packages/​desktop/​static/​locales/​es.json Adds Spanish strings.
packages/​desktop/​static/​locales/​en.json Adds English strings.
packages/​desktop/​static/​locales/​de.json Adds German strings.
packages/​desktop/​src/​shared/​types/​preferences.ts Extends preference types.
packages/​desktop/​src/​renderer/​src/​util/​revealTreeRow.ts Calculates reveal scrolling.
packages/​desktop/​src/​renderer/​src/​store/​treeCtrl.ts Expands ancestor folders.
packages/​desktop/​src/​renderer/​src/​store/​project.ts Exposes reveal actions.
packages/​desktop/​src/​renderer/​src/​store/​preferences.ts Adds renderer preference state.
packages/​desktop/​src/​renderer/​src/​prefComponents/​general/​index.vue Adds the settings toggle.
packages/​desktop/​src/​renderer/​src/​contextMenu/​tabs/​menuItems.ts Defines the context-menu item.
packages/​desktop/​src/​renderer/​src/​contextMenu/​tabs/​index.ts Inserts the menu item.
packages/​desktop/​src/​renderer/​src/​contextMenu/​tabs/​actions.ts Dispatches manual reveals.
packages/​desktop/​src/​renderer/​src/​components/​sideBar/​treeFile.vue Registers rendered rows.
packages/​desktop/​src/​renderer/​src/​components/​sideBar/​tree.vue Coordinates expansion and scrolling.
packages/​desktop/​src/​renderer/​src/​components/​sideBar/​rowRegistry.ts Defines the row registry.
packages/​desktop/​src/​renderer/​src/​commands/​index.ts Adds the reveal command.
packages/​desktop/​src/​renderer/​src/​commands/​descriptions.ts Maps its localized description.
packages/​desktop/​src/​main/​preferences/​schema.json Validates the preference.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Comment on lines +418 to +423
watch(
() => editorStore.currentFile?.pathname,
(pathname) => {
revealPath(pathname)
},
{ flush: 'post' }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced and fixed. The failing case is common, not exotic: MarkText restores the last session (folder + tabs) and the side bar tree is filled by the watcher after currentFile is set, so the one-shot reveal ran against an empty tree and never retried. Same for "open the folder I am working in" while a file is already active.

Fix: the project store now exposes a treeVersion counter bumped for every structural tree event (add / addDir / unlink / unlinkDir — in-place mutations are invisible to watchers), and the reveal watcher depends on it together with currentFile.pathname, so each delivery is a new chance to reveal. Added an e2e test for exactly this ordering (file active → folder opened): it failed before the fix and passes now.

Comment on lines +172 to +187
const chain: TreeFolder[] = []
let currentFolder: TreeFolder = tree
let currentSubFolders: TreeFolder[] = tree.folders
for (const directoryName of subDirectories) {
const childFolder = currentSubFolders.find((f) => f.name === directoryName)
if (!childFolder) return null
childFolder.isCollapsed = false
chain.push(childFolder)
currentFolder = childFolder
currentSubFolders = childFolder.folders
}

const isTracked = currentFolder.files.some((f) =>
window.fileUtils.isSamePathSync(f.pathname, pathname)
)
return isTracked ? chain : null

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, fixed. expandAncestors now walks the chain without touching it, verifies the leaf is in the deepest folder, and only then clears isCollapsed, so a failed reveal no longer changes the collapse state. Covered by a unit test asserting the folders stay collapsed for an untracked path.

Open a document in a deep folder, collapse that folder, switch to another file
and back: there was no way to find it again. The title bar shows the path but is
not navigable, while Atom, IntelliJ and VS Code all reveal the active file in
their project tree.

Reveal it the way VS Code's Explorer does for `explorer.autoReveal`:

- expand only the folders leading to the file, never collapsing anything, and
  only once the file is known to be in the tree;
- scroll only when the row is not already fully visible, then centre it, using
  the same formula as `ListWidget.reveal(index, 0.5)`;
- leave the caret, the tree selection and `activeItem` alone, so F2 and Delete
  keep acting on what the user picked;
- do nothing while the file list is off screen, and retry as the tree fills in,
  so a project opened or restored after that file is revealed as well.

A single watcher on `currentFile` plus the tree's change counter drives it, so
every existing way of switching files — tab click, cycle, quick open, sidebar
click, startup restore — gets the behaviour without touching those call sites.
Moving the folder collapse state onto the tree node (#5632) is what lets the
store expand ancestors before the render instead of racing freshly mounted
folder components.

Also adds `autoRevealInSidebar` (on by default), a "Show in Side Bar" tab
context-menu entry and a `file.reveal-in-sidebar` command for the manual case,
with unit tests for the expansion and scroll maths and an e2e spec that drives
the real app against a generated project.
@Jocs
Jocs force-pushed the feat/sidebar-auto-reveal-528 branch from 365b97f to 4ef39d4 Compare October 8, 2026 08:26
@Jocs
Jocs merged commit 48a770a into develop Oct 8, 2026
16 checks passed
@Jocs
Jocs deleted the feat/sidebar-auto-reveal-528 branch October 23, 2026 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto reveal the file position in tree view, or add a button to click to scroll to the file position in tree view.

2 participants