refactor(async): decompose the process_async_messages god function#2425
Merged
Conversation
`Editor::process_async_messages` had grown into a 1000+ line god function: a single `match` over ~50 `AsyncMessage` variants where most arms already delegated to a `handle_*` method, but a dozen arms inlined 15–140 lines of logic each (TerminalExited ~140, RemoteAttachReady ~126, GrammarRegistryBuilt ~70, the plugin-message sub-match ~66, TerminalOutput ~58, LspError ~62, …). The fat arms drowned the dispatch structure and made the function impossible to read at a glance. Extract each fat arm into a dedicated private `handle_*` method on `Editor`, following the convention the file already used for its thin arms. The `match` is now a uniform dispatch table: every arm is a pattern plus a single delegating call. The two early-out `continue`s in the remote-attach arms become `return` in their handlers (same control flow: skip the rest of this message, move on to the next). No behavior change — logic is moved verbatim, not rewritten. No new traits, generics, or macros. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AsDPyw77hJM7ECYBXF1xD5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Editor::process_async_messages(incrates/fresh-editor/src/app/async_dispatch.rs) was the lowest-hanging maintainability fruit in the project: a single 1033-line function whose body is onematchover ~50AsyncMessagevariants.The offense was inconsistency under load. Most arms already delegated to a
handle_*method (one line each), but ~12 arms inlined substantial logic directly in the dispatcher:TerminalExitedRemoteAttachReadyGrammarRegistryBuiltPlugin(..)(nested sub-match)LspErrorTerminalOutputRemoteAttachFailed,LspInitialized,QuickOpenFilesLoaded,LspApplyEdit,LspCodeActionResolved,PathChangedThese fat arms buried the dispatch structure — you couldn't see the routing for the logic.
Change
Extract each fat arm into a dedicated private
handle_*method onEditor, following the convention the file already established for its thin arms. Thematchis now a uniform dispatch table: every arm is a pattern plus a single delegating call.process_async_messagesdrops from 1033 → 424 lines (the remainder being multi-line match patterns and the unchanged post-loop orchestration).continues in the remote-attach arms becamereturnin their handlers — identical control flow (skip the rest of this message, continue to the next).Conflict avoidance
Selected specifically to avoid the active PR surface. The two larger god functions (
app/render.rs::render,input/actions.rs::action_to_events) were rejected because they're under active development (#2388 touchesrender.rs; #2292 touchesactions.rs), as areinput.rs(#2292) andplugin_dispatch.rs(#2399).async_dispatch.rshas no pending changes in any open PR.Verification
cargo check -p fresh-editor --lib— clean (only pre-existing, unrelated warnings)cargo fmt— appliedcargo clippy -p fresh-editor --lib— zero warnings in the changed file🤖 Generated with Claude Code
https://claude.ai/code/session_01AsDPyw77hJM7ECYBXF1xD5
Generated by Claude Code