joaotavora / eglot

A client for Language Server Protocol servers
GNU General Public License v3.0
2.25k stars 200 forks source link

`language-id` not changing after the first file #1384

Closed minikN closed 6 months ago

minikN commented 6 months ago

When working with a project that is supposed to have different language-ids for different file types (I test it with a js CRA application) eglot seems to send the correct language-id for the first file I open, but doesn't change it accordingly if I open a file of a different type afterwards (unless I'm misunderstanding the spec and this isn't suppose to happen anyway).

I am aware of commit 2fbcab293e and tried to configure eglot correctly in two ways:

  1. I evaluated
    
    (add-to-list 'auto-mode-alist '("\\.js\\'" . js-mode))
    (add-to-list 'auto-mode-alist '("\\.jsx\\'" . js-jsx-mode))

(put 'js-mode 'eglot-language-id "javascript") (put 'js-jsx-mode 'eglot-language-id "javascriptreact")

(with-eval-after-load 'eglot (add-to-list 'eglot-server-programs '((js-mode js-jsx-mode) . ("/gnu/store/928xbf54h864yz55j90m29f09l5bkp07-node-typescript-language-server-0.11.1/bin/typescript-language-server" "--stdio" :initializationOptions (:tsserver (:path "/gnu/store/807nkysq2myq6dgb3fsa0padsbin5rl0-node-typescript-4.7.3/lib/node_modules/typescript/lib"))))))

(get 'js-mode 'eglot-language-id) => "javascript" (get 'js-jsx-mode 'eglot-language-id) => "javascriptreact"

Then Iopened `App.js` of the CRA project, in events buffer I can see `languageId` being set to `javascript`. If I open another file `Test.jsx` afterwards, in the new `textDocument/didOpen` event I also see `languageId` being set to `javascript`, but it should be `javascriptreact` in this case.

2. I also tried evaluating (I restarted emacs before trying this)
```lisp
(add-to-list 'auto-mode-alist '("\\.js\\'" . js-mode))
(add-to-list 'auto-mode-alist '("\\.jsx\\'" . js-jsx-mode))

(with-eval-after-load
    'eglot
  (add-to-list
   'eglot-server-programs
   '(((js-jsx-mode :language-id "javascriptreact")
   (js-mode :language-id "javascript")) .
  ("/gnu/store/928xbf54h864yz55j90m29f09l5bkp07-node-typescript-language-server-0.11.1/bin/typescript-language-server" "--stdio" :initializationOptions
  (:tsserver
   (:path "/gnu/store/807nkysq2myq6dgb3fsa0padsbin5rl0-node-typescript-4.7.3/lib/node_modules/typescript/lib"))))))

But the languageId I see in the events buffer are the same.

Here are two examples:

  1. First App.js, then Test.jsx: http://paste.debian.net/1313114/
  2. First Test.jsx, then App.js: http://paste.debian.net/1313112/ (I restarted emacs inbetween)

MRE:

  1. emacs -Q
  2. eval
    
    (add-to-list 'auto-mode-alist '("\\.js\\'" . js-mode))
    (add-to-list 'auto-mode-alist '("\\.jsx\\'" . js-jsx-mode))

(with-eval-after-load 'eglot (add-to-list 'eglot-server-programs '(((js-jsx-mode :language-id "javascriptreact") (js-mode :language-id "javascript")) . ("/gnu/store/928xbf54h864yz55j90m29f09l5bkp07-node-typescript-language-server-0.11.1/bin/typescript-language-server" "--stdio" :initializationOptions (:tsserver (:path "/gnu/store/807nkysq2myq6dgb3fsa0padsbin5rl0-node-typescript-4.7.3/lib/node_modules/typescript/lib"))))))


3. Open `js` file in project
4. `M-x eglot`
5. Switch to `jsx` file
6. Inspect events buffer

If I misunderstood the spec or something in eglot I apologize in advance.

Thanks.
joaotavora commented 6 months ago

If I misunderstood the spec or something in eglot I apologize in advance.

No need to apologize, but you should rather refer to Emacs's commits, not this repository, because Eglot's development happens in Emacs.

Here, I think the problem here is simply that js-jsx-mode derives from js-mode. So any buffer that is in js-jsx-mode is -- by definition -- also in js-mode. So this is ultimately question of major mode design.

But it's also a real problem (I presume) and is not an easy question to solve. There are attempts to solve it. I participated in one of those attempts with multiple ideas -- designed at more closely associating major modes with languages -- but my ideas were not accepted. I have since abandoned those discussions, and I plan to keep doing that. But I encourage you to participate:

bug#68246 https://debbugs.gnu.org/cgi/bugreport.cgi?bug=68246 bug#68217: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=68217

I've been away from them for quite a while now (again, I plan to stay away, so please don't CC me). @monnier had a solution ready, perhaps it will work here too.