 ;;; Packages
 
 (use-package! lsp-mode
-  :defer t
+  :commands lsp-install-server
   :init
   (setq lsp-session-file (concat doom-etc-dir "lsp-session"))
   ;; Don't prompt the user for the project root every time we open a new

        lsp-intelephense-storage-path (concat doom-cache-dir "lsp-intelephense/"))

  :config
  (set-lookup-handlers! 'lsp-mode :async t
    :documentation 'lsp-describe-thing-at-point
    :definition 'lsp-find-definition

           nil)
          ((lsp--find-root-interactively session))))

  (defadvice! +lsp-init-a (orig-fn &optional arg)
    "Enable `lsp-mode' in the current buffer.

Meant to gimp `lsp', which is too eager about installing LSP servers,
initializing lsp-ui-mode, company, yasnippet and flycheck. Instead, these are
handled by our modules. Also see:

+ `+lsp-init-company-h' (on `lsp-mode-hook')
+ `+lsp-init-ui-flycheck-or-flymake-h' (on `lsp-ui-mode-hook')

This also logs the resolved project root, if found, so we know where we are."
    :around #'lsp
    (interactive "P")
    (require 'lsp-mode)
    (cl-letf
        (;; Our system, our rules. Don't install things on it without our
         ;; permission. Use `lsp-install-server' deliberately, instead.
         ((symbol-function #'lsp--client-download-server-fn)
          (lambda (&rest _) nil))
         ;; Don't set up company, lsp-ui, yasnippet, or flycheck. We have
         ;; modules dedicated to these things already.
         ((symbol-function #'lsp--auto-configure)
          (lambda (&rest _)
            ;; Announce what project root we're using, for diagnostic
            ;; purposes
            (if-let (root (lsp--calculate-root (lsp-session) (buffer-file-name)))
                (lsp--info "Guessed project root is %s" (abbreviate-file-name root))
              (lsp--info "Could not guess project root."))))))
    (apply orig-fn args))

  ;; Don't prompt to restart LSP servers while quitting Emacs
  (add-hook! 'kill-emacs-hook (setq lsp-restart 'ignore)))
