cargo-lsp 0.0.5

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

use mylsp::prelude::*;

use crate::{
    cargo::{props, tables},
    toml::TomlCursor,
};

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

impl Doc {
    pub fn new(uri: &str, text: String) -> 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(&text, &tree);
        Self {
            uri: uri.to_string(),
            text,
            tree,
            diagnostics,
        }
    }

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

    pub fn completions_at(&self, pos: Position) -> CompletionList {
        let mut tt: TomlCursor = self.cursor();
        let mut items = vec![];
        let prefix = tt.prefix(pos);
        let list = if prefix.is_empty() {
            tables()
        } else {
            props(&prefix)
        };
        for prop in list {
            items.push(CompletionItem {
                label: prop,
                kind: Some(CompletionItemKind::Keyword),
                detail: None,
            });
        }
        CompletionList {
            is_incomplete: false,
            items,
        }
    }

    fn cursor(&self) -> TomlCursor<'_> {
        (self.text.as_str(), self.tree.walk()).into()
    }
}

#[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(text: &str, tree: &Tree) -> Diagnostics {
    let mut res = vec![];
    let mut tt: TomlCursor = (text, tree.walk()).into();
    tt.visit_all(|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,
        },
    }
}

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

    #[test]
    fn test_empty() {
        let doc = Doc::new("", String::new());
        assert_no_errors(&doc);
    }

    #[test]
    fn test_error() {
        let doc = Doc::new("", "[a]b".to_string());
        assert_errors(&doc, &[r(0, 0, 0, 4)]);
    }

    #[test]
    fn test_error_nested() {
        let doc = Doc::new(
            "",
            r#"
[a]
b={c="d"}
[a]b
            "#
            .to_string(),
        );
        assert_errors(&doc, &[r(3, 3, 4, 0)]);
    }

    #[test]
    fn test_completions_empty_doc() {
        let doc = Doc::new("", String::new());
        assert_completions(
            &doc,
            (0, 0).into(),
            &[
                c("[package]", KW),
                c("[features]", KW),
                c("[dependencies]", KW),
                c("[dev-dependencies]", KW),
                c("[build-dependencies]", KW),
                c("[lints]", KW),
                c("[lints.clippy]", KW),
            ],
        );
    }

    #[test]
    fn test_completions_package_doc() {
        let doc = Doc::new(
            "",
            r"
[package]
na
            "
            .to_string(),
        );
        assert_completions(
            &doc,
            (2, 2).into(),
            &[
                c("name", KW),
                c("version", KW),
                c("edition", KW),
                c("authors", KW),
                c("license", KW),
                c("description", KW),
                c("homepage", KW),
                c("repository", KW),
                c("documentation", KW),
                c("readme", KW),
                c("keywords", KW),
                c("categories", KW),
            ],
        );
    }

    const KW: CompletionItemKind = CompletionItemKind::Keyword;

    // c for completions
    fn c(text: &str, kind: CompletionItemKind) -> CompletionItem {
        CompletionItem {
            label: text.to_string(),
            kind: Some(kind),
            detail: None,
        }
    }

    fn assert_completions(doc: &Doc, pos: Position, completions: &[CompletionItem]) {
        let dcs_at = doc.completions_at(pos);
        let mut dcs = dcs_at.items.iter();
        for c in completions {
            let dc = dcs
                .next()
                .expect("Assert failed, completions count not matching");
            assert_eq!(dc, c);
        }
        assert!(dcs.next().is_none());
    }

    // r for range
    fn r(
        start_line: usize,
        start_character: usize,
        end_line: usize,
        end_character: usize,
    ) -> Range {
        ((start_line, start_character).into()..(end_line, end_character).into()).into()
    }

    fn assert_no_errors(doc: &Doc) {
        assert!(doc.diagnostics().is_empty());
        let root = doc.tree.root_node();
        assert!(!root.has_error());
    }

    fn assert_errors(doc: &Doc, ranges: &[Range]) {
        let mut dgs = doc.diagnostics().iter();
        for range in ranges {
            let dg = dgs
                .next()
                .expect("Assert failed, ranges and diagnostic count not matching");
            assert_eq!(&dg.range, range);
        }
        assert!(dgs.next().is_none());

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