mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2026-03-27 20:40:59 +00:00
Merge pull request #21397 from Shourya742/2026-01-03-fix-source-text
Fix source text
This commit is contained in:
@@ -16,7 +16,7 @@ mod proc_macros;
|
||||
|
||||
use std::{any::TypeId, iter, ops::Range, sync};
|
||||
|
||||
use base_db::{RootQueryDb, SourceDatabase};
|
||||
use base_db::RootQueryDb;
|
||||
use expect_test::Expect;
|
||||
use hir_expand::{
|
||||
AstId, ExpansionInfo, InFile, MacroCallId, MacroCallKind, MacroKind,
|
||||
@@ -387,7 +387,7 @@ struct IdentityWhenValidProcMacroExpander;
|
||||
impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
subtree: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &base_db::Env,
|
||||
|
||||
@@ -4,7 +4,7 @@ use core::fmt;
|
||||
use std::any::Any;
|
||||
use std::{panic::RefUnwindSafe, sync};
|
||||
|
||||
use base_db::{Crate, CrateBuilderId, CratesIdMap, Env, ProcMacroLoadingError, SourceDatabase};
|
||||
use base_db::{Crate, CrateBuilderId, CratesIdMap, Env, ProcMacroLoadingError};
|
||||
use intern::Symbol;
|
||||
use rustc_hash::FxHashMap;
|
||||
use span::Span;
|
||||
@@ -25,7 +25,7 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe + Any {
|
||||
/// [`ProcMacroKind::Attr`]), environment variables, and span information.
|
||||
fn expand(
|
||||
&self,
|
||||
db: &dyn SourceDatabase,
|
||||
db: &dyn ExpandDatabase,
|
||||
subtree: &tt::TopSubtree,
|
||||
attrs: Option<&tt::TopSubtree>,
|
||||
env: &Env,
|
||||
|
||||
@@ -11,15 +11,16 @@ extern crate rustc_driver as _;
|
||||
use std::{any::Any, collections::hash_map::Entry, mem, path::Path, sync};
|
||||
|
||||
use crossbeam_channel::{Receiver, unbounded};
|
||||
use hir_expand::proc_macro::{
|
||||
ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind, ProcMacroLoadResult,
|
||||
ProcMacrosBuilder,
|
||||
use hir_expand::{
|
||||
db::ExpandDatabase,
|
||||
proc_macro::{
|
||||
ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind, ProcMacroLoadResult,
|
||||
ProcMacrosBuilder,
|
||||
},
|
||||
};
|
||||
use ide_db::{
|
||||
ChangeWithProcMacros, FxHashMap, RootDatabase,
|
||||
base_db::{
|
||||
CrateGraphBuilder, Env, ProcMacroLoadingError, SourceDatabase, SourceRoot, SourceRootId,
|
||||
},
|
||||
ChangeWithProcMacros, EditionedFileId, FxHashMap, RootDatabase,
|
||||
base_db::{CrateGraphBuilder, Env, ProcMacroLoadingError, SourceRoot, SourceRootId},
|
||||
prime_caches,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
@@ -530,7 +531,7 @@ struct Expander(proc_macro_api::ProcMacro);
|
||||
impl ProcMacroExpander for Expander {
|
||||
fn expand(
|
||||
&self,
|
||||
db: &dyn SourceDatabase,
|
||||
db: &dyn ExpandDatabase,
|
||||
subtree: &tt::TopSubtree,
|
||||
attrs: Option<&tt::TopSubtree>,
|
||||
env: &Env,
|
||||
@@ -541,30 +542,40 @@ impl ProcMacroExpander for Expander {
|
||||
) -> 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);
|
||||
let file_id = FileId::from_raw(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 { file_id, ast_id, start, end } => {
|
||||
let raw_file_id = FileId::from_raw(file_id);
|
||||
let editioned_file_id = span::EditionedFileId::from_raw(file_id);
|
||||
let ast_id = span::ErasedFileAstId::from_raw(ast_id);
|
||||
let hir_file_id = EditionedFileId::from_span_guess_origin(db, editioned_file_id);
|
||||
let anchor_offset = db
|
||||
.ast_id_map(hir_expand::HirFileId::FileId(hir_file_id))
|
||||
.get_erased(ast_id)
|
||||
.text_range()
|
||||
.start();
|
||||
let anchor_offset = u32::from(anchor_offset);
|
||||
let abs_start = start + anchor_offset;
|
||||
let abs_end = end + anchor_offset;
|
||||
let source = db.file_text(raw_file_id).text(db);
|
||||
let text = source.get(abs_start as usize..abs_end as usize).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);
|
||||
let file_id = FileId::from_raw(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();
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::{
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum SubRequest {
|
||||
FilePath { file_id: u32 },
|
||||
SourceText { file_id: u32, start: u32, end: u32 },
|
||||
SourceText { file_id: u32, ast_id: u32, start: u32, end: u32 },
|
||||
LocalFilePath { file_id: u32 },
|
||||
}
|
||||
|
||||
|
||||
@@ -194,8 +194,9 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl
|
||||
}
|
||||
}
|
||||
|
||||
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String> {
|
||||
match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, start, end }) {
|
||||
fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String> {
|
||||
match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, ast_id, start, end })
|
||||
{
|
||||
Some(bidirectional::BidirectionalMessage::SubResponse(
|
||||
bidirectional::SubResponse::SourceTextResult { text },
|
||||
)) => text,
|
||||
|
||||
@@ -95,7 +95,7 @@ pub type ProcMacroClientHandle<'a> = &'a mut (dyn ProcMacroClientInterface + Syn
|
||||
|
||||
pub trait ProcMacroClientInterface {
|
||||
fn file(&mut self, file_id: u32) -> String;
|
||||
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String>;
|
||||
fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String>;
|
||||
fn local_file(&mut self, file_id: u32) -> Option<String>;
|
||||
}
|
||||
|
||||
|
||||
@@ -154,10 +154,16 @@ impl server::Span for RaSpanServer<'_> {
|
||||
/// https://github.com/rust-lang/rust/pull/55780
|
||||
fn source_text(&mut self, span: Self::Span) -> Option<String> {
|
||||
let file_id = span.anchor.file_id;
|
||||
let ast_id = span.anchor.ast_id;
|
||||
let start: u32 = span.range.start().into();
|
||||
let end: u32 = span.range.end().into();
|
||||
|
||||
self.callback.as_mut()?.source_text(file_id.file_id().index(), start, end)
|
||||
self.callback.as_mut()?.source_text(
|
||||
file_id.file_id().index(),
|
||||
ast_id.into_raw(),
|
||||
start,
|
||||
end,
|
||||
)
|
||||
}
|
||||
|
||||
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
|
||||
|
||||
@@ -19,9 +19,8 @@
|
||||
//! # The Call-site Hierarchy
|
||||
//!
|
||||
//! `ExpnData::call_site` in rustc, [`MacroCallLoc::call_site`] in rust-analyzer.
|
||||
use std::fmt;
|
||||
|
||||
use crate::Edition;
|
||||
use std::fmt;
|
||||
|
||||
/// A syntax context describes a hierarchy tracking order of macro definitions.
|
||||
#[cfg(feature = "salsa")]
|
||||
|
||||
@@ -738,7 +738,7 @@ struct IdentityProcMacroExpander;
|
||||
impl ProcMacroExpander for IdentityProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
subtree: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
@@ -761,7 +761,7 @@ struct Issue18089ProcMacroExpander;
|
||||
impl ProcMacroExpander for Issue18089ProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
subtree: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
@@ -797,7 +797,7 @@ struct AttributeInputReplaceProcMacroExpander;
|
||||
impl ProcMacroExpander for AttributeInputReplaceProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
_: &TopSubtree,
|
||||
attrs: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
@@ -821,7 +821,7 @@ struct Issue18840ProcMacroExpander;
|
||||
impl ProcMacroExpander for Issue18840ProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
fn_: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
@@ -858,7 +858,7 @@ struct MirrorProcMacroExpander;
|
||||
impl ProcMacroExpander for MirrorProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
input: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
@@ -897,7 +897,7 @@ struct ShortenProcMacroExpander;
|
||||
impl ProcMacroExpander for ShortenProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
input: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
@@ -942,7 +942,7 @@ struct Issue17479ProcMacroExpander;
|
||||
impl ProcMacroExpander for Issue17479ProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
subtree: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
@@ -973,7 +973,7 @@ struct Issue18898ProcMacroExpander;
|
||||
impl ProcMacroExpander for Issue18898ProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
subtree: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
@@ -1027,7 +1027,7 @@ struct DisallowCfgProcMacroExpander;
|
||||
impl ProcMacroExpander for DisallowCfgProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
subtree: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
@@ -1059,7 +1059,7 @@ struct GenerateSuffixedTypeProcMacroExpander;
|
||||
impl ProcMacroExpander for GenerateSuffixedTypeProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
_: &dyn SourceDatabase,
|
||||
_: &dyn ExpandDatabase,
|
||||
subtree: &TopSubtree,
|
||||
_attrs: Option<&TopSubtree>,
|
||||
_env: &Env,
|
||||
|
||||
Reference in New Issue
Block a user