Skip to content

Commit 5ea74ab

Browse files
committed
Add support for include_str
1 parent 6a067ce commit 5ea74ab

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Builtin macro
2+
use std::ops::Deref;
3+
24
use crate::{
35
db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId,
46
MacroDefId, MacroDefKind, TextSize,
@@ -99,6 +101,7 @@ register_builtin! {
99101
EAGER:
100102
(concat, Concat) => concat_expand,
101103
(include, Include) => include_expand,
104+
(include_str, IncludeStr) => include_str_expand,
102105
(env, Env) => env_expand,
103106
(option_env, OptionEnv) => option_env_expand
104107
}
@@ -331,6 +334,20 @@ fn include_expand(
331334
Ok((res, FragmentKind::Items))
332335
}
333336

337+
fn include_str_expand(
338+
db: &dyn AstDatabase,
339+
arg_id: EagerMacroId,
340+
tt: &tt::Subtree,
341+
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
342+
let path = parse_string(tt)?;
343+
let file_id =
344+
relative_file(db, arg_id.into(), &path).ok_or_else(|| mbe::ExpandError::ConversionError)?;
345+
let text = db.file_text(file_id);
346+
let text = text.deref();
347+
348+
Ok((quote!(#text), FragmentKind::Expr))
349+
}
350+
334351
fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
335352
let krate = db.lookup_intern_eager_expansion(arg_id).krate;
336353
db.crate_graph()[krate].env.get(key)

crates/ra_hir_expand/src/name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub mod known {
191191
stringify,
192192
concat,
193193
include,
194+
include_str,
194195
format_args,
195196
format_args_nl,
196197
env,

0 commit comments

Comments
 (0)