tinymist_world/
debug_loc.rs

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