Skip to content

Commit

Permalink
Modify hacks::parse_expr_from_str() to take an edition too
Browse files Browse the repository at this point in the history
This will be needed as we parse unknown identifiers and want to insert them into source code.
  • Loading branch information
ChayimFriedman2 committed Aug 24, 2024
1 parent e6d59e6 commit ddbb28d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions crates/ide-assists/src/handlers/remove_dbg.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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()?;
Expand Down
6 changes: 4 additions & 2 deletions crates/ide-completion/src/completions/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ide_db::{
use itertools::Itertools;
use syntax::{
ast::{self, AttrKind},
AstNode, SyntaxKind, T,
AstNode, Edition, SyntaxKind, T,
};

use crate::{
Expand Down Expand Up @@ -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>>(),
)
}
Expand Down
10 changes: 6 additions & 4 deletions crates/ide-db/src/syntax_helpers/node_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions crates/syntax/src/hacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ddbb28d

Please sign in to comment.