Add span to callbacks and correct the source_text implementation

This commit is contained in:
bit-aloo 2026-01-03 19:18:36 +05:30
parent be566f4e6b
commit 4f5502e8fb
No known key found for this signature in database

View file

@ -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::FilePath { file_id } => {
let file = FileId::from_raw(file_id);
let source_root_id = db.file_source_root(file).source_root_id(db);
let source_root = db.source_root(source_root_id).source_root(db);
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 { 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();