Categories
IDE linguaggio javascript

Emacs (6) con eglot

Emacs è un IDE, che può appoggiarsi ad un server LSP (Language Server Protocol) per le funzioni di code-completation, symbol-search, per la documentazione, e altre ben note “code action (o source action).

Per farlo è necessario trasformare Emacs in un client LSP, ed è possibile usando elpa-lsp-mode, ma come installare e configurare il server LSP?

Per altri linguaggi, la distribuzione Debian GNU/Linux fornisce dei pacchetti che implementano un server LSP per qualche linguaggio, come:

  • clangd-16 per il linguaggio C++.
  • gopls per il linguaggio Go.

La distribuzione Arch Linux suggerisce Deno per JavaScript.

Oggi la ricerca non è andata a buon fine (dicembre 2023), finchè non ho trovato Eglot.

Eglot

Rispetto al già citato elpa-lsp-mode , Eglot (Emacs Polyglot) permette di connettersi ad un server, ma suggerisce anche come implementare il server.

Eglot si può installare su Emacs con

M-x packet-install RET eglot

Basta aprire un file qualsiasi .js e digitare

M-x eglot

Si ottiene un messaggio in cui ci chiede di poter avviare un programma (che però non viene trovato):

I guess you want to run ` typescript-language-server --stdio ` but I can't find it in PATH.

Infatti nella documentazione di Eglot per JavaScript si chiede di installare preventivamente il seguente programma1 usando il gestore npm

L’opzione globale -g richiede il superutente su Debian

npm install -g typescript-language-server typescript

Server LSP

Il server appena installato accetta dal client due possibili tipi di comandi (due workspace):

  • lsp
  • tsp

A questo punto si può avviare Emacs e avviare eglot

M-x eglot

Il quale ci comunicherà che basta premere invio per avviare il server LSP:

Enter program to execute: typescript-language-server --stdio

Premendo invio si ottiene:

Connected! Server `EGLOT (progettoApp/(js-mode js-ts-mode typescript-ts-mode typescript-mode))' Now Managing ` js-mode js-ts-mode typescript-ts-mode typescript-mode ` buffers in project

Nel buffer apparirà il numero di errori (in rosso) e di warning (in arancio).

Si possono leggere anche due minor mode FlyMake e ElDoc

Eglot insieme al server LSP sono in grado di fare tutto quello che si chiede ad un software Prettier.

Features

Le funzioni che si possono attivare con Eglot richiedono altri pacchetti Emacs, ad esempio:

  • company, pacchetto ELPA per il completamento company-mode.
    • (add-hook 'after-init-hook 'global-company-mode)
  • yasnippet, pacchetto ELPA per l’inserimento automatico di pezzi di codice (templates e snippets)
  • Flymake
  • Xref
  • Imenu

Elenco delle features2

  • at-point documentation
  • at-point documentation formatted
  • on-the-fly diagnostic annotations with server-suggested fixes, via the Flymake package
  • finding definitions and uses of identifiers, via Xref package
  • buffer navigation by name of function, class, method, etc., via Imenu package
  • enhanced completion of symbol at point
  • automatic reformatting of source code as you type it
  • renaming symbols con eglot-rename
  • hints about positional parameter names in function calls con eglot-inlay-hints-mode

Elenco dei comandi3

  1. github.com/typescript-language-server/typescript-language-server []
  2. https://www.gnu.org/software/emacs/manual/html_node/eglot/Eglot-Features.html []
  3. https://www.gnu.org/software/emacs/manual/html_node/eglot/Eglot-Commands.html []