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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use std::io::Cursor;
use std::ops::{Range, RangeInclusive};
use ariadne::{Label, Report, ReportKind, Source};
use serde::Serialize;
use tsify::Tsify;
/// Completion result with an exclusive range matching CodeMirror's convention.
/// `at` is a half-open range [start, end) where `start..end` covers the text to replace.
/// An empty range like `5..5` means "insert at position 5, replace nothing."
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Tsify)]
#[tsify(into_wasm_abi)]
pub struct Completion {
pub at: Range<usize>,
pub filter: Option<bool>,
pub items: Vec<CompletionItem>,
}
impl Completion {
/// Create a new Completion from an inclusive interval (as produced by `interval()`).
/// Converts the inclusive end to an exclusive end internally.
pub fn new(at: RangeInclusive<usize>) -> Self {
Self::new_exclusive(*at.start()..*at.end() + 1)
}
/// Create a new Completion from an exclusive range (matching CodeMirror's convention and allowing empty ranges).
pub fn new_exclusive(at: Range<usize>) -> Self {
Self {
at,
filter: None,
items: Vec::default(),
}
}
pub fn add_item(&mut self, item: CompletionItem) {
self.items.push(item);
}
pub fn add_items(&mut self, items: Vec<CompletionItem>) {
self.items.extend(items);
}
pub fn filter(&mut self, filter: bool) {
self.filter = Some(filter);
}
pub fn make_pretty(&self, hamelin: &String) -> Result<String, std::fmt::Error> {
// Needed to make write!(string_buf, ...) work
use std::fmt::Write;
let input = hamelin.as_str();
let source = Source::from(input);
let mut string_buf = String::new();
let mut colors = ariadne::ColorGenerator::default();
let kind = ReportKind::Advice;
let mut report =
Report::build(kind, self.at.clone()).with_message("Completion suggestions to offer");
for item in self.items.iter() {
report = report.with_label(
Label::new(self.at.clone())
.with_message(format!(
"{} {}({:?})",
item.label,
item.detail
.as_ref()
.map(|d| format!("[{}] ", d))
.unwrap_or_default(),
item.kind
))
.with_color(colors.next()),
);
}
if let Some(filter) = self.filter {
report = report.with_note(format!("editor should filter: {}", filter))
}
let final_report = report.finish();
let buf = Vec::new();
let mut cursor = Cursor::new(buf);
final_report
.write(source, &mut cursor)
.map_err(|_| std::fmt::Error)?;
write!(string_buf, "{}", String::from_utf8_lossy(cursor.get_ref()))?;
Ok(string_buf)
}
}
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Tsify)]
#[tsify(into_wasm_abi)]
pub struct CompletionItem {
/// What to show
pub label: String,
/// What to actually put down
pub apply_string: String,
/// Small text next to the label.
pub detail: Option<String>,
/// Floating shit out to the right.
pub description: Option<String>,
/// Icon
pub kind: CompletionItemKind,
// TODO: Structure this, somehow?
// Yeeting back code-mirror snippet style doesn't seem right.
pub snippet: Option<String>,
/// The group header this item sorts under. Currently only ever a space name.
///
/// Unlike [`Self::boost`] this is not a nudge. Members of a section never interleave
/// with anything outside it: the editor ranks the sections against each other, by the
/// best-matching member of each, and every member then moves as a block. An item with
/// no section takes no group offset at all, which places it above *every* section
/// however badly it matches -- so leaving something unsectioned is a strong claim, not
/// a neutral default.
///
/// Most completions do carry no section -- fields, functions, commands, keywords --
/// which is only harmless because they never share a completion set with sectioned
/// ones. Among the `FROM` dataset completions, which do, pipeline definitions are the
/// only unsectioned item.
pub section: Option<String>,
/// Ranking nudge applied by the editor on top of its own match score.
///
/// Only meaningful when the editor is doing the filtering: a completion that
/// sets `filter(false)` is emitted in order and this is ignored. Keep values
/// inside ±99, which is what CodeMirror documents.
pub boost: Option<i32>,
}
impl CompletionItem {
pub fn new<S>(label: S) -> Self
where
S: Into<String> + Clone,
{
Self {
label: label.clone().into(),
apply_string: label.into(),
detail: None,
description: None,
kind: CompletionItemKind::Text,
snippet: None,
section: None,
boost: None,
}
}
pub fn with_apply_string<S>(mut self, apply_string: S) -> Self
where
S: Into<String>,
{
self.apply_string = apply_string.into();
self
}
pub fn with_detail<S>(mut self, detail: S) -> Self
where
S: Into<String>,
{
self.detail = Some(detail.into());
self
}
pub fn with_description<S>(mut self, description: S) -> Self
where
S: Into<String>,
{
self.description = Some(description.into());
self
}
pub fn with_kind(mut self, kind: CompletionItemKind) -> Self {
self.kind = kind;
self
}
pub fn with_snippet(mut self, snippet: String) -> Self {
self.snippet = Some(snippet);
self
}
pub fn with_section(mut self, section: String) -> Self {
self.section = Some(section);
self
}
pub fn with_boost(mut self, boost: i32) -> Self {
self.boost = Some(boost);
self
}
}
#[derive(Debug, Clone, Serialize, PartialEq, Eq, Tsify)]
#[tsify(into_wasm_abi)]
pub enum CompletionItemKind {
Text,
Variable,
Function,
Command,
Keyword,
}