Unnamed repository; edit this file 'description' to name the repository.
Modify `hacks::parse_expr_from_str()` to take an edition too
This will be needed as we parse unknown identifiers and want to insert them into source code.
Chayim Refael Friedman 2024-08-25
parent e6d59e6 · commit ddbb28d
-rw-r--r--crates/ide-assists/src/handlers/remove_dbg.rs4
-rw-r--r--crates/ide-completion/src/completions/attribute.rs6
-rw-r--r--crates/ide-db/src/syntax_helpers/node_ext.rs10
-rw-r--r--crates/syntax/src/hacks.rs4
4 files changed, 14 insertions, 10 deletions
diff --git a/crates/ide-assists/src/handlers/remove_dbg.rs b/crates/ide-assists/src/handlers/remove_dbg.rs
index cffa3f55c9..0e9c463e02 100644
--- a/crates/ide-assists/src/handlers/remove_dbg.rs
+++ b/crates/ide-assists/src/handlers/remove_dbg.rs
@@ -1,7 +1,7 @@
use itertools::Itertools;
use syntax::{
ast::{self, make, AstNode, AstToken},
- match_ast, ted, NodeOrToken, SyntaxElement, TextRange, TextSize, T,
+ match_ast, ted, Edition, NodeOrToken, SyntaxElement, TextRange, TextSize, T,
};
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -77,7 +77,7 @@ fn compute_dbg_replacement(macro_expr: ast::MacroExpr) -> Option<(TextRange, Opt
let input_expressions = input_expressions
.into_iter()
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
- .map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
+ .map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join(""), Edition::CURRENT))
.collect::<Option<Vec<ast::Expr>>>()?;
let parent = macro_expr.syntax().parent()?;
diff --git a/crates/ide-completion/src/completions/attribute.rs b/crates/ide-completion/src/completions/attribute.rs
index 75bd327608..9821fb4a2f 100644
--- a/crates/ide-completion/src/completions/attribute.rs
+++ b/crates/ide-completion/src/completions/attribute.rs
@@ -14,7 +14,7 @@ use ide_db::{
use itertools::Itertools;
use syntax::{
ast::{self, AttrKind},
- AstNode, SyntaxKind, T,
+ AstNode, Edition, SyntaxKind, T,
};
use crate::{
@@ -373,7 +373,9 @@ fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
input_expressions
.into_iter()
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
- .filter_map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
+ .filter_map(|mut tokens| {
+ syntax::hacks::parse_expr_from_str(&tokens.join(""), Edition::CURRENT)
+ })
.collect::<Vec<ast::Expr>>(),
)
}
diff --git a/crates/ide-db/src/syntax_helpers/node_ext.rs b/crates/ide-db/src/syntax_helpers/node_ext.rs
index fa82902a74..91e0b4495f 100644
--- a/crates/ide-db/src/syntax_helpers/node_ext.rs
+++ b/crates/ide-db/src/syntax_helpers/node_ext.rs
@@ -477,10 +477,12 @@ pub fn parse_tt_as_comma_sep_paths(
.into_iter()
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
.filter_map(|mut tokens| {
- syntax::hacks::parse_expr_from_str(&tokens.join("")).and_then(|expr| match expr {
- ast::Expr::PathExpr(it) => it.path(),
- _ => None,
- })
+ syntax::hacks::parse_expr_from_str(&tokens.join(""), Edition::CURRENT).and_then(
+ |expr| match expr {
+ ast::Expr::PathExpr(it) => it.path(),
+ _ => None,
+ },
+ )
})
.collect();
Some(paths)
diff --git a/crates/syntax/src/hacks.rs b/crates/syntax/src/hacks.rs
index 36615d11d8..9e63448ce9 100644
--- a/crates/syntax/src/hacks.rs
+++ b/crates/syntax/src/hacks.rs
@@ -6,9 +6,9 @@ use parser::Edition;
use crate::{ast, AstNode};
-pub fn parse_expr_from_str(s: &str) -> Option<ast::Expr> {
+pub fn parse_expr_from_str(s: &str, edition: Edition) -> Option<ast::Expr> {
let s = s.trim();
- let file = ast::SourceFile::parse(&format!("const _: () = {s};"), Edition::CURRENT);
+ let file = ast::SourceFile::parse(&format!("const _: () = {s};"), edition);
let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?;
if expr.syntax().text() != s {
return None;