dbml_language_server/
state.rs

1// src/state.rs
2use crate::ast::Document;
3use crate::analysis::semantic::SemanticModel;
4use lsp_types::{Diagnostic, Url};
5
6#[derive(Debug)]
7#[allow(dead_code)]
8pub struct DocumentState {
9    pub uri: Url,
10    pub content: String,
11    pub version: i32,
12    pub ast: Option<Document>,
13    pub semantic_model: Option<SemanticModel>,
14    pub diagnostics: Vec<Diagnostic>,
15}
16
17impl DocumentState {
18    pub fn new(uri: Url, content: String, version: i32) -> Self {
19        Self {
20            uri,
21            content,
22            version,
23            ast: None,
24            semantic_model: None,
25            diagnostics: Vec::new(),
26        }
27    }
28
29    pub fn update_content(&mut self, content: String, version: i32) {
30        self.content = content;
31        self.version = version;
32    }
33
34    pub fn analyze(&mut self) {
35        use crate::analysis::parser::parse;
36
37        // Parse the content
38        match parse(&self.content) {
39            Ok(ast) => {
40                // Perform semantic analysis
41                let semantic_model = SemanticModel::analyze(&ast);
42
43                // Convert semantic errors to diagnostics
44                self.diagnostics = semantic_model.to_diagnostics(&self.content);
45
46                self.ast = Some(ast);
47                self.semantic_model = Some(semantic_model);
48            }
49            Err(parse_errors) => {
50                // Convert parse errors to diagnostics
51                self.diagnostics = parse_errors
52                    .iter()
53                    .map(|err| Diagnostic {
54                        range: lsp_types::Range::new(
55                            lsp_types::Position::new(0, 0),
56                            lsp_types::Position::new(0, 0),
57                        ),
58                        severity: Some(lsp_types::DiagnosticSeverity::ERROR),
59                        code: None,
60                        code_description: None,
61                        source: Some("dbml-lsp".to_string()),
62                        message: format!("Parse error: {:?}", err),
63                        related_information: None,
64                        tags: None,
65                        data: None,
66                    })
67                    .collect();
68
69                self.ast = None;
70                self.semantic_model = None;
71            }
72        }
73    }
74}