1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use cairo_lang_diagnostics::DiagnosticLocation;
use cairo_lang_filesystem::span::TextSpan;
use cairo_lang_syntax::node::ids::SyntaxStablePtrId;
use cairo_lang_syntax::node::TypedSyntaxNode;

use crate::db::DefsGroup;
use crate::ids::ModuleFileId;

/// A stable location of a real, concrete syntax.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct StableLocation {
    pub module_file_id: ModuleFileId,
    pub stable_ptr: SyntaxStablePtrId,
}
impl StableLocation {
    pub fn new(module_file_id: ModuleFileId, stable_ptr: SyntaxStablePtrId) -> Self {
        Self { module_file_id, stable_ptr }
    }

    pub fn from_ast<TNode: TypedSyntaxNode>(module_file_id: ModuleFileId, node: &TNode) -> Self {
        Self { module_file_id, stable_ptr: node.as_syntax_node().stable_ptr() }
    }

    /// Returns the [DiagnosticLocation] that corresponds to the [StableLocation].
    pub fn diagnostic_location(&self, db: &dyn DefsGroup) -> DiagnosticLocation {
        let file_id =
            db.module_file(self.module_file_id).expect("Module in diagnostic does not exist");
        let syntax_node = db
            .file_syntax(file_id)
            .expect("File for diagnostic not found")
            .as_syntax_node()
            .lookup_ptr(db.upcast(), self.stable_ptr);
        DiagnosticLocation { file_id, span: syntax_node.span_without_trivia(db.upcast()) }
    }

    /// Returns the [DiagnosticLocation] that corresponds to the [StableLocation].
    pub fn diagnostic_location_until(
        &self,
        db: &dyn DefsGroup,
        until_stable_ptr: SyntaxStablePtrId,
    ) -> DiagnosticLocation {
        let syntax_db = db.upcast();
        let file_id =
            db.module_file(self.module_file_id).expect("Module in diagnostic does not exist");
        let root_node =
            db.file_syntax(file_id).expect("File for diagnostic not found").as_syntax_node();
        let start =
            root_node.lookup_ptr(syntax_db, self.stable_ptr).span_start_without_trivia(syntax_db);
        let end =
            root_node.lookup_ptr(syntax_db, until_stable_ptr).span_end_without_trivia(syntax_db);
        DiagnosticLocation { file_id, span: TextSpan { start, end } }
    }
}