-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathcompletions.rs
76 lines (68 loc) · 2.51 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::session::Session;
use anyhow::Result;
use pgt_workspace::{WorkspaceError, workspace};
use tower_lsp::lsp_types::{self, CompletionItem, CompletionItemLabelDetails};
#[tracing::instrument(level = "trace", skip_all)]
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 client_capabilities = session
.client_capabilities()
.expect("Client capabilities not established for current session.");
let line_index = session
.document(&url)
.map(|doc| doc.line_index)
.map_err(|_| anyhow::anyhow!("Document not found."))?;
let offset = pgt_lsp_converters::from_proto::offset(
&line_index,
params.text_document_position.position,
pgt_lsp_converters::negotiated_encoding(client_capabilities),
)?;
let completion_result =
match session
.workspace
.get_completions(workspace::GetCompletionsParams {
path,
position: offset,
}) {
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_workspace::workspace::CompletionItemKind,
) -> lsp_types::CompletionItemKind {
match pg_comp_kind {
pgt_workspace::workspace::CompletionItemKind::Function => {
lsp_types::CompletionItemKind::FUNCTION
}
pgt_workspace::workspace::CompletionItemKind::Table => lsp_types::CompletionItemKind::CLASS,
pgt_workspace::workspace::CompletionItemKind::Column => {
lsp_types::CompletionItemKind::FIELD
}
}
}