Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for template prefix #3767

Closed
senekor opened this issue May 17, 2024 · 3 comments
Closed

Add support for template prefix #3767

senekor opened this issue May 17, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@senekor
Copy link

senekor commented May 17, 2024

Is your feature request related to a problem? Please describe.

Adding the .tmpl suffix to any file usually breaks editor support for that file.
This is not necessary in my experience, most templating syntax can be stuffed into string literals and comments of languages like bash and toml.
I would like to enjoy a normal editor experience when editing templates in my chezmoi repo.

Describe the solution you'd like

Alongside the .tmpl suffix, it could be possible to add a tmpl_ prefix to a file name.
Editors would then recognize the correct file type.

Describe alternatives you've considered

None come to mind.

@senekor senekor added the enhancement New feature or request label May 17, 2024
@halostatue
Copy link
Collaborator

The Go Template extension for VS Code can perform Go template highlighting within a different syntax already, although some tweaking may be required. chezmoi.vim does the same nested syntax highlighting for Chezmoi tmpl files (note that it has strict loading requirements so it cannot be installed like any other Vim extension). chezmoi.el might be able to help here, too.

This is an interesting idea which is worth examining deeper. The template extension is already an attribute modifiable with chezmoi chattr template <target> so this shouldn’t be too hard to detect, but the default behaviour would probably be to continue to append .tmpl instead of prepending tmpl_.

@senekor
Copy link
Author

senekor commented May 17, 2024

I use helix 🙂

But now that you mention that other editors have support, I thought about how that support could be added to helix (it has no plugin system).
Turns out the configuration options for language detection is quite powerful:

[[language]]
name = "bash"
file-types = [ "sh", "bash", { glob = "*.sh.tmpl" } ]
[[language]]
name = "toml"
file-types = [ "toml", { glob = "*.toml.tmpl" } ]

This addition to ~/.config/helix/languages.toml fixes 99% of the problem for me.

I think my proposal could still be of value to other people.
Having to add support manually to each editor or telling users to add specific configuration isn't the best solution.
For me personally, the problem is mostly solved.

@twpayne
Copy link
Owner

twpayne commented May 18, 2024

.tmpl was deliberately used as a suffix (instead of a template_ prefix) because .tmpl files in chezmoi are Go templates, not bash scripts or TOML files or whatever, so highlighting them as bash scripts or TOML files would be incorrect.

Adding a template_ suffix in addition to a .tmpl prefix would add ambiguity. chezmoi often needs to generate the source name from the target name (e.g. when running chezmoi add --template) so would need extra configuration for the user express their preference for prefix or suffix, in addition to tracking which (or both) are used in existing files.

The correct thing to do, as has been described, is to configure your editor to highlight template files to your preferences.

I'll add a FAQ entry at some point. In the meantime, I'll close this as "not planned".

@twpayne twpayne closed this as not planned Won't fix, can't repro, duplicate, stale May 18, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants