curd-editor 0.27.1

A terminal code editor: file tree, tabs, git, language servers, run and jump to errors, and a panel for Claude or any other assistant
//! The list of suggestions from the language server.

use super::*;

impl Editor {
    // ---- Suggestions ---------------------------------------------------

    /// After a character was typed: narrows the open list of suggestions,
    /// or asks for one at the start of a word and after `.` or `::`.
    pub(super) fn after_typing(&mut self, c: char) {
        if self.completion.is_some() && is_word_char(c) {
            self.refilter();
            return;
        }
        self.completion = None;
        if is_word_char(c) || c == '.' || self.text_before_cursor().ends_with("::") {
            self.ask_completion(false);
        }
    }

    /// Asks the language server what could be typed here. When not asked
    /// for by hand (`manual`), each word is only asked about once.
    pub(super) fn ask_completion(&mut self, manual: bool) {
        let word = (self.word_start(), self.cursor_y);
        if !manual && self.completion_word == Some(word) {
            return;
        }
        let (line, units) = self.lsp_position(self.cursor_x, self.cursor_y);
        // The server must know about the latest edits before it answers.
        self.sync_lsp();
        let request = match (&self.lsp, &self.abs_path) {
            (Some(lsp), Some(path)) if lsp.borrow().is_ready() => {
                lsp.borrow_mut().completion(path, line, units)
            }
            _ => None,
        };
        match request {
            Some(_) => {
                self.completion_word = Some(word);
                self.completion_request = request;
            }
            None if manual => self.message = "No language server to suggest anything".into(),
            None => {}
        }
    }

    /// Keeps the suggestions that match what has been typed of the word:
    /// first those that start with it, then those that contain it. Closes
    /// the list when none are left or the cursor has left the word.
    pub(super) fn refilter(&mut self) {
        let Some(mut completion) = self.completion.take() else {
            return;
        };
        if self.cursor_y != completion.y || self.cursor_x < completion.start {
            return;
        }
        let typed: String = self
            .text_before_cursor()
            .chars()
            .skip(completion.start)
            .collect::<String>()
            .to_lowercase();
        let names: Vec<String> = completion
            .items
            .iter()
            .map(|item| item.filter.to_lowercase())
            .collect();
        let starts_with = |index: &usize| names[*index].starts_with(&typed);
        let only_contains = |index: &usize| !starts_with(index) && names[*index].contains(&typed);
        completion.shown = (0..names.len())
            .filter(starts_with)
            .chain((0..names.len()).filter(only_contains))
            .collect();
        completion.selected = 0;
        completion.moved = false;
        if !completion.shown.is_empty() {
            self.completion = Some(completion);
        }
    }

    pub(super) fn move_selection(&mut self, step: isize) {
        if let Some(completion) = &mut self.completion {
            let count = completion.shown.len() as isize;
            completion.selected = (completion.selected as isize + step).rem_euclid(count) as usize;
            completion.moved = true;
        }
    }

    /// Replaces the part of the word typed so far with the picked suggestion.
    pub(super) fn accept_completion(&mut self) {
        let Some(completion) = self.completion.take() else {
            return;
        };
        let item = &completion.items[completion.shown[completion.selected]];
        while self.cursor_x > completion.start {
            self.backspace();
        }
        self.insert_text(&item.insert);
    }
}