zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
47.7k stars 2.79k forks source link

Syntax highlighting for .env.* #8466

Open nk-tedo-001 opened 7 months ago

nk-tedo-001 commented 7 months ago

Check for existing issues

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

mrnugget commented 7 months ago

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?

nk-tedo-001 commented 7 months ago

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
mrnugget commented 7 months ago

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.

SomeoneToIgnore commented 7 months ago

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

mrnugget commented 7 months ago

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

rwese commented 7 months ago

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.

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.

JosephTLyons commented 5 months ago

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": {},
jaydenseric commented 3 months ago

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

lpillonel commented 2 months ago

you can simply add in your settings :

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

At least works for me : )