Crandel / tempel-collection

Collection tempel templates
GNU General Public License v3.0
76 stars 27 forks source link

loader doesn't support multiple major modes per template #14

Closed mpenet closed 6 months ago

mpenet commented 1 year ago

If you have a template file that has to be loaded/used by multiple modes (with no shared/contextual "parent" mode) the loader will only trigger loading for the one matching the name of the file.

ex: both major for clojure-mode and clojurescript-mode should load clojure.eld, right now only the former works. As tempel allows to specify templates for multiple modes per template file, that would be expected to work.

In theory if we want to support this, either we use symlinks (yuk), or we need to load all the files to know what are the mode dependencies, which kind of defeats the purpose of doing lazy loading... We could generate a mapping somewhere when templates are added modified too, moving the problem at "build" time, but not sure it's worth the effort.

I would be inclined to not bother with lazy loading personally.

minad commented 1 year ago

I would either use symlinks or add a variable tempel-collection--aliases which specifies mode aliases and maps clojurescript to clojure. I don't think the aliasing problem is widespread. In some sense clojure and clojurescript mode are doing it wrong, since they don't use inheritance. If they are sufficiently distinct it may even make sense to offer completely distinct template files.

I would be inclined to not bother with lazy loading personally.

I think it is nice to keep this. Custom loaders may even allow you to use different template formats in the future.

Crandel commented 1 year ago

I think we could handle this using aliases at the moment. This will be more explicit solution. If this will not work, we could introduce a special variable for aliasing, as @minad proposed

unuunc commented 1 year ago

aliasing problem will be widespread after emacs 29 , as there will be *-ts-mode for all major modes.

Crandel commented 1 year ago

Will they stay like this forever? I thought it will be a temporary solution until feature parity with original mode will happen, so they will replace current modes.

minad commented 1 year ago

I think it will stay like this forever. I find the design a bit questionable tbh. They are basically replacing all major programming modes in their entirety.

minad commented 1 year ago

But it shouldn't be a problem to add a special rule to the loader such that c-mode and c-ts-mode are treated the same way.

meliache commented 1 year ago

I stumbled upon the same issue. So I understand correctly that the problem is two-fold, right?

  1. Allow for several different major modes load the same eld file
  2. Avoid having to write down duplicate headings and template definitions for each major mode within each eld file

Problem 2. is something I even have in my own configuration. One hack was to just use e.g.

fundamental-mode :when (derived-mode-p 'rust-mode 'rust-ts-mode)

Though a more proper workaround would be write some custom elisp and work with template sources. @minad: If there is a more elegant solution for this, it would be nice to include it in the tempel documentation or examples, as this is not tempel-collection specific.

In some sense clojure and clojurescript mode are doing it wrong, since they don't use inheritance.

Regarding that, the python.el has an interesting approach, which avoids some of the issues here. It has a new base mode python-base-mode from which both python-mode and python-ts-mode inherit, all being in the same file and provided by (require python). Therefore I just added a python-base-mode heading in my personal template file. The problem is that this is not backwards compatible to older emacs versions as they don't have python-base-mode, so any functionality that aims to be backwards-compatible cannot rely on its existence.

minad commented 1 year ago

The problem is that the major mode is used to determine which file to load. This means we need symlinks or aliases, e.g., both python-mode and python-ts-mode should load the python.eld file. Before the introduction of the ts modes it wasn't a big problem, since most languages only had a single mode.

Crandel commented 1 year ago

I'll create aliases and let's see how it will goes. We could introduce more complex logic later if required.