Debian Codegraph @

helix-term/src/handlers/completion/item.rs

raw symbols SCIP
1 use std::mem;
2
3 use helix_core::completion::CompletionProvider;
4 use helix_lsp::{lsp, LanguageServerId};
5 use helix_view::handlers::completion::ResponseContext;
6
7 pub struct CompletionResponse {
8 pub items: CompletionItems,
9 pub provider: CompletionProvider,
10 pub context: ResponseContext,
11 }
12
13 pub enum CompletionItems {
14 Lsp(Vec<lsp::CompletionItem>),
15 Other(Vec<CompletionItem>),
16 }
17
18 impl CompletionItems {
19 pub fn is_empty(&self) -> bool {
20 match self {
21 CompletionItems::Lsp(items) => items.is_empty(),
22 CompletionItems::Other(items) => items.is_empty(),
23 }
24 }
25 }
26
27 impl CompletionResponse {
28 pub fn take_items(&mut self, dst: &mut Vec<CompletionItem>) {
29 match &mut self.items {
30 CompletionItems::Lsp(items) => dst.extend(items.drain(..).map(|item| {
31 CompletionItem::Lsp(LspCompletionItem {
32 item,
33 provider: match self.provider {
34 CompletionProvider::Lsp(provider) => provider,
35 _ => unreachable!(),
36 },
37 resolved: false,
38 provider_priority: self.context.priority,
39 })
40 })),
41 CompletionItems::Other(items) if dst.is_empty() => mem::swap(dst, items),
42 CompletionItems::Other(items) => dst.append(items),
43 }
44 }
45 }
46
47 #[derive(Debug, PartialEq, Clone)]
48 pub struct LspCompletionItem {
49 pub item: lsp::CompletionItem,
50 pub provider: LanguageServerId,
51 pub resolved: bool,
52 // TODO: we should not be filtering and sorting incomplete completion list
53 // according to the spec but vscode does that anyway and most servers (
54 // including rust-analyzer) rely on that.. so we can't do that without
55 // breaking completions.
56 pub provider_priority: i8,
57 }
58
59 impl LspCompletionItem {
60 #[inline]
61 pub fn filter_text(&self) -> &str {
62 self.item
63 .filter_text
64 .as_ref()
65 .unwrap_or(&self.item.label)
66 .as_str()
67 }
68 }
69
70 #[allow(clippy::large_enum_variant)] // TODO: In a separate PR attempt the `Box<LspCompletionItem>` pattern.
71 #[derive(Debug, PartialEq, Clone)]
72 pub enum CompletionItem {
73 Lsp(LspCompletionItem),
74 Other(helix_core::CompletionItem),
75 }
76
77 impl CompletionItem {
78 #[inline]
79 pub fn filter_text(&self) -> &str {
80 match self {
81 CompletionItem::Lsp(item) => item.filter_text(),
82 CompletionItem::Other(item) => &item.label,
83 }
84 }
85 }
86
87 impl PartialEq<CompletionItem> for LspCompletionItem {
88 fn eq(&self, other: &CompletionItem) -> bool {
89 match other {
90 CompletionItem::Lsp(other) => self == other,
91 _ => false,
92 }
93 }
94 }
95
96 impl PartialEq<CompletionItem> for helix_core::CompletionItem {
97 fn eq(&self, other: &CompletionItem) -> bool {
98 match other {
99 CompletionItem::Other(other) => self == other,
100 _ => false,
101 }
102 }
103 }
104
105 impl CompletionItem {
106 pub fn provider_priority(&self) -> i8 {
107 match self {
108 CompletionItem::Lsp(item) => item.provider_priority,
109 // sorting path completions after LSP for now
110 CompletionItem::Other(_) => 1,
111 }
112 }
113
114 pub fn provider(&self) -> CompletionProvider {
115 match self {
116 CompletionItem::Lsp(item) => CompletionProvider::Lsp(item.provider),
117 CompletionItem::Other(item) => item.provider,
118 }
119 }
120
121 pub fn preselect(&self) -> bool {
122 match self {
123 CompletionItem::Lsp(LspCompletionItem { item, .. }) => item.preselect.unwrap_or(false),
124 CompletionItem::Other(_) => false,
125 }
126 }
127 }