9 stable releases

1.6.0 Feb 10, 2025
1.5.0 Feb 10, 2025
1.1.0 Jan 20, 2025

#468 in Command line utilities

Download history 141/week @ 2025-01-14 80/week @ 2025-01-21 595/week @ 2025-02-04 90/week @ 2025-02-11

810 downloads per month

Unlicense

63KB
1K SLoC

ec2hx - EditorConfig to Helix

This is a CLI tool that translates an EditorConfig file to a project-specific configuration for the Helix editor.

Usage

  1. Install using your preferred method:

    using binstall cargo binstall ec2hx
    from source cargo install ec2hx
    script, manual etc. see release page
  2. Run ec2hx in your project directory.

  3. Start Helix or run :config-reload.

Upstream support

At the time of writing, Helix does not support EditorConfig. The maintainers don't want to add it in core, they want a plugin to handle it. However, the plugin system is still a work in progress. That's why ec2hx may be your best option at the moment.

Subscribe to the following issues and PRs to stay up-to-date with developments upstream:

Setting expectations

EditorConfig is more flexible than the config of Helix, which means support is only partial. However, I believe the support is more than sufficient for "sane" configurations and even a couple insane ones. This section describes in detail what is and isn't supported.

Properties

Almost all properties are supported, but there are some caveats. The only unsupported key is charset, which doesn't have a matching Helix configuration key. For reference, here is the list of official EditorConfig properties.

  • indent_style (fully supported)

  • indent_size (fully supported)

  • tab_width (overruled by indent_size, weird setups where the two don't match are not supported)

  • max_line_length (use the CLI flag --rulers to add matching rulers)

  • end_of_line (only in the global [*] section, not per-language)

  • insert_final_newline (only in the global [*] section, not per-language)

  • trim_trailing_whitespace has pretty good support, but may need a little manual intervention.

    It is achieved with a built-in formatter that does the trimming. ec2hx configures Helix to call ec2hx trim-trailing-whitespace to format files. However, this formatter config is not applied if there is already a formatter or LSP installed, because they may already handle formatting (and probably better than ec2hx ever could). This detection is done by parsing the output of hx --health.

    There are two situations where you may want to manually intervene:

    • You have an LSP installed, but it doesn't actually handle formatting. In that case, you can manually add the following formatter config to the language, either to your user configuration (~/.config/helix/languages.toml) or the project-specific configuration ($proj_dir/.helix/languages.toml).

      formatter = { command = "ec2hx", args = ["trim-trailing-whitespace"] }
      auto-format = true
      
    • You didn't have an LSP or formatter installed for a language in the past, ran ec2hx and later installed an LSP or formatter. In that case your new formatter is "shadowed" and you may want to remove the formatter config generated by ec2hx. In order to do that, simply run ec2hx again.

File processing

The CLI only reads the toplevel .editorconfig. Adding support for .editorconfig files in parent or subdirectories is technically feasible, but I'm not aware of this feature being used in the wild. Please open an issue if you would like this to be supported.

Glob expressions

Glob expressions are generally supported. However, the best support is for section headers that cleanly map to a set of file types. Examples of such well-supported section headers are:

  • [Makefile]
  • [*.py]
  • [*.{json,js}]
  • [*.[ch]]
  • [{*.{awk,c,dts},Kconfig,Makefile}]

Globs which match against paths (e.g. when they contain / or **) are also supported, but there are some caveats.

click here if you're interested in how that even works in the first place

Helix does not directly support configuring properties based on file globs. It's only possible to set these properties either globally or for a specific language.

The trick is that you can define a custom language which matches against a weirdly specific glob with its file-types key.

So, what ec2hx does is first try to figure out the actual file type the glob matches against. Then it will copy the existing Helix configuration for that language (even respecting your user configuration) to a new artificial language definition.

Helix will then recognize files that match this glob as the synthetic language and apply the appropriate config.

One slight downside of this approach is that syntax highlighting only works for languages that have appropriate queries in the Helix runtime directory. At the time of writing, Helix doesn't support project specific runtime files. Therefore, ec2hx will generate the necessary queries into your user configuration directory. For example, that would be ~/.config/helix/runtime/queries on Linux. The directories generated by ec2hx are prefixed with ec2hx-glob-lang-, so there shouldn't be any conflicts.

If you don't like it when programs vomit auto-generated garbage into your config directory... I agree with you and I'm sorry! If Helix adds support for it, it might be possible to avoid this in the future.

No inheritance between path glob sections

Path glob sections don't inhert configuration from previous path glob sections that also match the same files. They only inherit configuration from previous sections that cleanly map to a file type. Consider the following example:

[*.md]
indent_size = 4
indent_style = tab

[docs/**.md]
indent_size = 2

[docs/internal/**.md]
indent_style = space

According to the EditorConfig specification, the [docs/internal/**.md] section should inherit indent_size = 2 from the [docs/**.md] section, but it only inherits indent_size = 4 from the [*.md] section.

Rare special characters are unsupported

The characters !, .. and \ aren't used in any configuration I'm testing against. Sections which contain them in the header will be ignored. I don't think it's possible to support !, but .. and \ are technically feasible. Please open an issue if you would like them to be supported.

File types

Some of the properties like indent_size and indent_style can only be configured per-language in Helix. So what happens if they appear in a section that applies to all file types, not just specific ones? (examples: [*], [src/**], [docs/**])

ec2hx will generate a language configuration with these properties for every single language Helix supports. Unfortunately, that means this configuration won't apply to file types Helix doesn't recognize. *.txt is a hardcoded exception and you can specify additional file globs to which such config should apply via the CLI, for example:

ec2hx --fallback-globs '*.foo,*.bar'

Contributing

A good way to contribute is to provide an example EditorConfig file that you think could be handled better. You can open an issue about it or a PR adding it to the test_data/ directory next to the other examples. It will automatically be picked up by the snapshot tests (using insta).

Dependencies

~6–17MB
~221K SLoC