lsp: Provide completion reason in the request (#12893)

This should help LS make a better call about the completions it should
return back to the caller. For example, it speeds up import completions
for typescript.
Before: 


https://github.com/zed-industries/zed/assets/24362066/b38fd565-f9ff-4db7-a87f-c3b31a9fdc96

after: 


https://github.com/zed-industries/zed/assets/24362066/d4fbc9ae-9aab-4543-b9f6-16acf1619576


This should be merged after 06.12 Preview to give it some time on
Nightly.

Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2024-06-13 14:38:34 +02:00 committed by GitHub
parent eb7b5a7131
commit 0a13b9ee01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 86 additions and 26 deletions

View file

@ -16,8 +16,9 @@ use language::{
OffsetRangeExt, PointUtf16, ToOffset, ToPointUtf16, Transaction, Unclipped,
};
use lsp::{
CompletionListItemDefaultsEditRange, DocumentHighlightKind, LanguageServer, LanguageServerId,
LinkedEditingRangeServerCapabilities, OneOf, ServerCapabilities,
CompletionContext, CompletionListItemDefaultsEditRange, CompletionTriggerKind,
DocumentHighlightKind, LanguageServer, LanguageServerId, LinkedEditingRangeServerCapabilities,
OneOf, ServerCapabilities,
};
use std::{cmp::Reverse, ops::Range, path::Path, sync::Arc};
use text::{BufferId, LineEnding};
@ -127,6 +128,7 @@ pub(crate) struct GetHover {
pub(crate) struct GetCompletions {
pub position: PointUtf16,
pub context: CompletionContext,
}
#[derive(Clone)]
@ -1464,7 +1466,7 @@ impl LspCommand for GetCompletions {
lsp::TextDocumentIdentifier::new(lsp::Uri::from_file_path(path).unwrap().into()),
point_to_lsp(self.position),
),
context: Default::default(),
context: Some(self.context.clone()),
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
}
@ -1649,7 +1651,13 @@ impl LspCommand for GetCompletions {
})
})
.ok_or_else(|| anyhow!("invalid position"))??;
Ok(Self { position })
Ok(Self {
position,
context: CompletionContext {
trigger_kind: CompletionTriggerKind::INVOKED,
trigger_character: None,
},
})
}
fn response_to_proto(

View file

@ -58,7 +58,7 @@ use language::{
};
use log::error;
use lsp::{
DiagnosticSeverity, DiagnosticTag, DidChangeWatchedFilesRegistrationOptions,
CompletionContext, DiagnosticSeverity, DiagnosticTag, DidChangeWatchedFilesRegistrationOptions,
DocumentHighlightKind, Edit, FileSystemWatcher, LanguageServer, LanguageServerBinary,
LanguageServerId, LspRequestFuture, MessageActionItem, OneOf, ServerCapabilities,
ServerHealthStatus, ServerStatus, TextEdit, Uri,
@ -651,6 +651,12 @@ pub enum SearchResult {
LimitReached,
}
#[cfg(any(test, feature = "test-support"))]
pub const DEFAULT_COMPLETION_CONTEXT: CompletionContext = CompletionContext {
trigger_kind: lsp::CompletionTriggerKind::INVOKED,
trigger_character: None,
};
impl Project {
pub fn init_settings(cx: &mut AppContext) {
WorktreeSettings::register(cx);
@ -5875,6 +5881,7 @@ impl Project {
&self,
buffer: &Model<Buffer>,
position: PointUtf16,
context: CompletionContext,
cx: &mut ModelContext<Self>,
) -> Task<Result<Vec<Completion>>> {
let language_registry = self.languages.clone();
@ -5908,7 +5915,10 @@ impl Project {
this.request_lsp(
buffer.clone(),
LanguageServerToQuery::Other(server_id),
GetCompletions { position },
GetCompletions {
position,
context: context.clone(),
},
cx,
),
));
@ -5935,7 +5945,7 @@ impl Project {
let task = self.send_lsp_proto_request(
buffer.clone(),
project_id,
GetCompletions { position },
GetCompletions { position, context },
cx,
);
let language = buffer.read(cx).language().cloned();
@ -5969,10 +5979,11 @@ impl Project {
&self,
buffer: &Model<Buffer>,
position: T,
context: CompletionContext,
cx: &mut ModelContext<Self>,
) -> Task<Result<Vec<Completion>>> {
let position = position.to_point_utf16(buffer.read(cx));
self.completions_impl(buffer, position, cx)
self.completions_impl(buffer, position, context, cx)
}
pub fn resolve_completions(

View file

@ -2499,7 +2499,7 @@ async fn test_completions_without_edit_ranges(cx: &mut gpui::TestAppContext) {
let text = "let a = b.fqn";
buffer.update(cx, |buffer, cx| buffer.set_text(text, cx));
let completions = project.update(cx, |project, cx| {
project.completions(&buffer, text.len(), cx)
project.completions(&buffer, text.len(), DEFAULT_COMPLETION_CONTEXT, cx)
});
fake_server
@ -2526,7 +2526,7 @@ async fn test_completions_without_edit_ranges(cx: &mut gpui::TestAppContext) {
let text = "let a = \"atoms/cmp\"";
buffer.update(cx, |buffer, cx| buffer.set_text(text, cx));
let completions = project.update(cx, |project, cx| {
project.completions(&buffer, text.len() - 1, cx)
project.completions(&buffer, text.len() - 1, DEFAULT_COMPLETION_CONTEXT, cx)
});
fake_server
@ -2591,7 +2591,7 @@ async fn test_completions_with_carriage_returns(cx: &mut gpui::TestAppContext) {
let text = "let a = b.fqn";
buffer.update(cx, |buffer, cx| buffer.set_text(text, cx));
let completions = project.update(cx, |project, cx| {
project.completions(&buffer, text.len(), cx)
project.completions(&buffer, text.len(), DEFAULT_COMPLETION_CONTEXT, cx)
});
fake_server