Skip to content

Commit

Permalink
allow for a name in scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 24, 2024
1 parent 4368078 commit e842435
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 17 deletions.
2 changes: 1 addition & 1 deletion site/src/editor/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ pub fn progressive_strings(input: &str) -> Vec<String> {
Item::Binding(binding) => {
lines.push(vec![binding.span().as_str(&inputs, |s| s.into())])
}
Item::TestScope(items) => lines.push(vec![items.span.as_str(&inputs, |s| s.into())]),
Item::Module(items) => lines.push(vec![items.span.as_str(&inputs, |s| s.into())]),
Item::Import(import) => lines.push(vec![import.span().as_str(&inputs, |s| s.into())]),
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub enum Item {
Binding(Binding),
/// An import
Import(Import),
/// A test scope
TestScope(Sp<Vec<Item>>),
/// A scope
Module(Sp<ScopedModule>),
}

/// A binding
Expand Down Expand Up @@ -51,6 +51,15 @@ impl Binding {
}
}

/// A scoped module
#[derive(Debug, Clone)]
pub struct ScopedModule {
/// The name of the scope
pub name: Option<Sp<Ident>>,
/// The items
pub items: Vec<Item>,
}

/// An import
#[derive(Debug, Clone)]
pub struct Import {
Expand Down
25 changes: 16 additions & 9 deletions src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ pub(crate) struct Scope {

#[derive(Clone, Copy, PartialEq, Eq)]
enum ScopeKind {
/// A scope at the top level of a file
File,
/// A scope at the top level of a file or in a named module
Module,
/// A temporary scope, probably for a macro
Temp,
/// A test scope between `---`s
Expand All @@ -184,7 +184,7 @@ enum ScopeKind {
impl Default for Scope {
fn default() -> Self {
Self {
kind: ScopeKind::File,
kind: ScopeKind::Module,
file_path: None,
comment: None,
names: IndexMap::new(),
Expand Down Expand Up @@ -493,9 +493,16 @@ code:
(words.iter()).any(|w| matches!(&w.value, Word::SemanticComment(_)))
}
let mut lines = match item {
Item::TestScope(items) => {
Item::Module(module) => {
prev_comment.take();
self.in_scope(ScopeKind::Test, |env| env.items(items.value, true))?;
let is_test =
(module.value.name.as_ref()).map_or(true, |name| name.value == "test");
let scope_kind = if is_test {
ScopeKind::Test
} else {
ScopeKind::Module
};
self.in_scope(scope_kind, |env| env.items(module.value.items, true))?;
return Ok(());
}
Item::Words(lines) => lines,
Expand Down Expand Up @@ -770,7 +777,7 @@ code:
format!("Cycle detected importing {}", path.to_string_lossy()),
));
}
let import = self.in_scope(ScopeKind::File, |env| {
let import = self.in_scope(ScopeKind::Module, |env| {
env.load_str_src(&input, &path).map(drop)
})?;
self.imports.insert(path.clone(), import);
Expand Down Expand Up @@ -1504,8 +1511,8 @@ code:
}
let mut hit_file = false;
for scope in self.higher_scopes.iter().rev() {
if scope.kind == ScopeKind::File {
if hit_file || self.scope.kind == ScopeKind::File {
if scope.kind == ScopeKind::Module {
if hit_file || self.scope.kind == ScopeKind::Module {
break;
}
hit_file = true;
Expand Down Expand Up @@ -2169,7 +2176,7 @@ code:
.or_else(|| {
self.higher_scopes
.last()
.filter(|_| self.scope.kind != ScopeKind::File)
.filter(|_| self.scope.kind != ScopeKind::Module)
.and_then(|scope| scope.names.get(name))
})
.map_or(true, |l| l.index != local.index)
Expand Down
2 changes: 1 addition & 1 deletion src/compile/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ impl Compiler {
Item::Import(import) => self
.import(import, None)
.map_err(|e| e.trace_macro(span.clone()))?,
Item::TestScope(_) => {
Item::Module(_) => {
self.add_error(span.clone(), "Macros may not generate test scopes")
}
};
Expand Down
7 changes: 5 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,14 @@ impl<'a> Formatter<'a> {
}
fn format_item(&mut self, item: &Item) {
match item {
Item::TestScope(items) => {
Item::Module(m) => {
self.prev_import_function = None;
self.output.push_str("---");
if let Some(name) = &m.value.name {
self.push(&name.span, &name.value);
}
self.output.push('\n');
self.format_items(&items.value);
self.format_items(&m.value.items);
self.output.push_str("---");
}
Item::Words(lines) => {
Expand Down
2 changes: 1 addition & 1 deletion src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl Spanner {
let mut spans = Vec::new();
for item in items {
match item {
Item::TestScope(items) => spans.extend(self.items_spans(&items.value)),
Item::Module(m) => spans.extend(self.items_spans(&m.value.items)),
Item::Words(lines) => {
for line in lines {
spans.extend(self.words_spans(line))
Expand Down
5 changes: 4 additions & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,17 @@ impl<'i> Parser<'i> {
Item::Words(lines)
} else if parse_scopes {
let start = self.try_exact(TripleMinus.into())?;
self.try_spaces();
let name = self.try_ident();
let items = self.items(false);
let span = if let Some(end) = self.try_exact(TripleMinus.into()) {
start.merge(end)
} else {
self.errors.push(self.expected([TripleMinus]));
start
};
Item::TestScope(span.sp(items))
let module = ScopedModule { name, items };
Item::Module(span.sp(module))
} else {
return None;
}
Expand Down

0 comments on commit e842435

Please sign in to comment.