cargo-lsp 0.0.2

LSP for Cargo.toml files
use std::ops::{Deref, DerefMut};
use tree_sitter::{Node, Parser, Tree};

use mylsp::prelude::*;

#[derive(Clone, Debug)]
pub struct Doc {
    uri: String,
    tree: Tree,
    diagnostics: Diagnostics,
}

impl Doc {
    pub fn new(uri: &str, text: &str) -> Self {
        let mut parser = Parser::new();
        parser
            .set_language(tree_sitter_toml::language())
            .expect("Error loading toml grammar");
        let tree = parser
            .parse(text, None)
            .expect("Error parsing with tree-sitter");
        let diagnostics = make_diagnostics(&tree);
        Self {
            uri: uri.to_string(),
            tree,
            diagnostics,
        }
    }

    pub fn diagnostics(&self) -> &Diagnostics {
        &self.diagnostics
    }
}

#[derive(Default, Clone, Debug)]
pub struct Diagnostics(Vec<Diagnostic>);

impl From<Vec<Diagnostic>> for Diagnostics {
    fn from(value: Vec<Diagnostic>) -> Self {
        Self(value)
    }
}

impl Deref for Diagnostics {
    type Target = Vec<Diagnostic>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Diagnostics {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<&Doc> for Notification {
    fn from(value: &Doc) -> Self {
        (
            "textDocument/publishDiagnostics",
            PublishDiagnosticsParams {
                uri: value.uri.clone(),
                version: None,
                diagnostics: value.diagnostics().to_vec(),
            },
        )
            .into()
    }
}

impl From<&Doc> for DocumentDiagnosticReport {
    fn from(value: &Doc) -> Self {
        DocumentDiagnosticReport {
            kind: "full".to_string(),
            result_id: None,
            items: value.diagnostics().to_vec(),
        }
    }
}

fn make_diagnostics(tree: &Tree) -> Diagnostics {
    let mut res = vec![];
    visit_all(tree, |n| {
        if n.is_error() {
            res.push(error_node_to_diagnostic(n));
        }
    });
    res.into()
}

fn error_node_to_diagnostic(n: Node) -> Diagnostic {
    Diagnostic {
        range: node_to_range(&n),
        message: n.to_sexp(),
        severity: Some(DiagnosticSeverity::Error),
        ..Default::default()
    }
}

fn node_to_range(n: &Node) -> Range {
    let start = n.start_position();
    let end = n.end_position();
    Range {
        start: Position {
            line: start.row,
            character: start.column,
        },
        end: Position {
            line: end.row,
            character: end.column,
        },
    }
}

fn visit_all<F: FnMut(Node)>(tree: &Tree, mut f: F) {
    let mut c = tree.walk();
    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;
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_empty() {
        let doc = Doc::new("", "");
        assert!(doc.diagnostics().is_empty());

        let root = doc.tree.root_node();
        assert!(!root.has_error());
        assert_eq!(root.child_count(), 0);
    }

    #[test]
    fn test_error() {
        let doc = Doc::new("", "[a]b");
        assert!(!doc.diagnostics().is_empty());

        let root = doc.tree.root_node();
        assert!(root.has_error());
    }

    #[test]
    fn test_error_nested() {
        let doc = Doc::new(
            "",
            r#"
            [a]
            b={c="d"}
            [a]b
            "#,
        );
        assert!(!doc.diagnostics().is_empty());

        let root = doc.tree.root_node();
        assert!(root.has_error());
    }
}