Skip to content

Commit bfc263f

Browse files
committed
introduce hacks module
1 parent aa1788d commit bfc263f

File tree

6 files changed

+34
-51
lines changed

6 files changed

+34
-51
lines changed

crates/ide_completion/src/completions/attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
309309
input_expressions
310310
.into_iter()
311311
.filter_map(|(is_sep, group)| (!is_sep).then(|| group))
312-
.filter_map(|mut tokens| ast::Expr::parse(&tokens.join("")).ok())
312+
.filter_map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
313313
.collect::<Vec<ast::Expr>>(),
314314
)
315315
}

crates/ide_completion/src/snippet.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,14 @@ fn validate_snippet(
212212
) -> Option<(Box<[GreenNode]>, String, Option<Box<str>>)> {
213213
let mut imports = Vec::with_capacity(requires.len());
214214
for path in requires.iter() {
215-
let path = ast::Path::parse(path).ok()?;
216-
let valid_use_path = path.segments().all(|seg| {
217-
matches!(seg.kind(), Some(ast::PathSegmentKind::Name(_)))
218-
|| seg.generic_arg_list().is_none()
219-
});
220-
if !valid_use_path {
215+
let use_path = ast::SourceFile::parse(&format!("use {};", path))
216+
.syntax_node()
217+
.descendants()
218+
.find_map(ast::Path::cast)?;
219+
if use_path.syntax().text() != path.as_str() {
221220
return None;
222221
}
223-
let green = path.syntax().green().into_owned();
222+
let green = use_path.syntax().green().into_owned();
224223
imports.push(green);
225224
}
226225
let snippet = snippet.iter().join("\n");

crates/ide_db/src/helpers.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ pub fn get_path_at_cursor_in_tt(cursor: &ast::Ident) -> Option<ast::Path> {
6767
.filter_map(SyntaxElement::into_token)
6868
.take_while(|tok| tok != cursor);
6969

70-
ast::Path::parse(&path_tokens.chain(iter::once(cursor.clone())).join("")).ok()
70+
syntax::hacks::parse_expr_from_str(&path_tokens.chain(iter::once(cursor.clone())).join(""))
71+
.and_then(|expr| match expr {
72+
ast::Expr::PathExpr(it) => it.path(),
73+
_ => None,
74+
})
7175
}
7276

7377
/// Parses and resolves the path at the cursor position in the given attribute, if it is a derive.
@@ -323,7 +327,12 @@ pub fn parse_tt_as_comma_sep_paths(input: ast::TokenTree) -> Option<Vec<ast::Pat
323327
let paths = input_expressions
324328
.into_iter()
325329
.filter_map(|(is_sep, group)| (!is_sep).then(|| group))
326-
.filter_map(|mut tokens| ast::Path::parse(&tokens.join("")).ok())
330+
.filter_map(|mut tokens| {
331+
syntax::hacks::parse_expr_from_str(&tokens.join("")).and_then(|expr| match expr {
332+
ast::Expr::PathExpr(it) => it.path(),
333+
_ => None,
334+
})
335+
})
327336
.collect();
328337
Some(paths)
329338
}

crates/syntax/src/hacks.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Things which exist to solve practial issues, but which shouldn't exist.
2+
//!
3+
//! Please avoid adding new usages of the functions in this module
4+
5+
use crate::{ast, AstNode};
6+
7+
pub fn parse_expr_from_str(s: &str) -> Option<ast::Expr> {
8+
let file = ast::SourceFile::parse(&format!("const _: () = {};", s));
9+
let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?;
10+
if expr.syntax().text() != s {
11+
return None;
12+
}
13+
Some(expr)
14+
}

crates/syntax/src/lib.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub mod ast;
4040
pub mod fuzz;
4141
pub mod utils;
4242
pub mod ted;
43+
pub mod hacks;
4344

4445
use std::{marker::PhantomData, sync::Arc};
4546

@@ -167,26 +168,6 @@ impl SourceFile {
167168
}
168169
}
169170

170-
// FIXME: `parse` functions shouldn't hang directly from AST nodes, and they
171-
// shouldn't return `Result`.
172-
//
173-
// We need a dedicated module for parser entry points, and they should always
174-
// return `Parse`.
175-
176-
impl ast::Path {
177-
/// Returns `text`, parsed as a path, but only if it has no errors.
178-
pub fn parse(text: &str) -> Result<Self, ()> {
179-
parsing::parse_text_as(text, parser::ParserEntryPoint::Path)
180-
}
181-
}
182-
183-
impl ast::Expr {
184-
/// Returns `text`, parsed as an expression, but only if it has no errors.
185-
pub fn parse(text: &str) -> Result<Self, ()> {
186-
parsing::parse_text_as(text, parser::ParserEntryPoint::Expr)
187-
}
188-
}
189-
190171
/// Matches a `SyntaxNode` against an `ast` type.
191172
///
192173
/// # Example:

crates/syntax/src/parsing.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod reparsing;
55

66
use rowan::TextRange;
77

8-
use crate::{syntax_node::GreenNode, AstNode, SyntaxError, SyntaxNode, SyntaxTreeBuilder};
8+
use crate::{syntax_node::GreenNode, SyntaxError, SyntaxTreeBuilder};
99

1010
pub(crate) use crate::parsing::reparsing::incremental_reparse;
1111

@@ -17,26 +17,6 @@ pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
1717
(node, errors)
1818
}
1919

20-
/// Returns `text` parsed as a `T` provided there are no parse errors.
21-
pub(crate) fn parse_text_as<T: AstNode>(
22-
text: &str,
23-
entry_point: parser::ParserEntryPoint,
24-
) -> Result<T, ()> {
25-
let lexed = parser::LexedStr::new(text);
26-
if lexed.errors().next().is_some() {
27-
return Err(());
28-
}
29-
let parser_input = lexed.to_input();
30-
let parser_output = parser::parse(&parser_input, entry_point);
31-
let (node, errors, eof) = build_tree(lexed, parser_output, true);
32-
33-
if !errors.is_empty() || !eof {
34-
return Err(());
35-
}
36-
37-
SyntaxNode::new_root(node).first_child().and_then(T::cast).ok_or(())
38-
}
39-
4020
pub(crate) fn build_tree(
4121
lexed: parser::LexedStr<'_>,
4222
parser_output: parser::Output,

0 commit comments

Comments
 (0)