Skip to content

Commit 89831ff

Browse files
committed
Add span to callbacks and correct the source_text implementation
1 parent cff5182 commit 89831ff

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

crates/load-cargo/src/lib.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -541,31 +541,37 @@ impl ProcMacroExpander for Expander {
541541
current_dir: String,
542542
) -> Result<tt::TopSubtree, ProcMacroExpansionError> {
543543
let mut cb = |req| match req {
544-
SubRequest::LocalFilePath { file_id } => {
545-
let file = FileId::from_raw(file_id);
546-
let source_root_id = db.file_source_root(file).source_root_id(db);
544+
SubRequest::LocalFilePath { span } => {
545+
let file_id = span.anchor.file_id.file_id();
546+
let source_root_id = db.file_source_root(file_id).source_root_id(db);
547547
let source_root = db.source_root(source_root_id).source_root(db);
548-
549548
let name = source_root
550-
.path_for_file(&file)
549+
.path_for_file(&file_id)
551550
.and_then(|path| path.as_path())
552551
.map(|path| path.to_string());
553552

554553
Ok(SubResponse::LocalFilePathResult { name })
555554
}
556-
SubRequest::SourceText { file_id, start, end } => {
557-
let file = FileId::from_raw(file_id);
558-
let text = db.file_text(file).text(db);
559-
let slice = text.get(start as usize..end as usize).map(ToOwned::to_owned);
560-
Ok(SubResponse::SourceTextResult { text: slice })
555+
SubRequest::SourceText { span } => {
556+
let anchor = span.anchor;
557+
let file_id = EditionedFileId::from_span_guess_origin(db, anchor.file_id);
558+
let range = db
559+
.ast_id_map(hir_expand::HirFileId::FileId(file_id))
560+
.get_erased(anchor.ast_id)
561+
.text_range();
562+
let source = db.file_text(anchor.file_id.file_id()).text(db);
563+
let text = source
564+
.get(usize::from(range.start())..usize::from(range.end()))
565+
.map(ToOwned::to_owned);
566+
567+
Ok(SubResponse::SourceTextResult { text })
561568
}
562-
SubRequest::FilePath { file_id } => {
563-
let file = FileId::from_raw(file_id);
564-
let source_root_id = db.file_source_root(file).source_root_id(db);
569+
SubRequest::FilePath { span } => {
570+
let file_id = span.anchor.file_id.file_id();
571+
let source_root_id = db.file_source_root(file_id).source_root_id(db);
565572
let source_root = db.source_root(source_root_id).source_root(db);
566-
567573
let name = source_root
568-
.path_for_file(&file)
574+
.path_for_file(&file_id)
569575
.and_then(|path| path.as_path())
570576
.map(|path| path.to_string())
571577
.unwrap_or_default();

0 commit comments

Comments
 (0)