Skip to content

Refactor main_phase7.mm into 22 focused modules#57

Merged
hybridmachine merged 2 commits intomacos-portfrom
refactor/modularize-main-phase7
Mar 16, 2026
Merged

Refactor main_phase7.mm into 22 focused modules#57
hybridmachine merged 2 commits intomacos-portfrom
refactor/modularize-main-phase7

Conversation

@hybridmachine
Copy link
Copy Markdown
Owner

Summary

  • Split the monolithic 4,097-line main_phase7.mm into 22 focused modules with clear boundaries and layered dependencies
  • Centralized all mutable state in an AppContext struct accessed via ctx(), per review feedback to prefer context over globals
  • Removed 5 legacy phase files (main.mm, main_phase3.mm, main_phase4.mm, main_phase5.mm, main_phase7.mm) — 9,384 lines of superseded code
  • Net reduction of ~5,070 lines; no functional changes

Module Architecture

Layer Modules Purpose
1 npp_constants.h, document_data.h, string_utils.h Pure data headers
2 language_defs, lexer_styles Language/style data
3 app_state AppContext struct with ctx() accessor
4 14 feature modules document_manager, file_operations, find_replace, edit_commands, bookmarks, autocomplete, context_menu, preferences_dialog, status_bar, recent_files, session_manager, split_view, scintilla_config, appearance
5 menu_builder, wndproc, app_delegate, app_main Dispatch and entry

Review Feedback Addressed

  • AppContext over globals: All state in AppContext struct, accessed via ctx()
  • Centralized notifications: Scintilla notification handling in app_delegate.mm and split_view.mm
  • Tightened module boundaries: Explicit header APIs between session_manager ↔ split_view and app_delegate ↔ wndproc
  • Defensive cleanup: Legacy file removal conditional on successful build

Test plan

  • cmake --build . --target MacOSNotePP builds successfully
  • cmake --build . --target npp_build_test builds successfully
  • Launch app, verify syntax highlighting and code folding
  • Open/save files, switch tabs, verify encoding/EOL in status bar
  • Test Find/Replace (find next, count, replace all)
  • Test split view (split, move to other, unsplit)
  • Test session restore (quit and relaunch)
  • Test drag-and-drop files from Finder
  • Test dark/light mode switching

Closes #55

🤖 Generated with Claude Code

Split the monolithic 4,097-line main_phase7.mm into focused modules
to unblock parallel work and make code review practical. All mutable
state is centralized in an AppContext struct accessed via ctx().

New module structure:
- Layer 1: npp_constants.h, document_data.h, string_utils.h
- Layer 2: language_defs, lexer_styles
- Layer 3: app_state (AppContext)
- Layer 4: 14 feature modules (document_manager, file_operations,
  find_replace, edit_commands, bookmarks, autocomplete, context_menu,
  preferences_dialog, status_bar, recent_files, session_manager,
  split_view, scintilla_config, appearance)
- Layer 5: menu_builder, wndproc, app_delegate, app_main

Also removes 5 legacy phase files (main.mm, main_phase3-5.mm,
main_phase7.mm) totaling 9,384 lines of superseded code.

Pure refactor — no functional changes. Build verified.

Closes #55

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 16, 2026 03:47
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors the macOS Phase 7 app implementation by splitting the previous monolithic entry file into focused modules and centralizing mutable state in an AppContext accessed via ctx(), while updating the macOS CMake target wiring and removing legacy phase entrypoints.

Changes:

  • Introduces a modular architecture (window proc, menus, documents, find/replace, split view, session, appearance, etc.) wired through app_delegate + app_main.
  • Centralizes app-wide mutable state into AppContext (app_state.h/.mm) and updates modules to use it.
  • Updates macos/CMakeLists.txt to compile the new module set and removes legacy phase sources.

Reviewed changes

