zed icon indicating copy to clipboard operation
zed copied to clipboard

Syntax highlighting for .env.*

Open nk-tedo-001 opened this issue 1 year ago • 7 comments

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

If I edit the .env.local file, Zed does not recognize it as an environment file. Therefore, there is no highlighting, commenting, or other editor features.

I suggest that every env file should be handled properly. Refer to https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used for more information.

Environment

Zed: v0.123.6 (Zed) OS: macOS 14.1.1 Memory: 32 GiB Architecture: x86_64

nk-tedo-001 avatar Feb 27 '24 07:02 nk-tedo-001

I think you'd just have to add it here, right? https://github.com/zed-industries/zed/blob/cbdc07dcd0fe7811615333c91d95d93815eee9f5/crates/languages/src/bash/config.toml#L3

But would that fix your problem? From reading through those docs it sounds like you also want to support .env.production, etc., right?

mrnugget avatar Feb 27 '24 10:02 mrnugget

I think you'd just have to add it here, right?

https://github.com/zed-industries/zed/blob/cbdc07dcd0fe7811615333c91d95d93815eee9f5/crates/languages/src/bash/config.toml#L3

Most likely, all configurations are contained within the Zed binary, as I was unable to locate any separate configuration files. Therefore, I am unable to test this myself.

But would that fix your problem? From reading through those docs it sounds like you also want to support .env.production, etc., right?

We can add the most commonly used names as

  1. .env (which currently works),
  2. .env.*,
  3. and .env.*.*

If a wildcard cannot be applied, we can enumerate a short list of files:

  1. .env,
  2. .env.local,
  3. .env.dev, .env.development, .env.dev.local, .env.development.local,
  4. .env.test, and .env.test.local.
  5. .env.stage, .env.stage.local,
  6. .env.prod, and .env.prod.local

nk-tedo-001 avatar Feb 27 '24 11:02 nk-tedo-001

Most likely, all configurations are contained within the Zed binary, as I was unable to locate any separate configuration files. Therefore, I am unable to test this myself.

Yep, sorry, I meant: you can add this in a PR :)

But yeah, we don't seem to support wildcards so far, only suffixes. What we need is a path_prefix.

mrnugget avatar Feb 27 '24 12:02 mrnugget

Also note that there's https://github.com/zed-industries/zed/pull/8453 for extension-less files, and it looks like its logic matches what one would want for .env*? https://github.com/zed-industries/zed/pull/8453/files#diff-bde575654544e89aff5972d3565828c6d6deb5fb3b9fbc01299b1b82bf5a8569R106-R110

SomeoneToIgnore avatar Feb 27 '24 13:02 SomeoneToIgnore

Ahh, interesting. Yeah, stems seems like something we'd need in the language config then.

mrnugget avatar Feb 27 '24 13:02 mrnugget

I've looked into it on how to get this done, as i thought it would be trivial, while it is not hard per-se it requires a change here:

language_registry.rs :: language_for_file will need a change, as it uses the path.extension_or_hidden_file_name which will only return the last extension.

  • Crate path: new function path.get_all_extensions_or_hidden_file_name(): Option<Vec<&str>>
  • language_for_file:
  • iterate over all found extensions
  • take the last recognized language

Additionally, it would be an option to enable support for globbing / wildcard matches in path_suffixes of languages config.toml, this would add extra refactoring work.

Sorry I wasn't more help, but I gave it an honest try.

rwese avatar Mar 03 '24 19:03 rwese

Just to mention it, we do have a file_types setting that might be useful here, as a temporary bandaid:

// An object whose keys are language names, and whose values
// are arrays of filenames or extensions of files that should
// use those languages.
//
// For example, to treat files like `foo.notjs` as JavaScript,
// and 'Embargo.lock' as TOML:
//
// {
//   "JavaScript": ["notjs"],
//   "TOML": ["Embargo.lock"]
// }
//
"file_types": {},

JosephTLyons avatar Apr 18 '24 20:04 JosephTLyons

Please whatever the solution, allow syntax highlighting .env.example. And perhaps also .env.whatever.example.

jaydenseric avatar Jun 19 '24 02:06 jaydenseric

you can simply add in your settings :

"file_types": {
  "Shell Script": [".env.*"]
}

At least works for me : )

lpillonel avatar Jul 19 '24 07:07 lpillonel