dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

eglot.texi (58282B)


      1 \input texinfo  @c -*-texinfo-*-
      2 @c %**start of header
      3 @setfilename ../../eglot.info
      4 @settitle Eglot: The Emacs Client for the Language Server Protocol
      5 @include docstyle.texi
      6 @syncodeindex vr cp
      7 @syncodeindex fn cp
      8 @c %**end of header
      9 
     10 @copying
     11 This manual is for Eglot, the Emacs LSP client.
     12 
     13 Copyright @copyright{} 2022--2023 Free Software Foundation, Inc.
     14 
     15 @quotation
     16 Permission is granted to copy, distribute and/or modify this document
     17 under the terms of the GNU Free Documentation License, Version 1.3 or
     18 any later version published by the Free Software Foundation; with no
     19 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual'',
     20 and with the Back-Cover Texts as in (a) below.  A copy of the license
     21 is included in the section entitled ``GNU Free Documentation License''.
     22 
     23 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
     24 modify this GNU manual.''
     25 @end quotation
     26 @end copying
     27 
     28 @dircategory Emacs misc features
     29 @direntry
     30 * Eglot: (eglot).             Language Server Protocol client for Emacs.
     31 @end direntry
     32 
     33 @titlepage
     34 @sp 4
     35 @c The title is printed in a large font.
     36 @center @titlefont{User's Guide}
     37 @sp 1
     38 @center @titlefont{to}
     39 @sp 1
     40 @center @titlefont{Eglot: The Emacs LSP Client}
     41 @ignore
     42 @sp 2
     43 @center release 1.8
     44 @c -release-
     45 @end ignore
     46 @sp 3
     47 @center Jo@~ao T@'avora & Eli Zaretskii
     48 @c -date-
     49 
     50 @page
     51 @vskip 0pt plus 1filll
     52 @insertcopying
     53 @end titlepage
     54 
     55 @contents
     56 
     57 @ifnottex
     58 @node Top
     59 @top Eglot
     60 
     61 @cindex LSP
     62 @cindex language server protocol
     63 Eglot is the Emacs client for the @dfn{Language Server Protocol}
     64 (@acronym{LSP}).  The name ``Eglot'' is an acronym that stands for
     65 @ifhtml
     66 ``@emph{E}macs Poly@emph{glot}''.
     67 @end ifhtml
     68 @ifnothtml
     69 ``Emacs polyGLOT''.
     70 @end ifnothtml
     71 @footnote{
     72 A @dfn{polyglot} is a
     73 person who is able to use several languages.
     74 } Eglot provides infrastructure and a set of commands for enriching
     75 the source code editing capabilities of Emacs via LSP@.  LSP is a
     76 standardized communications protocol between source code editors (such
     77 as Emacs) and language servers---programs external to Emacs which
     78 analyze the source code on behalf of Emacs.  The protocol allows Emacs
     79 to receive various source code services from the server, such as
     80 description and location of function calls, types of variables, class
     81 definitions, syntactic errors, etc.  This way, Emacs doesn't need to
     82 implement the language-specific parsing and analysis capabilities in
     83 its own code, but is still capable of providing sophisticated editing
     84 features that rely on such capabilities, such as automatic code
     85 completion, go-to definition of function/class, documentation of
     86 symbol at-point, refactoring, on-the-fly diagnostics, and more.
     87 
     88 Eglot itself is completely language-agnostic, but it can support any
     89 programming language for which there is a language server and an Emacs
     90 major mode.
     91 
     92 This manual documents how to configure, use, and customize Eglot.
     93 
     94 @insertcopying
     95 
     96 @menu
     97 * Quick Start::                 For the impatient.
     98 * Eglot and LSP Servers::       How to work with language servers.
     99 * Using Eglot::                 Important Eglot commands and variables.
    100 * Customizing Eglot::           Eglot customization and advanced features.
    101 * Advanced server configuration::  Fine-tune a specific language server
    102 * Troubleshooting Eglot::       Troubleshooting and reporting bugs.
    103 * GNU Free Documentation License::  The license for this manual.
    104 * Index::
    105 @end menu
    106 @end ifnottex
    107 
    108 @node Quick Start
    109 @chapter Quick Start
    110 @cindex quick start
    111 
    112 This chapter provides concise instructions for setting up and using
    113 Eglot with your programming project in common usage scenarios.  For
    114 more detailed instructions regarding Eglot setup, @pxref{Eglot and LSP
    115 Servers}.  @xref{Using Eglot}, for detailed description of using Eglot,
    116 and see @ref{Customizing Eglot}, for adapting Eglot to less common use
    117 patterns.
    118 
    119 Here's how to start using Eglot with your programming project:
    120 
    121 @enumerate
    122 @item
    123 Select and install a language server.
    124 
    125 Eglot comes pre-configured with many popular language servers, see the
    126 value of @code{eglot-server-programs}.  If the server(s) mentioned
    127 there satisfy your needs for the programming language(s) with which
    128 you want to use Eglot, you just need to make sure those servers are
    129 installed on your system.  Alternatively, install one or more servers
    130 of your choice and add them to the value of
    131 @code{eglot-server-programs}, as described in @ref{Setting Up LSP
    132 Servers}.
    133 
    134 @item
    135 Turn on Eglot for your project.
    136 
    137 To start using Eglot for a project, type @kbd{M-x eglot @key{RET}} in
    138 a buffer visiting any file that belongs to the project.  This starts
    139 the language server configured for the programming language of that
    140 buffer, and causes Eglot to start managing all the files of the
    141 project which use the same programming language.  The notion of a
    142 ``project'' used by Eglot is the same Emacs uses (@pxref{Projects,,,
    143 emacs, GNU Emacs Manual}): in the simplest case, the ``project'' is
    144 the single file you are editing, but it can also be all the files in a
    145 single directory or a directory tree under some version control
    146 system, such as Git.
    147 
    148 Alternatively, you can start Eglot automatically from the major-mode
    149 hook of the mode used for the programming language; see @ref{Starting
    150 Eglot}.
    151 
    152 @item
    153 Use Eglot.
    154 
    155 Most Eglot facilities are integrated into Emacs features, such as
    156 ElDoc, Flymake, Xref, and Imenu.  However, Eglot also provides
    157 commands of its own, mainly to perform tasks by the language server,
    158 such as @kbd{M-x eglot-rename} (to rename an identifier across the
    159 entire project), @kbd{M-x eglot-format} (to reformat and reindent
    160 code), and some others.  @xref{Eglot Commands}, for the detailed list
    161 of Eglot commands.
    162 
    163 @item
    164 That's it!
    165 @end enumerate
    166 
    167 @node Eglot and LSP Servers
    168 @chapter Eglot and LSP Servers
    169 
    170 This chapter describes how to set up Eglot for your needs, and how to
    171 start it.
    172 
    173 @menu
    174 * Setting Up LSP Servers::   How to configure LSP servers for your needs.
    175 * Starting Eglot::              Ways of starting Eglot for your project.
    176 * Shutting Down LSP Servers::
    177 @end menu
    178 
    179 @node Setting Up LSP Servers
    180 @section Setting Up LSP Servers
    181 @cindex setting up LSP server for Eglot
    182 @cindex LSP server for Eglot, setting up
    183 @cindex language server for Eglot
    184 
    185 For Eglot to be useful, it must first be combined with a suitable
    186 language server.  Usually, that means running the server program
    187 locally as a child process of Emacs (@pxref{Processes,,, elisp, GNU
    188 Emacs Lisp Reference Manual}) and communicating with it via the
    189 standard input and output streams.
    190 
    191 The language server program must be installed separately, and is not
    192 further discussed in this manual; refer to the documentation of the
    193 particular server(s) you want to install.
    194 
    195 To use a language server, Eglot must know how to start it and which
    196 programming languages each server supports.  This information is
    197 provided by the variable @code{eglot-server-programs}.
    198 
    199 @defvar eglot-server-programs
    200 This variable associates major modes with names and command-line
    201 arguments of the language server programs corresponding to the
    202 programming language of each major mode.  It provides all the
    203 information that Eglot needs to know about the programming language of
    204 the source you are editing.
    205 
    206 The value of the variable is an alist, whose elements are of the form
    207 @w{@code{(@var{major-mode} . @var{server})}}.
    208 
    209 The @var{major-mode} of the alist elements can be either a symbol of
    210 an Emacs major mode or a list of the form @w{@code{(@var{mode}
    211 :language-id @var{id})}}, with @var{mode} being a major-mode symbol
    212 and @var{id} a string that identifies the language to the server (if
    213 Eglot cannot by itself convert the major-mode to the language
    214 identifier string required by the server).  In addition,
    215 @var{major-mode} can be a list of several major modes specified in one
    216 of the above forms -- this means a running instance of the associated
    217 server is responsible for files of multiple major modes or languages
    218 in the project.
    219 
    220 The @var{server} part of the alist elements can be one of the
    221 following:
    222 
    223 @table @code
    224 @item (@var{program} @var{args}@dots{})
    225 This says to invoke @var{program} with zero or more arguments
    226 @var{args}; the program is expected to communicate with Emacs via the
    227 standard input and standard output streams.
    228 
    229 @item (@var{program} @var{args}@dots{} :initializationOptions @var{options}@dots{})
    230 @var{program} is invoked with @var{args} but @var{options} specifies
    231 how to construct the @samp{:initializationOptions} JSON object to pass
    232 the server on during the LSP handshake (@pxref{Advanced server
    233 configuration}).
    234 
    235 @item (@var{host} @var{port} @var{args}@dots{})
    236 Here @var{host} is a string and @var{port} is a positive integer
    237 specifying a TCP connection to a remote server.  The @var{args} are
    238 passed to @code{open-network-stream}, e.g.@: if the connection needs
    239 to use encryption or other non-default parameters (@pxref{Network,,,
    240 elisp, GNU Emacs Lisp Reference Manual}).
    241 
    242 @item (@var{program} @var{args}@dots{} :autoport @var{moreargs}@dots{})
    243 @var{program} is started with a command line constructed from
    244 @var{args} followed by an available server port and the rest of
    245 arguments in @var{moreargs}; Eglot then establishes a TCP connection
    246 with the server via that port on the local host.
    247 
    248 @item @var{function}
    249 This should be a function of a single argument: non-@code{nil} if the
    250 connection was requested interactively (e.g., by the @code{eglot}
    251 command), otherwise @code{nil}.  The function should return a value of
    252 any of the forms described above.  This allows interaction with the
    253 user for determining the program to start and its command-line
    254 arguments.
    255 @end table
    256 
    257 @end defvar
    258 
    259 Eglot comes with a fairly complete set of associations of major-modes
    260 to popular language servers predefined.  If you need to add server
    261 associations to the default list, use @code{add-to-list}.  For
    262 example, if there is a hypothetical language server program
    263 @command{fools} for the language @code{Foo} which is supported by an
    264 Emacs major-mode @code{foo-mode}, you can add it to the alist like
    265 this:
    266 
    267 @lisp
    268 (with-eval-after-load 'eglot
    269   (add-to-list 'eglot-server-programs
    270                '(foo-mode . ("fools" "--stdio"))))
    271 @end lisp
    272 
    273 This will invoke the program @command{fools} with the command-line
    274 argument @option{--stdio} in support of editing source files for which
    275 Emacs turns on @code{foo-mode}, and will communicate with the program
    276 via the standard streams.  As usual with invoking programs, the
    277 executable file @file{fools} should be in one of the directories
    278 mentioned by the @code{exec-path} variable (@pxref{Subprocess
    279 Creation,,, elisp, GNU Emacs Lisp Reference Manual}), for Eglot to be
    280 able to find it.
    281 
    282 Sometimes, multiple servers are acceptable alternatives for handling a
    283 given major-mode.  In those cases, you may combine the helper function
    284 @code{eglot-alternatives} with the functional form of
    285 @code{eglot-server-programs}.
    286 
    287 @lisp
    288 (with-eval-after-load 'eglot
    289   (add-to-list 'eglot-server-programs
    290                `(foo-mode . ,(eglot-alternatives
    291                                '(("fools" "--stdio")
    292                                  ("phewls" "--fast"))))))
    293 @end lisp
    294 
    295 If you have @command{fools} and @command{phewls} installed, the
    296 function produced by @code{eglot-alternatives} will prompt for the
    297 server to use in @code{foo-mode} buffers.  Else it will use whichever
    298 is available.
    299 
    300 @node Starting Eglot
    301 @section Starting Eglot
    302 @cindex starting Eglot
    303 @cindex activating Eglot for a project
    304 
    305 @findex eglot
    306 The most common way to start Eglot is to simply visit a source file of
    307 a given language and use the command @kbd{M-x eglot}.  This starts the
    308 language server suitable for the visited file's major-mode, and
    309 attempts to connect to it.  If the connection to the language server
    310 is successful, you will see the @code{[eglot:@var{project}]} indicator
    311 on the mode line which reflects the server that was started.  If the
    312 server program couldn't be started or connection to it failed, you
    313 will see an error message; in that case, try to troubleshoot the
    314 problem as described in @ref{Troubleshooting Eglot}.  Once a language
    315 server was successfully started and Eglot connected to it, you can
    316 immediately start using the Emacs features supported by Eglot, as
    317 described in @ref{Eglot Features}.
    318 
    319 A single Eglot session for a certain major-mode usually serves all the
    320 buffers under that mode which visit files from the same project, so
    321 you don't need to invoke @kbd{M-x eglot} again when you visit another
    322 file from the same project which is edited using the same major-mode.
    323 This is because Eglot uses the Emacs project infrastructure, as
    324 described in @ref{Eglot and Buffers}, and this knows about files that
    325 belong to the same project.  Thus, after starting an Eglot session for
    326 some buffer, that session is automatically reused when visiting files
    327 in the same project with the same major-mode.
    328 
    329 @findex eglot-ensure
    330 Alternatively, you could configure Eglot to start automatically for
    331 one or more major-modes from the respective mode hooks.  Here's an
    332 example for a hypothetical @code{foo-mode}:
    333 
    334 @lisp
    335  (add-hook 'foo-mode-hook 'eglot-ensure)
    336 @end lisp
    337 
    338 @noindent
    339 The function @code{eglot-ensure} will start an Eglot session for each
    340 buffer in which @code{foo-mode} is turned on, if there isn't already
    341 an Eglot session that handles the buffer.  Note that this variant of
    342 starting an Eglot session is non-interactive, so it should be used
    343 only when you are confident that Eglot can be started reliably for any
    344 file which may be visited with the major-mode in question.
    345 
    346 When Eglot connects to a language server for the first time in an
    347 Emacs session, it runs the hook @code{eglot-connect-hook}
    348 (@pxref{Eglot Variables}).
    349 
    350 @node Shutting Down LSP Servers
    351 @section Shutting Down LSP Servers
    352 @cindex shutting down LSP server
    353 
    354 When Eglot is turned on, it arranges for turning itself off
    355 automatically if the language server process terminates.  Turning off
    356 Eglot means that it shuts down the server connection, ceases its
    357 management of all the buffers that use the server connection which was
    358 terminated, deactivates its minor mode, and restores the original
    359 values of the Emacs variables that Eglot changed when it was turned
    360 on.  @xref{Eglot and Buffers}, for more details of what Eglot
    361 management of a buffer entails.
    362 
    363 @findex eglot-shutdown
    364 You can also shut down a language server manually, by using the
    365 command @kbd{M-x eglot-shutdown}.  This prompts for the server (unless
    366 there's only one connection and it's used in the current buffer), and
    367 then shuts it down.  By default, it also kills the server's events
    368 buffer (@pxref{Troubleshooting Eglot}), but a prefix argument prevents
    369 that.
    370 
    371 Alternatively, you can customize the variable
    372 @code{eglot-autoshutdown} to a non-@code{nil} value, in which case
    373 Eglot will automatically shut down the language server process when
    374 the last buffer served by that language server is killed.  The default
    375 of this variable is @code{nil}, so that visiting another file would
    376 automatically activate Eglot even when the project which started Eglot
    377 with the server no longer has any buffer associated with it.  This
    378 default allows you to start a server only once in each Emacs session.
    379 
    380 @node Using Eglot
    381 @chapter Using Eglot
    382 
    383 This chapter describes in detail the features that Eglot provides and
    384 how it does that.  It also provides reference sections for Eglot
    385 commands and variables.
    386 
    387 @menu
    388 * Eglot Features::
    389 * Eglot and Buffers::
    390 * Eglot Commands::
    391 * Eglot Variables::
    392 @end menu
    393 
    394 @node Eglot Features
    395 @section Eglot Features
    396 @cindex features in buffers supported by Eglot
    397 
    398 Once Eglot is enabled in a buffer, it uses LSP and the language-server
    399 capabilities to activate, enable, and enhance modern IDE features in
    400 Emacs.  The features themselves are usually provided via other Emacs
    401 packages.  Here's the list of the main features that Eglot enables and
    402 provides:
    403 
    404 @itemize @bullet
    405 @item
    406 At-point documentation: when point is at or near a symbol or an
    407 identifier, the information about the symbol/identifier, such as the
    408 signature of a function or class method and server-generated
    409 diagnostics, is made available via the ElDoc package (@pxref{Lisp
    410 Doc,,, emacs, GNU Emacs Manual}).  This allows major modes to provide
    411 extensive help and documentation about the program identifiers.
    412 
    413 @item
    414 On-the-fly diagnostic annotations with server-suggested fixes, via the
    415 Flymake package (@pxref{Top,,, flymake, GNU Flymake manual}).  This
    416 improves and enhances the Flymake diagnostics, replacing the other
    417 Flymake backends.
    418 
    419 @item
    420 Finding definitions and uses of identifiers, via Xref (@pxref{Xref,,,
    421 emacs, GNU Emacs Manual}).  Eglot provides a backend for the Xref
    422 capabilities which uses the language-server understanding of the
    423 program source.  In particular, it eliminates the need to generate
    424 tags tables (@pxref{Tags tables,,, emacs, GNU Emacs Manual}) for
    425 languages which are only supported by the @code{etags} backend.
    426 
    427 @item
    428 Buffer navigation by name of function, class, method, etc., via Imenu
    429 (@pxref{Imenu,,, emacs, GNU Emacs Manual}).  Eglot provides its own
    430 variant of @code{imenu-create-index-function}, which generates the
    431 index for the buffer based on language-server program source analysis.
    432 
    433 @item
    434 Enhanced completion of symbol at point by the
    435 @code{completion-at-point} command (@pxref{Symbol Completion,,, emacs,
    436 GNU Emacs Manual}).  This uses the language-server's parser data for
    437 the completion candidates.
    438 
    439 @item
    440 Automatic reformatting of source code as you type it.  This is similar
    441 to what the @code{eglot-format} command does (see below), but is
    442 activated automatically as you type.
    443 
    444 @item
    445 If a completion package such as @code{company-mode}, a popular
    446 third-party completion package (or any other completion package), is
    447 installed, Eglot enhances it by providing completion candidates based
    448 on the language-server analysis of the source code.
    449 (@code{company-mode} can be installed from GNU ELPA.)
    450 
    451 @item
    452 If @code{yasnippet}, a popular third-party package for automatic
    453 insertion of code templates (snippets), is installed, and the language
    454 server supports snippet completion candidates, Eglot arranges for the
    455 completion package to instantiate these snippets using
    456 @code{yasnippet}.  (@code{yasnippet} can be installed from GNU ELPA.)
    457 
    458 @item
    459 If the popular third-party package @code{markdown-mode} is installed,
    460 and the server provides at-point documentation formatted as Markdown
    461 in addition to plain text, Eglot arranges for the ElDoc package to
    462 enrich this text with fontifications and other nice formatting before
    463 displaying it to the user.  This makes the documentation shown by
    464 ElDoc look nicer on display.
    465 
    466 @item
    467 In addition to enabling and enhancing other features and packages,
    468 Eglot also provides a number of user commands based on the
    469 capabilities of language servers.  Examples include renaming symbols
    470 with @kbd{eglot-rename} and asking to automatically correct problems
    471 with @kbd{eglot-code-actions}.  @xref{Eglot Commands}.
    472 @end itemize
    473 
    474 Not all servers support the full set of LSP capabilities, but most of
    475 them support enough to enable the basic set of features mentioned
    476 above.  Conversely, some servers offer capabilities for which no
    477 equivalent Emacs package exists yet, and so Eglot cannot (yet) expose
    478 these capabilities to Emacs users.
    479 
    480 @node Eglot and Buffers
    481 @section Buffers, Projects, and Eglot
    482 @cindex buffers managed by Eglot
    483 @cindex projects and Eglot
    484 
    485 @cindex workspace
    486 One of the main strong points of using a language server is that a
    487 language server has a broad view of the program: it considers more
    488 than just the single source file you are editing.  Ideally, the
    489 language server should know about all the source files of your program
    490 which are written in the language supported by the server.  In the
    491 language-server parlance, the set of the source files of a program is
    492 known as a @dfn{workspace}.  The Emacs equivalent of a workspace is a
    493 @dfn{project} (@pxref{Projects,,, emacs, GNU Emacs Manual}).  Eglot
    494 fully supports Emacs projects, and considers the file in whose buffer
    495 Eglot is turned on as belonging to a project.  In the simplest case,
    496 that file is the entire project, i.e.@: your project consists of a
    497 single file.  But there are other more complex projects:
    498 
    499 @itemize @bullet
    500 @item
    501 A single-directory project: several source files in a single common
    502 directory.
    503 
    504 @item
    505 A VC project: source files in a directory hierarchy under some VCS,
    506 e.g.@: a VCS repository (@pxref{Version Control,,, emacs, GNU Emacs
    507 Manual}).
    508 
    509 @item
    510 An EDE project: source files in a directory hierarchy managed via the
    511 Emacs Development Environment (@pxref{EDE,,, emacs, GNU Emacs
    512 Manual}).
    513 @end itemize
    514 
    515 Eglot uses Emacs's project management infrastructure to figure out
    516 which files and buffers belong to what project, so any kind of project
    517 supported by that infrastructure is automatically supported by Eglot.
    518 
    519 When Eglot starts a server program, it does so in the project's root
    520 directory, which is usually the top-level directory of the project's
    521 directory hierarchy.  This ensures the language server has the same
    522 comprehensive view of the project's files as you do.
    523 
    524 For example, if you visit the file @file{~/projects/fooey/lib/x.foo}
    525 and @file{x.foo} belongs to a project rooted at
    526 @file{~/projects/fooey} (perhaps because a @file{.git} directory
    527 exists there), then @kbd{M-x eglot} causes the server program to start
    528 with that root as the current working directory.  The server then will
    529 analyze not only the file @file{lib/x.foo} you visited, but likely
    530 also all the other @file{*.foo} files under the
    531 @file{~/projects/fooey} directory.
    532 
    533 In some cases, additional information specific to a given project will
    534 need to be provided to the language server when starting it.  The
    535 variable @code{eglot-workspace-configuration} (@pxref{Customizing
    536 Eglot}) exists for that purpose.  It specifies the parameters and
    537 their values to communicate to each language server which needs that.
    538 
    539 When Eglot is active for a project, it performs several background
    540 activities on behalf of the project and its buffers:
    541 
    542 @itemize @bullet
    543 @cindex mode-line indication of language server
    544 @cindex mouse clicks on mode-line, and Eglot
    545 @vindex eglot-menu
    546 @item
    547 All of the project's file-visiting buffers under the same major-mode
    548 are served by a single language-server connection.  (If the project
    549 uses several programming languages, there will usually be a separate
    550 server connection for each group of files written in the same language
    551 and using the same Emacs major-mode.)  Eglot adds the
    552 @samp{[eglot:@var{project}]} indication to the mode line of
    553 each such buffer, where @var{server} is the name of the server and
    554 @var{project} identifies the project by its root directory.  Clicking
    555 the mouse on the Eglot mode-line indication activates a menu with
    556 server-specific items.
    557 
    558 @item
    559 For each buffer in which Eglot is active, it notifies the language
    560 server that Eglot is @dfn{managing} the file visited by that buffer.
    561 This tells the language server that the file's contents on disk may no
    562 longer be up-to-date due to unsaved edits.  Eglot reports to the
    563 server any changes in the text of each managed buffer, to make the
    564 server aware of unsaved changes.  This includes your editing of the
    565 buffer and also changes done automatically by other Emacs features and
    566 commands.  Killing a buffer relinquishes its management by Eglot and
    567 notifies the server that the file on disk is up-to-date.
    568 
    569 @vindex eglot-managed-mode-hook
    570 @vindex eglot-managed-p
    571 @item
    572 Eglot turns on a special minor mode in each buffer it manages.  This
    573 minor mode ensures the server is notified about files Eglot manages,
    574 and also arranges for other Emacs features supported by Eglot
    575 (@pxref{Eglot Features}) to receive information from the language
    576 server, by changing the settings of these features.  Unlike other
    577 minor-modes, this special minor mode is not activated manually by the
    578 user, but automatically, as the result of starting an Eglot session
    579 for the buffer.  However, this minor mode provides a hook variable
    580 @code{eglot-managed-mode-hook} that can be used to customize the Eglot
    581 management of the buffer.  This hook is run both when the minor mode
    582 is turned on and when it's turned off; use the variable
    583 @code{eglot-managed-p} to tell if current buffer is still being
    584 managed or not.  When Eglot stops managing the buffer, this minor mode
    585 is turned off, and all the settings that Eglot changed are restored to
    586 their original values.
    587 
    588 @item
    589 When you visit a file under the same project, whether an existing or a
    590 new file, its buffer is automatically added to the set of buffers
    591 managed by Eglot, and the server which supports the buffer's
    592 major-mode is notified about that.  Thus, visiting a non-existent file
    593 @file{/home/joe/projects/fooey/lib/y.foo} in the above example will
    594 notify the server of the @file{*.foo} files' language that a new file
    595 was added to the project, even before the file appears on disk.  The
    596 special Eglot minor mode is also turned on automatically in the buffer
    597 visiting the file.
    598 @end itemize
    599 
    600 @node Eglot Commands
    601 @section Eglot Commands
    602 @cindex commands, Eglot
    603 
    604 This section provides a reference for the most commonly used Eglot
    605 commands:
    606 
    607 @ftable @code
    608 @item M-x eglot
    609 This command adds the current buffer and the file it visits to the
    610 group of buffers and files managed by Eglot on behalf of a suitable
    611 language server.  If a language server for the buffer's
    612 @code{major-mode} (@pxref{Major Modes,,, emacs, GNU Emacs Manual}) is
    613 not yet running, it will be started; otherwise the buffer and its file
    614 will be added to those managed by an existing server session.
    615 
    616 The command attempts to figure out the buffer's major mode and the
    617 suitable language server; in case it fails, it might prompt for the
    618 major mode to use and for the server program to start.  If invoked
    619 with @kbd{C-u}, it always prompts for the server program, and if
    620 invoked with @kbd{C-u C-u}, it also prompts for the major mode.
    621 
    622 If the language server is successfully started and contacted, this
    623 command arranges for any other buffers belonging to the same project
    624 and using the same major mode to use the same language-server session.
    625 That includes any buffers created by visiting files after this command
    626 succeeds to connect to a language server.
    627 
    628 All the Emacs features that are capable of using Eglot services
    629 (@pxref{Eglot Features}) are automatically configured by this command
    630 to start using the language server via Eglot.  To customize which
    631 Emacs features will be configured to use Eglot, use the
    632 @code{eglot-stay-out-of} option (@pxref{Customizing Eglot}).
    633 
    634 @item M-x eglot-reconnect
    635 This command shuts down the current connection to the language
    636 server and immediately restarts it using the same options used
    637 originally.  This can sometimes be useful to unclog a partially
    638 malfunctioning server connection.
    639 
    640 @item M-x eglot-shutdown
    641 This command shuts down a language server.  It prompts for a language
    642 server to shut down (unless there's only one server session, and it
    643 manages the current buffer).  Then the command shuts down the server
    644 and stops managing the buffers the server was used for.  Emacs
    645 features (@pxref{Eglot Features}) that Eglot configured to work with
    646 the language server are restored back to their original configuration.
    647 
    648 Normally, this command kills the buffers used for communicating with
    649 the language server, but if invoked with a prefix argument @kbd{C-u},
    650 the command doesn't kill those buffers, allowing them to be used for
    651 diagnostics and problem reporting (@pxref{Troubleshooting Eglot}).
    652 
    653 @item M-x eglot-shutdown-all
    654 This command shuts down all the language servers active in the current
    655 Emacs session.  As with @code{eglot-shutdown}, invoking this command
    656 with a prefix argument avoids killing the buffers used for
    657 communications with the language servers.
    658 
    659 @item M-x eglot-rename
    660 This command renames the program symbol (a.k.a.@: @dfn{identifier}) at
    661 point to another name.  It prompts for the new name of the symbol, and
    662 then modifies all the files in the project which are managed by the
    663 language server of the current buffer to implement the renaming.
    664 
    665 @item M-x eglot-format
    666 This command reformats the active region according to the
    667 language-server rules.  If no region is active, it reformats the
    668 entire current buffer.
    669 
    670 @item M-x eglot-format-buffer
    671 This command reformats the current buffer, in the same manner as
    672 @code{eglot-format} does.
    673 
    674 @cindex code actions
    675 @item M-x eglot-code-actions
    676 @itemx M-x eglot-code-action-organize-imports
    677 @itemx M-x eglot-code-action-quickfix
    678 @itemx M-x eglot-code-action-extract
    679 @itemx M-x eglot-code-action-inline
    680 @itemx M-x eglot-code-action-rewrite
    681 These commands allow you to invoke the so-called @dfn{code actions}:
    682 requests for the language server to provide editing commands for
    683 correcting, refactoring or beautifying your code.  These commands may
    684 affect more than one visited file belong to the project.
    685 
    686 The command @code{eglot-code-actions} asks the server if there any
    687 code actions for any point in the buffer or contained in the active
    688 region.  If there are, you the choice to execute one of them via the
    689 minibuffer.
    690 
    691 A common use of code actions is fixing the Flymake error diagnostics
    692 issued by Eglot (@pxref{Top,,, flymake, GNU Flymake manual}).
    693 Clicking on a diagnostic with @kbd{mouse-2} invokes
    694 @code{eglot-code-actions-at-mouse} which pops up a menu of available
    695 code actions.  The variable @code{eglot-diagnostics-map} can be used
    696 to control the mouse binding.
    697 
    698 Other commands execute a specific code action.  For example,
    699 @code{eglot-code-action-organize-imports} rearranges the program's
    700 @dfn{imports}---declarations of modules whose capabilities the program
    701 uses.
    702 
    703 @cindex inlay hints
    704 @item M-x eglot-inlay-hints-mode
    705 This command toggles LSP @dfn{inlay hints} on and off for the current
    706 buffer.  Inlay hints are small text annotations to specific parts of
    707 the whole buffer, not unlike diagnostics, but designed to help
    708 readability instead of indicating problems.  For example, a C++
    709 language server can serve hints about positional parameter names in
    710 function calls and a variable's automatically deduced type.  Inlay
    711 hints help the user not have to remember these things by heart.
    712 @end ftable
    713 
    714 The following Eglot commands are used less commonly, mostly for
    715 diagnostic and troubleshooting purposes:
    716 
    717 @ftable @code
    718 @item M-x eglot-events-buffer
    719 This command pops up the events buffer used for communication with the
    720 language server of the current buffer.
    721 
    722 @item M-x eglot-stderr-buffer
    723 This command pops up the buffer with the debug info printed by the
    724 language server to its standard error stream.
    725 
    726 @item M-x eglot-forget-pending-continuations
    727 Forget pending requests for the server of the current buffer.
    728 @c FIXME: Better description of the need.
    729 
    730 @item M-x eglot-signal-didChangeConfiguration
    731 This command updates the language server configuration according to
    732 the current value of the variable @code{eglot-workspace-configuration}
    733 (@pxref{Customizing Eglot}).
    734 
    735 @item M-x eglot-clear-status
    736 Clear the last JSONRPC error for the server of the current buffer.
    737 Eglot keeps track of erroneous situations encountered by the server in
    738 its mode-line indication so that the user may inspect the
    739 communication leading up to it (@pxref{Troubleshooting Eglot}).  If
    740 the situation is deemed uninteresting or temporary, this command can
    741 be used to ``forget'' the error.  Note that the command @code{M-x
    742 eglot-reconnect} can sometimes be used to unclog a temporarily
    743 malfunctioning server.
    744 @end ftable
    745 
    746 As described in @ref{Eglot Features} most features associated with
    747 Eglot are actually provided by other Emacs packages and features, and
    748 Eglot only enhances them by allowing them to use the information
    749 coming from the language servers.  For completeness, here's the list
    750 of commands of those other packages that are very commonly used in
    751 Eglot-managed buffers:
    752 
    753 @c Not @ftable, because the index entries should mention Eglot
    754 @table @code
    755 @cindex eldoc, and Eglot
    756 @cindex documentation using Eglot
    757 @item M-x eldoc
    758 Ask the ElDoc system for help at point.
    759 
    760 @cindex flymake, and Eglot
    761 @cindex on-the-fly diagnostics using Eglot
    762 @item M-x flymake-show-buffer-diagnostics
    763 Ask Flymake system to display diagnostics for the current buffer.
    764 
    765 @item M-x flymake-show-project-diagnostics
    766 Ask Flymake to list diagnostics for all the files in the current
    767 project.
    768 
    769 @cindex xref, and Eglot
    770 @cindex finding definitions of identifiers using Eglot
    771 @item M-x xref-find-definitions
    772 Ask Xref to go the definition of the identifier at point.
    773 
    774 @cindex imenu navigation using Eglot
    775 @item M-x imenu
    776 Let the user navigate the program source code using buffer index,
    777 categorizing program elements by syntactic class (class, method,
    778 variable, etc.) and offering completion.
    779 
    780 @cindex symbol completion using Eglot
    781 @item M-x completion-at-point
    782 Request completion of the symbol at point.
    783 @end table
    784 
    785 @node Eglot Variables
    786 @section Eglot Variables
    787 @cindex variables, Eglot
    788 
    789 This section provides a reference for the Eglot user options.
    790 
    791 @vtable @code
    792 @item eglot-autoreconnect
    793 This option controls the ability to reconnect automatically to the
    794 language server when Eglot detects that the server process terminated
    795 unexpectedly.  The default value @code{3} means to attempt reconnection only
    796 if the previous successful connection lasted for more than that number
    797 of seconds; a different positive value changes the minimal length of
    798 the connection to trigger reconnection.  A value of @code{t} means
    799 always reconnect automatically, and @code{nil} means never reconnect
    800 (in which case you will need to reconnect manually using @kbd{M-x
    801 eglot}).
    802 
    803 @item eglot-connect-timeout
    804 This specifies the number of seconds before connection attempt to a
    805 language server times out.  The value of @code{nil} means never time
    806 out.  The default is 30 seconds.
    807 
    808 @item eglot-sync-connect
    809 This setting is mainly important for connections which are slow to
    810 establish.  Whereas the variable @code{eglot-connect-timeout} controls
    811 how long to wait for, this variable controls whether to block Emacs's
    812 user interface while waiting.  The default value is @code{3}; a positive
    813 value means block for that many seconds, then wait for the connection
    814 in the background.  The value of @code{t} means block during the whole
    815 waiting period.  The value of @code{nil} or @code{0} means don't block at
    816 all during the waiting period.
    817 
    818 @item eglot-events-buffer-size
    819 This determines the size of the Eglot events buffer.  @xref{Eglot
    820 Commands, eglot-events-buffer}, for how to display that buffer.  If
    821 the value is changed, for it to take effect the connection should be
    822 restarted using @kbd{M-x eglot-reconnect}.
    823 @c FIXME: Shouldn't the defcustom do this by itself using the :set
    824 @c attribute?
    825 @xref{Troubleshooting Eglot}, for when this could be useful.
    826 
    827 @item eglot-autoshutdown
    828 If this is non-@code{nil}, Eglot shuts down a language server when the
    829 last buffer managed by it is killed.  @xref{Shutting Down LSP Servers}.
    830 The default is @code{nil}; if you want to shut down a server, use
    831 @kbd{M-x eglot-shutdown} (@pxref{Eglot Commands}).
    832 
    833 @item eglot-confirm-server-initiated-edits
    834 Various Eglot commands and code actions result in the language server
    835 sending editing commands to Emacs.  If this option's value is
    836 non-@code{nil} (the default), Eglot will ask for confirmation before
    837 performing edits initiated by the server or edits whose scope affects
    838 buffers other than the one where the user initiated the request.
    839 
    840 @item eglot-ignored-server-capabilities
    841 This variable's value is a list of language server capabilities that
    842 Eglot should not use.  The default is @code{nil}: Eglot uses all of
    843 the capabilities supported by each server.
    844 
    845 @item eglot-extend-to-xref
    846 If this is non-@code{nil}, and @kbd{M-.}
    847 (@code{xref-find-definitions}) lands you in a file outside of your
    848 project, such as a system-installed library or header file,
    849 transiently consider that file as managed by the same language server.
    850 That file is still outside your project (i.e. @code{project-find-file}
    851 won't find it), but Eglot and the server will consider it to be part
    852 of the workspace.  The default is @code{nil}.
    853 
    854 @item eglot-mode-map
    855 This variable is the keymap for binding Eglot-related command.  It is
    856 in effect only as long as the buffer is managed by Eglot.  By default,
    857 it is empty, with the single exception: @kbd{C-h .} is remapped to
    858 invoke @code{eldoc-doc-buffer}.  You can bind additional commands in
    859 this map.  For example:
    860 
    861 @lisp
    862   (define-key eglot-mode-map (kbd "C-c r") 'eglot-rename)
    863   (define-key eglot-mode-map (kbd "C-c o") 'eglot-code-action-organize-imports)
    864   (define-key eglot-mode-map (kbd "C-c h") 'eldoc)
    865   (define-key eglot-mode-map (kbd "<f6>") 'xref-find-definitions)
    866 @end lisp
    867 
    868 @end vtable
    869 
    870 Additional variables, which are relevant for customizing the server
    871 connections, are documented in @ref{Customizing Eglot}.
    872 
    873 @node Customizing Eglot
    874 @chapter Customizing Eglot
    875 @cindex customizing Eglot
    876 
    877 Eglot itself has a relatively small number of customization options.
    878 A large part of customizing Eglot to your needs and preferences should
    879 actually be done via options of the Emacs packages and features which
    880 Eglot supports and enhances (@pxref{Eglot Features}).  For example:
    881 
    882 @itemize @bullet
    883 @item
    884 To configure the face used for server-derived errors and warnings,
    885 customize the Flymake faces @code{flymake-error} and
    886 @code{flymake-warning}.
    887 
    888 @item
    889 To configure the amount of space taken up by documentation in the
    890 echo area, customize the ElDoc variable
    891 @code{eldoc-echo-area-use-multiline-p}.
    892 
    893 @item
    894 To completely change how ElDoc displays the at-point documentation
    895 destination, customize the ElDoc variable
    896 @code{eldoc-display-functions}.
    897 @end itemize
    898 
    899 For this reason, this manual describes only how to customize
    900 Eglot's own operation, which mainly has to do with the server
    901 connections and the server features to be used by Eglot.
    902 
    903 @c @table, not @vtable, because some of the variables are indexed
    904 @c elsewhere
    905 @table @code
    906 @item eglot-server-programs
    907 This variable determines which language server to start for each
    908 supported major mode, and how to invoke that server's program.
    909 @xref{Setting Up LSP Servers}, for the details.
    910 
    911 @vindex eglot-strict-mode
    912 @item eglot-strict-mode
    913 This is @code{nil} by default, meaning that Eglot is generally lenient
    914 about non-conforming servers.  If you need to debug a server, set this
    915 to @w{@code{(disallow-non-standard-keys enforce-required-keys)}}.
    916 
    917 @vindex eglot-server-initialized-hook
    918 @item eglot-server-initialized-hook
    919 A hook run after the server object is successfully initialized.
    920 
    921 @vindex eglot-connect-hook
    922 @item eglot-connect-hook
    923 A hook run after connection to the server is successfully
    924 established.  @xref{Starting Eglot}.
    925 
    926 @item eglot-managed-mode-hook
    927 A hook run after Eglot started or stopped managing a buffer.
    928 @xref{Eglot and Buffers}, for details of its usage.
    929 
    930 @vindex eglot-stay-out-of
    931 @item eglot-stay-out-of
    932 This variable's value lists Emacs features that Eglot shouldn't
    933 automatically try to manage on the user's behalf.  It is useful, for
    934 example, when you need to use non-LSP Flymake or Company back-ends.
    935 To have Eglot stay away from some Emacs feature, add that feature's
    936 symbol or a regexp that will match a symbol's name to the list: for
    937 example, the symbol @code{xref} to leave Xref alone, or the string
    938 @samp{company} to stay away from your Company customizations.  Here's an
    939 example:
    940 
    941 @lisp
    942 (add-to-list 'eglot-stay-out-of 'flymake)
    943 @end lisp
    944 
    945 Note that you can still configure the excluded Emacs features manually
    946 to use Eglot in your @code{eglot-managed-mode-hook} or via some other
    947 mechanism.
    948 
    949 @vindex eglot-report-progress
    950 @cindex progress
    951 @item eglot-report-progress
    952 Set this variable to true if you'd like progress notifications coming
    953 from the language server to be handled as Emacs's progress reporting
    954 facilities.
    955 @end table
    956 
    957 @node Advanced server configuration
    958 @chapter Advanced server configuration
    959 
    960 Though many language servers work well out-of-the-box, most allow
    961 fine-grained control of their operation via specific configuration
    962 options that are transmitted over the LSP protocol and vary from
    963 server to server.  A small number of servers require such special
    964 configuration to work acceptably, or even to work at all.
    965 
    966 After having setup a server executable program in
    967 @code{eglot-server-programs} (@pxref{Setting Up LSP Servers}) and
    968 ensuring Eglot can invoke it, you may want to take advantage of some
    969 of these options.  You should first distinguish two main kinds of
    970 server configuration:
    971 
    972 @itemize @bullet
    973 @item
    974 Project-specific, applying to a specific project;
    975 
    976 @item
    977 User-specific, applying to all projects the server is used for.
    978 @end itemize
    979 
    980 When you have decided which kind you need, the following sections
    981 teach how Eglot's user variables can be used to achieve it:
    982 
    983 @menu
    984 * Project-specific configuration::
    985 * User-specific configuration::
    986 * JSONRPC objects in Elisp::
    987 @end menu
    988 
    989 It's important to note that not all servers allow both kinds of
    990 configuration, nor is it guaranteed that user options can be copied
    991 over to project options, and vice-versa.  When in doubt, consult your
    992 language server's documentation.
    993 
    994 It's also worth noting that some language servers can read these
    995 settings from configuration files in the user's @code{HOME} directory
    996 or in a project's directory.  For example, the @command{pylsp} Python
    997 server reads the file @file{~/.config/pycodestyle} for user
    998 configuration.  The @command{clangd} C/C++ server reads both
    999 @file{~/.config/clangd/config.yaml} for user configuration and
   1000 @file{.clangd} for project configuration.  It may be advantageous to
   1001 use these mechanisms instead of Eglot's, as this will probably work
   1002 with other LSP clients and may be easier to debug than options riding
   1003 on the LSP wire.
   1004 
   1005 @node Project-specific configuration
   1006 @section Project-specific configuration
   1007 @vindex eglot-workspace-configuration
   1008 @cindex workspace configuration
   1009 
   1010 To set project-specific settings, which the LSP specification calls
   1011 @dfn{workspace configuration}, the variable
   1012 @code{eglot-workspace-configuration} may be used.
   1013 
   1014 This variable is a directory-local variable (@pxref{Directory
   1015 Variables, , Per-directory Local Variables, emacs, The GNU Emacs
   1016 Manual}).  It's important to recognize that this variable really only
   1017 makes sense when set directory-locally.  It usually does not make
   1018 sense to set it file-locally or in a major-mode hook.
   1019 
   1020 The most common way to set @code{eglot-workspace-configuration } is
   1021 using a @file{.dir-locals.el} file in the root of your project.  If
   1022 you can't do that, you may also set it from Elisp code via the
   1023 @code{dir-locals-set-class-variables} function.  (@pxref{Directory
   1024 Local Variables,,, elisp, GNU Emacs Lisp Reference Manual}).
   1025 
   1026 However you choose to set it, the variable's value is a plist
   1027 (@pxref{Property Lists,,, elisp, GNU Emacs Lisp Reference Manual}) with
   1028 the following format:
   1029 
   1030 @lisp
   1031  (@var{:server1} @var{plist1} @var{:server2} @var{plist2} @dots{})
   1032 @end lisp
   1033 
   1034 @noindent
   1035 Here, @var{:server1} and @var{:server2} are keywords whose names
   1036 identify the LSP language servers to target.  Consult server
   1037 documentation to find out what name to use.  @var{plist1} and
   1038 @var{plist2} are plists of options, possibly nesting other plists.
   1039 
   1040 @findex eglot-show-workspace-configuration
   1041 When experimenting with workspace settings, you can use the command
   1042 @kbd{M-x eglot-show-workspace-configuration} to inspect and debug the
   1043 value of this variable in its final JSON form, ready to be sent to the
   1044 server (@pxref{JSONRPC objects in Elisp}).  This helper command works
   1045 even before actually connecting to the server.
   1046 
   1047 These variable's value doesn't take effect immediately.  That happens
   1048 upon establishing the connection, in response to an explicit query
   1049 from the server, or when issuing the command @kbd{M-x
   1050 eglot-signal-didChangeConfiguration} which notifies the server during
   1051 an ongoing Eglot session.
   1052 
   1053 @subsection Examples
   1054 
   1055 For some users, setting @code{eglot-workspace-configuration} is a
   1056 somewhat daunting task.  One of the reasons is having to manage the
   1057 general Elisp syntax of per-mode directory-local variables, which uses
   1058 alists (@pxref{Association Lists,,, elisp, GNU Emacs Lisp Reference
   1059 Manual}), and the specific syntax of Eglot's variable, which uses
   1060 plists.  Some examples are useful.
   1061 
   1062 Let's say you want to configure two language servers to be used in a
   1063 project written in a combination of the Python and Go languages.  You
   1064 want to use the @command{pylsp} and @command{gopls} languages
   1065 servers.  In the documentation of the servers in question (or in some
   1066 other editor's configuration file, or in some blog article), you find
   1067 the following configuration options in informal dotted-notation
   1068 syntax:
   1069 
   1070 @example
   1071 pylsp.plugins.jedi_completion.include_params: true
   1072 pylsp.plugins.jedi_completion.fuzzy: true
   1073 pylsp.pylint.enabled: false
   1074 gopls.usePlaceholders: true
   1075 @end example
   1076 
   1077 To apply this to Eglot, and assuming you chose the
   1078 @file{.dir-locals.el} file method, the contents of that file could be:
   1079 
   1080 @lisp
   1081 ((nil
   1082   . ((eglot-workspace-configuration
   1083       . (:pylsp (:plugins (:jedi_completion (:include_params t
   1084                                              :fuzzy t)
   1085                            :pylint (:enabled :json-false)))
   1086          :gopls (:usePlaceholders t)))))
   1087  (python-mode . ((indent-tabs-mode . nil)))
   1088  (go-mode     . ((indent-tabs-mode . t))))
   1089 @end lisp
   1090 
   1091 @noindent
   1092 This sets the value of @code{eglot-workspace-configuration} in all the
   1093 buffers inside the project; each server will use only the section of
   1094 the parameters intended for that server, and ignore the rest.  Note
   1095 how alists are used for associating Emacs mode names with alists
   1096 associating variable names with variable values.  Then notice how
   1097 plists are used inside the value of
   1098 @code{eglot-workspace-configuration}.
   1099 
   1100 This following form may also be used:
   1101 
   1102 @lisp
   1103 ((python-mode
   1104   . ((eglot-workspace-configuration
   1105       . (:pylsp (:plugins (:jedi_completion (:include_params t
   1106                                              :fuzzy t)
   1107                            :pylint (:enabled :json-false)))))
   1108      (indent-tabs-mode . nil)))
   1109  (go-mode
   1110   . ((eglot-workspace-configuration
   1111       . (:gopls (:usePlaceholders t)))
   1112      (indent-tabs-mode . t))))
   1113 @end lisp
   1114 
   1115 @noindent
   1116 This sets up the value of @code{eglot-workspace-configuration}
   1117 separately depending on the major mode of each of that project's
   1118 buffers.  @code{python-mode} buffers will have the variable set to
   1119 @code{(:pylsp (:plugins ...))}.  @code{go-mode} buffers will have the
   1120 variable set to @code{(:gopls (:usePlaceholders t))}.
   1121 
   1122 Some servers will issue workspace configuration for specific files
   1123 inside your project.  For example, if you know @code{gopls} is asking
   1124 about specific files in the @code{src/imported} subdirectory and you
   1125 want to set a different option for @code{gopls.usePlaceholders} , you
   1126 may use something like:
   1127 
   1128 @lisp
   1129 ((python-mode
   1130   . ((eglot-workspace-configuration
   1131       . (:pylsp (:plugins (:jedi_completion (:include_params t
   1132                                              :fuzzy t)
   1133                            :pylint (:enabled :json-false)))))
   1134      (indent-tabs-mode nil)))
   1135  (go-mode
   1136   . ((eglot-workspace-configuration
   1137       . (:gopls (:usePlaceholders t)))
   1138      (indent-tabs-mode t)))
   1139  ("src/imported"
   1140    . ((eglot-workspace-configuration
   1141       . (:gopls (:usePlaceholders nil))))))
   1142 @end lisp
   1143 
   1144 Finally, if one needs to determine the workspace configuration based
   1145 on some dynamic context, @code{eglot-workspace-configuration} can be
   1146 set to a function.  The function is called with the
   1147 @code{eglot-lsp-server} instance of the connected server (if any) and
   1148 with @code{default-directory} set to the root of the project.  The
   1149 function should return a plist suitable for use as the variable's
   1150 value.
   1151 
   1152 @node User-specific configuration
   1153 @section User-specific configuration
   1154 @cindex initializationOptions
   1155 @cindex command-line arguments
   1156 
   1157 This kind of configuration applies to all projects the server is used
   1158 for.  Here, there are a number of ways to do this inside Eglot.
   1159 
   1160 A common way is to pass command-line options to the server invocation
   1161 via @code{eglot-server-programs}.  Let's say we want to configure
   1162 where the @command{clangd} server reads its
   1163 @code{compile_commands.json} from.  This can be done like so:
   1164 
   1165 @lisp
   1166 (with-eval-after-load 'eglot
   1167   (add-to-list 'eglot-server-programs
   1168                `(c++-mode . ("clangd" "--compile-commands-dir=/tmp"))))
   1169 
   1170 @end lisp
   1171 
   1172 @noindent
   1173 Another way is to have Eglot pass a JSON object to the server during
   1174 the LSP handshake.  This is done using the
   1175 @code{:initializationOptions} syntax of @code{eglot-server-programs}:
   1176 
   1177 @lisp
   1178 (with-eval-after-load 'eglot
   1179   (add-to-list 'eglot-server-programs
   1180                `(c++-mode . ("clangd" :initializationOptions
   1181                                       (:compilationDatabasePath "/tmp")))))
   1182 @end lisp
   1183 
   1184 @noindent
   1185 The argument @code{(:compilationDatabasePath "/tmp")} is Emacs's
   1186 representation in plist format of a simple JSON object
   1187 @code{@{"compilationDatabasePath": "/tmp"@}}.  To learn how to
   1188 represent more deeply nested options in this format, @pxref{JSONRPC
   1189 objects in Elisp}.
   1190 
   1191 In this case, the two examples achieve exactly the same, but notice
   1192 how the option's name has changed between them.
   1193 
   1194 @vindex eglot-workspace-configuration
   1195 Finally there is another way to do user-specific configuration of
   1196 language servers, which may be used if the methods above are not
   1197 supported.  It consists of @emph{globally} setting
   1198 @code{eglot-workspace-configuration}, a variable originally intended
   1199 for project-specific configuration.  This has the same effect as
   1200 giving all your projects a certain default configuration, as described
   1201 in @ref{Project-specific configuration}.  Here is an example:
   1202 
   1203 @lisp
   1204 (setq-default eglot-workspace-configuration
   1205               '(:pylsp (:plugins (:jedi_completion (:include_params t
   1206                                                     :fuzzy t)
   1207                                   :pylint (:enabled :json-false)))
   1208                 :gopls (:usePlaceholders t)))
   1209 @end lisp
   1210 
   1211 Note that the global value of @code{eglot-workspace-configuration} is
   1212 always overriden if a directory-local value is detected.
   1213 
   1214 @node JSONRPC objects in Elisp
   1215 @section JSONRPC objects in Elisp
   1216 
   1217 Emacs's preferred way of representing JSON is via Lisp lists.  In
   1218 Eglot, the syntax of this list is the simplest possible (the one with
   1219 fewer parenthesis), a plist (@pxref{Property Lists,,, elisp, GNU Emacs
   1220 Lisp Reference Manual}).
   1221 
   1222 The plist may be arbitrarily complex, and generally containing other
   1223 keyword-value property sub-plists corresponding to JSON sub-objects.
   1224 
   1225 For representing the JSON leaf values @code{true}, @code{false},
   1226 @code{null} and @code{@{@}}, you can use the Lisp values @code{t},
   1227 @code{:json-false}, @code{nil}, and @code{eglot-@{@}}, respectively.
   1228 JSON arrays are represented as Elisp vectors surrounded by square brackets
   1229 (@pxref{Vectors,,,elisp,GNU Emacs Lisp Reference Manual}).
   1230 
   1231 For example, the plist
   1232 
   1233 @lisp
   1234 (:pylsp (:plugins (:jedi_completion (:include_params t
   1235                                      :fuzzy t
   1236                                      :cache_for ["pandas" "numpy"]
   1237                    :pylint (:enabled :json-false))))
   1238  :gopls (:usePlaceholders t))
   1239 @end lisp
   1240 
   1241 @noindent
   1242 is serialized by Eglot to the following JSON text:
   1243 
   1244 @example
   1245 @{
   1246   "pylsp": @{
   1247     "plugins": @{
   1248       "jedi_completion": @{
   1249         "include_params": true,
   1250         "fuzzy": true,
   1251         "cache_for": [ "pandas", "numpy" ],
   1252       @},
   1253       "pylint": @{
   1254         "enabled": false
   1255       @}
   1256     @}
   1257   @},
   1258   "gopls": @{
   1259     "usePlaceholders":true
   1260   @},
   1261 @}
   1262 @end example
   1263 
   1264 @node Troubleshooting Eglot
   1265 @chapter Troubleshooting Eglot
   1266 @cindex troubleshooting Eglot
   1267 
   1268 This chapter documents commands and variables that can be used to
   1269 troubleshoot Eglot problems.  It also provides guidelines for
   1270 reporting Eglot bugs in a way that facilitates their resolution.
   1271 
   1272 When you encounter problems with Eglot, try first using the commands
   1273 @kbd{M-x eglot-events-buffer} and @kbd{M-x eglot-stderr-buffer}.  They
   1274 pop up special buffers that can be used to inspect the communications
   1275 between the Eglot and language server.  In many cases, this will
   1276 indicate the problems or at least provide a hint.
   1277 
   1278 @menu
   1279 * Performance::
   1280 * Getting the latest version::
   1281 * Reporting bugs::
   1282 @end menu
   1283 
   1284 @node Performance
   1285 @section Performance
   1286 @cindex performance problems, with Eglot
   1287 A common and easy-to-fix cause of performance problems is the length
   1288 of the Eglot events buffer because it represent additional work that
   1289 Eglot must do.  After verifying Eglot is operating correctly but
   1290 slowly, try to customize the variable @code{eglot-events-buffer-size}
   1291 (@pxref{Eglot Variables}) to 0.  This will disable any debug logging
   1292 and may speed things up.
   1293 
   1294 In other situations, the cause of poor performance lies in the
   1295 language server itself.  Servers use aggressive caching and other
   1296 techniques to improve their performance.  Often, this can be tweaked
   1297 by changing the server configuration (@pxref{Advanced server
   1298 configuration}).
   1299 
   1300 @node Getting the latest version
   1301 @section Getting the latest version
   1302 @cindex upgrading Eglot
   1303 
   1304 To install the latest Eglot in an Emacs version that does not bundle
   1305 Eglot, use @kbd{M-x package-install}.
   1306 
   1307 Often, a newer Eglot version exists that has fixed a longstanding bug,
   1308 has more LSP features, or just better supports a particular language
   1309 server.  Recent Eglot versions can self-update via the command
   1310 @kbd{M-x eglot-update}.  This will replace any currently installed
   1311 version with the newest one available from the ELPA archives
   1312 configured in @code{package-archives}.
   1313 
   1314 You can also update Eglot through other methods, such as
   1315 @code{use-package} (@pxref{Installing packages,,, use-package,
   1316 use-package User Manual}), @code{package-install},
   1317 @code{list-packages} or the newer @code{package-upgrade}
   1318 (@pxref{Packages,,, emacs, GNU Emacs Manual}).  However, do read the
   1319 docstrings of the command you intend to use before you use it, as some
   1320 of them may not work in exactly the same way across Emacs versions,
   1321 meaning your configuration may be not portable.
   1322 
   1323 @node Reporting bugs
   1324 @section Reporting bugs
   1325 @cindex bug reports
   1326 
   1327 If you think you have found a bug, we want to hear about it.  Before
   1328 reporting a bug, keep in mind that interaction with language servers
   1329 represents a large quantity of unknown variables.  Therefore, it is
   1330 generally both @emph{difficult} and @emph{absolutely essential} that
   1331 the maintainers reproduce bugs exactly as they happened to you, the
   1332 user.
   1333 
   1334 To report an Eglot bug, send e-mail to @email{bug-gnu-emacs@@gnu.org}.
   1335 
   1336 To understand how to write this email, get acquainted with Emacs's bug
   1337 reporting guidelines (@pxref{Bugs,,, emacs, GNU Emacs Manual}).  Then,
   1338 follow this Eglot-specific checklist:
   1339 
   1340 @enumerate
   1341 @item
   1342 Include the transcript of JSONRPC events obtained from the buffer
   1343 popped up by @kbd{M-x eglot-events-buffer}.  You may narrow down the
   1344 transcript if you are sure of where the problematic exchange is, but
   1345 it's safer to include the whole transcript, either attached or inline.
   1346 
   1347 @item
   1348 If Emacs signaled an error (an error message was seen or heard), make
   1349 sure to repeat the process after turning on @code{debug-on-error} via
   1350 @kbd{M-x toggle-debug-on-error}.  This normally produces a backtrace
   1351 of the error that should also be attached to the bug report.
   1352 
   1353 @item
   1354 Include a description of how the maintainer should obtain, install,
   1355 and configure the language server you used.  Maintainers usually have
   1356 access to GNU/Linux systems, though not necessarily the distribution
   1357 that you may be using.  If possible, try to replicate the problem with
   1358 the C/C@t{++} or Python servers, as these are very easy to install.
   1359 
   1360 @item
   1361 Describe how to setup a @emph{minimal} project directory where Eglot
   1362 should be started for the problem to happen.  Describe each file's
   1363 name and its contents.  Alternatively, you can supply the address of a
   1364 public Git repository.
   1365 
   1366 @item
   1367 Include versions of the software used.  The Emacs version can be
   1368 obtained with @kbd{M-x emacs-version}.
   1369 
   1370 We welcome bug reports about all Eglot versions, but it is helpful to
   1371 first check if the problem isn't already fixed in the latest version
   1372 (@pxref{Getting the latest version}).
   1373 
   1374 It's also essential to include the version of ELPA packages that are
   1375 explicitly or implicitly loaded.  The optional but popular Company or
   1376 Markdown packages are distributed as GNU ELPA packages, not to mention
   1377 Eglot itself in some situations.  Some major modes (Go, Rust, etc.)
   1378 are provided by ELPA packages.  It's sometimes easy to miss these,
   1379 since they are usually implicitly loaded when visiting a file in that
   1380 language.
   1381 
   1382 ELPA packages usually live in @code{~/.emacs.d/elpa} (or what is in
   1383 @code{package-user-dir}).  Including a listing of files in that
   1384 directory is a way to tell the maintainers about ELPA package
   1385 versions.
   1386 
   1387 @item
   1388 Include a recipe to replicate the problem with @emph{a clean Emacs
   1389 run}.  The invocation @code{emacs -Q -f package-initialize} starts
   1390 Emacs with no configuration and initializes the ELPA packages.  A very
   1391 minimal @file{.emacs} initialization file (10 lines or less) is also
   1392 acceptable and good means to describe changes to variables.
   1393 
   1394 There is usually no need to include @code{require} statements in the
   1395 recipe, as Eglot's functionality uses autoloads.
   1396 
   1397 Likewise, there is rarely the need to use things like
   1398 @code{use-package} or @code{eglot-ensure}.  This just makes the recipe
   1399 harder to follow.  Prefer setting variables with @code{setq} and
   1400 adding to hooks with @code{add-hook}.  Prefer starting Eglot with
   1401 @code{M-x eglot}.
   1402 
   1403 @item
   1404 Make sure to double check all the above elements and re-run the recipe
   1405 to see that the problem is reproducible.  Following the recipe should
   1406 produce event transcript and error backtraces that are very similar to
   1407 the ones you included.  If the problem only happens sometimes, mention
   1408 this in your report.
   1409 @end enumerate
   1410 
   1411 Please keep in mind that some problems reported against Eglot may
   1412 actually be bugs in the language server or the Emacs feature/package
   1413 that used Eglot to communicate with the language server.  Eglot is, in
   1414 many cases, just a frontend to that functionality.
   1415 
   1416 @node GNU Free Documentation License
   1417 @appendix GNU Free Documentation License
   1418 @include doclicense.texi
   1419 
   1420 @node Index
   1421 @unnumbered Index
   1422 @printindex cp
   1423 
   1424 @bye