compiler: Store an optional version number with the document

A None value means the file on disk is the golden version.

We have an editor, the LSP and the preview that all need to at least
notice when they have newer data then their peers. So IMHO it makes
sense to have an optional document version around.

The language server protocol makes use of a version number already. This
patch moves that code into the compiler so that it is stored with the
actual data getting versioned.
This commit is contained in:
Tobias Hunger 2023-12-05 21:22:01 +01:00 committed by Tobias Hunger
parent dc536ac599
commit d785f2d5df
20 changed files with 116 additions and 52 deletions

View file

@ -60,6 +60,8 @@ pub trait Spanned {
}
}
pub type SourceFileVersion = Option<i32>;
#[derive(Default)]
pub struct SourceFileInner {
path: PathBuf,
@ -69,17 +71,21 @@ pub struct SourceFileInner {
/// The offset of each linebreak
line_offsets: once_cell::unsync::OnceCell<Vec<usize>>,
/// The version of the source file. `None` means "as seen on disk"
version: SourceFileVersion,
}
impl std::fmt::Debug for SourceFileInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.path)
let v = if let Some(v) = self.version { format!("@{v}") } else { String::new() };
write!(f, "{:?}{v}", self.path)
}
}
impl SourceFileInner {
pub fn new(path: PathBuf, source: String) -> Self {
Self { path, source: Some(source), line_offsets: Default::default() }
pub fn new(path: PathBuf, source: String, version: SourceFileVersion) -> Self {
Self { path, source: Some(source), line_offsets: Default::default(), version }
}
pub fn path(&self) -> &Path {
@ -137,6 +143,10 @@ impl SourceFileInner {
pub fn source(&self) -> Option<&str> {
self.source.as_deref()
}
pub fn version(&self) -> SourceFileVersion {
self.version.clone()
}
}
pub type SourceFile = Rc<SourceFileInner>;
@ -566,7 +576,7 @@ component MainWindow inherits Window {
"#.to_string();
let sf = SourceFileInner::new(PathBuf::from("foo.slint"), content.clone());
let sf = SourceFileInner::new(PathBuf::from("foo.slint"), content.clone(), None);
let mut line = 1;
let mut column = 1;