mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
use macros database in analysis
This commit is contained in:
parent
d753656192
commit
57030f587b
@ -98,5 +98,8 @@ salsa::database_storage! {
|
||||
fn struct_data() for hir::db::StructDataQuery;
|
||||
fn enum_data() for hir::db::EnumDataQuery;
|
||||
}
|
||||
impl hir::MacroDatabase {
|
||||
fn expand_macro() for hir::ExpandMacroQuery;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,10 +23,10 @@ fn extend_selection_in_macro(
|
||||
frange: FileRange,
|
||||
) -> Option<TextRange> {
|
||||
let macro_call = find_macro_call(source_file.syntax(), frange.range)?;
|
||||
let exp = crate::macros::expand(db, frange.file_id, macro_call)?;
|
||||
let dst_range = exp.map_range_forward(frange.range)?;
|
||||
let dst_range = ra_editor::extend_selection(exp.source_file().syntax(), dst_range)?;
|
||||
let src_range = exp.map_range_back(dst_range)?;
|
||||
let (off, exp) = crate::macros::expand(db, frange.file_id, macro_call)?;
|
||||
let dst_range = exp.map_range_forward(frange.range - off)?;
|
||||
let dst_range = ra_editor::extend_selection(exp.syntax().borrowed(), dst_range)?;
|
||||
let src_range = exp.map_range_back(dst_range)? + off;
|
||||
Some(src_range)
|
||||
}
|
||||
|
||||
|
@ -1,15 +1,18 @@
|
||||
/// Begining of macro expansion.
|
||||
///
|
||||
/// This code should be moved out of ra_analysis into hir (?) ideally.
|
||||
use ra_syntax::{ast, AstNode, SourceFileNode, TextRange};
|
||||
use std::sync::Arc;
|
||||
|
||||
use ra_syntax::{ast, AstNode, TextUnit};
|
||||
use hir::MacroDatabase;
|
||||
|
||||
use crate::{db::RootDatabase, FileId};
|
||||
|
||||
pub(crate) fn expand(
|
||||
_db: &RootDatabase,
|
||||
db: &RootDatabase,
|
||||
_file_id: FileId,
|
||||
macro_call: ast::MacroCall,
|
||||
) -> Option<MacroExpansion> {
|
||||
) -> Option<(TextUnit, Arc<hir::MacroExpansion>)> {
|
||||
let path = macro_call.path()?;
|
||||
if path.qualifier().is_some() {
|
||||
return None;
|
||||
@ -18,58 +21,12 @@ pub(crate) fn expand(
|
||||
if name_ref.text() != "ctry" {
|
||||
return None;
|
||||
}
|
||||
let arg = macro_call.token_tree()?.syntax();
|
||||
|
||||
let arg = macro_call.token_tree()?;
|
||||
let text = format!(
|
||||
r"
|
||||
fn dummy() {{
|
||||
match {} {{
|
||||
None => return Ok(None),
|
||||
Some(it) => it,
|
||||
}}
|
||||
}}",
|
||||
arg.syntax().text()
|
||||
);
|
||||
let file = SourceFileNode::parse(&text);
|
||||
let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?;
|
||||
let match_arg = match_expr.expr()?;
|
||||
let ranges_map = vec![(arg.syntax().range(), match_arg.syntax().range())];
|
||||
let res = MacroExpansion {
|
||||
source_file: file,
|
||||
ranges_map,
|
||||
let def = hir::MacroDef::CTry;
|
||||
let input = hir::MacroInput {
|
||||
text: arg.text().to_string(),
|
||||
};
|
||||
Some(res)
|
||||
}
|
||||
|
||||
pub(crate) struct MacroExpansion {
|
||||
pub(crate) source_file: SourceFileNode,
|
||||
pub(crate) ranges_map: Vec<(TextRange, TextRange)>,
|
||||
}
|
||||
|
||||
impl MacroExpansion {
|
||||
pub(crate) fn source_file(&self) -> &SourceFileNode {
|
||||
&self.source_file
|
||||
}
|
||||
pub(crate) fn map_range_back(&self, tgt_range: TextRange) -> Option<TextRange> {
|
||||
for (s_range, t_range) in self.ranges_map.iter() {
|
||||
if tgt_range.is_subrange(&t_range) {
|
||||
let tgt_at_zero_range = tgt_range - tgt_range.start();
|
||||
let tgt_range_offset = tgt_range.start() - t_range.start();
|
||||
let src_range = tgt_at_zero_range + tgt_range_offset + s_range.start();
|
||||
return Some(src_range);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
pub(crate) fn map_range_forward(&self, src_range: TextRange) -> Option<TextRange> {
|
||||
for (s_range, t_range) in self.ranges_map.iter() {
|
||||
if src_range.is_subrange(&s_range) {
|
||||
let src_at_zero_range = src_range - src_range.start();
|
||||
let src_range_offset = src_range.start() - s_range.start();
|
||||
let src_range = src_at_zero_range + src_range_offset + t_range.start();
|
||||
return Some(src_range);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
let exp = db.expand_macro(def, input)?;
|
||||
Some((arg.range().start(), exp))
|
||||
}
|
||||
|
@ -15,13 +15,13 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<Hi
|
||||
.descendants()
|
||||
.filter_map(ast::MacroCall::cast)
|
||||
{
|
||||
if let Some(exp) = crate::macros::expand(db, file_id, macro_call) {
|
||||
let mapped_ranges = ra_editor::highlight(exp.source_file().syntax())
|
||||
if let Some((off, exp)) = crate::macros::expand(db, file_id, macro_call) {
|
||||
let mapped_ranges = ra_editor::highlight(exp.syntax().borrowed())
|
||||
.into_iter()
|
||||
.filter_map(|r| {
|
||||
let mapped_range = exp.map_range_back(r.range)?;
|
||||
let res = HighlightedRange {
|
||||
range: mapped_range,
|
||||
range: mapped_range + off,
|
||||
tag: r.tag,
|
||||
};
|
||||
Some(res)
|
||||
|
@ -46,6 +46,7 @@ pub use self::{
|
||||
path::{Path, PathKind},
|
||||
name::Name,
|
||||
krate::Crate,
|
||||
macros::{MacroDef, MacroInput, MacroExpansion, MacroDatabase, ExpandMacroQuery},
|
||||
module::{Module, ModuleId, Problem, nameres::{ItemMap, PerNs, Namespace}, ModuleScope, Resolution},
|
||||
function::{Function, FnScopes},
|
||||
adt::{Struct, Enum},
|
||||
|
@ -1,7 +1,10 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use ra_db::SyntaxDatabase;
|
||||
use ra_syntax::{TextRange, TextUnit, SourceFileNode, AstNode, ast};
|
||||
use ra_db::{SyntaxDatabase, LocalSyntaxPtr};
|
||||
use ra_syntax::{
|
||||
TextRange, TextUnit, SourceFileNode, AstNode, SyntaxNode,
|
||||
ast,
|
||||
};
|
||||
|
||||
// Hard-coded defs for now :-(
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
@ -12,18 +15,19 @@ pub enum MacroDef {
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct MacroInput {
|
||||
// Should be token trees
|
||||
text: String,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct MacroExpansion {
|
||||
text: String,
|
||||
ranges_map: Vec<(TextRange, TextRange)>,
|
||||
ptr: LocalSyntaxPtr,
|
||||
}
|
||||
|
||||
salsa::query_group! {
|
||||
|
||||
pub trait MacrosDatabase: SyntaxDatabase {
|
||||
pub trait MacroDatabase: SyntaxDatabase {
|
||||
fn expand_macro(def: MacroDef, input: MacroInput) -> Option<Arc<MacroExpansion>> {
|
||||
type ExpandMacroQuery;
|
||||
}
|
||||
@ -32,7 +36,7 @@ pub trait MacrosDatabase: SyntaxDatabase {
|
||||
}
|
||||
|
||||
fn expand_macro(
|
||||
_db: &impl MacrosDatabase,
|
||||
_db: &impl MacroDatabase,
|
||||
def: MacroDef,
|
||||
input: MacroInput,
|
||||
) -> Option<Arc<MacroExpansion>> {
|
||||
@ -50,8 +54,45 @@ fn expand_macro(
|
||||
let file = SourceFileNode::parse(&text);
|
||||
let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?;
|
||||
let match_arg = match_expr.expr()?;
|
||||
let ptr = LocalSyntaxPtr::new(match_arg.syntax());
|
||||
let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text));
|
||||
let ranges_map = vec![(src_range, match_arg.syntax().range())];
|
||||
let res = MacroExpansion { text, ranges_map };
|
||||
let res = MacroExpansion {
|
||||
text,
|
||||
ranges_map,
|
||||
ptr,
|
||||
};
|
||||
Some(Arc::new(res))
|
||||
}
|
||||
|
||||
impl MacroExpansion {
|
||||
pub fn file(&self) -> SourceFileNode {
|
||||
SourceFileNode::parse(&self.text)
|
||||
}
|
||||
|
||||
pub fn syntax(&self) -> SyntaxNode {
|
||||
self.ptr.resolve(&self.file())
|
||||
}
|
||||
pub fn map_range_back(&self, tgt_range: TextRange) -> Option<TextRange> {
|
||||
for (s_range, t_range) in self.ranges_map.iter() {
|
||||
if tgt_range.is_subrange(&t_range) {
|
||||
let tgt_at_zero_range = tgt_range - tgt_range.start();
|
||||
let tgt_range_offset = tgt_range.start() - t_range.start();
|
||||
let src_range = tgt_at_zero_range + tgt_range_offset + s_range.start();
|
||||
return Some(src_range);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
pub fn map_range_forward(&self, src_range: TextRange) -> Option<TextRange> {
|
||||
for (s_range, t_range) in self.ranges_map.iter() {
|
||||
if src_range.is_subrange(&s_range) {
|
||||
let src_at_zero_range = src_range - src_range.start();
|
||||
let src_range_offset = src_range.start() - s_range.start();
|
||||
let src_range = src_at_zero_range + src_range_offset + t_range.start();
|
||||
return Some(src_range);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user