Unnamed repository; edit this file 'description' to name the repository.
Add span to callbacks and correct the source_text implementation
| -rw-r--r-- | crates/load-cargo/src/lib.rs | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/crates/load-cargo/src/lib.rs b/crates/load-cargo/src/lib.rs index 94fac0bd33..474046ee2f 100644 --- a/crates/load-cargo/src/lib.rs +++ b/crates/load-cargo/src/lib.rs @@ -541,31 +541,37 @@ impl ProcMacroExpander for Expander { current_dir: String, ) -> Result<tt::TopSubtree, ProcMacroExpansionError> { let mut cb = |req| match req { - SubRequest::LocalFilePath { file_id } => { - let file = FileId::from_raw(file_id); - let source_root_id = db.file_source_root(file).source_root_id(db); + SubRequest::LocalFilePath { span } => { + let file_id = span.anchor.file_id.file_id(); + let source_root_id = db.file_source_root(file_id).source_root_id(db); let source_root = db.source_root(source_root_id).source_root(db); - let name = source_root - .path_for_file(&file) + .path_for_file(&file_id) .and_then(|path| path.as_path()) .map(|path| path.to_string()); Ok(SubResponse::LocalFilePathResult { name }) } - SubRequest::SourceText { file_id, start, end } => { - let file = FileId::from_raw(file_id); - let text = db.file_text(file).text(db); - let slice = text.get(start as usize..end as usize).map(ToOwned::to_owned); - Ok(SubResponse::SourceTextResult { text: slice }) + SubRequest::SourceText { span } => { + let anchor = span.anchor; + let file_id = EditionedFileId::from_span_guess_origin(db, anchor.file_id); + let range = db + .ast_id_map(hir_expand::HirFileId::FileId(file_id)) + .get_erased(anchor.ast_id) + .text_range(); + let source = db.file_text(anchor.file_id.file_id()).text(db); + let text = source + .get(usize::from(range.start())..usize::from(range.end())) + .map(ToOwned::to_owned); + + Ok(SubResponse::SourceTextResult { text }) } - SubRequest::FilePath { file_id } => { - let file = FileId::from_raw(file_id); - let source_root_id = db.file_source_root(file).source_root_id(db); + SubRequest::FilePath { span } => { + let file_id = span.anchor.file_id.file_id(); + let source_root_id = db.file_source_root(file_id).source_root_id(db); let source_root = db.source_root(source_root_id).source_root(db); - let name = source_root - .path_for_file(&file) + .path_for_file(&file_id) .and_then(|path| path.as_path()) .map(|path| path.to_string()) .unwrap_or_default(); |