Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalPage.cpp (C++)

Code before:

            auto newControl = _InitControl(controlSettings, controlConnection);

            // Hookup our event handlers to the new terminal
            _RegisterTerminalEvents(newControl, *focusedTab);

            _UnZoomIfNeeded();

            focusedTab->SplitPane(realSplitType, splitSize, realGuid, newControl);
        }
        CATCH_LOG();
    }

    // Method Description:
    // - Split the focused pane of the given tab, either horizontally or vertically, and place the
    //   given TermControl into the newly created pane.
    // - If splitType == SplitState::None, this method does nothing.
    // Arguments:
    // - tab: The tab that is going to be split.
    // - splitType: one value from the TerminalApp::SplitState enum, indicating how the
    //   new pane should be split from its parent.
    // - splitMode: value from TerminalApp::SplitType enum, indicating the profile to be used in the newly split pane.
    // - newTerminalArgs: An object that may contain a blob of parameters to
    //   control which profile is created and with possible other
    //   configurations. See CascadiaSettings::BuildSettings for more details.
    void TerminalPage::_SplitPaneAnyTab(TerminalTab& tab,
                                        const SplitState splitType,
                                        const SplitType splitMode,
                                        const float splitSize,
                                        const NewTerminalArgs& newTerminalArgs)
    {
        // Do nothing if we're requesting no split.
        if (splitType == SplitState::None)
        {
            return;
        }

        try
        {
            TerminalSettingsCreateResult controlSettings{ nullptr };
            GUID realGuid;
            bool profileFound = false;

            if (splitMode == SplitType::Duplicate)
            {
                std::optional<GUID> current_guid = tab.GetFocusedProfile();
                if (current_guid)
                {
                    profileFound = true;
                    controlSettings = TerminalSettings::CreateWithProfileByID(_settings, current_guid.value(), *_bindings);
                    const auto workingDirectory = tab.GetActiveTerminalControl().WorkingDirectory();

Review comment (bug):
This is what I was talking to you about the other day. Most of this function is copied code from `TerminalPage::_SplitPane(<4 params>...)`.

Try refactoring these two functions to be something like this...
1. rename `_SplitPaneAnyTab` to `_SplitPane(<5 params>...)`
2. make `_SplitPane(<4 params>...)` call `_SplitPane(<5 params>...)`

```cpp
void TerminalPage::_SplitPane(const SplitState splitType,
                              const SplitType splitMode,
                              const float splitSize,
                              const NewTerminalArgs& newTerminalArgs)
    {
        const auto focusedTab{ _GetFocusedTabImpl() };

        // Do nothing if no TerminalTab is focused
        if (!focusedTab)
        {
            return;
        }

        // Now that we have the focusedTab, forward that and the other parameters
        // to the more generic version of this function.
        _SplitPane(focusedTab, splitType, splitMode, splitSize, newTerminalArgs);
```

This way, if we ever need to update `_SplitPane`, we don't need to update both. And we can avoid the situation of somebody forgetting to update one over the other and introducing a bug.

Code after:
                // the focused pane, and use that to build a new instance of the
                // settings so we can duplicate this tab/pane.
                //
                // Currently, if the profile doesn't exist anymore in our
                // settings, we'll silently do nothing.
                //
                // In the future, it will be preferable to just duplicate the
                // current control's settings, but we can't do that currently,
                // because we won't be able to create a new instance of the
                // connection without keeping an instance of the original Profile
                // object around.
            }
            if (!profileFound)
            {
                realGuid = _settings.GetProfileForArgs(newTerminalArgs);
                controlSettings = TerminalSettings::CreateWithNewTerminalArgs(_settings, newTerminalArgs, *_bindings);
            }

            const auto controlConnection = _CreateConnectionFromSettings(realGuid, controlSettings.DefaultSettings());

            const float contentWidth = ::base::saturated_cast<float>(_tabContent.ActualWidth());
            const float contentHeight = ::base::saturated_cast<float>(_tabContent.ActualHeight());
            const winrt::Windows::Foundation::Size availableSpace{ contentWidth, contentHeight };

            auto realSplitType = splitType;
            if (realSplitType == SplitState::Automatic)
            {
                realSplitType = tab.PreCalculateAutoSplit(availableSpace);
            }

            const auto canSplit = tab.PreCalculateCanSplit(realSplitType, splitSize, availableSpace);
            if (!canSplit)
            {
                return;
            }

            auto newControl = _InitControl(controlSettings, controlConnection);

            // Hookup our event handlers to the new terminal
            _RegisterTerminalEvents(newControl, tab);

            _UnZoomIfNeeded();

            tab.SplitPane(realSplitType, splitSize, realGuid, newControl);
        }
        CATCH_LOG();
    }

    // Method Description:
    // - Switches the split orientation of the currently focused pane.
    // Arguments: