From 9e213385c9d06db3c8ca20812779e2b8f8ad2c71 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 30 Mar 2019 13:25:53 +0300 Subject: switch to new rowan --- crates/ra_assists/src/add_derive.rs | 6 +++-- crates/ra_assists/src/add_impl.rs | 8 ++++--- crates/ra_assists/src/add_missing_impl_members.rs | 13 +++++++---- crates/ra_assists/src/assist_ctx.rs | 12 +++++----- crates/ra_assists/src/auto_import.rs | 13 +++++------ crates/ra_assists/src/change_visibility.rs | 10 ++++---- crates/ra_assists/src/flip_binexpr.rs | 2 +- crates/ra_assists/src/flip_comma.rs | 10 ++++---- crates/ra_assists/src/inline_local_variable.rs | 6 +++-- crates/ra_assists/src/introduce_variable.rs | 28 +++++++++++------------ crates/ra_assists/src/remove_dbg.rs | 6 ++--- crates/ra_assists/src/split_import.rs | 4 ++-- 12 files changed, 62 insertions(+), 56 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/add_derive.rs b/crates/ra_assists/src/add_derive.rs index e91b5eb8d..0c4cf2615 100644 --- a/crates/ra_assists/src/add_derive.rs +++ b/crates/ra_assists/src/add_derive.rs @@ -33,8 +33,10 @@ pub(crate) fn add_derive(mut ctx: AssistCtx) -> Option // Insert `derive` after doc comments. fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option { - let non_ws_child = - nominal.syntax().children().find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?; + let non_ws_child = nominal + .syntax() + .children_with_tokens() + .find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?; Some(non_ws_child.range().start()) } diff --git a/crates/ra_assists/src/add_impl.rs b/crates/ra_assists/src/add_impl.rs index b292f188d..fa1c85890 100644 --- a/crates/ra_assists/src/add_impl.rs +++ b/crates/ra_assists/src/add_impl.rs @@ -1,7 +1,7 @@ use join_to_string::join; use hir::db::HirDatabase; use ra_syntax::{ - ast::{self, AstNode, AstToken, NameOwner, TypeParamsOwner}, + ast::{self, AstNode, NameOwner, TypeParamsOwner}, TextUnit, }; @@ -22,8 +22,10 @@ pub(crate) fn add_impl(mut ctx: AssistCtx) -> Option { buf.push_str(" "); buf.push_str(name.text().as_str()); if let Some(type_params) = type_params { - let lifetime_params = - type_params.lifetime_params().filter_map(|it| it.lifetime()).map(|it| it.text()); + let lifetime_params = type_params + .lifetime_params() + .filter_map(|it| it.lifetime_token()) + .map(|it| it.text()); let type_params = type_params.type_params().filter_map(|it| it.name()).map(|it| it.text()); join(lifetime_params.chain(type_params)).surround_with("<", ">").to_buf(&mut buf); diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index e13f54c4f..5b01e898e 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs @@ -1,3 +1,5 @@ +use std::fmt::Write; + use crate::{Assist, AssistId, AssistCtx}; use hir::Resolver; @@ -91,8 +93,9 @@ fn add_missing_impl_members_inner( }; let changed_range = { - let children = impl_item_list.syntax().children(); - let last_whitespace = children.filter_map(ast::Whitespace::cast).last(); + let children = impl_item_list.syntax().children_with_tokens(); + let last_whitespace = + children.filter_map(|it| ast::Whitespace::cast(it.as_token()?)).last(); last_whitespace.map(|w| w.syntax().range()).unwrap_or_else(|| { let in_brackets = impl_item_list.syntax().range().end() - TextUnit::of_str("}"); @@ -134,13 +137,13 @@ fn resolve_target_trait_def( fn build_func_body(def: &ast::FnDef) -> String { let mut buf = String::new(); - for child in def.syntax().children() { - match (child.prev_sibling().map(|c| c.kind()), child.kind()) { + for child in def.syntax().children_with_tokens() { + match (child.prev_sibling_or_token().map(|c| c.kind()), child.kind()) { (_, SyntaxKind::SEMI) => buf.push_str(" { unimplemented!() }"), (_, SyntaxKind::ATTR) | (_, SyntaxKind::COMMENT) => {} (Some(SyntaxKind::ATTR), SyntaxKind::WHITESPACE) | (Some(SyntaxKind::COMMENT), SyntaxKind::WHITESPACE) => {} - _ => child.text().push_to(&mut buf), + _ => write!(buf, "{}", child).unwrap(), }; } diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 4ad21c74b..bb5742bd9 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -2,8 +2,8 @@ use hir::db::HirDatabase; use ra_text_edit::TextEditBuilder; use ra_db::FileRange; use ra_syntax::{ - SourceFile, TextRange, AstNode, TextUnit, SyntaxNode, - algo::{find_leaf_at_offset, find_node_at_offset, find_covering_node, LeafAtOffset}, + SourceFile, TextRange, AstNode, TextUnit, SyntaxNode, SyntaxElement, SyntaxToken, + algo::{find_token_at_offset, find_node_at_offset, find_covering_element, TokenAtOffset}, }; use ra_fmt::{leading_indent, reindent}; @@ -104,15 +104,15 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { Some(self.assist) } - pub(crate) fn leaf_at_offset(&self) -> LeafAtOffset<&'a SyntaxNode> { - find_leaf_at_offset(self.source_file.syntax(), self.frange.range.start()) + pub(crate) fn token_at_offset(&self) -> TokenAtOffset> { + find_token_at_offset(self.source_file.syntax(), self.frange.range.start()) } pub(crate) fn node_at_offset(&self) -> Option<&'a N> { find_node_at_offset(self.source_file.syntax(), self.frange.range.start()) } - pub(crate) fn covering_node(&self) -> &'a SyntaxNode { - find_covering_node(self.source_file.syntax(), self.frange.range) + pub(crate) fn covering_element(&self) -> SyntaxElement<'a> { + find_covering_element(self.source_file.syntax(), self.frange.range) } } diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 685dbed06..3fdf6b0d9 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -21,7 +21,7 @@ fn collect_path_segments_raw<'a>( ) -> Option { let oldlen = segments.len(); loop { - let mut children = path.syntax().children(); + let mut children = path.syntax().children_with_tokens(); let (first, second, third) = ( children.next().map(|n| (n, n.kind())), children.next().map(|n| (n, n.kind())), @@ -29,11 +29,11 @@ fn collect_path_segments_raw<'a>( ); match (first, second, third) { (Some((subpath, PATH)), Some((_, COLONCOLON)), Some((segment, PATH_SEGMENT))) => { - path = ast::Path::cast(subpath)?; - segments.push(ast::PathSegment::cast(segment)?); + path = ast::Path::cast(subpath.as_node()?)?; + segments.push(ast::PathSegment::cast(segment.as_node()?)?); } (Some((segment, PATH_SEGMENT)), _, _) => { - segments.push(ast::PathSegment::cast(segment)?); + segments.push(ast::PathSegment::cast(segment.as_node()?)?); break; } (_, _, _) => return None, @@ -514,8 +514,7 @@ fn apply_auto_import<'a>( } pub(crate) fn auto_import(mut ctx: AssistCtx) -> Option { - let node = ctx.covering_node(); - let path = node.ancestors().find_map(ast::Path::cast)?; + let path: &ast::Path = ctx.node_at_offset()?; // We don't want to mess with use statements if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { return None; @@ -537,7 +536,7 @@ pub(crate) fn auto_import(mut ctx: AssistCtx) -> Option) -> Option) -> Option { - let item_keyword = ctx.leaf_at_offset().find(|leaf| match leaf.kind() { + let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { FN_KW | MOD_KW | STRUCT_KW | ENUM_KW | TRAIT_KW => true, _ => false, }); let (offset, target) = if let Some(keyword) = item_keyword { - let parent = keyword.parent()?; + let parent = keyword.parent(); let def_kws = vec![FN_DEF, MODULE, STRUCT_DEF, ENUM_DEF, TRAIT_DEF]; // Parent is not a definition, can't add visibility if !def_kws.iter().any(|&def_kw| def_kw == parent.kind()) { @@ -33,8 +33,8 @@ fn add_vis(mut ctx: AssistCtx) -> Option { } (vis_offset(parent), keyword.range()) } else { - let ident = ctx.leaf_at_offset().find(|leaf| leaf.kind() == IDENT)?; - let field = ident.ancestors().find_map(ast::NamedFieldDef::cast)?; + let ident = ctx.token_at_offset().find(|leaf| leaf.kind() == IDENT)?; + let field = ident.parent().ancestors().find_map(ast::NamedFieldDef::cast)?; if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() { return None; } @@ -51,7 +51,7 @@ fn add_vis(mut ctx: AssistCtx) -> Option { } fn vis_offset(node: &SyntaxNode) -> TextUnit { - node.children() + node.children_with_tokens() .skip_while(|it| match it.kind() { WHITESPACE | COMMENT | ATTR => true, _ => false, diff --git a/crates/ra_assists/src/flip_binexpr.rs b/crates/ra_assists/src/flip_binexpr.rs index ec377642e..02d27f66d 100644 --- a/crates/ra_assists/src/flip_binexpr.rs +++ b/crates/ra_assists/src/flip_binexpr.rs @@ -8,7 +8,7 @@ pub(crate) fn flip_binexpr(mut ctx: AssistCtx) -> Option()?; let lhs = expr.lhs()?.syntax(); let rhs = expr.rhs()?.syntax(); - let op_range = expr.op()?.range(); + let op_range = expr.op_token()?.range(); // The assist should be applied only if the cursor is on the operator let cursor_in_range = ctx.frange.range.is_subrange(&op_range); if !cursor_in_range { diff --git a/crates/ra_assists/src/flip_comma.rs b/crates/ra_assists/src/flip_comma.rs index 6b98cac68..a9b108111 100644 --- a/crates/ra_assists/src/flip_comma.rs +++ b/crates/ra_assists/src/flip_comma.rs @@ -8,13 +8,13 @@ use ra_syntax::{ use crate::{AssistCtx, Assist, AssistId}; pub(crate) fn flip_comma(mut ctx: AssistCtx) -> Option { - let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?; - let prev = non_trivia_sibling(comma, Direction::Prev)?; - let next = non_trivia_sibling(comma, Direction::Next)?; + let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == COMMA)?; + let prev = non_trivia_sibling(comma.into(), Direction::Prev)?; + let next = non_trivia_sibling(comma.into(), Direction::Next)?; ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { edit.target(comma.range()); - edit.replace(prev.range(), next.text()); - edit.replace(next.range(), prev.text()); + edit.replace(prev.range(), next.to_string()); + edit.replace(next.range(), prev.to_string()); }); ctx.build() diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs index bd3cdb970..2258ca139 100644 --- a/crates/ra_assists/src/inline_local_variable.rs +++ b/crates/ra_assists/src/inline_local_variable.rs @@ -46,8 +46,10 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Opt | ExprKind::BlockExpr(_) => false, }; - let delete_range = if let Some(whitespace) = - let_stmt.syntax().next_sibling().and_then(ast::Whitespace::cast) + let delete_range = if let Some(whitespace) = let_stmt + .syntax() + .next_sibling_or_token() + .and_then(|it| ast::Whitespace::cast(it.as_token()?)) { TextRange::from_to(let_stmt.syntax().range().start(), whitespace.syntax().range().end()) } else { diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index 353bc4105..fb7333c8c 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs @@ -2,9 +2,8 @@ use test_utils::tested_by; use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode}, - SyntaxKind::{ - WHITESPACE, MATCH_ARM, LAMBDA_EXPR, PATH_EXPR, BREAK_EXPR, LOOP_EXPR, RETURN_EXPR, COMMENT - }, SyntaxNode, TextUnit, + SyntaxNode, TextUnit, + SyntaxKind::{WHITESPACE, MATCH_ARM, LAMBDA_EXPR, PATH_EXPR, BREAK_EXPR, LOOP_EXPR, RETURN_EXPR, COMMENT}, }; use crate::{AssistCtx, Assist, AssistId}; @@ -13,14 +12,14 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option if ctx.frange.range.is_empty() { return None; } - let node = ctx.covering_node(); + let node = ctx.covering_element(); if node.kind() == COMMENT { tested_by!(introduce_var_in_comment_is_not_applicable); return None; } let expr = node.ancestors().find_map(valid_target_expr)?; let (anchor_stmt, wrap_in_block) = anchor_stmt(expr)?; - let indent = anchor_stmt.prev_sibling()?; + let indent = anchor_stmt.prev_sibling_or_token()?.as_token()?; if indent.kind() != WHITESPACE { return None; } @@ -54,16 +53,15 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option // We want to maintain the indent level, // but we do not want to duplicate possible // extra newlines in the indent block - for chunk in indent.text().chunks() { - if chunk.starts_with("\r\n") { - buf.push_str("\r\n"); - buf.push_str(chunk.trim_start_matches("\r\n")); - } else if chunk.starts_with("\n") { - buf.push_str("\n"); - buf.push_str(chunk.trim_start_matches("\n")); - } else { - buf.push_str(chunk); - } + let text = indent.text(); + if text.starts_with("\r\n") { + buf.push_str("\r\n"); + buf.push_str(text.trim_start_matches("\r\n")); + } else if text.starts_with("\n") { + buf.push_str("\n"); + buf.push_str(text.trim_start_matches("\n")); + } else { + buf.push_str(text); } edit.target(expr.syntax().range()); diff --git a/crates/ra_assists/src/remove_dbg.rs b/crates/ra_assists/src/remove_dbg.rs index 6ea48d909..ae9958f11 100644 --- a/crates/ra_assists/src/remove_dbg.rs +++ b/crates/ra_assists/src/remove_dbg.rs @@ -62,15 +62,15 @@ fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option Some(true), diff --git a/crates/ra_assists/src/split_import.rs b/crates/ra_assists/src/split_import.rs index dd5be4e91..4bf1852db 100644 --- a/crates/ra_assists/src/split_import.rs +++ b/crates/ra_assists/src/split_import.rs @@ -8,8 +8,8 @@ use ra_syntax::{ use crate::{AssistCtx, Assist, AssistId}; pub(crate) fn split_import(mut ctx: AssistCtx) -> Option { - let colon_colon = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?; - let path = colon_colon.parent().and_then(ast::Path::cast)?; + let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == COLONCOLON)?; + let path = ast::Path::cast(colon_colon.parent())?; let top_path = generate(Some(path), |it| it.parent_path()).last()?; let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast); -- cgit v1.2.3