Copilot reviewed 49 out of 50 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
macos/platform/app_main.mm New slim entry point that boots NSApplication and the app delegate.
macos/platform/app_delegate.h Declares NppAppDelegate and drop target view.
macos/platform/app_delegate.mm App lifecycle wiring, main window construction, notifications, drag-and-drop, settings/session integration.
macos/platform/app_state.h Defines AppContext and ctx() accessor API.
macos/platform/app_state.mm Provides the single global AppContext instance backing ctx().
macos/platform/npp_constants.h Centralizes command IDs and Scintilla constants/messages used across modules.
macos/platform/wndproc.h Declares the main window procedure interface.
macos/platform/wndproc.mm Implements WM_* dispatch for commands, notifications, timers, sizing, close.
macos/platform/menu_builder.h Declares menu construction API.
macos/platform/menu_builder.mm Builds menu bar including File/Edit/Search/View/Format/Language menus.
macos/platform/document_data.h Defines per-tab DocumentData plus encoding/EOL helpers.
macos/platform/document_manager.h Declares document/tab management and state save/restore APIs.
macos/platform/document_manager.mm Implements tab switching, open/close tab flows, and per-view state persistence.
macos/platform/file_operations.h Declares file open/save, encoding/EOL detection, encode/decode helpers.
macos/platform/file_operations.mm Implements file I/O, encoding detection, save logic, and recent/status updates.
macos/platform/find_replace.h Declares find/replace dialog + search/replace operations.
macos/platform/find_replace.mm Implements find/replace UI, search flags, wrapped search, replace operations, go-to-line.
macos/platform/edit_commands.h Declares edit command handlers (case/line/comment/sort/join).
macos/platform/edit_commands.mm Implements edit operations invoked from menus/commands.
macos/platform/bookmarks.h Declares bookmark toggle/navigation.
macos/platform/bookmarks.mm Implements bookmark marker operations in Scintilla.
macos/platform/autocomplete.h Declares auto-complete trigger.
macos/platform/autocomplete.mm Implements word-based auto-completion list building and display.
macos/platform/scintilla_config.h Declares Scintilla configuration routine.
macos/platform/scintilla_config.mm Applies baseline Scintilla settings (margins, markers, folding, caret line).
macos/platform/lexer_styles.h Declares lexer style tables and language application helpers.
macos/platform/lexer_styles.mm Implements per-lexer style tables and language switching using Lexilla.
macos/platform/language_defs.h Declares language table and extension-based detection.
macos/platform/language_defs.mm Defines language metadata table and guessLanguage() implementation.
macos/platform/appearance.h Declares dark/light appearance application functions.
macos/platform/appearance.mm Implements theme switching + fold marker/bookmark styling.
macos/platform/split_view.h Declares split view operations (split/unsplit/move/clone/layout).
macos/platform/split_view.mm Implements split view creation, layout, and per-view document operations.
macos/platform/status_bar.h Declares status bar update function.
macos/platform/status_bar.mm Implements status bar content updates from active Scintilla/doc state.
macos/platform/recent_files.h Declares recent files list/menu operations.
macos/platform/recent_files.mm Implements recent file list management and menu rebuild/open logic.
macos/platform/session_manager.h Declares session save/restore API and session path helper.
macos/platform/session_manager.mm Implements session save/restore to JSON under the user home directory.
macos/platform/preferences_dialog.h Declares preferences dialog entry point.
macos/platform/preferences_dialog.mm Implements preferences UI and applies settings to Scintilla + persistence.
macos/platform/context_menu.h Declares context menu entry point.
macos/platform/context_menu.mm Implements context menu creation and popup wiring.
macos/platform/string_utils.h Adds wchar_t/NSString conversion helpers for cross-module use.
macos/platform/main.mm Removes legacy phase entrypoint source.
macos/platform/main_phase3.mm Removes legacy phase entrypoint source.
macos/CMakeLists.txt Updates macOS app target sources to include new modules and removes old main entry source.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread macos/platform/wndproc.h Outdated
Comment on lines +8 to +17
// Win32 types from the shim — these are the actual typedefs
typedef void* HWND;
typedef unsigned int UINT;
typedef uintptr_t WPARAM;
typedef intptr_t LPARAM;
typedef intptr_t LRESULT;

#ifndef CALLBACK
#define CALLBACK
#endif
Comment on lines +4 to +8
#include "recent_files.h"
#include "npp_constants.h"
#include "app_state.h"
#include "string_utils.h"
#include "windows.h"
Comment on lines +4 to +10
#include "autocomplete.h"
#include "npp_constants.h"
#include "app_state.h"
#include "scintilla_bridge.h"
#include <set>
#include <algorithm>

Comment on lines +6 to +12
#import <Foundation/Foundation.h>
#include <string>

inline NSString* WideToNSString(const wchar_t* wstr)
{
if (!wstr) return @"";
size_t len = wcslen(wstr);

intptr_t startLine = ScintillaBridge_sendMessage(sci, SCI_LINEFROMPOSITION, selStart, 0);
intptr_t endLine = ScintillaBridge_sendMessage(sci, SCI_LINEFROMPOSITION, selEnd, 0);

#include "language_defs.h"
#include "scintilla_bridge.h"
#include "ILexer.h"
#include "Lexilla.h"
Comment thread macos/platform/menu_builder.h Outdated
Comment on lines +6 to +8
// HMENU is void* in the Win32 shim
typedef void* HMENU;

Comment thread macos/platform/recent_files.mm Outdated
Comment on lines +10 to +12
// Forward declaration — defined in file_operations.mm
bool openFileAtPath(NSString* path);

Comment on lines +4 to +10
#include "edit_commands.h"
#include "npp_constants.h"
#include "app_state.h"
#include "language_defs.h"
#include "scintilla_bridge.h"
#include <algorithm>

- Use shim windows.h instead of local typedefs in wndproc.h and
  menu_builder.h to avoid redefinition conflicts
- Add missing standard includes: <algorithm> in recent_files.mm,
  <cctype> in autocomplete.mm and edit_commands.mm, <cwchar> in
  string_utils.h, <cstring> in lexer_styles.mm and edit_commands.mm
- Replace forward declaration of openFileAtPath with #include
  "file_operations.h" in recent_files.mm
- Add endLine adjustment in doSortLines to match doToggleLineComment
  behavior when selection ends at start of next line

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hybridmachine hybridmachine merged commit aed399e into macos-port Mar 16, 2026
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.

P0-15: Refactor main_phase7.mm into separate modules

2 participants