Skip to content

Comments

Refactor Config::load#1

Open
thomie wants to merge 2 commits intofix-merge-toml-valuesfrom
refactor-config-load
Open

Refactor Config::load#1
thomie wants to merge 2 commits intofix-merge-toml-valuesfrom
refactor-config-load

Conversation

@thomie
Copy link
Owner

@thomie thomie commented Apr 11, 2025

Split Config::load into 2 smaller functions:

  1. A function to load a ConfigRaw from a path
  2. A function to apply a ConfigRaw to a Config

The resulting code is both shorter, more readable, and more extensible (e.g., it can support more than two config files).

This refactor revealed the bug described in helix-editor#13332.

thomie added 2 commits April 11, 2025 11:55
Currently `Config::load` merges editor configs with
`merge_toml_values(global, local, 3)`

Besides the puzzling use of the magic number `3`, this is a bug,
because it concatenates arrays from the global config file with
those from the local config file.

For example `editor.shell=["bash", "-c"]` and
`editor.shell=["fish", "-c"]` would become
`editor.shell=["bash", "-c", "fish", "-c"]`.

This commit adds a new function `merge_toml_values_with_strategy` and
changes `Config::load` to call:
```
  merge_toml_values_with_strategy(
    global,
    local,
    &MergeStrategy {
      array: MergeMode::Never,
      table: MergeMode::Always,
    },
  )
```

For backward compatability, the original function `merge_toml_values`
calls:
```
  merge_toml_values_with_strategy(
    left,
    right,
    &MergeStrategy {
      array: MergeMode::MaxDepth(max_merge_depth),
      table: MergeMode::MaxDepth(max_merge_depth),
    },
  )
```
, so there should be no change in the way themes and languages.toml
are merged.

Also update doc string and add tests.
Split `Config::load` into 2 smaller functions:
 1. A function to load a `ConfigRaw` from a path
 2. A function to apply a `ConfigRaw` to a `Config`

The resulting code is both shorter, more readable, and more extensible
(e.g., it can support more than two config files).

This refactor revealed the bug described in helix-editor#13332.
@thomie thomie force-pushed the fix-merge-toml-values branch from bdc2303 to dcf139c Compare August 2, 2025 18:31
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.

1 participant