Skip to content

Add support for include_str #5100

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

Merged
merged 1 commit into from
Jun 27, 2020
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
38 changes: 34 additions & 4 deletions crates/ra_hir_expand/src/builtin_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ register_builtin! {
EAGER:
(concat, Concat) => concat_expand,
(include, Include) => include_expand,
(include_str, IncludeStr) => include_str_expand,
(env, Env) => env_expand,
(option_env, OptionEnv) => option_env_expand
}
Expand Down Expand Up @@ -292,11 +293,16 @@ fn concat_expand(
Ok((quote!(#text), FragmentKind::Expr))
}

fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> {
fn relative_file(
db: &dyn AstDatabase,
call_id: MacroCallId,
path: &str,
allow_recursion: bool,
) -> Option<FileId> {
let call_site = call_id.as_file().original_file(db);
let res = db.resolve_path(call_site, path)?;
// Prevent include itself
if res == call_site {
if res == call_site && !allow_recursion {
None
} else {
Some(res)
Expand All @@ -319,8 +325,8 @@ fn include_expand(
tt: &tt::Subtree,
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
let path = parse_string(tt)?;
let file_id =
relative_file(db, arg_id.into(), &path).ok_or_else(|| mbe::ExpandError::ConversionError)?;
let file_id = relative_file(db, arg_id.into(), &path, false)
.ok_or_else(|| mbe::ExpandError::ConversionError)?;

// FIXME:
// Handle include as expression
Expand All @@ -331,6 +337,30 @@ fn include_expand(
Ok((res, FragmentKind::Items))
}

fn include_str_expand(
db: &dyn AstDatabase,
arg_id: EagerMacroId,
tt: &tt::Subtree,
) -> Result<(tt::Subtree, FragmentKind), mbe::ExpandError> {
let path = parse_string(tt)?;

// FIXME: we're not able to read excluded files (which is most of them because
// it's unusual to `include_str!` a Rust file), but we can return an empty string.
// Ideally, we'd be able to offer a precise expansion if the user asks for macro
// expansion.
let file_id = match relative_file(db, arg_id.into(), &path, true) {
Some(file_id) => file_id,
None => {
return Ok((quote!(""), FragmentKind::Expr));
}
};

let text = db.file_text(file_id);
let text = &*text;

Ok((quote!(#text), FragmentKind::Expr))
}

fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
let krate = db.lookup_intern_eager_expansion(arg_id).krate;
db.crate_graph()[krate].env.get(key)
Expand Down
1 change: 1 addition & 0 deletions crates/ra_hir_expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ pub mod known {
stringify,
concat,
include,
include_str,
format_args,
format_args_nl,
env,
Expand Down
40 changes: 25 additions & 15 deletions crates/rust-analyzer/tests/heavy_tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ fn main() {
}
//- /src/main.rs
#[rustc_builtin_macro] macro_rules! include {}
#[rustc_builtin_macro] macro_rules! include_str {}
#[rustc_builtin_macro] macro_rules! concat {}
#[rustc_builtin_macro] macro_rules! env {}

Expand All @@ -512,6 +513,7 @@ fn main() {
let va = A;
let vb = B;
let should_be_str = message();
let another_str = include_str!("main.rs");
}
"###,
)
Expand All @@ -523,7 +525,15 @@ fn main() {
let res = server.send_request::<HoverRequest>(HoverParams {
text_document_position_params: TextDocumentPositionParams::new(
server.doc_id("src/main.rs"),
Position::new(18, 10),
Position::new(19, 10),
),
work_done_progress_params: Default::default(),
});
assert!(res.to_string().contains("&str"));
let res = server.send_request::<HoverRequest>(HoverParams {
text_document_position_params: TextDocumentPositionParams::new(
server.doc_id("src/main.rs"),
Position::new(20, 10),
),
work_done_progress_params: Default::default(),
});
Expand All @@ -532,23 +542,23 @@ fn main() {
GotoDefinitionParams {
text_document_position_params: TextDocumentPositionParams::new(
server.doc_id("src/main.rs"),
Position::new(16, 9),
Position::new(17, 9),
),
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
},
json!([{
"originSelectionRange": {
"end": { "character": 10, "line": 16 },
"start": { "character": 8, "line": 16 }
"end": { "character": 10, "line": 17 },
"start": { "character": 8, "line": 17 }
},
"targetRange": {
"end": { "character": 9, "line": 7 },
"start": { "character": 0, "line": 6 }
"end": { "character": 9, "line": 8 },
"start": { "character": 0, "line": 7 }
},
"targetSelectionRange": {
"end": { "character": 8, "line": 7 },
"start": { "character": 7, "line": 7 }
"end": { "character": 8, "line": 8 },
"start": { "character": 7, "line": 8 }
},
"targetUri": "file:///[..]src/main.rs"
}]),
Expand All @@ -557,23 +567,23 @@ fn main() {
GotoDefinitionParams {
text_document_position_params: TextDocumentPositionParams::new(
server.doc_id("src/main.rs"),
Position::new(17, 9),
Position::new(18, 9),
),
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
},
json!([{
"originSelectionRange": {
"end": { "character": 10, "line": 17 },
"start": { "character": 8, "line": 17 }
"end": { "character": 10, "line": 18 },
"start": { "character": 8, "line": 18 }
},
"targetRange": {
"end": { "character": 9, "line": 11 },
"start": { "character": 0, "line":10 }
"end": { "character": 9, "line": 12 },
"start": { "character": 0, "line":11 }
},
"targetSelectionRange": {
"end": { "character": 8, "line": 11 },
"start": { "character": 7, "line": 11 }
"end": { "character": 8, "line": 12 },
"start": { "character": 7, "line": 12 }
},
"targetUri": "file:///[..]src/main.rs"
}]),
Expand Down