aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui/completion.rs
blob: 74a82dabbf8e2bf4a3ae44888ec0f427c5f4369e (plain) (blame)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use crate::compositor::{Component, Compositor, Context, EventResult};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use tui::buffer::Buffer as Surface;

use std::borrow::Cow;

use helix_core::{Position, Transaction};
use helix_view::{
    graphics::{Color, Rect, Style},
    Editor,
};

use crate::commands;
use crate::ui::{menu, Markdown, Menu, Popup, PromptEvent};

use helix_lsp::lsp;
use lsp::CompletionItem;

impl menu::Item for CompletionItem {
    fn filter_text(&self) -> &str {
        self.filter_text.as_ref().unwrap_or(&self.label).as_str()
    }

    fn label(&self) -> &str {
        self.label.as_str()
    }

    fn row(&self) -> menu::Row {
        menu::Row::new(vec![
            menu::Cell::from(self.label.as_str()),
            menu::Cell::from(match self.kind {
                Some(lsp::CompletionItemKind::Text) => "text",
                Some(lsp::CompletionItemKind::Method) => "method",
                Some(lsp::CompletionItemKind::Function) => "function",
                Some(lsp::CompletionItemKind::Constructor) => "constructor",
                Some(lsp::CompletionItemKind::Field) => "field",
                Some(lsp::CompletionItemKind::Variable) => "variable",
                Some(lsp::CompletionItemKind::Class) => "class",
                Some(lsp::CompletionItemKind::Interface) => "interface",
                Some(lsp::CompletionItemKind::Module) => "module",
                Some(lsp::CompletionItemKind::Property) => "property",
                Some(lsp::CompletionItemKind::Unit) => "unit",
                Some(lsp::CompletionItemKind::Value) => "value",
                Some(lsp::CompletionItemKind::Enum) => "enum",
                Some(lsp::CompletionItemKind::Keyword) => "keyword",
                Some(lsp::CompletionItemKind::Snippet) => "snippet",
                Some(lsp::CompletionItemKind::Color) => "color",
                Some(lsp::CompletionItemKind::File) => "file",
                Some(lsp::CompletionItemKind::Reference) => "reference",
                Some(lsp::CompletionItemKind::Folder) => "folder",
                Some(lsp::CompletionItemKind::EnumMember) => "enum_member",
                Some(lsp::CompletionItemKind::Constant) => "constant",
                Some(lsp::CompletionItemKind::Struct) => "struct",
                Some(lsp::CompletionItemKind::Event) => "event",
                Some(lsp::CompletionItemKind::Operator) => "operator",
                Some(lsp::CompletionItemKind::TypeParameter) => "type_param",
                None => "",
            }),
            // self.detail.as_deref().unwrap_or("")
            // self.label_details
            //     .as_ref()
            //     .or(self.detail())
            //     .as_str(),
        ])
    }
}

/// Wraps a Menu.
pub struct Completion {
    popup: Popup<Menu<CompletionItem>>,
    trigger_offset: usize,
    // TODO: maintain a completioncontext with trigger kind & trigger char
}

impl Completion {
    pub fn new(
        items: Vec<CompletionItem>,
        offset_encoding: helix_lsp::OffsetEncoding,
        trigger_offset: usize,
    ) -> Self {
        // let items: Vec<CompletionItem> = Vec::new();
        let mut menu = Menu::new(items, move |editor: &mut Editor, item, event| {
            match event {
                PromptEvent::Abort => {}
                PromptEvent::Validate => {
                    let (view, doc) = current!(editor);

                    // always present here
                    let item = item.unwrap();

                    use helix_lsp::{lsp, util};

                    // if more text was entered, remove it
                    let cursor = doc.selection(view.id).cursor();
                    if trigger_offset < cursor {
                        let remove = Transaction::change(
                            doc.text(),
                            vec![(trigger_offset, cursor, None)].into_iter(),
                        );
                        doc.apply(&remove, view.id);
                    }

                    use helix_lsp::OffsetEncoding;
                    let transaction = if let Some(edit) = &item.text_edit {
                        let edit = match edit {
                            lsp::CompletionTextEdit::Edit(edit) => edit.clone(),
                            lsp::CompletionTextEdit::InsertAndReplace(item) => {
                                unimplemented!("completion: insert_and_replace {:?}", item)
                            }
                        };
                        util::generate_transaction_from_edits(
                            doc.text(),
                            vec![edit],
                            offset_encoding, // TODO: should probably transcode in Client
                        )
                    } else {
                        let text = item.insert_text.as_ref().unwrap_or(&item.label);
                        let cursor = doc.selection(view.id).cursor();
                        Transaction::change(
                            doc.text(),
                            vec![(cursor, cursor, Some(text.as_str().into()))].into_iter(),
                        )
                    };

                    doc.apply(&transaction, view.id);

                    if let Some(additional_edits) = &item.additional_text_edits {
                        // gopls uses this to add extra imports
                        if !additional_edits.is_empty() {
                            let transaction = util::generate_transaction_from_edits(
                                doc.text(),
                                additional_edits.clone(),
                                offset_encoding, // TODO: should probably transcode in Client
                            );
                            doc.apply(&transaction, view.id);
                        }
                    }
                }
                _ => (),
            };
        });
        let popup = Popup::new(menu);
        Self {
            popup,
            trigger_offset,
        }
    }

