Repository: CodeEditApp/CodeEdit
File: CodeEdit/AppDelegate.swift (Swift)

Code before:
import CodeEditSymbols

final class CodeEditApplication: NSApplication {
    let strongDelegate = AppDelegate()

    override init() {
        super.init()
        self.delegate = strongDelegate
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
    var updater: SoftwareUpdater = SoftwareUpdater()

    func applicationWillFinishLaunching(_ notification: Notification) {
        _ = CodeEditDocumentController.shared

        // This enables window restoring on normal quit (instead of only on force-quit).
        UserDefaults.standard.setValue(true, forKey: "NSQuitAlwaysKeepsWindows")
    }

    func applicationDidFinishLaunching(_ notification: Notification) {
        AppPreferencesModel.shared.preferences.general.appAppearance.applyAppearance()
        checkForFilesToOpen()

        DispatchQueue.main.async {
            var needToHandleOpen = true

            // If no windows were reopened by NSQuitAlwaysKeepsWindows, do default behavior.
            if !NSApp.windows.isEmpty {
                needToHandleOpen = false
            }

            for index in 0..<CommandLine.arguments.count {
                if CommandLine.arguments[index] == "--open" && (index + 1) < CommandLine.arguments.count {
                    let path = CommandLine.arguments[index+1]
                    let url = URL(fileURLWithPath: path)

                    CodeEditDocumentController.shared.reopenDocument(
                        for: url,
                        withContentsOf: url,
                        display: true
                    ) { document, _, _ in
                        document?.windowControllers.first?.synchronizeWindowTitleWithDocumentName()

Review comment (suggestion):
This is more of an open suggestion, but we could wrap lines like these in a private function, describing what it does, instead of adding a comment above it. In this case it could be `enableWindowSizeSaveOnQuit()` for example. And we can always add a function description block for a better explanation. 

Code after:
import CodeEditSymbols

final class CodeEditApplication: NSApplication {
    let strongDelegate = AppDelegate()

    override init() {
        super.init()
        self.delegate = strongDelegate
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

@main
final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
    var updater: SoftwareUpdater = SoftwareUpdater()

    func applicationWillFinishLaunching(_ notification: Notification) {
        _ = CodeEditDocumentController.shared
        enableWindowSizeSaveOnQuit()
    }

    func applicationDidFinishLaunching(_ notification: Notification) {
        AppPreferencesModel.shared.preferences.general.appAppearance.applyAppearance()
        checkForFilesToOpen()

        DispatchQueue.main.async {
            var needToHandleOpen = true

            // If no windows were reopened by NSQuitAlwaysKeepsWindows, do default behavior.
            if !NSApp.windows.isEmpty {
                needToHandleOpen = false
            }

            for index in 0..<CommandLine.arguments.count {
                if CommandLine.arguments[index] == "--open" && (index + 1) < CommandLine.arguments.count {
                    let path = CommandLine.arguments[index+1]
                    let url = URL(fileURLWithPath: path)

                    CodeEditDocumentController.shared.reopenDocument(
                        for: url,
                        withContentsOf: url,
                        display: true
                    ) { document, _, _ in
                        document?.windowControllers.first?.synchronizeWindowTitleWithDocumentName()
                    }