# delphidabbler/codesnip architecture

> Architecture diagram of the GitHub repository delphidabbler/codesnip, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/delphidabbler/codesnip
- Repository: https://github.com/delphidabbler/codesnip

## Overview

##### Project type and purpose
- CodeSnip is a native Windows desktop application written in Delphi (Pascal).
- Its purpose is to store, manage and view code snippets (focused on Pascal/Delphi) with features for importing from external snippet collections, syntax-highlighting, formatting/export and compile-testing snippets.

##### High-level architectural layers and top-level directories
- Presentation Layer (UI & Forms) – the bulk of `Src/*.dfm` and corresponding `.pas` files under `Src/` (e.g. `FmMain.pas`, `FmSnippetsEditorDlg.pas`, `FrHTMLPreview.pas`, etc.).
- Controller/Manager Layer – classes prefixed `U…Mgr`, `U…Action`, `UDialogMgr`, `UDispatchList` that coordinate UI events with business logic.
- Business Logic Layer – core snippet/domain classes under `Src/DB.*.pas` (e.g. `DB.UMain.pas`, `DB.USnippet.pas`), snippet validators (`ActiveText.UValidator.pas`), compile manager (`UCompileMgr.pas`), export/import managers (`SWAG.UImporter.pas`, `UCodeImportExport.pas`)
- Data Access & Persistence Layer – interfaces and implementations in `Src/DBIO.*.pas`, `Src/UDatabaseLoader.pas`, `UIniDataLoader.pas`, `UREMLDataIO.pas` that read/write the main database, user DB, INI files, XML and backup formats.
- Rendering & Preview Layer – HTML and RTF rendering modules (`ActiveText.UHTMLRenderer.pas`, `URTFBuilder.pas`, `UHTMLTemplate.pas`) plus an embedded browser interface (`Browser.UController.pas`, `Browser.IntfDocHostUI.pas`).
- External Services & 3rd-party – the “3rdParty” folder (e.g. `PJMD5.pas`), COM interop (shell folders, SysInfo), jQuery and scripts under `Res/Scripts`, and installer scripts under `Install/`.
- Help & Documentation – compiled HTML help under `Help/` and design docs in `Docs/Design/FileFormats/`.

##### Key architectural patterns and principles
- MVC-style separation: Forms (`View`), Manager/Action classes (`Controller`), Domain & DB classes (`Model`).
- Singleton and Factory patterns (`USingleton.pas`, `UActionFactory.pas`).
- Observer / Event dispatch (`UMultiCastEvents.pas`, `UDispatchList.pas`) to decouple UI updates.
- Plugin-style importers for SWAG and Dabbler snippet libraries (`SWAG.UImporter.pas`, `CodeSnip.iss`).

##### Main components & interactions
A. Presentation Layer (VCL Forms & Dialogs)
– FmMain, all Fm* dialog forms, Fr* frame forms.
B. UI Controller Layer
– Dialog manager, Action & Event dispatch classes (UDialogMgr, UActionFactory, UDispatchList).
C. Business Logic Layer
– SnippetManager, CategoryManager, CompileManager, FavouritesManager.
D. Data Persistence Layer
– DB I/O interfaces (DatabaseIO, XMLDataIO, IniDataReader), DB loaders/savers.
E. Rendering & Preview Layer
– HTMLRenderer, RTFRenderer, TextRenderer, embedded browser host.
F. External Services & Helpers
– FileSystem, Shell, HTTP, Logger, MD5, VersionInfo utilities.
G. Installer & Deployment
– Inno Setup script, portable vs standard edition.
H. Test Suite
– DUnit tests under `Tests/Src/DUnit`.

- UI → UI Controller (user events)
- UI Controller → Business Logic (invoke snippet CRUD, compile, import/export)
- Business Logic → Data Persistence (read/write snippet store)
- Business Logic → Rendering Layer (generate preview HTML/RTF) → Embedded Browser
- Business Logic → External Helpers (e.g. File I/O, Shell operations, MD5)
- Installer → Deployment of binaries + resources

