2024-11-17 22:34:43 +01:00
|
|
|
use std::{collections::HashMap, ops::Range};
|
2023-08-20 12:15:48 +02:00
|
|
|
|
|
|
|
use anyhow::Context;
|
|
|
|
use codespan_reporting::{
|
|
|
|
diagnostic::{Diagnostic, Label, LabelStyle, Severity},
|
|
|
|
term::termcolor::{ColorChoice, StandardStream},
|
|
|
|
};
|
2024-11-26 22:58:02 +01:00
|
|
|
use tracing::instrument;
|
2023-08-20 12:15:48 +02:00
|
|
|
use ulid::Ulid;
|
|
|
|
|
2024-11-17 22:34:43 +01:00
|
|
|
use crate::{
|
2025-08-24 13:18:51 +02:00
|
|
|
doc::Doc,
|
2024-11-17 22:34:43 +01:00
|
|
|
tree::{SemaBranchId, SemaRoots, SemaTree},
|
2024-11-26 22:58:02 +01:00
|
|
|
vfs::{VPath, VPathBuf},
|
2024-11-17 22:34:43 +01:00
|
|
|
};
|
2024-09-28 23:43:05 +02:00
|
|
|
|
2024-01-18 22:46:57 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Source {
|
2024-11-26 22:58:02 +01:00
|
|
|
Tree { input: String, tree_path: VPathBuf },
|
2024-01-18 22:46:57 +01:00
|
|
|
Other(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Source {
|
|
|
|
pub fn input(&self) -> &str {
|
|
|
|
match &self {
|
|
|
|
Source::Tree { input, .. } => input,
|
|
|
|
Source::Other(source) => source,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<str> for Source {
|
|
|
|
fn as_ref(&self) -> &str {
|
|
|
|
self.input()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-26 22:58:02 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct File {
|
|
|
|
pub path: VPathBuf,
|
|
|
|
pub source: Source,
|
|
|
|
pub line_starts: Vec<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl File {
|
|
|
|
fn line_start(&self, line_index: usize) -> Result<usize, codespan_reporting::files::Error> {
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
|
|
|
match line_index.cmp(&self.line_starts.len()) {
|
|
|
|
Ordering::Less => Ok(self
|
|
|
|
.line_starts
|
|
|
|
.get(line_index)
|
|
|
|
.cloned()
|
|
|
|
.expect("failed despite previous check")),
|
|
|
|
Ordering::Equal => Ok(self.source.as_ref().len()),
|
|
|
|
Ordering::Greater => Err(codespan_reporting::files::Error::LineTooLarge {
|
|
|
|
given: line_index,
|
|
|
|
max: self.line_starts.len() - 1,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub struct FileId(usize);
|
2023-08-20 12:15:48 +02:00
|
|
|
|
2025-08-24 13:18:51 +02:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct Tag {
|
|
|
|
pub files: Vec<FileId>,
|
|
|
|
}
|
|
|
|
|
2023-08-20 12:15:48 +02:00
|
|
|
/// Treehouse compilation context.
|
|
|
|
pub struct Treehouse {
|
2024-11-26 22:58:02 +01:00
|
|
|
pub files: Vec<File>,
|
2025-07-10 16:50:41 +02:00
|
|
|
pub files_by_tree_path: HashMap<VPathBuf, FileId>, // trees only
|
|
|
|
pub files_by_doc_path: HashMap<VPathBuf, FileId>, // docs only
|
2025-08-24 13:18:51 +02:00
|
|
|
pub tags: HashMap<String, Tag>,
|
2023-08-20 12:15:48 +02:00
|
|
|
|
2023-08-22 22:19:43 +02:00
|
|
|
pub tree: SemaTree,
|
|
|
|
pub branches_by_named_id: HashMap<String, SemaBranchId>,
|
2024-11-26 22:58:02 +01:00
|
|
|
pub roots: HashMap<FileId, SemaRoots>,
|
2023-08-22 22:19:43 +02:00
|
|
|
|
2025-08-24 13:18:51 +02:00
|
|
|
pub docs: HashMap<FileId, Doc>,
|
|
|
|
|
2024-06-13 21:18:41 +02:00
|
|
|
pub branch_redirects: HashMap<String, SemaBranchId>,
|
|
|
|
|
2024-11-26 22:58:02 +01:00
|
|
|
pub missingno_generator: ulid::Generator,
|
2023-08-20 12:15:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Treehouse {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2024-11-26 22:58:02 +01:00
|
|
|
files: vec![],
|
|
|
|
files_by_tree_path: HashMap::new(),
|
2025-07-10 16:50:41 +02:00
|
|
|
files_by_doc_path: HashMap::new(),
|
2025-08-24 13:18:51 +02:00
|
|
|
tags: HashMap::new(),
|
2023-08-20 12:15:48 +02:00
|
|
|
|
2023-08-22 22:19:43 +02:00
|
|
|
tree: SemaTree::default(),
|
|
|
|
branches_by_named_id: HashMap::new(),
|
2023-08-27 14:50:46 +02:00
|
|
|
roots: HashMap::new(),
|
2023-08-22 22:19:43 +02:00
|
|
|
|
2025-08-24 13:18:51 +02:00
|
|
|
docs: HashMap::new(),
|
|
|
|
|
2024-06-13 21:18:41 +02:00
|
|
|
branch_redirects: HashMap::new(),
|
|
|
|
|
2023-08-20 12:15:48 +02:00
|
|
|
missingno_generator: ulid::Generator::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-26 22:58:02 +01:00
|
|
|
pub fn add_file(&mut self, path: VPathBuf, source: Source) -> FileId {
|
|
|
|
let id = FileId(self.files.len());
|
|
|
|
self.files.push(File {
|
|
|
|
line_starts: codespan_reporting::files::line_starts(source.input()).collect(),
|
|
|
|
|
|
|
|
path,
|
|
|
|
source,
|
|
|
|
});
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the name of a file, assuming it was previously registered.
|
|
|
|
pub fn path(&self, file_id: FileId) -> &VPath {
|
|
|
|
&self.files[file_id.0].path
|
2023-08-21 21:12:06 +02:00
|
|
|
}
|
|
|
|
|
2023-08-20 12:15:48 +02:00
|
|
|
/// Get the source code of a file, assuming it was previously registered.
|
2024-01-18 22:46:57 +01:00
|
|
|
pub fn source(&self, file_id: FileId) -> &Source {
|
2024-11-26 22:58:02 +01:00
|
|
|
&self.files[file_id.0].source
|
2023-08-20 12:15:48 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 22:58:02 +01:00
|
|
|
pub fn set_source(&mut self, file_id: FileId, source: Source) {
|
|
|
|
self.files[file_id.0].line_starts =
|
|
|
|
codespan_reporting::files::line_starts(source.input()).collect();
|
|
|
|
self.files[file_id.0].source = source;
|
2023-08-20 12:15:48 +02:00
|
|
|
}
|
|
|
|
|
2024-11-26 22:58:02 +01:00
|
|
|
pub fn tree_path(&self, file_id: FileId) -> Option<&VPath> {
|
2024-01-18 22:46:57 +01:00
|
|
|
match self.source(file_id) {
|
|
|
|
Source::Tree { tree_path, .. } => Some(tree_path),
|
|
|
|
Source::Other(_) => None,
|
|
|
|
}
|
2023-08-21 21:12:06 +02:00
|
|
|
}
|
|
|
|
|
2023-08-20 12:15:48 +02:00
|
|
|
pub fn next_missingno(&mut self) -> Ulid {
|
|
|
|
self.missingno_generator
|
|
|
|
.generate()
|
|
|
|
.expect("just how much disk space do you have?")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-23 19:12:00 +01:00
|
|
|
impl Default for Treehouse {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-26 22:58:02 +01:00
|
|
|
impl<'a> codespan_reporting::files::Files<'a> for Treehouse {
|
|
|
|
type FileId = FileId;
|
|
|
|
|
|
|
|
type Name = &'a VPath;
|
|
|
|
|
|
|
|
type Source = &'a str;
|
|
|
|
|
|
|
|
fn name(&'a self, id: Self::FileId) -> Result<Self::Name, codespan_reporting::files::Error> {
|
|
|
|
Ok(self.path(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn source(
|
|
|
|
&'a self,
|
|
|
|
id: Self::FileId,
|
|
|
|
) -> Result<Self::Source, codespan_reporting::files::Error> {
|
|
|
|
Ok(self.source(id).input())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line_index(
|
|
|
|
&'a self,
|
|
|
|
id: Self::FileId,
|
|
|
|
byte_index: usize,
|
|
|
|
) -> Result<usize, codespan_reporting::files::Error> {
|
|
|
|
let file = &self.files[id.0];
|
|
|
|
Ok(file
|
|
|
|
.line_starts
|
|
|
|
.binary_search(&byte_index)
|
|
|
|
.unwrap_or_else(|next_line| next_line - 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line_range(
|
|
|
|
&'a self,
|
|
|
|
id: Self::FileId,
|
|
|
|
line_index: usize,
|
|
|
|
) -> Result<Range<usize>, codespan_reporting::files::Error> {
|
|
|
|
let file = &self.files[id.0];
|
|
|
|
let line_start = file.line_start(line_index)?;
|
|
|
|
let next_line_start = file.line_start(line_index + 1)?;
|
|
|
|
|
|
|
|
Ok(line_start..next_line_start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 12:15:48 +02:00
|
|
|
pub struct TomlError {
|
|
|
|
pub message: String,
|
|
|
|
pub span: Option<Range<usize>>,
|
|
|
|
pub file_id: FileId,
|
|
|
|
pub input_range: Range<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn toml_error_to_diagnostic(error: TomlError) -> Diagnostic<FileId> {
|
|
|
|
Diagnostic {
|
|
|
|
severity: Severity::Error,
|
|
|
|
code: Some("toml".into()),
|
|
|
|
message: error.message,
|
|
|
|
labels: error
|
|
|
|
.span
|
|
|
|
.map(|span| Label {
|
|
|
|
style: LabelStyle::Primary,
|
|
|
|
file_id: error.file_id,
|
|
|
|
range: error.input_range.start + span.start..error.input_range.start + span.end,
|
|
|
|
message: String::new(),
|
|
|
|
})
|
|
|
|
.into_iter()
|
|
|
|
.collect(),
|
|
|
|
notes: vec![],
|
|
|
|
}
|
|
|
|
}
|
2024-09-28 23:43:05 +02:00
|
|
|
|
2024-11-26 22:58:02 +01:00
|
|
|
#[instrument(skip(files, diagnostics))]
|
|
|
|
pub fn report_diagnostics(
|
|
|
|
files: &Treehouse,
|
|
|
|
diagnostics: &[Diagnostic<FileId>],
|
|
|
|
) -> anyhow::Result<()> {
|
2024-09-28 23:43:05 +02:00
|
|
|
let writer = StandardStream::stderr(ColorChoice::Auto);
|
|
|
|
let config = codespan_reporting::term::Config::default();
|
|
|
|
for diagnostic in diagnostics {
|
|
|
|
codespan_reporting::term::emit(&mut writer.lock(), &config, files, diagnostic)
|
|
|
|
.context("could not emit diagnostic")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn has_errors(diagnostics: &[Diagnostic<FileId>]) -> bool {
|
|
|
|
diagnostics.iter().any(|d| d.severity == Severity::Error)
|
|
|
|
}
|