    pub fn update(&mut self, cx: &mut commands::Context) {
        // recompute menu based on matches
        let menu = self.popup.contents_mut();
        let (view, doc) = current!(cx.editor);

        // cx.hooks()
        // cx.add_hook(enum type,  ||)
        // cx.trigger_hook(enum type, &str, ...) <-- there has to be enough to identify doc/view
        // callback with editor & compositor
        //
        // trigger_hook sends event into channel, that's consumed in the global loop and
        // triggers all registered callbacks
        // TODO: hooks should get processed immediately so maybe do it after select!(), before
        // looping?

        let cursor = doc.selection(view.id).cursor();
        if self.trigger_offset <= cursor {
            let fragment = doc.text().slice(self.trigger_offset..cursor);
            let text = Cow::from(fragment);
            // TODO: logic is same as ui/picker
            menu.score(&text);
        }
    }

    pub fn is_empty(&self) -> bool {
        self.popup.contents().is_empty()
    }
}

// need to:
// - trigger on the right trigger char
//   - detect previous open instance and recycle
// - update after input, but AFTER the document has changed
// - if no more matches, need to auto close
//
// missing bits:
// - a more robust hook system: emit to a channel, process in main loop
// - a way to find specific layers in compositor
// - components register for hooks, then unregister when terminated
// ... since completion is a special case, maybe just build it into doc/render?

impl Component for Completion {
    fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
        // let the Editor handle Esc instead
        if let Event::Key(KeyEvent {
            code: KeyCode::Esc, ..
        }) = event
        {
            return EventResult::Ignored;
        }
        self.popup.handle_event(event, cx)
    }

    fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
        self.popup.required_size(viewport)
    }

    fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
        self.popup.render(area, surface, cx);

        // if we have a selection, render a markdown popup on top/below with info
        if let Some(option) = self.popup.contents().selection() {
            // need to render:
            // option.detail
            // ---
            // option.documentation

            let (view, doc) = current!(cx.editor);
            let language = doc
                .language()
                .and_then(|scope| scope.strip_prefix("source."))
                .unwrap_or("");
            let cursor_pos = doc.selection(view.id).cursor();
            let cursor_pos = (helix_core::coords_at_pos(doc.text().slice(..), cursor_pos).row
                - view.first_line) as u16;

            let doc = match &option.documentation {
                Some(lsp::Documentation::String(contents))
                | Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
                    kind: lsp::MarkupKind::PlainText,
                    value: contents,
                })) => {
                    // TODO: convert to wrapped text
                    Markdown::new(
                        format!(
                            "```{}\n{}\n```\n{}",
                            language,
                            option.detail.as_deref().unwrap_or_default(),
                            contents.clone()
                        ),
                        cx.editor.syn_loader.clone(),
                    )
                }
                Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
                    kind: lsp::MarkupKind::Markdown,
                    value: contents,
                })) => {
                    // TODO: set language based on doc scope
                    Markdown::new(
                        format!(
                            "```{}\n{}\n```\n{}",
                            language,
                            option.detail.as_deref().unwrap_or_default(),
                            contents.clone()
                        ),
                        cx.editor.syn_loader.clone(),
                    )
                }
                None if option.detail.is_some() => {
                    // TODO: copied from above

                    // TODO: set language based on doc scope
                    Markdown::new(
                        format!(
                            "```{}\n{}\n```",
                            language,
                            option.detail.as_deref().unwrap_or_default(),
                        ),
                        cx.editor.syn_loader.clone(),
                    )
                }
                None => return,
            };

            let half = area.height / 2;
            let height = 15.min(half);
            // we want to make sure the cursor is visible (not hidden behind the documentation)
            let y = if cursor_pos + view.area.y
                >= (cx.editor.tree.area().height - height - 2/* statusline + commandline */)
            {
                0
            } else {
                // -2 to subtract command line + statusline. a bit of a hack, because of splits.
                area.height.saturating_sub(height).saturating_sub(2)
            };

            let area = Rect::new(0, y, area.width, height);

            // clear area
            let background = cx.editor.theme.get("ui.popup");
            surface.clear_with(area, background);
            doc.render(area, surface, cx);
        }
    }
}