Skip to content

refactor: restore CodeScene code health in the worst files - #2022

Merged
wmfeht merged 160 commits into
mainfrom
refactor/1686-code-health
Nov 2, 2026
Merged

wmfeht merged 160 commits into
mainfrom
refactor/1686-code-health

Conversation

@l0gicgate

Copy link
Copy Markdown
Contributor

Description

CodeScene scored the tree at a LoC-weighted average of 6.67 (production 6.56, tests 6.79), with the five hotspot files between 1.4 and 4.1. This pull request restores code health in the worst 75 files and their test modules without changing behaviour, so the work lands as one change rather than a series of partial clean-ups.

What changed, file by file, follows the same shapes everywhere:

  • Large builder closures and 300-line methods are split into staged, named functions; signal-handler bodies become named functions or methods; long match dispatchers delegate to per-domain handlers.
  • Functions with five or more parameters take a context or request struct instead (for example DeviceActionContext, PastePlan, DirectoryLoad, ReleaseRequest, PreviewLoad, RefreshTarget and Walk in services::search).
  • Complex conditionals become named predicates; bumpy-road functions use early returns and let … else.
  • Duplicated list/icons and copy/move/restore pairs collapse behind one generic function or a small enum-driven helper.
  • Files well over the brain-class size (src/ui/window.rs 4,919 lines, src/app/browser.rs 5,424 lines, src/adapters/local_operations.rs 4,276 lines, …) are split into submodules by responsibility. Moved items are re-exported from their original path, so callers did not change.
  • Mega test files are split into focused modules with shared fixtures, builders, assertion helpers and table-driven cases. Every assertion keeps its meaning; no scenario was dropped or merged. #[test] count: 3,756 on main, 3,757 here (one case split in two); ignored tests unchanged at 11.
  • Two gtk_test child runs pointed at a stale test path and so silently ran zero tests; they now run (one of them was already stale on main).

Measured with the CodeScene CLI on every file in the tree (scripts/code-health.sh):

scope before after
all files, LoC-weighted average 6.67 9.07
production files 6.56 9.09
test files 6.79 9.05
files scoring below 8 214 119 (none touched here)

The worst files before and after:

file before after
src/ui/browser_modes.rs 1.42 10.0
src/ui/chooser.rs 1.52 10.0
src/adapters/local_operations.rs 1.66 9.68
src/adapters/local_operations/tests/merge.rs 2.13 9.38
src/ui/window.rs 2.21 declaration-only hub (160 lines)
src/ui/preview.rs 2.21 9.52
src/ui/transfer tests, window keyboard-dispatch tests, window preference tests, chooser acceptance tests 2.9–3.5 10.0
src/ui/settings.rs 2.97 9.17
src/services/document.rs 2.97 9.38
src/adapters/local_files.rs 3.28 8.28
src/app/browser.rs 4.06 9.57
src/app/navigation.rs 4.46 10.0

Every file touched by this pull request, and every new module, scores 8.0 or higher. The diff is large (715 files) because the splits move code; reviewing by package is easiest: each merge commit on the branch is one file-disjoint package (window, browser modes, local operations, app, preview, chooser, settings, browser view, browser features, services, and four test groups).

Also added: scripts/code-health.sh (reviews files or --changed files with the CodeScene CLI and can gate on --min N) and a short "Code health" section in AGENTS.md so future changes do not regress the score.

Follow-ups, out of scope here: the 119 untouched files still below 8 (largest debt: src/ui/browser/location/tests.rs, src/ui/window/composition/tabs/tests.rs, src/adapters/local_operations/copy_engine/manifest.rs, src/ui/theme/tests.rs, src/ui/settings/keyboard.rs), and a CI delta gate using the script, which needs a CS_ACCESS_TOKEN repository secret.

Visual evidence

N/A. The change is a behaviour-preserving refactor; no user-visible text, layout, ordering or timing changed.

How to test

  1. ./scripts/quality.sh fmt, ./scripts/quality.sh clippy and ./scripts/quality.sh test in the pinned container.
  2. ./scripts/qa.sh --test-threads 6 to run every QA scenario.
  3. With CS_ACCESS_TOKEN set, ./scripts/code-health.sh --min 8 --changed to re-score every changed file.
  4. Launch the app and exercise a few of the refactored areas by hand: open a folder in each view mode, copy and move files with a conflict, undo the move, eject a device from the sidebar, open Settings and change a theme, use the file chooser portal, and preview a PDF, an image and a 3D model.

Expected result: every suite passes with the same test count as main (3,770 passing in the container run, 656 ignored); the code-health script lists every changed file at 8.0 or above and exits 0; the app behaves exactly as before.

Related issue

Closes #1686

…flict and decoder tests into focused modules
@github-actions github-actions Bot added the refactor Code restructuring without behavior change label Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Code restructuring without behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restore code health: hotspots 3.13 → 7+, average 6.90 → 7.5+

2 participants