mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-26 03:45:04 +00:00
Remove deprecated function
This commit is contained in:
parent
26b98b07aa
commit
b0f03db51d
4 changed files with 48 additions and 54 deletions
|
@ -5,14 +5,13 @@ use std::mem;
|
||||||
use algo::find_covering_element;
|
use algo::find_covering_element;
|
||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use ra_db::{FileId, FileRange};
|
use ra_db::{FileId, FileRange};
|
||||||
use ra_fmt::{leading_indent, reindent};
|
|
||||||
use ra_ide_db::{
|
use ra_ide_db::{
|
||||||
source_change::{SourceChange, SourceFileEdit},
|
source_change::{SourceChange, SourceFileEdit},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::{self, find_node_at_offset, SyntaxRewriter},
|
algo::{self, find_node_at_offset, SyntaxRewriter},
|
||||||
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize,
|
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxToken, TextRange, TextSize,
|
||||||
TokenAtOffset,
|
TokenAtOffset,
|
||||||
};
|
};
|
||||||
use text_edit::{TextEdit, TextEditBuilder};
|
use text_edit::{TextEdit, TextEditBuilder};
|
||||||
|
@ -269,20 +268,6 @@ impl AssistBuilder {
|
||||||
pub(crate) fn replace_ast<N: AstNode>(&mut self, old: N, new: N) {
|
pub(crate) fn replace_ast<N: AstNode>(&mut self, old: N, new: N) {
|
||||||
algo::diff(old.syntax(), new.syntax()).into_text_edit(&mut self.edit)
|
algo::diff(old.syntax(), new.syntax()).into_text_edit(&mut self.edit)
|
||||||
}
|
}
|
||||||
/// Replaces specified `node` of text with a given string, reindenting the
|
|
||||||
/// string to maintain `node`'s existing indent.
|
|
||||||
// FIXME: remove in favor of syntax::edit::IndentLevel::increase_indent
|
|
||||||
pub(crate) fn replace_node_and_indent(
|
|
||||||
&mut self,
|
|
||||||
node: &SyntaxNode,
|
|
||||||
replace_with: impl Into<String>,
|
|
||||||
) {
|
|
||||||
let mut replace_with = replace_with.into();
|
|
||||||
if let Some(indent) = leading_indent(node) {
|
|
||||||
replace_with = reindent(&replace_with, &indent)
|
|
||||||
}
|
|
||||||
self.replace(node.text_range(), replace_with)
|
|
||||||
}
|
|
||||||
pub(crate) fn rewrite(&mut self, rewriter: SyntaxRewriter) {
|
pub(crate) fn rewrite(&mut self, rewriter: SyntaxRewriter) {
|
||||||
let node = rewriter.rewrite_root().unwrap();
|
let node = rewriter.rewrite_root().unwrap();
|
||||||
let new = rewriter.rewrite(&node);
|
let new = rewriter.rewrite(&node);
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
|
use std::iter;
|
||||||
|
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, BlockExpr, Expr, LoopBodyOwner},
|
ast::{self, make, BlockExpr, Expr, LoopBodyOwner},
|
||||||
AstNode, SyntaxNode,
|
AstNode, SyntaxNode,
|
||||||
};
|
};
|
||||||
|
use test_utils::mark;
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
use test_utils::mark;
|
|
||||||
|
|
||||||
// Assist: change_return_type_to_result
|
// Assist: change_return_type_to_result
|
||||||
//
|
//
|
||||||
|
@ -44,7 +46,13 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex
|
||||||
tail_return_expr_collector.collect_tail_exprs(block_expr);
|
tail_return_expr_collector.collect_tail_exprs(block_expr);
|
||||||
|
|
||||||
for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap {
|
for ret_expr_arg in tail_return_expr_collector.exprs_to_wrap {
|
||||||
builder.replace_node_and_indent(&ret_expr_arg, format!("Ok({})", ret_expr_arg));
|
let ok_wrapped = make::expr_call(
|
||||||
|
make::expr_path(make::path_unqualified(make::path_segment(make::name_ref(
|
||||||
|
"Ok",
|
||||||
|
)))),
|
||||||
|
make::arg_list(iter::once(ret_expr_arg.clone())),
|
||||||
|
);
|
||||||
|
builder.replace_ast(ret_expr_arg, ok_wrapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
match ctx.config.snippet_cap {
|
match ctx.config.snippet_cap {
|
||||||
|
@ -60,7 +68,7 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TailReturnCollector {
|
struct TailReturnCollector {
|
||||||
exprs_to_wrap: Vec<SyntaxNode>,
|
exprs_to_wrap: Vec<ast::Expr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TailReturnCollector {
|
impl TailReturnCollector {
|
||||||
|
@ -86,7 +94,8 @@ impl TailReturnCollector {
|
||||||
if let Some(last_exprs) = get_tail_expr_from_block(&expr) {
|
if let Some(last_exprs) = get_tail_expr_from_block(&expr) {
|
||||||
for last_expr in last_exprs {
|
for last_expr in last_exprs {
|
||||||
let last_expr = match last_expr {
|
let last_expr = match last_expr {
|
||||||
NodeType::Node(expr) | NodeType::Leaf(expr) => expr,
|
NodeType::Node(expr) => expr,
|
||||||
|
NodeType::Leaf(expr) => expr.syntax().clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(last_expr) = Expr::cast(last_expr.clone()) {
|
if let Some(last_expr) = Expr::cast(last_expr.clone()) {
|
||||||
|
@ -113,12 +122,12 @@ impl TailReturnCollector {
|
||||||
}
|
}
|
||||||
Expr::ReturnExpr(ret_expr) => {
|
Expr::ReturnExpr(ret_expr) => {
|
||||||
if let Some(ret_expr_arg) = &ret_expr.expr() {
|
if let Some(ret_expr_arg) = &ret_expr.expr() {
|
||||||
self.exprs_to_wrap.push(ret_expr_arg.syntax().clone());
|
self.exprs_to_wrap.push(ret_expr_arg.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expr::BreakExpr(break_expr) if collect_break => {
|
Expr::BreakExpr(break_expr) if collect_break => {
|
||||||
if let Some(break_expr_arg) = &break_expr.expr() {
|
if let Some(break_expr_arg) = &break_expr.expr() {
|
||||||
self.exprs_to_wrap.push(break_expr_arg.syntax().clone());
|
self.exprs_to_wrap.push(break_expr_arg.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expr::IfExpr(if_expr) => {
|
Expr::IfExpr(if_expr) => {
|
||||||
|
@ -166,14 +175,11 @@ impl TailReturnCollector {
|
||||||
NodeType::Leaf(expr) => {
|
NodeType::Leaf(expr) => {
|
||||||
self.exprs_to_wrap.push(expr.clone());
|
self.exprs_to_wrap.push(expr.clone());
|
||||||
}
|
}
|
||||||
NodeType::Node(expr) => match &Expr::cast(expr.clone()) {
|
NodeType::Node(expr) => {
|
||||||
Some(last_expr) => {
|
if let Some(last_expr) = Expr::cast(expr.clone()) {
|
||||||
self.fetch_tail_exprs(last_expr);
|
self.fetch_tail_exprs(&last_expr);
|
||||||
}
|
}
|
||||||
None => {
|
}
|
||||||
self.exprs_to_wrap.push(expr.clone());
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -182,7 +188,7 @@ impl TailReturnCollector {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum NodeType {
|
enum NodeType {
|
||||||
Leaf(SyntaxNode),
|
Leaf(ast::Expr),
|
||||||
Node(SyntaxNode),
|
Node(SyntaxNode),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,25 +239,26 @@ fn get_tail_expr_from_block(expr: &Expr) -> Option<Vec<NodeType>> {
|
||||||
|
|
||||||
Some(arms)
|
Some(arms)
|
||||||
}
|
}
|
||||||
Expr::BreakExpr(expr) => expr.expr().map(|e| vec![NodeType::Leaf(e.syntax().clone())]),
|
Expr::BreakExpr(expr) => expr.expr().map(|e| vec![NodeType::Leaf(e)]),
|
||||||
Expr::ReturnExpr(ret_expr) => Some(vec![NodeType::Node(ret_expr.syntax().clone())]),
|
Expr::ReturnExpr(ret_expr) => Some(vec![NodeType::Node(ret_expr.syntax().clone())]),
|
||||||
Expr::CallExpr(call_expr) => Some(vec![NodeType::Leaf(call_expr.syntax().clone())]),
|
|
||||||
Expr::Literal(lit_expr) => Some(vec![NodeType::Leaf(lit_expr.syntax().clone())]),
|
Expr::CallExpr(_)
|
||||||
Expr::TupleExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::Literal(_)
|
||||||
Expr::ArrayExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::TupleExpr(_)
|
||||||
Expr::ParenExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::ArrayExpr(_)
|
||||||
Expr::PathExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::ParenExpr(_)
|
||||||
Expr::RecordExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::PathExpr(_)
|
||||||
Expr::IndexExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::RecordExpr(_)
|
||||||
Expr::MethodCallExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::IndexExpr(_)
|
||||||
Expr::AwaitExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::MethodCallExpr(_)
|
||||||
Expr::CastExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::AwaitExpr(_)
|
||||||
Expr::RefExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::CastExpr(_)
|
||||||
Expr::PrefixExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::RefExpr(_)
|
||||||
Expr::RangeExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::PrefixExpr(_)
|
||||||
Expr::BinExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::RangeExpr(_)
|
||||||
Expr::MacroCall(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::BinExpr(_)
|
||||||
Expr::BoxExpr(expr) => Some(vec![NodeType::Leaf(expr.syntax().clone())]),
|
| Expr::MacroCall(_)
|
||||||
|
| Expr::BoxExpr(_) => Some(vec![NodeType::Leaf(expr.clone())]),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,6 @@ use syntax::{
|
||||||
SyntaxNode, SyntaxToken, T,
|
SyntaxNode, SyntaxToken, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn reindent(text: &str, indent: &str) -> String {
|
|
||||||
let indent = format!("\n{}", indent);
|
|
||||||
text.lines().intersperse(&indent).collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// If the node is on the beginning of the line, calculate indent.
|
/// If the node is on the beginning of the line, calculate indent.
|
||||||
pub fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> {
|
pub fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> {
|
||||||
for token in prev_tokens(node.first_token()?) {
|
for token in prev_tokens(node.first_token()?) {
|
||||||
|
|
|
@ -134,6 +134,9 @@ pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
|
||||||
let token = token(op);
|
let token = token(op);
|
||||||
expr_from_text(&format!("{}{}", token, expr))
|
expr_from_text(&format!("{}{}", token, expr))
|
||||||
}
|
}
|
||||||
|
pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr {
|
||||||
|
expr_from_text(&format!("{}{}", f, arg_list))
|
||||||
|
}
|
||||||
fn expr_from_text(text: &str) -> ast::Expr {
|
fn expr_from_text(text: &str) -> ast::Expr {
|
||||||
ast_from_text(&format!("const C: () = {};", text))
|
ast_from_text(&format!("const C: () = {};", text))
|
||||||
}
|
}
|
||||||
|
@ -151,6 +154,10 @@ pub fn condition(expr: ast::Expr, pattern: Option<ast::Pat>) -> ast::Condition {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn arg_list(args: impl IntoIterator<Item = ast::Expr>) -> ast::ArgList {
|
||||||
|
ast_from_text(&format!("fn main() {{ ()({}) }}", args.into_iter().format(", ")))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn ident_pat(name: ast::Name) -> ast::IdentPat {
|
pub fn ident_pat(name: ast::Name) -> ast::IdentPat {
|
||||||
return from_text(name.text());
|
return from_text(name.text());
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue