You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
LSP (Language Server Protocol, not Lisp Server Pages) can provide some handy project specific auto completion and navigation tools but it's bit unclear how to integrate well with web-mode.
Generally Eglot server executables can be configured on mode basis, but as web-mode uses multiple "engines" each relying on their own LSP server executables, resolving the correct one requires some extra tricks.
With some trial and error I figured out how to conditionally set the LSP server program after the web-mode-engine has been resolved, before initializing Eglot:
;; Add hook after web-mode's hack-local-variables-hook logic that has;; access to the web-mode-engine variable for setting eglot's lsp server;; program accordingly, and finally start eglot
(add-hook'web-mode-hook
(lambda ()
(add-hook'hack-local-variables-hook
(lambda ()
(when (derived-mode-p'web-mode)
(cond
((string= web-mode-engine "django")
;; Django-specific code here
(message"Django engine detected")
(add-to-list'eglot-server-programs
`(web-mode . ("/home/user/.local/bin/django-template-lsp"))))
((string= web-mode-engine "twig")
;; Twig-specific code here (TODO)
(message"Twig engine detected"))
((string= web-mode-engine "astro")
;; Astro-specific code here (TODO)
(message"Astro engine detected"))))
(eglot-ensure))
90t))) ;; <- inner hook here is delayed after web-mode does it's magic
Quick demo with django-template-lsp using Eglot xrefs navigation and completion for static files (Emacs 31).
Kooha-2025-11-15-09-47-42.mp4
It's a bit hacky but improvement ideas are welcome. Maybe it would require some changes to web-mode for having cleanest way to do it, something like following might be more ergonomic:
;; This is pseudocode / example
(setq web-mode-engine-eglot-executables-alist '(("django"."/home/user/.local/bin/django-template-lsp")
("twig"."/path/to/twiggy-lsp")
("astro"."/path/to/astro-lsp"))
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
LSP (Language Server Protocol, not Lisp Server Pages) can provide some handy project specific auto completion and navigation tools but it's bit unclear how to integrate well with web-mode.
Generally Eglot server executables can be configured on mode basis, but as web-mode uses multiple "engines" each relying on their own LSP server executables, resolving the correct one requires some extra tricks.
With some trial and error I figured out how to conditionally set the LSP server program after the web-mode-engine has been resolved, before initializing Eglot:
Quick demo with django-template-lsp using Eglot xrefs navigation and completion for static files (Emacs 31).
Kooha-2025-11-15-09-47-42.mp4
It's a bit hacky but improvement ideas are welcome. Maybe it would require some changes to web-mode for having cleanest way to do it, something like following might be more ergonomic:
Beta Was this translation helpful? Give feedback.
All reactions