Skip to content

Commit be36d16

Browse files
authored
Docs: register a server / file watchers (#4364)
* doc: Registering a server I was originally planning to fix a few grammar problems but ended up making a bit more extensive changes. * grammar stuff * Tried to make the reason for the indirection from major mode to language name to server a bit clearer, since I struggled with this a bit myself. * Added permalink pointer to the `lsp-client` defstruct which has a lot of good docs in its comments. * Indented the elisp code using standard Emacs indentation. * `lsp-restart-workspace` (deprecated) -> `lsp-workspace-restart` * doc: File watchers - grammar and linking * fixed some grammar * link to PRs and issues
1 parent 1b13d7c commit be36d16

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

docs/page/adding-new-language.md

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,51 @@ root_file: docs/page/adding-new-language.md
33
---
44
# Adding support for languages
55

6-
## Registering server
6+
## Register a server
77

8-
Here it is the minimal configuration that is needed for new language
9-
server registration. Refer to the documentation of `lsp-mode.el` for
10-
the additional settings supported on registration time.
11-
`lsp-language-id-configuration` must be updated to contain the
12-
corresponding mode -\> language id - in this case `(python-mode .
13-
"python")`
8+
Your language server must be registered with `lsp-mode` in order for the `lsp`
9+
command to figure out how to communicate with it. This section explains how to
10+
do that.
1411

12+
The language being used is identified by either an Emacs major mode or by a
13+
regular expression matching the buffer's filename. A language server matching
14+
that language is then started.
15+
16+
Here's a minimal example using Python. First we tell `lsp-mode` that buffers in
17+
`python-mode` correspond to the language `"python"`:
1518

1619
``` elisp
17-
(defvar lsp-language-id-configuration
18-
'(...
19-
(python-mode . "python")
20-
...))
21-
;; if you are adding the support for your language server in separate repo use
22-
;; (add-to-list 'lsp-language-id-configuration '(python-mode . "python"))
20+
(add-to-list 'lsp-language-id-configuration '(python-mode . "python"))
21+
```
2322

24-
(lsp-register-client
25-
(make-lsp-client :new-connection (lsp-stdio-connection "pyls")
26-
:activation-fn (lsp-activate-on "python")
27-
:server-id 'pyls))
23+
If there is no major mode corresponding to your language, or the major mode is
24+
not sufficient to determine the language (e.g. `web-mode` is used for
25+
`javascript`, `html`, and `css`) you can register it using a regular expression
26+
that matches the filename instead:
27+
28+
``` elisp
29+
(add-to-list 'lsp-language-id-configuration '(".*\\.svelte$" . "svelte"))
30+
```
31+
32+
**Note:** If adding support directly in the `lsp-mode.el` source obviously you
33+
should modify `lsp-language-id-configuration` directly instead of calling
34+
`add-to-list`.
35+
36+
Now we tell `lsp-mode` how to start the `pyls` language server when a buffer
37+
uses the `"python"` language:
38+
39+
``` elisp
40+
(lsp-register-client (make-lsp-client
41+
:new-connection (lsp-stdio-connection "pyls")
42+
:activation-fn (lsp-activate-on "python")
43+
:server-id 'pyls))
2844
```
2945

30-
`lsp-mode` is using `lsp-language-id-configuration` to determine what is the
31-
buffer language. When the `major-mode` is not sufficient to determine the
32-
language (e.g. `web-mode` is used for `javascript`, `html`, and `css`) you can put regex.
46+
There are many more options for the client (see [the `lsp-client`
47+
struct](https://github.com/emacs-lsp/lsp-mode/blob/1b13d7c1b39aaad12073095ef7719952568c45db/lsp-mode.el#L1528)
48+
which documents each option) but what we've shown above is the minimal required
49+
configuration. One common case, setting environment variables for the server,
50+
is shown below.
3351

3452
**Note:** In the above example, when a new client is created using
3553
`make-lsp-client`, a new connection to the language server is created
@@ -45,22 +63,22 @@ is a common mistake that keeps reoccurring.
4563
Here's an example of how to set up a custom language server in your `init.el` file:
4664

4765
```elisp
48-
;; Use shopify-cli / theme-check-language-server for Shopify's liquid syntax
66+
;; Use shopify-cli / theme-check-language-server for Shopify's liquid syntax.
4967
(with-eval-after-load 'lsp-mode
5068
(add-to-list 'lsp-language-id-configuration
51-
'(shopify-mode . "shopify"))
69+
'(shopify-mode . "shopify"))
5270
5371
(lsp-register-client
54-
(make-lsp-client :new-connection (lsp-stdio-connection "theme-check-language-server")
55-
:activation-fn (lsp-activate-on "shopify")
56-
:server-id 'theme-check)))
72+
(make-lsp-client :new-connection (lsp-stdio-connection "theme-check-language-server")
73+
:activation-fn (lsp-activate-on "shopify")
74+
:server-id 'theme-check)))
5775
```
5876

5977
**Note:** This example assumes that you've already set up a major mode of your own either by [deriving it](https://www.gnu.org/software/emacs/manual/html_node/elisp/Derived-Modes.html) from `web-mode` or perhaps by writing it yourself.
6078

6179
If the language server supports environment variables to control
6280
additional behavior, you can register that by using the
63-
`:environment-fn` function, like the Bash language client does:
81+
`:environment-fn` option, like the Bash language client does:
6482

6583
``` elisp
6684
(lsp-register-client
@@ -76,12 +94,12 @@ additional behavior, you can register that by using the
7694
`lsp-bash-explainshell-endpoint` and `lsp-bash-highlight-parsing-errors`
7795
are language client `defcustom` that expose supported server environment
7896
settings in a type-safe way. If you change any of those variables,
79-
restart the language server with `lsp-restart-workspace` for the changes
97+
restart the language server with `lsp-workspace-restart` for the changes
8098
to be applied.
8199

82-
Also, if new client support customizing language server path. It's recommended
100+
Also, if your client supports customizing the language server path it's recommended
83101
to make a wrapper function so the user can customize the value even after the
84-
client has been loaded.
102+
client has been loaded. For example:
85103

86104
``` elisp
87105
(defcustom lsp-tex-executable "digestif"
@@ -102,9 +120,9 @@ client has been loaded.
102120
## Sections
103121

104122
`lsp-mode` provides tools to bridge emacs `defcustom` as a language
105-
configuration sections properties(see [specification
123+
configuration sections properties (see [specification
106124
workspace/configuration](https://microsoft.github.io/language-server-protocol/specification#workspace_configuration)). In addition you may use `lsp-generate-settings`
107-
from [Generate Settings script](https://github.com/emacs-lsp/lsp-mode/blob/master/scripts/lsp-generate-settings.el) to generate `lsp-defcustom` from `package.json`
125+
from the [Generate Settings script](https://github.com/emacs-lsp/lsp-mode/blob/master/scripts/lsp-generate-settings.el) to generate `lsp-defcustom` from the `package.json`
108126
VScode plugin manifest. Example:
109127

110128
``` elisp

docs/page/file-watchers.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ root_file: docs/page/file-watchers.md
33
---
44
# File watchers
55

6-
When some of the workspaces that are active in the current project requests file notifications via `workspace/didChangeWatchedFiles`, `lsp-mode` will start monitoring each of the folders in the workspace for changes to notify the server about that.
6+
When a workspace that is active in the current project requests file notifications via `workspace/didChangeWatchedFiles`, `lsp-mode` will monitor the workspace folders for changes and notify the server about them.
77

8-
In case you have issues with file watchers, first, check what folders are being watched, they are logged on `*lsp-log*` when the server starts, then you may consider check the below:
8+
If you have problems with file watchers, first check what folders are being watched (they are logged in the `*lsp-log*` buffer when the server starts) and then check the below:
99

10-
- If your project has some specific folder that should not be watched, you can exclude it with:
10+
- If your project has a specific folder that should not be watched, you can exclude it with:
1111

1212
```emacs-lisp
1313
(with-eval-after-load 'lsp-mode
@@ -16,6 +16,6 @@ In case you have issues with file watchers, first, check what folders are being
1616
(add-to-list 'lsp-file-watch-ignored-files "[/\\\\]\\.my-files\\'"))
1717
```
1818

19-
- Increase the file watch warning threshold, the default is `1000`, `(setq lsp-file-watch-threshold 2000)`
20-
- If the folder is some kind of cache folder or something that should always be excluded for everyone, consider opening a pull request or informing the maintainers to add to the [common regex](https://github.com/emacs-lsp/lsp-mode/blob/master/lsp-mode.el#L306).
21-
- In last case, disable file watchers with `(setq lsp-enable-file-watchers nil)` (you may use dir-locals).
19+
- Increase the file watch warning threshold, the default is `1000`: `(setq lsp-file-watch-threshold 2000)`
20+
- If the folder is some kind of cache folder or something that should always be excluded for everyone, consider [opening a pull request](https://github.com/emacs-lsp/lsp-mode/pulls) or [filing a bug](https://github.com/emacs-lsp/lsp-mode/issues) to add to the [common regex](https://github.com/emacs-lsp/lsp-mode/blob/1b13d7c1b39aaad12073095ef7719952568c45db/lsp-mode.el#L340).
21+
- As a last resort, disable file watchers with `(setq lsp-enable-file-watchers nil)` (you may use dir-locals).

0 commit comments

Comments
 (0)