[ty] Refactor to handle unimported completions

This rejiggers some stuff in the main completions entrypoint
in `ty_ide`. A more refined `Completion` type is defined
with more information. In particular, to support auto-import,
we now include a module name and an "edit" for inserting an
import.

This also rolls the old "detailed completion" into the new
completion type. Previously, we were relying on the completion
type for `ty_python_semantic`. But `ty_ide` is really the code
that owns completions.

Note that this code doesn't build as-is. The next commit will
add the importer used here in `add_unimported_completions`.
This commit is contained in:
Andrew Gallant 2025-09-16 13:26:35 -04:00 committed by Andrew Gallant
parent 02ee22db78
commit bcc8d6910b
4 changed files with 182 additions and 71 deletions

View file

@ -3,15 +3,16 @@ use std::time::Instant;
use lsp_types::request::Completion;
use lsp_types::{
CompletionItem, CompletionItemKind, CompletionParams, CompletionResponse, Documentation, Url,
CompletionItem, CompletionItemKind, CompletionItemLabelDetails, CompletionParams,
CompletionResponse, Documentation, TextEdit, Url,
};
use ruff_db::source::{line_index, source_text};
use ruff_source_file::OneIndexed;
use ty_ide::{CompletionSettings, completion};
use ruff_text_size::Ranged;
use ty_ide::{CompletionKind, CompletionSettings, completion};
use ty_project::ProjectDatabase;
use ty_python_semantic::CompletionKind;
use crate::document::PositionExt;
use crate::document::{PositionExt, ToRangeExt};
use crate::server::api::traits::{
BackgroundDocumentRequestHandler, RequestHandler, RetriableRequestHandler,
};
@ -70,11 +71,27 @@ impl BackgroundDocumentRequestHandler for CompletionRequestHandler {
.enumerate()
.map(|(i, comp)| {
let kind = comp.kind(db).map(ty_kind_to_lsp_kind);
let type_display = comp.ty.map(|ty| ty.display(db).to_string());
let import_edit = comp.import.as_ref().map(|edit| {
let range =
edit.range()
.to_lsp_range(&source, &line_index, snapshot.encoding());
TextEdit {
range,
new_text: edit.content().map(ToString::to_string).unwrap_or_default(),
}
});
CompletionItem {
label: comp.inner.name.into(),
label: comp.name.into(),
kind,
sort_text: Some(format!("{i:-max_index_len$}")),
detail: comp.inner.ty.map(|ty| ty.display(db).to_string()),
detail: type_display.clone(),
label_details: Some(CompletionItemLabelDetails {
detail: type_display,
description: comp.module_name.map(ToString::to_string),
}),
insert_text: comp.insert.map(String::from),
additional_text_edits: import_edit.map(|edit| vec![edit]),
documentation: comp
.documentation
.map(|docstring| Documentation::String(docstring.render_plaintext())),