Add completions.lsp_insert_mode setting to control what ranges are replaced when a completion is inserted (#27453)

This PR adds `completions.lsp_insert_mode` and effectively changes the
default from `"replace"` to `"replace_suffix"`, which automatically
detects whether to use the LSP `replace` range instead of `insert`
range.

`"replace_suffix"` was chosen as a default because it's more
conservative than `"replace_subsequence"`, considering that deleting
text is usually faster and less disruptive than having to rewrite a long
replaced word.

Fixes #27197
Fixes #23395 (again)
Fixes #4816 (again)

Release Notes:

- Added new setting `completions.lsp_insert_mode` that changes what will
be replaced when an LSP completion is accepted. The default is
`"replace_suffix"`, but it accepts 4 values: `"insert"` for replacing
only the text before the cursor, `"replace"` for replacing the whole
text, `"replace_suffix"` that acts like `"replace"` when the text after
the cursor is a suffix of the completion, and `"replace_subsequence"`
that acts like `"replace"` when the text around your cursor is a
subsequence of the completion (similiar to a fuzzy match). Check [the
documentation](https://zed.dev/docs/configuring-zed#LSP-Insert-Mode) for
more information.

---------

Co-authored-by: João Marcos <marcospb19@hotmail.com>
Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
frederik-uni 2025-04-02 21:55:03 +02:00 committed by GitHub
parent 108ae0b5b0
commit 07a77792c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 467 additions and 13 deletions

View file

@ -2051,6 +2051,68 @@ Examples:
`boolean` values
## Completions
- Description: Controls how completions are processed for this language.
- Setting: `completions`
- Default:
```json
{
"completions": {
"words": "fallback",
"lsp": true,
"lsp_fetch_timeout_ms": 0,
"lsp_insert_mode": "replace_suffix"
}
}
```
### Words
- Description: Controls how words are completed. For large documents, not all words may be fetched for completion.
- Setting: `words`
- Default: `fallback`
**Options**
1. `enabled` - Always fetch document's words for completions along with LSP completions
2. `fallback` - Only if LSP response errors or times out, use document's words to show completions
3. `disabled` - Never fetch or complete document's words for completions (word-based completions can still be queried via a separate action)
### LSP
- Description: Whether to fetch LSP completions or not.
- Setting: `lsp`
- Default: `true`
**Options**
`boolean` values
### LSP Fetch Timeout (ms)
- Description: When fetching LSP completions, determines how long to wait for a response of a particular server. When set to 0, waits indefinitely.
- Setting: `lsp_fetch_timeout_ms`
- Default: `0`
**Options**
`integer` values representing milliseconds
### LSP Insert Mode
- Description: Controls what range to replace when accepting LSP completions.
- Setting: `lsp_insert_mode`
- Default: `replace_suffix`
**Options**
1. `insert` - Replaces text before the cursor, using the `insert` range described in the LSP specification
2. `replace` - Replaces text before and after the cursor, using the `replace` range described in the LSP specification
3. `replace_subsequence` - Behaves like `"replace"` if the text that would be replaced is a subsequence of the completion text, and like `"insert"` otherwise
4. `replace_suffix` - Behaves like `"replace"` if the text after the cursor is a suffix of the completion, and like `"insert"` otherwise
## Show Completions On Input
- Description: Whether or not to show completions as you type.