Skip to content

Commit 7d9f8e2

Browse files
simplify, keep api boundary, add traits
1 parent 1fd51cc commit 7d9f8e2

File tree

8 files changed

+89
-123
lines changed

8 files changed

+89
-123
lines changed

crates/pg_completions/src/builder.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
use tower_lsp::lsp_types::CompletionItem;
2-
3-
use crate::{item::CompletionItemWithScore, CompletionResult};
1+
use crate::{item::CompletionItem, CompletionResult};
42

53
pub(crate) struct CompletionBuilder {
6-
items: Vec<CompletionItemWithScore>,
4+
items: Vec<CompletionItem>,
75
}
86

97
impl CompletionBuilder {
108
pub fn new() -> Self {
119
CompletionBuilder { items: vec![] }
1210
}
1311

14-
pub fn add_item(&mut self, item: CompletionItemWithScore) {
12+
pub fn add_item(&mut self, item: CompletionItem) {
1513
self.items.push(item);
1614
}
1715

1816
pub fn finish(mut self) -> CompletionResult {
19-
self.items.sort_by(|a, b| {
20-
b.score
21-
.cmp(&a.score)
22-
.then_with(|| a.label().cmp(&b.label()))
23-
});
17+
self.items
18+
.sort_by(|a, b| b.score.cmp(&a.score).then_with(|| a.label.cmp(&b.label)));
2419

25-
self.items.dedup_by(|a, b| a.label() == b.label());
20+
self.items.dedup_by(|a, b| a.label == b.label);
2621
self.items.truncate(crate::LIMIT);
2722

2823
let should_preselect_first_item = self.should_preselect_first_item();
@@ -33,7 +28,7 @@ impl CompletionBuilder {
3328
.enumerate()
3429
.map(|(idx, mut item)| {
3530
if idx == 0 {
36-
item.set_preselected(should_preselect_first_item);
31+
item.preselected = Some(should_preselect_first_item);
3732
}
3833
item.into()
3934
})

crates/pg_completions/src/complete.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use text_size::TextSize;
2-
use tower_lsp::lsp_types::CompletionItem;
32

4-
use crate::{builder::CompletionBuilder, context::CompletionContext, providers::complete_tables};
3+
use crate::{
4+
builder::CompletionBuilder, context::CompletionContext, item::CompletionItem,
5+
providers::complete_tables,
6+
};
57

68
pub const LIMIT: usize = 50;
79

@@ -18,6 +20,14 @@ pub struct CompletionResult {
1820
pub items: Vec<CompletionItem>,
1921
}
2022

23+
impl IntoIterator for CompletionResult {
24+
type Item = CompletionItem;
25+
type IntoIter = <Vec<CompletionItem> as IntoIterator>::IntoIter;
26+
fn into_iter(self) -> Self::IntoIter {
27+
self.items.into_iter()
28+
}
29+
}
30+
2131
pub fn complete(params: CompletionParams) -> CompletionResult {
2232
let ctx = CompletionContext::new(&params);
2333

crates/pg_completions/src/data.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

crates/pg_completions/src/item.rs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,49 @@
1-
use tower_lsp::lsp_types::{self, CompletionItem};
2-
3-
use crate::{data::CompletionItemData, relevance::CompletionRelevance};
1+
#[derive(Debug)]
2+
pub enum CompletionItemKind {
3+
Table,
4+
}
45

56
#[derive(Debug)]
6-
pub struct CompletionItemWithScore {
7-
pub item: lsp_types::CompletionItem,
8-
pub score: i32,
7+
pub struct CompletionItem {
8+
pub label: String,
9+
pub(crate) score: i32,
10+
pub description: String,
11+
pub preselected: Option<bool>,
12+
pub kind: CompletionItemKind,
913
}
1014

11-
impl CompletionItemWithScore {
12-
pub(crate) fn new(data: CompletionItemData, relevance: CompletionRelevance) -> Self {
13-
Self {
14-
item: data.into(),
15-
score: relevance.score(),
15+
impl From<CompletionItem> for tower_lsp::lsp_types::CompletionItem {
16+
fn from(i: CompletionItem) -> Self {
17+
tower_lsp::lsp_types::CompletionItem {
18+
label: i.label,
19+
label_details: Some(tower_lsp::lsp_types::CompletionItemLabelDetails {
20+
description: Some(i.description),
21+
detail: None,
22+
}),
23+
kind: Some(i.kind.into()),
24+
detail: None,
25+
documentation: None,
26+
deprecated: None,
27+
preselect: None,
28+
sort_text: None,
29+
filter_text: None,
30+
insert_text: None,
31+
insert_text_format: None,
32+
insert_text_mode: None,
33+
text_edit: None,
34+
additional_text_edits: None,
35+
commit_characters: None,
36+
data: None,
37+
tags: None,
38+
command: None,
1639
}
1740
}
18-
19-
pub(crate) fn label(&self) -> &str {
20-
&self.item.label
21-
}
22-
23-
pub(crate) fn set_preselected(&mut self, is_preselected: bool) {
24-
self.item.preselect = Some(is_preselected)
25-
}
2641
}
2742

28-
impl Into<CompletionItem> for CompletionItemWithScore {
29-
fn into(self) -> CompletionItem {
30-
self.item
43+
impl From<CompletionItemKind> for tower_lsp::lsp_types::CompletionItemKind {
44+
fn from(value: CompletionItemKind) -> Self {
45+
match value {
46+
CompletionItemKind::Table => tower_lsp::lsp_types::CompletionItemKind::CLASS,
47+
}
3148
}
3249
}

crates/pg_completions/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod builder;
22
mod complete;
33
mod context;
4-
mod data;
54
mod item;
65
mod providers;
76
mod relevance;
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
1+
use pg_schema_cache::Table;
2+
13
use crate::{
2-
builder::CompletionBuilder, context::CompletionContext, data::CompletionItemData,
3-
item::CompletionItemWithScore, relevance::CompletionRelevance,
4+
builder::CompletionBuilder,
5+
context::CompletionContext,
6+
item::{CompletionItem, CompletionItemKind},
7+
relevance::CompletionRelevance,
48
};
59

610
pub fn complete_tables(ctx: &CompletionContext, builder: &mut CompletionBuilder) {
711
let available_tables = &ctx.schema_cache.tables;
812

9-
let completion_items: Vec<CompletionItemWithScore> = available_tables
13+
let completion_items: Vec<CompletionItem> = available_tables
1014
.iter()
11-
.map(|table| {
12-
let data = CompletionItemData::Table(table);
13-
let relevance = CompletionRelevance::new(&data, ctx);
14-
CompletionItemWithScore::new(data, relevance)
15+
.map(|table| CompletionItem {
16+
label: table.name.clone(),
17+
score: get_score(ctx, table),
18+
description: format!("Schema: {}", table.schema),
19+
preselected: None,
20+
kind: CompletionItemKind::Table,
1521
})
1622
.collect();
1723

1824
for item in completion_items {
1925
builder.add_item(item);
2026
}
2127
}
28+
29+
fn get_score(ctx: &CompletionContext, table: &Table) -> i32 {
30+
let mut relevance = CompletionRelevance::default();
31+
32+
relevance.check_matches_query_input(ctx, &table.name);
33+
relevance.check_matches_schema(ctx, &table.schema);
34+
relevance.check_if_catalog(ctx);
35+
36+
relevance.score()
37+
}

crates/pg_completions/src/relevance.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{context::CompletionContext, data::CompletionItemData};
1+
use crate::context::CompletionContext;
22

33
#[derive(Debug, Default)]
44
pub(crate) struct CompletionRelevance {
@@ -10,21 +10,7 @@ impl CompletionRelevance {
1010
self.score
1111
}
1212

13-
pub fn new(data: &CompletionItemData, ctx: &CompletionContext) -> Self {
14-
let mut relevance = CompletionRelevance::default();
15-
16-
match data {
17-
CompletionItemData::Table(tb) => {
18-
relevance.check_if_catalog(ctx);
19-
relevance.check_matches_schema(ctx, &tb.schema);
20-
relevance.check_matches_query_input(ctx, &tb.name);
21-
}
22-
}
23-
24-
relevance
25-
}
26-
27-
fn check_matches_query_input(&mut self, ctx: &CompletionContext, name: &str) {
13+
pub fn check_matches_query_input(&mut self, ctx: &CompletionContext, name: &str) {
2814
let node = ctx.ts_node.unwrap();
2915

3016
let content = match ctx.get_ts_node_content(node) {
@@ -42,7 +28,7 @@ impl CompletionRelevance {
4228
};
4329
}
4430

45-
fn check_matches_schema(&mut self, ctx: &CompletionContext, schema: &str) {
31+
pub fn check_matches_schema(&mut self, ctx: &CompletionContext, schema: &str) {
4632
if ctx.schema_name.is_none() {
4733
return;
4834
}
@@ -56,7 +42,7 @@ impl CompletionRelevance {
5642
}
5743
}
5844

59-
fn check_if_catalog(&mut self, ctx: &CompletionContext) {
45+
pub fn check_if_catalog(&mut self, ctx: &CompletionContext) {
6046
if ctx.schema_name.as_ref().is_some_and(|n| n == "pg_catalog") {
6147
return;
6248
}

crates/pg_lsp/src/session.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use pg_workspace::Workspace;
1010
use text_size::TextSize;
1111
use tokio::sync::RwLock;
1212
use tower_lsp::lsp_types::{
13-
CodeActionOrCommand, CompletionItem, CompletionItemKind, CompletionList, Hover, HoverContents,
14-
InlayHint, InlayHintKind, InlayHintLabel, MarkedString, Position, Range,
13+
CodeActionOrCommand, CompletionItem, CompletionList, Hover, HoverContents, InlayHint,
14+
InlayHintKind, InlayHintLabel, MarkedString, Position, Range,
1515
};
1616

1717
use crate::{db_connection::DbConnection, utils::line_index_ext::LineIndexExt};
@@ -235,7 +235,7 @@ impl Session {
235235

236236
let schema_cache = ide.schema_cache.read().expect("No Schema Cache");
237237

238-
let completion_items = pg_completions::complete(CompletionParams {
238+
let completion_items: Vec<CompletionItem> = pg_completions::complete(CompletionParams {
239239
position: offset - range.start() - TextSize::from(1),
240240
text: &stmt.text,
241241
tree: ide
@@ -245,29 +245,8 @@ impl Session {
245245
.and_then(|t| Some(t.as_ref())),
246246
schema: &schema_cache,
247247
})
248-
.items
249248
.into_iter()
250-
.map(|i| CompletionItem {
251-
// TODO: add more data
252-
label: i.label,
253-
label_details: None,
254-
kind: Some(CompletionItemKind::CLASS),
255-
detail: None,
256-
documentation: None,
257-
deprecated: None,
258-
preselect: None,
259-
sort_text: None,
260-
filter_text: None,
261-
insert_text: None,
262-
insert_text_format: None,
263-
insert_text_mode: None,
264-
text_edit: None,
265-
additional_text_edits: None,
266-
commit_characters: None,
267-
data: None,
268-
tags: None,
269-
command: None,
270-
})
249+
.map(|i| i.into())
271250
.collect();
272251

273252
Some(CompletionList {

0 commit comments

Comments
 (0)