Skip to content

Commit 9372eff

Browse files
committed
Autocomplete support
This outputs your written code alongside a compile error in the case of a build time error, so that you get feedback from Rust Analyzer for autocomplete.
1 parent 68aae61 commit 9372eff

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,26 @@ use std::io::Write;
99
/// Properly passes an error message to the compiler without crashing macro engines.
1010
macro_rules! build_error {
1111
($($arg:tt)*) => {
12-
format!("compile_error!(r#\"{}\"#)", format!($($arg)*))
12+
format!("compile_error!(r#\"{}\"#);", format!($($arg)*))
1313
.parse::<TokenStream>()
1414
.unwrap()
1515
};
1616
}
1717

1818
#[proc_macro]
1919
#[doc = include_str!("../README.md")]
20-
pub fn comptime(code: TokenStream) -> TokenStream {
20+
pub fn comptime(mut code: TokenStream) -> TokenStream {
2121
let mut out_dir = None;
2222
let mut externs = vec![];
2323

2424
let mut args = std::env::args();
2525
while let Some(arg) = args.next() {
2626
// Push deps to rustc so you don't need to explicitly link with 'extern crate'
2727
if arg == "--extern" {
28-
externs.push(args.next().unwrap());
28+
let ext = args.next().unwrap();
29+
if !externs.contains(&ext) {
30+
externs.push(ext)
31+
}
2932
} else if arg == "--out-dir" {
3033
out_dir = args.next().map(std::path::PathBuf::from);
3134
}
@@ -92,7 +95,9 @@ pub fn comptime(code: TokenStream) -> TokenStream {
9295
};
9396

9497
if !output.status.success() {
95-
return build_error!("{}", String::from_utf8_lossy(&output.stderr));
98+
// Extend the code so that autocompletion works.
99+
code.extend(build_error!("{}", String::from_utf8_lossy(&output.stderr)));
100+
return code;
96101
}
97102
}
98103

tests/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
use constime::comptime;
22

3+
#[test]
4+
fn test_const() {
5+
const FORMATTED: &str = comptime! { format!("abc{}", 218) };
6+
assert_eq!(FORMATTED, "abc218");
7+
}
8+
39
#[test]
410
fn test_base() {
11+
let x: Result<&str, std::env::VarError> = comptime! { std::env::var("CARGO_PKG_NAME") };
12+
assert_eq!(x, Ok(env!("CARGO_PKG_NAME")));
13+
514
assert_eq!(comptime! { 239 + 259 * 23 }, 239 + 259 * 23);
615
assert_eq!(comptime! { 2 + 259 * 7 }, 2 + 259 * 7);
716
assert_eq!(comptime! { String::from("test") }, "test");

0 commit comments

Comments
 (0)