|
| 1 | +use crate::session::Session; |
| 2 | +use anyhow::Result; |
| 3 | +use pg_workspace_new::workspace::CompletionParams; |
| 4 | +use text_size::TextSize; |
| 5 | +use tower_lsp::lsp_types::{self, CompletionItem, CompletionItemLabelDetails}; |
| 6 | + |
| 7 | +pub fn get_completions( |
| 8 | + session: &Session, |
| 9 | + params: lsp_types::CompletionParams, |
| 10 | +) -> Result<lsp_types::CompletionResponse> { |
| 11 | + let pos = params.text_document_position.position; |
| 12 | + let url = params.text_document_position.text_document.uri; |
| 13 | + |
| 14 | + let path = session.file_path(&url)?; |
| 15 | + |
| 16 | + let completion_result = session.workspace.get_completions(CompletionParams { |
| 17 | + path, |
| 18 | + position: TextSize::from(pos.character), |
| 19 | + })?; |
| 20 | + |
| 21 | + let items: Vec<CompletionItem> = completion_result |
| 22 | + .into_iter() |
| 23 | + .map(|i| CompletionItem { |
| 24 | + label: i.label, |
| 25 | + label_details: Some(CompletionItemLabelDetails { |
| 26 | + description: Some(i.description), |
| 27 | + detail: None, |
| 28 | + }), |
| 29 | + preselect: Some(i.preselected), |
| 30 | + kind: Some(to_lsp_types_completion_item_kind(i.kind)), |
| 31 | + ..CompletionItem::default() |
| 32 | + }) |
| 33 | + .collect(); |
| 34 | + |
| 35 | + Ok(lsp_types::CompletionResponse::Array(items)) |
| 36 | +} |
| 37 | + |
| 38 | +fn to_lsp_types_completion_item_kind( |
| 39 | + pg_comp_kind: pg_completions::CompletionItemKind, |
| 40 | +) -> lsp_types::CompletionItemKind { |
| 41 | + match pg_comp_kind { |
| 42 | + pg_completions::CompletionItemKind::Function |
| 43 | + | pg_completions::CompletionItemKind::Table => lsp_types::CompletionItemKind::CLASS, |
| 44 | + } |
| 45 | +} |
0 commit comments