From 57030f587bb0bbe4dea9a97016b4e0f49a7ef5f3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 31 Dec 2018 19:19:50 +0300 Subject: use macros database in analysis --- crates/ra_analysis/src/db.rs | 3 ++ crates/ra_analysis/src/extend_selection.rs | 8 ++-- crates/ra_analysis/src/macros.rs | 67 +++++---------------------- crates/ra_analysis/src/syntax_highlighting.rs | 6 +-- 4 files changed, 22 insertions(+), 62 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index b072a5eba..712f72484 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -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; + } } } diff --git a/crates/ra_analysis/src/extend_selection.rs b/crates/ra_analysis/src/extend_selection.rs index 805e9059e..62133ef29 100644 --- a/crates/ra_analysis/src/extend_selection.rs +++ b/crates/ra_analysis/src/extend_selection.rs @@ -23,10 +23,10 @@ fn extend_selection_in_macro( frange: FileRange, ) -> Option { 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) } diff --git a/crates/ra_analysis/src/macros.rs b/crates/ra_analysis/src/macros.rs index b9feb7fad..21ec36cd6 100644 --- a/crates/ra_analysis/src/macros.rs +++ b/crates/ra_analysis/src/macros.rs @@ -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 { +) -> Option<(TextUnit, Arc)> { 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 { - 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 { - 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)) } diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index 7e9139a74..35a4630e9 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs @@ -15,13 +15,13 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable Date: Tue, 1 Jan 2019 18:11:04 +0300 Subject: move more macros to hir --- crates/ra_analysis/src/db.rs | 18 +++++++++------ crates/ra_analysis/src/extend_selection.rs | 4 ++-- crates/ra_analysis/src/lib.rs | 1 - crates/ra_analysis/src/macros.rs | 32 --------------------------- crates/ra_analysis/src/syntax_highlighting.rs | 2 +- 5 files changed, 14 insertions(+), 43 deletions(-) delete mode 100644 crates/ra_analysis/src/macros.rs (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 712f72484..2a71cc2b6 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -1,7 +1,6 @@ use std::{fmt, sync::Arc}; use salsa::{self, Database}; use ra_db::{LocationIntener, BaseDatabase}; -use hir::{self, DefId, DefLoc}; use crate::{ symbol_index, @@ -15,7 +14,8 @@ pub(crate) struct RootDatabase { #[derive(Default)] struct IdMaps { - defs: LocationIntener, + defs: LocationIntener, + macros: LocationIntener, } impl fmt::Debug for IdMaps { @@ -59,12 +59,18 @@ impl salsa::ParallelDatabase for RootDatabase { impl BaseDatabase for RootDatabase {} -impl AsRef> for RootDatabase { - fn as_ref(&self) -> &LocationIntener { +impl AsRef> for RootDatabase { + fn as_ref(&self) -> &LocationIntener { &self.id_maps.defs } } +impl AsRef> for RootDatabase { + fn as_ref(&self) -> &LocationIntener { + &self.id_maps.macros + } +} + salsa::database_storage! { pub(crate) struct RootDatabaseStorage for RootDatabase { impl ra_db::FilesDatabase { @@ -85,6 +91,7 @@ salsa::database_storage! { fn library_symbols() for symbol_index::LibrarySymbolsQuery; } impl hir::db::HirDatabase { + fn expand_macro_invocation() for hir::db::ExpandMacroInvocationQuery; fn module_tree() for hir::db::ModuleTreeQuery; fn fn_scopes() for hir::db::FnScopesQuery; fn file_items() for hir::db::SourceFileItemsQuery; @@ -98,8 +105,5 @@ 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; - } } } diff --git a/crates/ra_analysis/src/extend_selection.rs b/crates/ra_analysis/src/extend_selection.rs index 62133ef29..f1b77f981 100644 --- a/crates/ra_analysis/src/extend_selection.rs +++ b/crates/ra_analysis/src/extend_selection.rs @@ -18,12 +18,12 @@ pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRang } fn extend_selection_in_macro( - db: &RootDatabase, + _db: &RootDatabase, source_file: &SourceFileNode, frange: FileRange, ) -> Option { let macro_call = find_macro_call(source_file.syntax(), frange.range)?; - let (off, exp) = crate::macros::expand(db, frange.file_id, macro_call)?; + let (off, exp) = hir::MacroDef::ast_expand(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; diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index e6cfaecc3..08ecb125a 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -19,7 +19,6 @@ mod runnables; mod extend_selection; mod syntax_highlighting; -mod macros; use std::{fmt, sync::Arc}; diff --git a/crates/ra_analysis/src/macros.rs b/crates/ra_analysis/src/macros.rs deleted file mode 100644 index 21ec36cd6..000000000 --- a/crates/ra_analysis/src/macros.rs +++ /dev/null @@ -1,32 +0,0 @@ -/// Begining of macro expansion. -/// -/// This code should be moved out of ra_analysis into hir (?) ideally. -use std::sync::Arc; - -use ra_syntax::{ast, AstNode, TextUnit}; -use hir::MacroDatabase; - -use crate::{db::RootDatabase, FileId}; - -pub(crate) fn expand( - db: &RootDatabase, - _file_id: FileId, - macro_call: ast::MacroCall, -) -> Option<(TextUnit, Arc)> { - let path = macro_call.path()?; - if path.qualifier().is_some() { - return None; - } - let name_ref = path.segment()?.name_ref()?; - if name_ref.text() != "ctry" { - return None; - } - let arg = macro_call.token_tree()?.syntax(); - - let def = hir::MacroDef::CTry; - let input = hir::MacroInput { - text: arg.text().to_string(), - }; - let exp = db.expand_macro(def, input)?; - Some((arg.range().start(), exp)) -} diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index 35a4630e9..a644b3fe0 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs @@ -15,7 +15,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable Date: Tue, 1 Jan 2019 18:12:31 +0300 Subject: rename --- crates/ra_analysis/src/db.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 2a71cc2b6..86c15f4ee 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -15,7 +15,7 @@ pub(crate) struct RootDatabase { #[derive(Default)] struct IdMaps { defs: LocationIntener, - macros: LocationIntener, + macros: LocationIntener, } impl fmt::Debug for IdMaps { @@ -65,8 +65,8 @@ impl AsRef> for RootDatabase { } } -impl AsRef> for RootDatabase { - fn as_ref(&self) -> &LocationIntener { +impl AsRef> for RootDatabase { + fn as_ref(&self) -> &LocationIntener { &self.id_maps.macros } } @@ -91,7 +91,7 @@ salsa::database_storage! { fn library_symbols() for symbol_index::LibrarySymbolsQuery; } impl hir::db::HirDatabase { - fn expand_macro_invocation() for hir::db::ExpandMacroInvocationQuery; + fn expand_macro_invocation() for hir::db::ExpandMacroCallQuery; fn module_tree() for hir::db::ModuleTreeQuery; fn fn_scopes() for hir::db::FnScopesQuery; fn file_items() for hir::db::SourceFileItemsQuery; -- cgit v1.2.3 From e5b2fd67711eeab6146021542937028dff3bf8a7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 1 Jan 2019 19:23:03 +0300 Subject: hard-code expansion of query_group --- crates/ra_analysis/src/syntax_highlighting.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/syntax_highlighting.rs b/crates/ra_analysis/src/syntax_highlighting.rs index a644b3fe0..ccea4aee3 100644 --- a/crates/ra_analysis/src/syntax_highlighting.rs +++ b/crates/ra_analysis/src/syntax_highlighting.rs @@ -44,7 +44,7 @@ mod tests { fn main() { ctry!({ let x = 92; x}); } - ", + ", ); let highlights = analysis.highlight(file_id).unwrap(); assert_eq_dbg( @@ -60,4 +60,26 @@ mod tests { &highlights, ) } + + // FIXME: this test is not really necessary: artifact of the inital hacky + // macros implementation. + #[test] + fn highlight_query_group_macro() { + let (analysis, file_id) = single_file( + " + salsa::query_group! { + pub trait HirDatabase: SyntaxDatabase {} + } + ", + ); + let highlights = analysis.highlight(file_id).unwrap(); + assert_eq_dbg( + r#"[HighlightedRange { range: [20; 32), tag: "macro" }, + HighlightedRange { range: [13; 18), tag: "text" }, + HighlightedRange { range: [51; 54), tag: "keyword" }, + HighlightedRange { range: [55; 60), tag: "keyword" }, + HighlightedRange { range: [61; 72), tag: "function" }]"#, + &highlights, + ) + } } -- cgit v1.2.3 From 756e8781584fb28af208218615bafbbd9164dfbc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 1 Jan 2019 21:52:07 +0300 Subject: add items from macros to modules --- crates/ra_analysis/src/db.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index 86c15f4ee..adfec56d8 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -91,6 +91,7 @@ salsa::database_storage! { fn library_symbols() for symbol_index::LibrarySymbolsQuery; } impl hir::db::HirDatabase { + fn m_source_file() for hir::db::MSourceFileQuery; fn expand_macro_invocation() for hir::db::ExpandMacroCallQuery; fn module_tree() for hir::db::ModuleTreeQuery; fn fn_scopes() for hir::db::FnScopesQuery; -- cgit v1.2.3 From 37ed2f35badfb41cd6c50ef04d6fd6a6ce67e0d1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 1 Jan 2019 23:21:16 +0300 Subject: rename MFileId -> HirFileId --- crates/ra_analysis/src/completion/complete_scope.rs | 2 +- crates/ra_analysis/src/db.rs | 2 +- crates/ra_analysis/src/imp.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/completion/complete_scope.rs b/crates/ra_analysis/src/completion/complete_scope.rs index daf666505..4dead3689 100644 --- a/crates/ra_analysis/src/completion/complete_scope.rs +++ b/crates/ra_analysis/src/completion/complete_scope.rs @@ -27,7 +27,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> match res.import { None => true, Some(import) => { - let range = import.range(ctx.db, module.source().file_id()); + let range = import.range(ctx.db, module.file_id()); !range.is_subrange(&ctx.leaf.range()) } } diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index adfec56d8..d7740f0c4 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs @@ -91,7 +91,7 @@ salsa::database_storage! { fn library_symbols() for symbol_index::LibrarySymbolsQuery; } impl hir::db::HirDatabase { - fn m_source_file() for hir::db::MSourceFileQuery; + fn hir_source_file() for hir::db::HirSourceFileQuery; fn expand_macro_invocation() for hir::db::ExpandMacroCallQuery; fn module_tree() for hir::db::ModuleTreeQuery; fn fn_scopes() for hir::db::FnScopesQuery; diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 5669aa94d..ec7da437a 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -230,7 +230,7 @@ impl AnalysisImpl { Some(it) => it, }; let root = descr.crate_root(); - let file_id = root.source().file_id(); + let file_id = root.file_id(); let crate_graph = self.db.crate_graph(); let crate_id = crate_graph.crate_id_for_crate_root(file_id); @@ -283,7 +283,7 @@ impl AnalysisImpl { if let Some(child_module) = source_binder::module_from_declaration(&*self.db, position.file_id, module)? { - let file_id = child_module.source().file_id(); + let file_id = child_module.file_id(); let name = match child_module.name() { Some(name) => name.to_string().into(), None => "".into(), -- cgit v1.2.3