-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathcompletions.rs
54 lines (49 loc) · 1.98 KB
/
completions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::{adapters::get_cursor_position, session::Session};
use anyhow::Result;
use pgt_workspace::{WorkspaceError, features::completions::GetCompletionsParams};
use tower_lsp::lsp_types::{self, CompletionItem, CompletionItemLabelDetails};
#[tracing::instrument(level = "debug", skip(session), err)]
pub fn get_completions(
session: &Session,
params: lsp_types::CompletionParams,
) -> Result<lsp_types::CompletionResponse> {
let url = params.text_document_position.text_document.uri;
let path = session.file_path(&url)?;
let completion_result = match session.workspace.get_completions(GetCompletionsParams {
path,
position: get_cursor_position(session, &url, params.text_document_position.position)?,
}) {
Ok(result) => result,
Err(e) => match e {
WorkspaceError::DatabaseConnectionError(_) => {
return Ok(lsp_types::CompletionResponse::Array(vec![]));
}
_ => {
return Err(e.into());
}
},
};
let items: Vec<CompletionItem> = completion_result
.into_iter()
.map(|i| CompletionItem {
label: i.label,
label_details: Some(CompletionItemLabelDetails {
description: Some(i.description),
detail: None,
}),
preselect: Some(i.preselected),
kind: Some(to_lsp_types_completion_item_kind(i.kind)),
..CompletionItem::default()
})
.collect();
Ok(lsp_types::CompletionResponse::Array(items))
}
fn to_lsp_types_completion_item_kind(
pg_comp_kind: pgt_completions::CompletionItemKind,
) -> lsp_types::CompletionItemKind {
match pg_comp_kind {
pgt_completions::CompletionItemKind::Function => lsp_types::CompletionItemKind::FUNCTION,
pgt_completions::CompletionItemKind::Table => lsp_types::CompletionItemKind::CLASS,
pgt_completions::CompletionItemKind::Column => lsp_types::CompletionItemKind::FIELD,
}
}