Subject: Re: bug#61868: 29.0.60; Eglot: setting "workspace" configurations should be easier

From: Augusto Stoffel <[email-masked]>
Date: Tue, 28 Feb 2023 22:16:10 +0100

On Tue, 28 Feb 2023 at 21:35, Augusto Stoffel wrote:

>> My bet is that that two-liner would go a long way.
>
> No, this is not enough.  At the very least I need a history variable to
> look at the previous configurations.  This feature has to be a thing on
> top of of `eglot-show-workspace-configuration'.

Here's a hasty sketch of what I have in mind, sans the history commands.

--8<---------------cut here---------------start------------->8---
;; -*- lexical-binding: t; -*-

(defvar eglot-configuration-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\C-c\C-c" #'eglot-configuration-save)
    (define-key map "\C-c\C-v" #'eglot-configuration-apply)
    (define-key map "\C-c\C-k" #'kill-current-buffer)
    map))

(define-minor-mode eglot-configuration--mode
  "Mode to edit LSP config"
  :interactive nil
  :keymap eglot-configuration-map
  (setq-local header-line-format (substitute-command-keys "\
\\<eglot-configuration-map>\
Press \\[eglot-configuration-apply] to apply, \\[eglot-configuration-save] to save, \\[kill-current-buffer] to abort.")))

(defun eglot-configuration-apply (&optional save)
  (interactive)
  (let ((eglot-workspace-configuration
         (save-excursion
           (goto-char (point-min))
           (jsonrpc--json-read))))
    (with-current-buffer eglot-configure--buffer
      (eglot-signal-didChangeConfiguration (eglot--current-server-or-lose))
      (when save
        (save-window-excursion
          (let ((default-directory (project-root (eglot--current-project))))
            (add-dir-local-variable
             nil ;; Tricky choice
             'eglot-workspace-configuration
             eglot-workspace-configuration)
            (save-buffer)))))))

(defun eglot-configuration-save ()
  (interactive)
  (eglot-configuration-apply t))

(defvar-local eglot-configure--buffer nil) ;; Ugh, get rid of this?

(defun eglot-configure (&optional server)
  "Dump `eglot-workspace-configuration' as JSON for debugging."
  (interactive (list (and (eglot-current-server)
                          (eglot--read-server "Server configuration"
                                              (eglot-current-server)))))
  (let ((buffer (current-buffer))
        (conf (eglot--workspace-configuration-plist server)))
    (with-current-buffer (get-buffer-create "*EGLOT workspace configuration*")
      (erase-buffer)
      (insert (jsonrpc--json-encode conf))
      (with-no-warnings
        (require 'json)
        (when (functionp #'js-json-mode) (js-json-mode))
        (json-pretty-print-buffer))
      (setq eglot-configure--buffer buffer)
      (eglot-configuration--mode)
      (pop-to-buffer (current-buffer)))))
--8<---------------cut here---------------end--------------->8---