cargo-lsp 0.0.10

LSP for Cargo.toml files
//! toml syntax related functions

pub mod parsing;

use mylsp::Position;
use tree_sitter::{Node, TreeCursor};

pub struct TomlCursor<'a> {
    text: &'a str,
    cursor: TreeCursor<'a>,
}

impl TomlCursor<'_> {
    pub fn visit_all<F: FnMut(Node)>(&mut self, mut f: F) {
        let c = &mut self.cursor;
        let mut visited = vec![];
        loop {
            let n = c.node();
            if !visited.contains(&n.id()) {
                visited.push(n.id());
                f(n);
                if c.goto_first_child() {
                    continue;
                }
            }
            if !c.goto_next_sibling() && !c.goto_parent() {
                break;
            }
        }
    }

    pub fn blank_line(&mut self, pos: Position) -> bool {
        for (i, l) in self.text.lines().enumerate() {
            if i == pos.line && l.trim().is_empty() {
                return true;
            }
        }
        false
    }
}

impl<'a> From<(&'a str, TreeCursor<'a>)> for TomlCursor<'a> {
    fn from(value: (&'a str, TreeCursor<'a>)) -> Self {
        Self {
            text: value.0,
            cursor: value.1,
        }
    }
}