Skip to content

Fix project explorer tree collapsing on document save#53

Merged
mcgivrer merged 2 commits into
mainfrom
copilot/fix-explorer-tree-issue
Mar 19, 2026
Merged

Fix project explorer tree collapsing on document save#53
mcgivrer merged 2 commits into
mainfrom
copilot/fix-explorer-tree-issue

Conversation

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

When saving a document, ProjectExplorerPanel.refresh() rebuilt the TreeView from scratch — resetting all nodes to collapsed — causing users to lose their position in the file hierarchy.

Changes

TreeExpansionState (new)

  • Pure-Java generic utility decoupled from JavaFX, enabling unit testing without a JFX runtime
  • Node<T> interface abstracts tree nodes; isCollectable() controls which nodes participate in state capture (directories only, by default)
  • collectExpanded(Node<T>) / restoreExpanded(Node<T>, Set<T>) snapshot and replay expansion state

ProjectExplorerPanel

  • refresh() now snapshots expanded dirs + selected file before rebuilding the tree, then restores both after
  • asNode(TreeItem<File>) — private adapter bridging TreeItem<File> to TreeExpansionState.Node<File>; applies directory-only collection filter via isCollectable()

MarkNote

  • saveCurrentDocument() calls revealFile() explicitly after refresh() to handle "Save As" (new file not yet in saved snapshot)

Tests & build

  • TreeExpansionStateTest — 9 unit tests: null-safety, collect/restore semantics, and the full save→refresh round-trip
  • build script updated to compile test sources against pre-built main classes (avoids recompiling JFX-dependent main sources in the test step)
  • libs/junit-platform-console-standalone-6.0.1.jar added
// Before: all subdirs collapsed after every save
public void refresh() {
    TreeItem<File> rootItem = buildTreeItem(projectDir);
    rootItem.setExpanded(true);   // only root survives
    treeView.setRoot(rootItem);
}

// After: expanded state and selection survive the rebuild
public void refresh() {
    Set<File> expandedDirs = TreeExpansionState.collectExpanded(asNode(treeView.getRoot()));
    File selectedFile = /* current selection */;
    TreeItem<File> rootItem = buildTreeItem(projectDir);
    rootItem.setExpanded(true);
    treeView.setRoot(rootItem);
    TreeExpansionState.restoreExpanded(asNode(treeView.getRoot()), expandedDirs);
    if (selectedFile != null) revealFile(selectedFile);
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Issue when opening file, explorer tree is closed</issue_title>
<issue_description>Lorsque l'on édite un document et qu'on le sauvegarde, l'arbre de l'explorateur de projet est refermé.
Il faut le gardé ouvert sur l'item de l'arbre correspondant au document en cour d'édition.</issue_description>

<agent_instructions>- application is based on JDK 25 and you can insiatlize the JDK with sdlman (.sdkmanrc file provided

  • main dependency is JavaFX
  • Solve the issue
  • add unit test to check this fix. use only Junit-platform-console-standalone-6.0.1 only (nop othert library)
  • build only using the provided bash script 'build'
  • to run the app, use the bash script "build run"
  • to execute test, use the bash script "build test"</agent_instructions>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

- Add TreeExpansionState: pure-Java utility (Node<T> interface + collectExpanded/restoreExpanded static methods) for saving and restoring tree expansion state without JavaFX dependency
- Modify ProjectExplorerPanel.refresh() to save expanded directories and selected file before rebuilding, then restore them after
- Update MarkNote.saveCurrentDocument() to call revealFile() after refresh to ensure the saved file is revealed in the tree
- Add TreeExpansionStateTest with 9 unit tests covering the fix
- Update build script test section to compile test sources against pre-built main classes
- Add junit-platform-console-standalone-6.0.1.jar to libs/

Co-authored-by: mcgivrer <216852+mcgivrer@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix explorer tree closure on file save Fix project explorer tree collapsing on document save Mar 19, 2026
Copilot AI requested a review from mcgivrer March 19, 2026 22:33
@mcgivrer mcgivrer marked this pull request as ready for review March 19, 2026 22:58
@mcgivrer mcgivrer merged commit b1399a3 into main Mar 19, 2026
1 check passed
@mcgivrer mcgivrer deleted the copilot/fix-explorer-tree-issue branch March 22, 2026 16:38
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.

Issue when opening file, explorer tree is closed

2 participants