Skip to main content

mir_types/
location.rs

1use std::fmt;
2use std::sync::Arc;
3
4use serde::{Deserialize, Serialize};
5
6/// Source location: file path and a Unicode code-point span.
7///
8/// Columns are 0-based Unicode scalar value (code-point) counts, equivalent to
9/// LSP `utf-32` position encoding. Convert to UTF-16 code units at the LSP
10/// boundary for clients that do not advertise `utf-32` support.
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12pub struct Location {
13    pub file: Arc<str>,
14    /// 1-based start line.
15    pub line: u32,
16    /// 1-based end line (inclusive). Equal to `line` for single-line spans.
17    pub line_end: u32,
18    /// 0-based Unicode code-point column of the span start.
19    pub col_start: u16,
20    /// 0-based Unicode code-point column of the span end (exclusive).
21    pub col_end: u16,
22}
23
24impl Location {
25    pub fn new(file: Arc<str>, line: u32, line_end: u32, col_start: u16, col_end: u16) -> Self {
26        Self {
27            file,
28            line,
29            line_end,
30            col_start,
31            col_end,
32        }
33    }
34}
35
36impl fmt::Display for Location {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        write!(f, "{}:{}:{}", self.file, self.line, self.col_start)
39    }
40}