##### Detailed capture
- Import pipelines (SWAG → PacketCache → SnippetImporter → DB).
- Compile/test flow: SnippetSource → CompileManager → CompilerProcess (UBorland / UFreePascal runners) → CompileResults → UI.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer" 
        direction TB
        UI_Main["FmMain"]:::frontend
        UI_Editor["FmSnippetsEditorDlg"]:::frontend
        UI_PreviewFrame["FrHTMLPreview"]:::frontend
    end

    %% UI Controller Layer
    subgraph "UI Controller Layer"
        direction TB
        DialogMgr["UDialogMgr"]:::controller
        ActionFactory["UActionFactory"]:::controller
        DispatchList["UDispatchList"]:::controller
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        direction TB
        Snippet["DB.USnippet"]:::logic
        Category["DB.UCategory"]:::logic
        MainDomain["DB.UMain"]:::logic
        Validator["ActiveText.UValidator"]:::logic
        CompileMgr["UCompileMgr"]:::logic
        FavouritesMgr["Favourites.UManager"]:::logic
    end

    %% Data Persistence Layer
    subgraph "Data Persistence Layer"
        direction TB
        DatabaseIO["DB.UDatabaseIO"]:::persistence
        XMLIO["DBIO.UXMLDataIO"]:::persistence
        IniReader["DBIO.UIniDataReader"]:::persistence
        DBLoader["UDatabaseLoader"]:::persistence
        IniLoader["UIniDataLoader"]:::persistence
        REMLIO["UREMLDataIO"]:::persistence
    end

    %% Rendering & Preview Layer
    subgraph "Rendering & Preview Layer"
        direction TB
        HTMLRenderer["ActiveText.UHTMLRenderer"]:::rendering
        HTMLTemplate["UHTMLTemplate"]:::rendering
        RTFRenderer["ActiveText.URTFRenderer"]:::rendering
        RTFBuilder["URTFBuilder"]:::rendering
        TextRenderer["ActiveText.UTextRenderer"]:::rendering
        subgraph "Embedded Browser Host"
            direction TB
            BrowserCtrl["Browser.UController"]:::rendering
            DocHostUI["Browser.IntfDocHostUI"]:::rendering
        end
    end

    %% External Services & Helpers
    subgraph "External Services & Helpers"
        direction TB
        ThirdParty["3rdParty"]:::external
        HTTP["UHTTPProtocol"]:::external
        FileIO["UFileProtocol"]:::external
        VersionInfo["UVersionInfo"]:::external
        SystemInfo["USystemInfo"]:::external
    end

    %% Installer & Deployment
    subgraph "Installer & Deployment"
        direction TB
        InstallerScript["CodeSnip.iss"]:::installer
        InstallScripts["Install Scripts (.ps)"]:::installer
    end

    %% Test Suite
    subgraph "Test Suite"
        direction TB
        DUnitTests["DUnit Tests"]:::test
    end

    %% Connections
    UI_Main -->|user events| DialogMgr
    UI_Editor -->|user events| DialogMgr
    UI_PreviewFrame -->|user events| DispatchList

    DialogMgr -->|invoke actions| Snippet
    ActionFactory -->|create actions| MainDomain
    DispatchList -->|dispatch events| FavouritesMgr

    Snippet -->|CRUD operations| DatabaseIO
    Snippet -->|CRUD operations| XMLIO
    Snippet -->|CRUD operations| IniReader
    MainDomain -->|load/save| DBLoader
    MainDomain -->|load/save| IniLoader
    MainDomain -->|load/save| REMLIO

    Snippet -->|validate| Validator
    Snippet -->|compile| CompileMgr
    CompileMgr -->|launch compiler| SystemInfo

    Snippet -->|render| HTMLRenderer
    Snippet -->|render| RTFRenderer
    Snippet -->|render| TextRenderer
    HTMLRenderer -->|use template| HTMLTemplate
    RTFRenderer -->|use builder| RTFBuilder
    HTMLRenderer --> BrowserCtrl
    RTFRenderer --> BrowserCtrl

    Snippet -->|fs| FileIO
    Snippet -->|http| HTTP
    Snippet -->|md5| ThirdParty
    Snippet -->|version info| VersionInfo

    InstallerScript -->|deploy binaries| UI_Main
    InstallerScript -->|deploy binaries| DialogMgr
    InstallerScript -->|deploy binaries| Snippet

    DUnitTests -->|test| Snippet
    DUnitTests -->|test| CompileMgr
    DUnitTests -->|test| DatabaseIO

    %% Click Events
    click UI_Main "https://github.com/delphidabbler/codesnip/tree/master/Src"
    click UI_Editor "https://github.com/delphidabbler/codesnip/tree/master/Src"
    click UI_PreviewFrame "https://github.com/delphidabbler/codesnip/tree/master/Src"
    click DialogMgr "https://github.com/delphidabbler/codesnip/blob/master/Src/UDialogMgr.pas"
    click ActionFactory "https://github.com/delphidabbler/codesnip/blob/master/Src/UActionFactory.pas"
    click DispatchList "https://github.com/delphidabbler/codesnip/blob/master/Src/UDispatchList.pas"
    click Snippet "https://github.com/delphidabbler/codesnip/blob/master/Src/DB.USnippet.pas"
    click Category "https://github.com/delphidabbler/codesnip/blob/master/Src/DB.UCategory.pas"
    click MainDomain "https://github.com/delphidabbler/codesnip/blob/master/Src/DB.UMain.pas"
    click Validator "https://github.com/delphidabbler/codesnip/blob/master/Src/ActiveText.UValidator.pas"
    click CompileMgr "https://github.com/delphidabbler/codesnip/blob/master/Src/UCompileMgr.pas"
    click FavouritesMgr "https://github.com/delphidabbler/codesnip/blob/master/Src/Favourites.UManager.pas"
    click DatabaseIO "https://github.com/delphidabbler/codesnip/blob/master/Src/DB.UDatabaseIO.pas"
    click XMLIO "https://github.com/delphidabbler/codesnip/blob/master/Src/DBIO.UXMLDataIO.pas"
    click IniReader "https://github.com/delphidabbler/codesnip/blob/master/Src/DBIO.UIniDataReader.pas"
    click DBLoader "https://github.com/delphidabbler/codesnip/blob/master/Src/UDatabaseLoader.pas"
    click IniLoader "https://github.com/delphidabbler/codesnip/blob/master/Src/UIniDataLoader.pas"
    click REMLIO "https://github.com/delphidabbler/codesnip/blob/master/Src/UREMLDataIO.pas"
    click HTMLRenderer "https://github.com/delphidabbler/codesnip/blob/master/Src/ActiveText.UHTMLRenderer.pas"
    click HTMLTemplate "https://github.com/delphidabbler/codesnip/blob/master/Src/UHTMLTemplate.pas"
    click RTFRenderer "https://github.com/delphidabbler/codesnip/blob/master/Src/ActiveText.URTFRenderer.pas"
    click RTFBuilder "https://github.com/delphidabbler/codesnip/blob/master/Src/URTFBuilder.pas"
    click TextRenderer "https://github.com/delphidabbler/codesnip/blob/master/Src/ActiveText.UTextRenderer.pas"
    click BrowserCtrl "https://github.com/delphidabbler/codesnip/blob/master/Src/Browser.UController.pas"
    click DocHostUI "https://github.com/delphidabbler/codesnip/blob/master/Src/Browser.IntfDocHostUI.pas"
    click ThirdParty "https://github.com/delphidabbler/codesnip/tree/master/Src/3rdParty"
    click HTTP "https://github.com/delphidabbler/codesnip/blob/master/Src/UHTTPProtocol.pas"
    click FileIO "https://github.com/delphidabbler/codesnip/blob/master/Src/UFileProtocol.pas"
    click VersionInfo "https://github.com/delphidabbler/codesnip/blob/master/Src/UVersionInfo.pas"
    click SystemInfo "https://github.com/delphidabbler/codesnip/blob/master/Src/USystemInfo.pas"
    click InstallerScript "https://github.com/delphidabbler/codesnip/blob/master/Src/Install/CodeSnip.iss"
    click InstallScripts "https://github.com/delphidabbler/codesnip/tree/master/Src/Install"
    click DUnitTests "https://github.com/delphidabbler/codesnip/tree/master/Tests/Src/DUnit"

    %% Styles
    classDef frontend fill:#87CEFA,stroke:#000
    classDef controller fill:#90EE90,stroke:#000
    classDef logic fill:#FFA500,stroke:#000
    classDef persistence fill:#FFFF66,stroke:#000
    classDef rendering fill:#DDA0DD,stroke:#000
    classDef external fill:#D3D3D3,stroke:#000
    classDef installer fill:#FFB6C1,stroke:#000
    classDef test fill:#20B2AA,stroke:#000
```
