### Starting a Language Server - eglot Source: https://context7.com/joaotavora/eglot/llms.txt The main entry point for starting a language server. It can be invoked interactively or programmatically with explicit arguments. ```APIDOC ## Starting a Language Server - `eglot` ### Description The main entry point. Interactively, Eglot guesses the server from `eglot-server-programs` based on `major-mode`. Once a server is running, every buffer of that major mode inside the same project is automatically managed. ### Method (eglot modes project server-class contact language-ids) ### Parameters - **modes**: A list of Emacs major modes to manage. - **project**: The project object (e.g., `project-current`). - **server-class**: The Eglot server class (e.g., `'eglot-lsp-server`). - **contact**: Server contact information, which can be a program name and arguments, or a host and port for TCP servers. - **language-ids**: A list of language identifiers for the server. ### Request Example ```elisp ;; Interactive: open any source file and run M-x eglot ;; Eglot picks the server from eglot-server-programs automatically. ;; Programmatic: call with explicit arguments (eglot '(python-mode python-ts-mode) ; managed modes (project-current) ; project object 'eglot-lsp-server ; server class '("pylsp") ; contact: program + args '("python")) ; language IDs ;; With a TCP server (e.g. Godot built-in LSP on port 6008): (eglot '(gdscript-mode) (project-current) 'eglot-lsp-server '("localhost" 6008) '("gdscript")) ;; With initializationOptions: (eglot '(rust-mode rust-ts-mode) (project-current) 'eglot-lsp-server '("rust-analyzer" :initializationOptions (:checkOnSave (:command "clippy"))) '("rust")) ``` ``` -------------------------------- ### Start a Language Server with `eglot` Source: https://context7.com/joaotavora/eglot/llms.txt Interactively run `M-x eglot` to start a server for the current project. Programmatically, specify managed modes, project object, server class, contact (program/port), and language IDs. Supports TCP servers and initialization options. ```elisp ;; Interactive: open any source file and run M-x eglot ;; Eglot picks the server from eglot-server-programs automatically. ;; Programmatic: call with explicit arguments (eglot '(python-mode python-ts-mode) ; managed modes (project-current) ; project object 'eglot-lsp-server ; server class '("pylsp") ; contact: program + args '("python")) ; language IDs ;; With a TCP server (e.g. Godot built-in LSP on port 6008): (eglot '(gdscript-mode) (project-current) 'eglot-lsp-server '("localhost" 6008) '("gdscript")) ;; With initializationOptions: (eglot '(rust-mode rust-ts-mode) (project-current) 'eglot-lsp-server '("rust-analyzer" :initializationOptions (:checkOnSave (:command "clippy"))) '("rust")) ``` -------------------------------- ### Auto-start Eglot with `eglot-ensure` Source: https://context7.com/joaotavora/eglot/llms.txt Add `eglot-ensure` to major-mode hooks to automatically start an Eglot session for the current buffer. This is safe for modes that should always be LSP-managed. Eglot defers connection to `post-command-hook` to avoid slowing down file visits. ```elisp ;; Add to mode hooks so Eglot starts automatically: (add-hook 'python-mode-hook #'eglot-ensure) (add-hook 'rust-ts-mode-hook #'eglot-ensure) (add-hook 'go-ts-mode-hook #'eglot-ensure) ;; Eglot defers the actual connection to post-command-hook so it ;; doesn't slow down visiting a file. Only the first buffer per ;; project triggers a real server launch; subsequent buffers attach ;; to the already-running server at no extra cost. ``` -------------------------------- ### Trigger LSP Code Completion Programmatically Source: https://context7.com/joaotavora/eglot/llms.txt Example of programmatically triggering LSP textDocument/completion by inserting text and calling `completion-at-point`. ```elisp ;; Example: trigger completion programmatically (with-current-buffer (find-file-noselect "src/main.py") (goto-char (point-max)) (insert "import os\nos.") (completion-at-point)) ;; triggers LSP textDocument/completion ``` -------------------------------- ### Auto-starting on Mode Hooks - eglot-ensure Source: https://context7.com/joaotavora/eglot/llms.txt Ensures an Eglot session is started for the current buffer without prompting. This function is safe to add to major-mode hooks. ```APIDOC ## Auto-starting on Mode Hooks — `eglot-ensure` ### Description Ensures an Eglot session is started for the current buffer without prompting. Safe to add to major-mode hooks when you are certain every buffer of that mode should be LSP-managed. ### Method (eglot-ensure) ### Usage Add this function to the relevant major-mode hooks. ### Request Example ```elisp ;; Add to mode hooks so Eglot starts automatically: (add-hook 'python-mode-hook #'eglot-ensure) (add-hook 'rust-ts-mode-hook #'eglot-ensure) (add-hook 'go-ts-mode-hook #'eglot-ensure) ;; Eglot defers the actual connection to post-command-hook so it ;; doesn't slow down visiting a file. Only the first buffer per ;; project triggers a real server launch; subsequent buffers attach ;; to the already-running server at no extra cost. ``` ``` -------------------------------- ### Add GNU-Devel ELPA Repository Source: https://github.com/joaotavora/eglot/blob/master/README.md Configure GNU-Devel ELPA as an ELPA source repository to install the latest development version of Eglot. This is useful for users who want to use the most recent features. ```lisp (add-to-list 'package-archives '("gnu-devel" . "https://elpa.gnu.org/devel/")) ``` -------------------------------- ### Interactive Symbol Renaming with eglot-rename Source: https://context7.com/joaotavora/eglot/llms.txt Run `M-x eglot-rename` interactively to rename symbols. You can also bind this command to a key, for example, `C-c r` in `eglot-mode-map`. ```elisp ;; Interactive usage: place point on a symbol and run M-x eglot-rename ;; or bind it: (define-key eglot-mode-map (kbd "C-c r") #'eglot-rename) ``` ```elisp ;; Programmatic rename (bypasses interactive prompt): (eglot-rename "new_function_name") ``` ```elisp ;; Control whether edits require confirmation: (setq eglot-confirm-server-edits '((eglot-rename . nil) ; rename: apply silently (t . maybe-summary))) ; everything else: prompt if visiting buffers ``` ```elisp ;; Use diff view for all server-proposed edits: (setq eglot-confirm-server-edits 'diff) ``` -------------------------------- ### Enabling Inlay Hints with eglot-inlay-hints-mode Source: https://context7.com/joaotavora/eglot/llms.txt Use `eglot-inlay-hints-mode` to display LSP inlay hints (type annotations, parameter labels) as overlays. Enable it locally or globally via `eglot-managed-mode-hook`. ```elisp ;; Enable inlay hints in the current buffer: (eglot-inlay-hints-mode 1) ``` ```elisp ;; Enable globally for all managed buffers via the managed-mode hook: (add-hook 'eglot-managed-mode-hook (lambda () (when (eglot-server-capable :inlayHintProvider) (eglot-inlay-hints-mode 1)))) ``` ```elisp ;; Toggle hints while holding a key (approximates key-up binding): (define-key eglot-mode-map (kbd "C-c h") #'eglot-momentary-inlay-hints) ``` ```elisp ;; Inlay hint faces: ;; eglot-type-hint-face — type annotations (kind=1) ;; eglot-parameter-hint-face — parameter labels (kind=2) ;; eglot-inlay-hint-face — fallback (inherits shadow, height 0.8) (set-face-attribute 'eglot-inlay-hint-face nil :foreground "gray50") ``` -------------------------------- ### Inlay Hints Source: https://context7.com/joaotavora/eglot/llms.txt A minor mode to display LSP inlay hints (type annotations, parameter labels) as overlays. Supports global enabling and configuration of hint faces. ```APIDOC ## Inlay Hints — `eglot-inlay-hints-mode` A local minor mode that uses jit-lock to continuously display inlay hints (type annotations, parameter labels) provided by the LSP server as overlays. ```elisp ;; Enable inlay hints in the current buffer: (eglot-inlay-hints-mode 1) ;; Enable globally for all managed buffers via the managed-mode hook: (add-hook 'eglot-managed-mode-hook (lambda () (when (eglot-server-capable :inlayHintProvider) (eglot-inlay-hints-mode 1)))) ;; Toggle hints while holding a key (approximates key-up binding): (define-key eglot-mode-map (kbd "C-c h") #'eglot-momentary-inlay-hints) ;; Inlay hint faces: ;; eglot-type-hint-face — type annotations (kind=1) ;; eglot-parameter-hint-face — parameter labels (kind=2) ;; eglot-inlay-hint-face — fallback (inherits shadow, height 0.8) (set-face-attribute 'eglot-inlay-hint-face nil :foreground "gray50") ``` ``` -------------------------------- ### Extend Eglot with Hooks Source: https://context7.com/joaotavora/eglot/llms.txt Utilize `eglot-connect-hook`, `eglot-server-initialized-hook`, and `eglot-managed-mode-hook` for custom actions during server lifecycle and buffer management transitions. ```elisp ;; Run after a connection is established and server is initialized: (add-hook 'eglot-connect-hook (lambda (server) (message "Connected to %s" (jsonrpc-name server)))) ;; Run when the eglot-lsp-server instance is created (before connecting): (add-hook 'eglot-server-initialized-hook (lambda (server) (message "Server object created: %s" (eieio-object-class server)))) ;; Run after Eglot starts OR stops managing a buffer: (add-hook 'eglot-managed-mode-hook (lambda () (if (eglot-managed-p) (progn (eglot-inlay-hints-mode 1) (message "Eglot now managing %s" (buffer-name))) (message "Eglot stopped managing %s" (buffer-name))))) ``` -------------------------------- ### Configure `eglot-server-programs` Registry Source: https://context7.com/joaotavora/eglot/llms.txt Manage the alist mapping major modes to server contacts. Extend or override entries to add custom servers or pass extra flags. Use `eglot-alternatives` to try multiple servers sequentially. A custom contact function can be used for project-aware server selection. ```elisp ;; Add a new server for a custom major mode: (add-to-list 'eglot-server-programs '(my-lang-mode . ("my-lang-server" "--stdio"))) ;; Override the default Python server to always use pyright: (setq eglot-server-programs (cons '((python-mode python-ts-mode) . ("pyright-langserver" "--stdio")) (assoc-delete-all '(python-mode python-ts-mode) eglot-server-programs))) ;; Use eglot-alternatives to try multiple servers in order: (add-to-list 'eglot-server-programs `(kotlin-mode . ,(eglot-alternatives '("kotlin-language-server" ("java" "-jar" "/path/to/server.jar"))))) ;; Custom contact function for project-aware server selection: (add-to-list 'eglot-server-programs `(sml-mode . ,(lambda (_interactive project) (list "millet-ls" (project-root project))))) ``` -------------------------------- ### Configure Workspace Settings with eglot-workspace-configuration Source: https://context7.com/joaotavora/eglot/llms.txt Set server-specific configurations via a buffer-local variable, typically in .dir-locals.el. This sends LSP `workspace/didChangeConfiguration` notifications. Accepts a plist or a function. ```elisp ;; In .dir-locals.el at project root: ((nil . ((eglot-workspace-configuration . (:pylsp (:plugins (:jedi_completion (:include_params t :fuzzy t) :pylint (:enabled :json-false))) :gopls (:usePlaceholders t :gofumpt t)))))) ``` ```elisp ;; As a function (receives the eglot-lsp-server instance): ((python-mode . ((eglot-workspace-configuration . (lambda (server) `(:pylsp (:plugins (:pylint (:enabled ,(if (file-exists-p (expand-file-name ".pylintrc" (project-root (eglot--project server)))) t :json-false)))))))))) ``` ```elisp ;; Manually send updated configuration to the server: (eglot-signal-didChangeConfiguration (eglot-current-server)) ``` ```elisp ;; Inspect what Eglot will send (shows formatted JSON in a buffer): (eglot-show-workspace-configuration) ``` -------------------------------- ### Navigation with eglot-xref-backend Source: https://context7.com/joaotavora/eglot/llms.txt Leverage LSP for standard Emacs navigation commands like find definition (`M-.`) and find references (`M-?`). Eglot also provides specific commands for declaration, implementation, and type definition. ```elisp ;; Standard Emacs xref commands work automatically in managed buffers: ;; M-. => xref-find-definitions (textDocument/definition) ;; M-, => xref-pop-marker-stack ;; M-? => xref-find-references (textDocument/references) ;; C-M-. => xref-find-apropos (workspace/symbol) ``` ```elisp ;; Additional Eglot-specific navigation: (eglot-find-declaration) ; textDocument/declaration (eglot-find-implementation) ; textDocument/implementation (eglot-find-typeDefinition) ; textDocument/typeDefinition ``` ```elisp ;; Optional: activate Eglot in cross-referenced files outside the project: (setq eglot-extend-to-xref t) ``` ```elisp ;; Sort xref results with a custom comparator: (setq eglot-xref-lessp-function (lambda (a b) (string< (xref-item-summary a) (xref-item-summary b)))) ``` -------------------------------- ### Define Custom Server Subclass and Initialization Options Source: https://context7.com/joaotavora/eglot/llms.txt Subclass `eglot-lsp-server` to define custom initialization options for a specific server, such as rust-analyzer. This is useful for advanced configurations not covered by default settings. ```elisp (defclass my-rust-analyzer-server (eglot-lsp-server) () :documentation "Customized rust-analyzer connection.") (cl-defmethod eglot-initialization-options ((_server my-rust-analyzer-server)) '(:checkOnSave (:command "clippy") :cargo (:allFeatures t) :procMacro (:enable t))) ``` -------------------------------- ### Executing Code Actions with eglot-code-actions Source: https://context7.com/joaotavora/eglot/llms.txt Invoke `M-x eglot-code-actions` to request and execute LSP code actions. Use prefix arg to filter by action kind. Focused wrappers are available for common actions. ```elisp ;; Invoke interactively: M-x eglot-code-actions ;; With prefix arg, prompts for action kind filter. ;; Focused action commands (auto-generated wrappers): (eglot-code-action-quickfix) ; "quickfix" actions only (eglot-code-action-organize-imports) ; "source.organizeImports" (eglot-code-action-extract) ; "refactor.extract" (eglot-code-action-inline) ; "refactor.inline" (eglot-code-action-rewrite) ; "refactor.rewrite" ``` ```elisp ;; Programmatic: get list of available actions without executing: (eglot-code-actions (point) nil nil nil) ;; => ((:title "Fix import" :kind "quickfix" ...) ...) ``` ```elisp ;; Configure code action indicators: (setq eglot-code-action-indications '(eldoc-hint margin)) ;; Options: eldoc-hint, margin, nearby, mode-line ``` ```elisp ;; Bind in eglot-mode-map: (define-key eglot-mode-map (kbd "C-c a") #'eglot-code-actions) ``` -------------------------------- ### Navigation (Xref) Source: https://context7.com/joaotavora/eglot/llms.txt Integrates with Emacs's Xref system to route navigation commands like find definition and references through LSP. Provides additional commands for declaration, implementation, and type definition. ```APIDOC ## Navigation (Xref) — `eglot-xref-backend` Eglot registers an Xref backend that routes `M-.` (find definition), `M-?` (find references), and `M-x xref-find-apropos` through LSP. Extra navigation commands expose declaration, implementation, and type-definition. ```elisp ;; Standard Emacs xref commands work automatically in managed buffers: ;; M-. => xref-find-definitions (textDocument/definition) ;; M-, => xref-pop-marker-stack ;; M-? => xref-find-references (textDocument/references) ;; C-M-. => xref-find-apropos (workspace/symbol) ;; Additional Eglot-specific navigation: (eglot-find-declaration) ; textDocument/declaration (eglot-find-implementation) ; textDocument/implementation (eglot-find-typeDefinition) ; textDocument/typeDefinition ;; Optional: activate Eglot in cross-referenced files outside the project: (setq eglot-extend-to-xref t) ;; Sort xref results with a custom comparator: (setq eglot-xref-lessp-function (lambda (a b) (string< (xref-item-summary a) (xref-item-summary b)))) ``` ``` -------------------------------- ### Show Flymake Diagnostics Buffer Source: https://context7.com/joaotavora/eglot/llms.txt Display all diagnostics for the current buffer or the entire project in a dedicated buffer. ```elisp ;; Show all diagnostics in a list buffer: (flymake-show-buffer-diagnostics) ; current buffer (flymake-show-project-diagnostics) ; entire project ``` -------------------------------- ### Enable Semantic Token Highlighting Source: https://context7.com/joaotavora/eglot/llms.txt Use `eglot-semantic-tokens-mode` to enable richer syntax highlighting based on LSP semantic token data. It can be enabled locally or for all Eglot-managed buffers. ```elisp ;; Enable semantic token highlighting in the current buffer: (eglot-semantic-tokens-mode 1) ``` ```elisp ;; Enable for all managed buffers: (add-hook 'eglot-managed-mode-hook (lambda () (when (eglot-server-capable :semanticTokensProvider) (eglot-semantic-tokens-mode 1)))) ``` -------------------------------- ### Server Programs Registry - eglot-server-programs Source: https://context7.com/joaotavora/eglot/llms.txt An alist mapping major modes to server contacts. This can be extended or overridden to add custom servers or pass extra flags. ```APIDOC ## Server Programs Registry — `eglot-server-programs` ### Description An alist mapping major modes (or lists of modes) to server contacts. Extend or override entries to add custom servers or pass extra flags. ### Variable `eglot-server-programs` ### Usage Modify this variable to configure language servers for different major modes. ### Request Example ```elisp ;; Add a new server for a custom major mode: (add-to-list 'eglot-server-programs '(my-lang-mode . ("my-lang-server" "--stdio"))) ;; Override the default Python server to always use pyright: (setq eglot-server-programs (cons '((python-mode python-ts-mode) . ("pyright-langserver" "--stdio")) (assoc-delete-all '(python-mode python-ts-mode) eglot-server-programs))) ;; Use eglot-alternatives to try multiple servers in order: (add-to-list 'eglot-server-programs `(kotlin-mode . ,(eglot-alternatives '("kotlin-language-server" ("java" "-jar" "/path/to/server.jar"))))) ;; Custom contact function for project-aware server selection: (add-to-list 'eglot-server-programs `(sml-mode . ,(lambda (_interactive project) (list "millet-ls" (project-root project))))) ``` ``` -------------------------------- ### Check LSP Server Capabilities with eglot-server-capable Source: https://context7.com/joaotavora/eglot/llms.txt Query if the active LSP server supports a specific capability. Use this before calling potentially unsupported LSP features. Nested capabilities can also be checked. ```elisp ;; Check if the server supports hover docs: (when (eglot-server-capable :hoverProvider) (message "Server supports hover!")) ``` ```elisp ;; Check nested capability (completion trigger characters): (eglot-server-capable :completionProvider :triggerCharacters) ;; => (". ``` ```elisp ;; Signal an error if a capability is missing: (eglot-server-capable-or-lose :renameProvider) ;; => signals error: "Unsupported or ignored LSP capability `renameProvider'" ;; if the server doesn't support rename ``` ```elisp ;; Ignore a capability globally (disable document highlights): (add-to-list 'eglot-ignored-server-capabilities :documentHighlightProvider) ``` -------------------------------- ### Buffer Formatting with eglot-format-buffer and eglot-format Source: https://context7.com/joaotavora/eglot/llms.txt Format the current buffer or a specified region using LSP capabilities. Configure auto-formatting on save by adding `eglot-format-buffer` to `before-save-hook`. ```elisp ;; Format entire buffer: (eglot-format-buffer) ;; Or: M-x eglot-format-buffer ``` ```elisp ;; Format active region (interactively uses region bounds if active): (eglot-format (region-beginning) (region-end)) ``` ```elisp ;; Auto-format on save: (add-hook 'before-save-hook #'eglot-format-buffer nil t) ; buffer-local ``` ```elisp ;; Format respects buffer's indentation settings: ;; tab-width, indent-tabs-mode, require-final-newline, delete-trailing-lines ;; These are passed as LSP FormattingOptions automatically. ``` -------------------------------- ### Register Custom Server for Language Modes Source: https://context7.com/joaotavora/eglot/llms.txt Register a custom server subclass with Eglot for specific language modes using `eglot-server-programs`. This ensures Eglot uses your custom server when appropriate. ```elisp (add-to-list 'eglot-server-programs '((rust-mode rust-ts-mode) . (my-rust-analyzer-server "rust-analyzer"))) ``` -------------------------------- ### Convert Between URI and Path Source: https://context7.com/joaotavora/eglot/llms.txt Use `eglot-uri-to-path` and `eglot-path-to-uri` for seamless conversion between LSP URI strings and Emacs file paths, handling TRAMP, Windows paths, and percent-encoding. ```elisp ;; Convert LSP URI to a local file path: (eglot-uri-to-path "file:///home/user/project/src/main.py") ;; => "/home/user/project/src/main.py" ;; On a TRAMP remote buffer (e.g., /ssh:host:/path/file.py): ;; eglot-uri-to-path prepends the remote prefix automatically. ;; Convert a file path to an LSP URI: (eglot-path-to-uri "/home/user/project/src/main.py") ;; => "file:///home/user/project/src/main.py" ;; Windows path example: (eglot-path-to-uri "C:/Users/user/project/main.cs") ;; => "file:///C:/Users/user/project/main.cs" ``` -------------------------------- ### Configure Eglot Events Buffer Source: https://context7.com/joaotavora/eglot/llms.txt Customize the Eglot events buffer size and format using `eglot-events-buffer-config`. This allows for managing the buffer's memory footprint and the display format of events. ```elisp ;; Configure events buffer size and format: (setq eglot-events-buffer-config '(:size 4000000 ; 4 MB rolling buffer :format lisp)) ; pretty-printed Lisp instead of raw JSON ; other formats: 'full (default), 'short ``` -------------------------------- ### Code Actions Source: https://context7.com/joaotavora/eglot/llms.txt Request and execute LSP code actions such as quick-fixes and refactors. Provides focused wrappers for specific action kinds and programmatic access to action lists. ```APIDOC ## Code Actions — `eglot-code-actions` Request and execute LSP code actions (quick-fixes, refactors, source organizers) at the current position or region. Eglot also provides focused wrappers for specific action kinds. ```elisp ;; Invoke interactively: M-x eglot-code-actions ;; With prefix arg, prompts for action kind filter. ;; Focused action commands (auto-generated wrappers): (eglot-code-action-quickfix) ; "quickfix" actions only (eglot-code-action-organize-imports) ; "source.organizeImports" (eglot-code-action-extract) ; "refactor.extract" (eglot-code-action-inline) ; "refactor.inline" (eglot-code-action-rewrite) ; "refactor.rewrite" ;; Programmatic: get list of available actions without executing: (eglot-code-actions (point) nil nil nil) ;; => ((:title "Fix import" :kind "quickfix" ...) ...) ;; Configure code action indicators: (setq eglot-code-action-indications '(eldoc-hint margin)) ;; Options: eldoc-hint, margin, nearby, mode-line ;; Bind in eglot-mode-map: (define-key eglot-mode-map (kbd "C-c a") #'eglot-code-actions) ``` ``` -------------------------------- ### Buffer Formatting Source: https://context7.com/joaotavora/eglot/llms.txt Format the entire buffer or a selected region using LSP's `textDocument/formatting` or `textDocument/rangeFormatting`. Supports on-type formatting and respects buffer indentation settings. ```APIDOC ## Buffer Formatting — `eglot-format-buffer` / `eglot-format` Format the entire buffer or a region using the LSP server's `textDocument/formatting` or `textDocument/rangeFormatting` capabilities. On-type formatting is also supported. ```elisp ;; Format entire buffer: (eglot-format-buffer) ;; Or: M-x eglot-format-buffer ;; Format active region (interactively uses region bounds if active): (eglot-format (region-beginning) (region-end)) ;; Auto-format on save: (add-hook 'before-save-hook #'eglot-format-buffer nil t) ; buffer-local ;; Format respects buffer's indentation settings: ;; tab-width, indent-tabs-mode, require-final-newline, delete-trailing-lines ;; These are passed as LSP FormattingOptions automatically. ``` ``` -------------------------------- ### Opt Out of Eglot Managing Flymake Source: https://context7.com/joaotavora/eglot/llms.txt Add 'flymake' to `eglot-stay-out-of` to prevent Eglot from automatically managing Flymake mode. ```elisp ;; To opt out of Eglot managing Flymake: (add-to-list 'eglot-stay-out-of 'flymake) ``` -------------------------------- ### Display Call and Type Hierarchies Source: https://context7.com/joaotavora/eglot/llms.txt Use `eglot-show-call-hierarchy` and `eglot-show-type-hierarchy` to view interactive call or type hierarchies for symbols. The hierarchy buffer allows jumping to definitions and refreshing the tree. ```elisp ;; Show call hierarchy for function at point: ;; M-x eglot-show-call-hierarchy ;; With prefix arg, prompts for direction: incoming, outgoing, or both. ;; Show type hierarchy (supertypes / subtypes): ;; M-x eglot-show-type-hierarchy ;; In the hierarchy buffer: ;; RET or mouse-1 => jump to definition ;; mouse-3 => re-center tree on clicked node ;; (eglot-hierarchy-center-on-node) ;; g / revert => refresh the tree from scratch ;; Bind for convenience: (define-key eglot-mode-map (kbd "C-c C-h") #'eglot-show-call-hierarchy) (define-key eglot-mode-map (kbd "C-c C-t") #'eglot-show-type-hierarchy) ``` -------------------------------- ### Manage LSP Server Lifecycle Source: https://context7.com/joaotavora/eglot/llms.txt Control running LSP server processes using `eglot-shutdown`, `eglot-reconnect`, and `eglot-shutdown-all`. Eglot supports automatic reconnection and shutdown. ```elisp ;; Shut down the server for the current buffer: (eglot-shutdown (eglot-current-server)) ;; Interactive: M-x eglot-shutdown (prompts if multiple servers) ;; With prefix arg: preserves events/stderr buffers ;; Reconnect (useful after changing eglot-workspace-configuration): (eglot-reconnect (eglot-current-server)) ;; Shut down ALL active LSP servers: (eglot-shutdown-all) ;; Auto-reconnect after crash: reconnect only if session lasted > 5s (setq eglot-autoreconnect 5) ;; Auto-shutdown when last managed buffer is killed: (setq eglot-autoshutdown t) ;; Adjust connection timeouts: (setq eglot-connect-timeout 60) ; seconds before giving up (setq eglot-sync-connect 3) ; block for up to 3s, then background ``` -------------------------------- ### Handle Custom Server Notification Source: https://context7.com/joaotavora/eglot/llms.txt Override `eglot-handle-notification` to process custom notifications sent by a specific language server. This allows for custom actions based on server-specific events. ```elisp (cl-defmethod eglot-handle-notification ((_server my-rust-analyzer-server) (_method (eql rust-analyzer/publishDecorations)) &key &allow-other-keys) (message "rust-analyzer sent decorations")) ``` -------------------------------- ### Control Code Completion Caching in Eglot Source: https://context7.com/joaotavora/eglot/llms.txt Configure Eglot's session completion caching to improve performance. The default value is true. ```elisp ;; Control completion session caching (improves performance): (setq eglot-cache-session-completions t) ;; default t ``` -------------------------------- ### Inspect Eglot LSP Traffic Source: https://context7.com/joaotavora/eglot/llms.txt Use `eglot-events-buffer` and `eglot-stderr-buffer` to inspect live LSP traffic and server stderr output for debugging. `eglot-list-connections` shows all active connections. ```elisp ;; Show the JSON-RPC events log for the current server: (eglot-events-buffer (eglot-current-server)) ;; Buffer name: *EGLOT events* ``` ```elisp ;; Show server's stderr output: (eglot-stderr-buffer (eglot-current-server)) ``` ```elisp ;; List all active Eglot connections in a tabulated buffer: (eglot-list-connections) ;; Columns: Server name | Project | # buffers | Major modes | Command ``` ```elisp ;; Forget stuck pending requests: (eglot-forget-pending-continuations (eglot-current-server)) ``` ```elisp ;; Open Eglot's Info manual: (eglot-manual) ``` ```elisp ;; Update Eglot to the latest GNU-Devel ELPA version: (eglot-upgrade-eglot) ``` -------------------------------- ### Eglot Customization Variables Source: https://context7.com/joaotavora/eglot/llms.txt Configure Eglot's behavior using various customization variables. These settings control aspects like automatic reconnection, server shutdown, and progress reporting. ```elisp ;; Reconnect automatically if session lasted more than N seconds: (setq eglot-autoreconnect 3) ; integer, t, or nil ``` ```elisp ;; Shut down server when last managed buffer dies: (setq eglot-autoshutdown nil) ; boolean ``` ```elisp ;; Idle time before sending textDocument/didChange: (setq eglot-send-changes-idle-time 0.5) ; seconds ``` ```elisp ;; Prefer plaintext over Markdown in hover responses: (setq eglot-prefer-plaintext nil) ; boolean ``` ```elisp ;; Show server progress in mode line (vs *Messages*): (setq eglot-report-progress t) ; t | 'messages | nil ``` ```elisp ;; Suppress automatic cancellation notifications: (setq eglot-advertise-cancellation nil) ; boolean ``` ```elisp ;; Prevent Eglot from touching specific Emacs subsystems: (setq eglot-stay-out-of '(company xref flymake)) ``` ```elisp ;; Activate Eglot in xref-visited files outside project root: (setq eglot-extend-to-xref nil) ; boolean ``` ```elisp ;; Send LSP $/cancelRequest when Eglot forgets a request: (setq eglot-advertise-cancellation nil) ``` ```elisp ;; Mode line display string when Eglot is active: (setq eglot-menu-string "eglot") ``` ```elisp ;; Hide process ID from the server (useful in Docker): (setq eglot-withhold-process-id nil) ``` -------------------------------- ### Symbol Renaming Source: https://context7.com/joaotavora/eglot/llms.txt Interactively rename symbols across the workspace using `textDocument/rename`. Supports programmatic renaming and configuration for edit confirmation. ```APIDOC ## Symbol Renaming — `eglot-rename` Interactively rename the symbol at point across the entire workspace via `textDocument/rename`. Respects `eglot-confirm-server-edits` for confirmation before applying changes. ```elisp ;; Interactive usage: place point on a symbol and run M-x eglot-rename ;; or bind it: (define-key eglot-mode-map (kbd "C-c r") #'eglot-rename) ;; Programmatic rename (bypasses interactive prompt): (eglot-rename "new_function_name") ;; Control whether edits require confirmation: (setq eglot-confirm-server-edits '((eglot-rename . nil) ; rename: apply silently (t . maybe-summary))) ; everything else: prompt if visiting buffers ;; Use diff view for all server-proposed edits: (setq eglot-confirm-server-edits 'diff) ``` ``` -------------------------------- ### Disable Eglot Interference with Company Mode Source: https://context7.com/joaotavora/eglot/llms.txt Add 'company' to `eglot-stay-out-of` to prevent Eglot from interfering with Company mode's completion backend. ```elisp ;; Disable Eglot's interference with Company if desired: (add-to-list 'eglot-stay-out-of 'company) ``` -------------------------------- ### Convert LSP Range to Emacs Buffer Position Source: https://context7.com/joaotavora/eglot/llms.txt Use `eglot-range-region` to convert an LSP Range plist to Emacs buffer positions or markers. This is essential for custom LSP request handlers. ```elisp (eglot-range-region '(:start (:line 10 :character 4) :end (:line 10 :character 12))) ;; => (123 . 131) ; buffer positions ``` ```elisp ;; With markers instead of positions: (eglot-range-region range t) ;; => (# . #) ``` -------------------------------- ### Convert Emacs Buffer Position to LSP Range Source: https://context7.com/joaotavora/eglot/llms.txt Use `eglot-region-range` to convert Emacs buffer positions to an LSP Range plist. This is useful when constructing LSP requests that require range information. ```elisp ;; Convert Emacs buffer positions to an LSP Range plist: (eglot-region-range (region-beginning) (region-end)) ;; => (:start (:line 10 :character 4) :end (:line 10 :character 12)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.