Skip to content

Commit b46c77e

Browse files
committed
Provide more complete AST accessors to support usage in rustc
1 parent fb9b516 commit b46c77e

File tree

19 files changed

+4324
-1334
lines changed

19 files changed

+4324
-1334
lines changed

crates/ra_assists/src/handlers/add_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ra_syntax::{
2-
ast::{self, AstNode, NameOwner, TypeParamsOwner},
2+
ast::{self, AstNode, AstToken, NameOwner, TypeParamsOwner},
33
TextUnit,
44
};
55
use stdx::{format_to, SepBy};
@@ -42,7 +42,7 @@ pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> {
4242
if let Some(type_params) = type_params {
4343
let lifetime_params = type_params
4444
.lifetime_params()
45-
.filter_map(|it| it.lifetime_token())
45+
.filter_map(|it| it.lifetime())
4646
.map(|it| it.text().clone());
4747
let type_params =
4848
type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());

crates/ra_assists/src/handlers/add_new.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use hir::Adt;
22
use ra_syntax::{
33
ast::{
4-
self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner,
4+
self, AstNode, AstToken, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner,
5+
VisibilityOwner,
56
},
67
TextUnit, T,
78
};
@@ -105,7 +106,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String {
105106
if let Some(type_params) = type_params {
106107
let lifetime_params = type_params
107108
.lifetime_params()
108-
.filter_map(|it| it.lifetime_token())
109+
.filter_map(|it| it.lifetime())
109110
.map(|it| it.text().clone());
110111
let type_params =
111112
type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone());

crates/ra_assists/src/handlers/introduce_variable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ra_syntax::{
2-
ast::{self, AstNode},
2+
ast::{self, AstElement, AstNode},
33
SyntaxKind::{
44
BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR,
55
WHITESPACE,
@@ -124,7 +124,7 @@ fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> {
124124
}
125125
}
126126

127-
if ast::Stmt::cast(node.clone()).is_some() {
127+
if ast::Stmt::cast_element(node.clone().into()).is_some() {
128128
return Some((node, false));
129129
}
130130

crates/ra_assists/src/handlers/merge_imports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::iter::successors;
33
use ra_syntax::{
44
algo::{neighbor, SyntaxRewriter},
55
ast::{self, edit::AstNodeEdit, make},
6-
AstNode, Direction, InsertPosition, SyntaxElement, T,
6+
AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T,
77
};
88

99
use crate::{Assist, AssistCtx, AssistId};
@@ -82,7 +82,7 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTre
8282
.filter(|it| it.kind() != T!['{'] && it.kind() != T!['}']),
8383
);
8484
let use_tree_list = lhs.use_tree_list()?;
85-
let pos = InsertPosition::Before(use_tree_list.r_curly()?.into());
85+
let pos = InsertPosition::Before(use_tree_list.r_curly()?.syntax().clone().into());
8686
let use_tree_list = use_tree_list.insert_children(pos, to_insert);
8787
Some(lhs.with_use_tree_list(use_tree_list))
8888
}

crates/ra_fmt/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
6060
} else {
6161
// Unwrap `{ continue; }`
6262
let (stmt,) = block.statements().next_tuple()?;
63-
if has_anything_else(stmt.syntax()) {
64-
return None;
65-
}
6663
if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
64+
if has_anything_else(expr_stmt.syntax()) {
65+
return None;
66+
}
6767
let expr = expr_stmt.expr()?;
6868
match expr.syntax().kind() {
6969
CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),

crates/ra_hir_def/src/body/lower.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,17 @@ impl ExprCollector<'_> {
501501
self.collect_block_items(&block);
502502
let statements = block
503503
.statements()
504-
.map(|s| match s {
504+
.filter_map(|s| match s {
505505
ast::Stmt::LetStmt(stmt) => {
506506
let pat = self.collect_pat_opt(stmt.pat());
507507
let type_ref = stmt.ascribed_type().map(TypeRef::from_ast);
508508
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
509-
Statement::Let { pat, type_ref, initializer }
509+
Some(Statement::Let { pat, type_ref, initializer })
510510
}
511-
ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())),
511+
ast::Stmt::ExprStmt(stmt) => {
512+
Some(Statement::Expr(self.collect_expr_opt(stmt.expr())))
513+
}
514+
ast::Stmt::ModuleItem(_) => None,
512515
})
513516
.collect();
514517
let tail = block.expr().map(|e| self.collect_expr(e));
@@ -560,6 +563,7 @@ impl ExprCollector<'_> {
560563
let ast_id = self.expander.ast_id(&def);
561564
(TraitLoc { container, ast_id }.intern(self.db).into(), def.name())
562565
}
566+
ast::ModuleItem::ExternBlock(_) => continue, // FIXME: collect from extern blocks
563567
ast::ModuleItem::ImplDef(_)
564568
| ast::ModuleItem::UseItem(_)
565569
| ast::ModuleItem::ExternCrateItem(_)

crates/ra_hir_def/src/nameres/raw.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ impl RawItemsCollector {
266266
self.add_macro(current_module, it);
267267
return;
268268
}
269+
ast::ModuleItem::ExternBlock(_) => {
270+
// FIXME: add extern block
271+
return;
272+
}
269273
};
270274
if let Some(name) = name {
271275
let name = name.as_name();

crates/ra_hir_def/src/path/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
2828
loop {
2929
let segment = path.segment()?;
3030

31-
if segment.has_colon_colon() {
31+
if segment.coloncolon().is_some() {
3232
kind = PathKind::Abs;
3333
}
3434

crates/ra_hir_def/src/path/lower/lower_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) fn lower_use_tree(
3434
let alias = tree.alias().map(|a| {
3535
a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias)
3636
});
37-
let is_glob = tree.has_star();
37+
let is_glob = tree.star().is_some();
3838
if let Some(ast_path) = tree.path() {
3939
// Handle self in a path.
4040
// E.g. `use something::{self, <...>}`

crates/ra_hir_def/src/visibility.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ impl RawVisibility {
8484
let path = ModPath { kind: PathKind::Super(1), segments: Vec::new() };
8585
RawVisibility::Module(path)
8686
}
87+
ast::VisibilityKind::PubSelf => {
88+
let path = ModPath { kind: PathKind::Plain, segments: Vec::new() };
89+
RawVisibility::Module(path)
90+
}
8791
ast::VisibilityKind::Pub => RawVisibility::Public,
8892
}
8993
}

0 commit comments

Comments
 (0)