tinymist_analysis/
debug_loc.rs

1//! The debug location that can be used to locate a position in a document or a
2//! file.
3
4use serde::{Deserialize, Serialize};
5pub use typst::layout::Position as TypstPosition;
6
7/// A serializable physical position in a document.
8///
9/// Note that it uses [`f32`] instead of [`f64`] as same as
10/// `TypstPosition` for the coordinates to improve both performance
11/// of serialization and calculation. It does sacrifice the floating
12/// precision, but it is enough in our use cases.
13///
14/// Also see `TypstPosition`.
15#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
16pub struct DocumentPosition {
17    /// The page, starting at 1.
18    pub page_no: usize,
19    /// The exact x-coordinate on the page (from the left, as usual).
20    pub x: f32,
21    /// The exact y-coordinate on the page (from the top, as usual).
22    pub y: f32,
23}
24
25impl From<TypstPosition> for DocumentPosition {
26    fn from(position: TypstPosition) -> Self {
27        Self {
28            page_no: position.page.into(),
29            x: position.point.x.to_pt() as f32,
30            y: position.point.y.to_pt() as f32,
31        }
32    }
33}
34
35/// Raw representation of a source span.
36pub type RawSourceSpan = u64;
37
38/// A resolved source (text) location.
39///
40/// See [`CharPosition`] for the definition of the position inside a file.
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct FileLocation {
43    /// The file path.
44    pub filepath: String,
45}
46
47/// A resolved source (text) location.
48///
49/// See [`CharPosition`] for the definition of the position inside a file.
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct SourceLocation {
52    /// The file path.
53    pub filepath: String,
54    /// The position in the file.
55    pub pos: LspPosition,
56}
57
58impl SourceLocation {
59    /// Create a new source location.
60    pub fn from_flat(
61        flat: FlatSourceLocation,
62        i: &impl std::ops::Index<usize, Output = FileLocation>,
63    ) -> Self {
64        Self {
65            filepath: i[flat.filepath as usize].filepath.clone(),
66            pos: flat.pos,
67        }
68    }
69}
70
71/// A flat resolved source (text) location.
72///
73/// See [`CharPosition`] for the definition of the position inside a file.
74#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct FlatSourceLocation {
76    /// The file path.
77    pub filepath: u32,
78    /// The position in the file.
79    pub pos: LspPosition,
80}
81
82/// A resolved file position. The position is encoded in Utf-8, Utf-16 or
83/// Utf-32. The position encoding must be negotiated via some protocol like LSP.
84pub type LspPosition = lsp_types::Position;
85
86/// A resolved file range.
87///
88/// See [`LspPosition`] for the definition of the position inside a file.
89pub type LspRange = lsp_types::Range;
90
91/// The legacy name of the character position.
92#[deprecated(note = "Use `LspPosition` instead.")]
93pub type CharPosition = LspPosition;
94/// The legacy name of the character range.
95#[deprecated(note = "Use `LspRange` instead.")]
96pub type CharRange = LspRange;
97
98/// A resolved source (text) range.
99///
100/// See [`CharPosition`] for the definition of the position inside a file.
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct SourceRange {
103    /// The file path.
104    pub path: String,
105    /// The range in the file.
106    pub range: LspRange,
107}
108
109/// Unevaluated source span.
110/// The raw source span is unsafe to serialize and deserialize.
111/// Because the real source location is only known during liveness of
112/// the compiled document.
113pub type SourceSpan = typst::syntax::Span;
114
115/// Unevaluated source span with offset.
116///
117/// It adds an additional offset relative to the start of the span.
118///
119/// The offset is usually generated when the location is inside of some
120/// text or string content.
121#[derive(Debug, Clone, Copy)]
122pub struct SourceSpanOffset {
123    /// The source span.
124    pub span: SourceSpan,
125    /// The offset relative to the start of the span. This is usually useful
126    /// if the location is not a span created by the parser.
127    pub offset: usize,
128}
129
130/// Lifts a [`SourceSpan`] to [`SourceSpanOffset`].
131impl From<SourceSpan> for SourceSpanOffset {
132    fn from(span: SourceSpan) -> Self {
133        Self { span, offset: 0 }
134    }
135}
136
137/// Converts a [`SourceSpan`] and an in-text offset to [`SourceSpanOffset`].
138impl From<(SourceSpan, u16)> for SourceSpanOffset {
139    fn from((span, offset): (SourceSpan, u16)) -> Self {
140        Self {
141            span,
142            offset: offset as usize,
143        }
144    }
145}
146
147/// A point on the element tree.
148#[derive(Debug, Clone, Serialize, Deserialize)]
149pub struct ElementPoint {
150    /// The element kind.
151    pub kind: u32,
152    /// The index of the element.
153    pub index: u32,
154    /// The fingerprint of the element.
155    pub fingerprint: String,
156}
157
158impl From<(u32, u32, String)> for ElementPoint {
159    fn from((kind, index, fingerprint): (u32, u32, String)) -> Self {
160        Self {
161            kind,
162            index,
163            fingerprint,
164        }
165    }
166}
167
168/// A file system data source.
169#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
170pub struct FsDataSource {
171    /// The name of the data source.
172    pub path: String,
173}
174
175/// A in-memory data source.
176#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
177pub struct MemoryDataSource {
178    /// The name of the data source.
179    pub name: String,
180}
181
182/// Data source for a document.
183#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
184#[serde(tag = "kind")]
185pub enum DataSource {
186    /// File system data source.
187    #[serde(rename = "fs")]
188    Fs(FsDataSource),
189    /// Memory data source.
190    #[serde(rename = "memory")]
191    Memory(MemoryDataSource),
192}