Add dedicated actions for LSP completions insertion mode (#28121)

Adds actions so you can have customized keybindings for `insert` and
`replace` modes.

And add `shift-enter` as a default for `replace`, this will override the
default setting
`completions.lsp_insert_mode` which is set to `replace_suffix`, which
tries to "smartly"
decide whether to replace or insert based on the surrounding text.

For those who come from VSCode, if you want to mimic their behavior, you
only have to
set `completions.lsp_insert_mode` to `insert`.

If you want `tab` and `enter` to do different things, you need to remap
them, here is
an example:

```jsonc
[
  // ...
  {
    "context": "Editor && showing_completions",
    "bindings": {
      "enter": "editor::ConfirmCompletionInsert",
      "tab": "editor::ConfirmCompletionReplace"
    }
  },
]
```

Closes #24577

- [x] Make LSP completion insertion mode decision in guest's machine
(host is currently deciding it and not allowing guests to have their own
setting for it)
- [x] Add shift-enter as a hotkey for `replace` by default.
- [x] Test actions.
- [x] Respect the setting being specified per language, instead of using
the "defaults".
- [x] Move `insert_range` of `Completion` to the Lsp variant of
`.source`.
- [x] Fix broken default, forgotten after
https://github.com/zed-industries/zed/pull/27453#pullrequestreview-2736906628,
should be `replace_suffix` and not `insert`.

Release Notes:

- LSP completions: added actions `ConfirmCompletionInsert` and
`ConfirmCompletionReplace` that control how completions are inserted,
these override `completions.lsp_insert_mode`, by default, `shift-enter`
triggers `ConfirmCompletionReplace` which replaces the whole word.
This commit is contained in:
João Marcos 2025-04-08 19:03:03 -03:00 committed by GitHub
parent 0459b1d303
commit b15ee1b1cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 400 additions and 207 deletions

View file

@ -359,8 +359,14 @@ pub struct InlayHint {
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
pub enum CompletionIntent {
/// The user intends to 'commit' this result, if possible
/// completion confirmations should run side effects
/// completion confirmations should run side effects.
///
/// For LSP completions, will respect the setting `completions.lsp_insert_mode`.
Complete,
/// Similar to [Self::Complete], but behaves like `lsp_insert_mode` is set to `insert`.
CompleteWithInsert,
/// Similar to [Self::Complete], but behaves like `lsp_insert_mode` is set to `replace`.
CompleteWithReplace,
/// The user intends to continue 'composing' this completion
/// completion confirmations should not run side effects and
/// let the user continue composing their action
@ -377,11 +383,11 @@ impl CompletionIntent {
}
}
/// A completion provided by a language server
/// Similar to `CoreCompletion`, but with extra metadata attached.
#[derive(Clone)]
pub struct Completion {
/// The range of the buffer that will be replaced.
pub old_range: Range<Anchor>,
/// The range of text that will be replaced by this completion.
pub replace_range: Range<Anchor>,
/// The new text that will be inserted.
pub new_text: String,
/// A label for this completion that is shown in the menu.
@ -404,6 +410,8 @@ pub struct Completion {
#[derive(Debug, Clone)]
pub enum CompletionSource {
Lsp {
/// The alternate `insert` range, if provided by the LSP server.
insert_range: Option<Range<Anchor>>,
/// The id of the language server that produced this completion.
server_id: LanguageServerId,
/// The raw completion provided by the language server.
@ -508,7 +516,7 @@ impl CompletionSource {
impl std::fmt::Debug for Completion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Completion")
.field("old_range", &self.old_range)
.field("replace_range", &self.replace_range)
.field("new_text", &self.new_text)
.field("label", &self.label)
.field("documentation", &self.documentation)
@ -517,10 +525,10 @@ impl std::fmt::Debug for Completion {
}
}
/// A completion provided by a language server
/// A generic completion that can come from different sources.
#[derive(Clone, Debug)]
pub(crate) struct CoreCompletion {
old_range: Range<Anchor>,
replace_range: Range<Anchor>,
new_text: String,
source: CompletionSource,
}