Skip to content

Commit

Permalink
cli: ace editor integration (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roba1993 authored Aug 6, 2024
1 parent ddccb1e commit e36fcc3
Show file tree
Hide file tree
Showing 11 changed files with 607 additions and 4 deletions.
127 changes: 127 additions & 0 deletions crates/rune/src/cli/ace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use std::io::Write;
use std::path::PathBuf;

use crate::doc::Artifacts;

use anyhow::{Context, Result};
use clap::Parser;

use crate::alloc::prelude::*;
use crate::alloc::Vec;
use crate::cli::naming::Naming;
use crate::cli::{AssetKind, CommandBase, Config, Entry, EntryPoint, ExitCode, Io, SharedFlags};
use crate::compile::FileSourceLoader;
use crate::{Diagnostics, Options, Source, Sources};

#[derive(Parser, Debug)]
pub(super) struct Flags {
/// Exit with a non-zero exit-code even for warnings
#[arg(long)]
warnings_are_errors: bool,
/// Output directory to write documentation to.
#[arg(long)]
output: Option<PathBuf>,
/// Generate .await and ? extension for functions.
#[arg(long)]
extensions: bool,
}

impl CommandBase for Flags {
#[inline]
fn is_workspace(&self, _: AssetKind) -> bool {
true
}

#[inline]
fn describe(&self) -> &str {
"Documenting"
}
}

pub(super) fn run<'p, I>(
io: &mut Io<'_>,
entry: &mut Entry<'_>,
c: &Config,
flags: &Flags,
shared: &SharedFlags,
options: &Options,
entries: I,
) -> Result<ExitCode>
where
I: IntoIterator<Item = EntryPoint<'p>>,
{
let root = match &flags.output {
Some(root) => root.clone(),
None => match &c.manifest_root {
Some(path) => path.join("target").join("rune-ace"),
None => match std::env::var_os("CARGO_TARGET_DIR") {
Some(target) => {
let mut target = PathBuf::from(target);
target.push("rune-ace");
target
}
None => {
let mut target = PathBuf::new();
target.push("target");
target.push("rune-ace");
target
}
},
},
};

writeln!(io.stdout, "Building ace autocompletion: {}", root.display())?;

let context = shared.context(entry, c, None)?;

let mut visitors = Vec::new();

let mut naming = Naming::default();

for e in entries {
let item = naming.item(&e)?;

let mut visitor = crate::doc::Visitor::new(&item)?;
let mut sources = Sources::new();

let source = match Source::from_path(e.path()) {
Ok(source) => source,
Err(error) => return Err(error).context(e.path().display().try_to_string()?),
};

sources.insert(source)?;

let mut diagnostics = if shared.warnings || flags.warnings_are_errors {
Diagnostics::new()
} else {
Diagnostics::without_warnings()
};

let mut source_loader = FileSourceLoader::new();

let _ = crate::prepare(&mut sources)
.with_context(&context)
.with_diagnostics(&mut diagnostics)
.with_options(options)
.with_visitor(&mut visitor)?
.with_source_loader(&mut source_loader)
.build();

diagnostics.emit(&mut io.stdout.lock(), &sources)?;

if diagnostics.has_error() || flags.warnings_are_errors && diagnostics.has_warning() {
return Ok(ExitCode::Failure);
}

visitors.try_push(visitor)?;
}

let mut artifacts = Artifacts::new();
crate::doc::build_autocomplete(&mut artifacts, &context, &visitors, flags.extensions)?;

for asset in artifacts.assets() {
asset.build(&root)?;
}

Ok(ExitCode::Success)
}
12 changes: 11 additions & 1 deletion crates/rune/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! * Build a language server, which is aware of things only available in your
//! context.
mod ace;
mod benches;
mod check;
mod doc;
Expand Down Expand Up @@ -431,6 +432,8 @@ enum Command {
Check(CommandShared<check::Flags>),
/// Build documentation.
Doc(CommandShared<doc::Flags>),
/// Build ace autocompletion.
Ace(CommandShared<ace::Flags>),
/// Run all tests but do not execute
Test(CommandShared<tests::Flags>),
/// Run the given program as a benchmark
Expand All @@ -446,9 +449,10 @@ enum Command {
}

impl Command {
const ALL: [&'static str; 8] = [
const ALL: [&'static str; 9] = [
"check",
"doc",
"ace",
"test",
"bench",
"run",
Expand All @@ -461,6 +465,7 @@ impl Command {
let (shared, command): (_, &mut dyn CommandBase) = match self {
Command::Check(shared) => (&mut shared.shared, &mut shared.command),
Command::Doc(shared) => (&mut shared.shared, &mut shared.command),
Command::Ace(shared) => (&mut shared.shared, &mut shared.command),
Command::Test(shared) => (&mut shared.shared, &mut shared.command),
Command::Bench(shared) => (&mut shared.shared, &mut shared.command),
Command::Run(shared) => (&mut shared.shared, &mut shared.command),
Expand All @@ -476,6 +481,7 @@ impl Command {
let (shared, command): (_, &dyn CommandBase) = match self {
Command::Check(shared) => (&shared.shared, &shared.command),
Command::Doc(shared) => (&shared.shared, &shared.command),
Command::Ace(shared) => (&shared.shared, &shared.command),
Command::Test(shared) => (&shared.shared, &shared.command),
Command::Bench(shared) => (&shared.shared, &shared.command),
Command::Run(shared) => (&shared.shared, &shared.command),
Expand Down Expand Up @@ -911,6 +917,10 @@ where
let options = f.options()?;
return doc::run(io, entry, c, &f.command, &f.shared, &options, entries);
}
Command::Ace(f) => {
let options = f.options()?;
return ace::run(io, entry, c, &f.command, &f.shared, &options, entries);
}
Command::Fmt(f) => {
let options = f.options()?;
return format::run(io, entry, c, entries, &f.command, &f.shared, &options);
Expand Down
11 changes: 11 additions & 0 deletions crates/rune/src/compile/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::ops::Deref;

use crate::alloc::{self, Box, HashMap};
use crate::item::{IntoComponent, Item, ItemBuf};

Expand Down Expand Up @@ -54,6 +56,15 @@ impl Prelude {
Some(self.prelude.get(name)?)
}

/// Return the local name of an item
#[allow(dead_code)]
pub(crate) fn get_local<'a>(&'a self, item: &ItemBuf) -> Option<&'a str> {
self.prelude
.iter()
.find(|(_, i)| i == &item)
.map(|(s, _)| s.deref())
}

/// Define a prelude item.
fn add_prelude<I>(&mut self, local: &str, path: I) -> alloc::Result<()>
where
Expand Down
Loading

0 comments on commit e36fcc3

Please sign in to comment.