Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid doc conflicts #504

Merged
merged 3 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
334 changes: 165 additions & 169 deletions crates/rune/src/cli.rs

Large diffs are not rendered by default.

55 changes: 43 additions & 12 deletions crates/rune/src/cli/doc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;
use std::ffi::OsStr;
use std::io::Write;
use std::path::PathBuf;

Expand All @@ -7,8 +9,8 @@ use anyhow::{Context, Result};
use clap::Parser;

use crate::cli::{Config, Entry, EntryPoint, ExitCode, Io, SharedFlags};
use crate::compile::FileSourceLoader;
use crate::{Diagnostics, Options, Source, Sources};
use crate::compile::{FileSourceLoader, ItemBuf};
use crate::{Diagnostics, Options, Source, Sources, workspace};

#[derive(Parser, Debug)]
pub(super) struct Flags {
Expand All @@ -23,7 +25,7 @@ pub(super) struct Flags {
open: bool,
}

pub(super) fn run<I>(
pub(super) fn run<'p, I>(
io: &mut Io<'_>,
entry: &mut Entry<'_>,
c: &Config,
Expand All @@ -33,7 +35,7 @@ pub(super) fn run<I>(
entrys: I,
) -> Result<ExitCode>
where
I: IntoIterator<Item = EntryPoint>,
I: IntoIterator<Item = EntryPoint<'p>>,
{
let root = match &flags.output {
Some(root) => root.to_owned(),
Expand Down Expand Up @@ -61,15 +63,44 @@ where

let mut visitors = Vec::new();

for e in entrys {
let mut visitor = crate::doc::Visitor::new(e.item);
let mut sources = Sources::new();
let mut names = HashSet::new();

for path in &e.paths {
let source = Source::from_path(path)
.with_context(|| format!("reading file: {}", path.display()))?;
sources.insert(source);
}
for (index, e) in entrys.into_iter().enumerate() {
let name = match &e {
EntryPoint::Path(path) => {
match path.file_stem().and_then(OsStr::to_str) {
Some(name) => String::from(name),
None => String::from("entry"),
}
}
EntryPoint::Package(p) => {
let name = p.found.name.as_str();

let ext = match &p.found.kind {
workspace::FoundKind::Binary => "bin",
workspace::FoundKind::Test => "test",
workspace::FoundKind::Example => "example",
workspace::FoundKind::Bench => "bench",
};

format!("{}-{name}-{ext}", p.package.name)
},
};

// TODO: make it so that we can communicate different entrypoints in the
// visitors context instead of this hackery.
let name = if !names.insert(name.clone()) {
format!("{name}{index}")
} else {
name
};

let item = ItemBuf::with_crate(&name);
let mut visitor = crate::doc::Visitor::new(item);
let mut sources = Sources::new();
let source = Source::from_path(e.path())
.with_context(|| e.path().display().to_string())?;
sources.insert(source);

let mut diagnostics = if shared.warnings || flags.warnings_are_errors {
Diagnostics::new()
Expand Down
22 changes: 11 additions & 11 deletions crates/rune/src/cli/format.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::io::Write;

use anyhow::{Context, Result};
use clap::Parser;
use std::io::Write;
use std::path::PathBuf;

use crate::cli::{ExitCode, Io};
use crate::cli::{ExitCode, Io, EntryPoint};
use crate::termcolor::WriteColor;
use crate::Source;
use crate::{Source};

#[derive(Parser, Debug)]
pub(super) struct Flags {
Expand All @@ -18,7 +18,7 @@ pub(super) struct Flags {
check: bool,
}

pub(super) fn run(io: &mut Io<'_>, paths: &[PathBuf], flags: &Flags) -> Result<ExitCode> {
pub(super) fn run<'m, I>(io: &mut Io<'_>, entrys: I, flags: &Flags) -> Result<ExitCode> where I: IntoIterator<Item = EntryPoint<'m>> {
let mut red = crate::termcolor::ColorSpec::new();
red.set_fg(Some(crate::termcolor::Color::Red));

Expand All @@ -32,9 +32,9 @@ pub(super) fn run(io: &mut Io<'_>, paths: &[PathBuf], flags: &Flags) -> Result<E
let mut failed = 0;
let mut unchanged = 0;

for path in paths {
for e in entrys {
let source =
Source::from_path(path).with_context(|| format!("reading file: {}", path.display()))?;
Source::from_path(e.path()).with_context(|| format!("reading file: {}", e.path().display()))?;

match crate::fmt::layout_source(&source) {
Ok(val) => {
Expand All @@ -43,7 +43,7 @@ pub(super) fn run(io: &mut Io<'_>, paths: &[PathBuf], flags: &Flags) -> Result<E
io.stdout.set_color(&yellow)?;
write!(io.stdout, "== ")?;
io.stdout.reset()?;
writeln!(io.stdout, "{}", path.display())?;
writeln!(io.stdout, "{}", e.path().display())?;
}

unchanged += 1;
Expand All @@ -52,9 +52,9 @@ pub(super) fn run(io: &mut Io<'_>, paths: &[PathBuf], flags: &Flags) -> Result<E
io.stdout.set_color(&green)?;
write!(io.stdout, "++ ")?;
io.stdout.reset()?;
writeln!(io.stdout, "{}", path.display())?;
writeln!(io.stdout, "{}", e.path().display())?;
if !flags.check {
std::fs::write(path, &val)?;
std::fs::write(e.path(), &val)?;
}
}
}
Expand All @@ -63,7 +63,7 @@ pub(super) fn run(io: &mut Io<'_>, paths: &[PathBuf], flags: &Flags) -> Result<E
io.stdout.set_color(&red)?;
write!(io.stdout, "!! ")?;
io.stdout.reset()?;
writeln!(io.stdout, "{}: {}", path.display(), err)?;
writeln!(io.stdout, "{}: {}", e.path().display(), err)?;
}
}
}
Expand Down
39 changes: 23 additions & 16 deletions crates/rune/src/compile/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,25 +387,32 @@ impl Context {
/// Install a module, ensuring that its meta is defined.
fn install_module(&mut self, m: &Module) -> Result<(), ContextError> {
self.names.insert(&m.item);
let hash = Hash::type_hash(&m.item);

if let Some(existing) = self.item_to_hash.insert(m.item.to_owned(), hash) {
if hash != existing {
return Err(ContextError::ConflictingMetaHash {
item: m.item.to_owned(),
hash,
existing,
});
let mut current = Some((m.item.as_ref(), Some(&m.docs)));

while let Some((item, docs)) = current.take() {
let hash = Hash::type_hash(item);

if let Some(existing) = self.item_to_hash.insert(item.to_owned(), hash) {
if hash != existing {
return Err(ContextError::ConflictingMetaHash {
item: item.to_owned(),
hash,
existing,
});
}
}
}

self.meta.entry(hash).or_default().push(ContextMeta {
hash,
associated_container: None,
item: m.item.to_owned(),
kind: meta::Kind::Module,
docs: m.docs.clone(),
});
self.meta.entry(hash).or_default().push(ContextMeta {
hash,
associated_container: None,
item: item.to_owned(),
kind: meta::Kind::Module,
docs: docs.cloned().unwrap_or_default(),
});

current = item.parent().map(|item| (item, None));
}

Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions crates/rune/src/doc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ pub(crate) enum MetaSource<'a> {

#[derive(Debug, Clone, Copy)]
pub(crate) struct Meta<'a> {
/// Item of the meta.
pub(crate) item: &'a Item,
/// The meta source.
#[allow(unused)]
pub(crate) source: MetaSource<'a>,
/// Item of the meta.
pub(crate) item: &'a Item,
/// Type hash for the meta item.
pub(crate) hash: Hash,
/// Kind of the meta item.
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<'a> Context<'a> {
}

/// Iterate over all types associated with the given hash.
pub(crate) fn associated(&self, hash: Hash) -> impl Iterator<Item = Assoc<'_>> {
pub(crate) fn associated(&self, hash: Hash) -> impl Iterator<Item = Assoc<'a>> {
fn visitor_to_associated(
hash: Hash,
visitor: &Visitor,
Expand Down Expand Up @@ -223,7 +223,7 @@ impl<'a> Context<'a> {
}

/// Lookup all meta matching the given item.
pub(crate) fn meta(&self, item: &Item) -> Vec<Meta<'_>> {
pub(crate) fn meta(&self, item: &Item) -> Vec<Meta<'a>> {
let mut out = Vec::new();

for visitor in self.visitors {
Expand Down
Loading