SSR: A few small refactorings

This commit is contained in:
David Lattimore 2020-08-18 20:39:55 +10:00
parent a4a504e132
commit 29e6238cb7
4 changed files with 10 additions and 6 deletions

1
Cargo.lock generated
View file

@ -1450,6 +1450,7 @@ dependencies = [
"expect", "expect",
"hir", "hir",
"ide_db", "ide_db",
"itertools",
"rustc-hash", "rustc-hash",
"syntax", "syntax",
"test_utils", "test_utils",

View file

@ -12,6 +12,7 @@ doctest = false
[dependencies] [dependencies]
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
itertools = "0.9.0"
text_edit = { path = "../text_edit" } text_edit = { path = "../text_edit" }
syntax = { path = "../syntax" } syntax = { path = "../syntax" }

View file

@ -1,9 +1,11 @@
//! Code for applying replacement templates for matches that have previously been found. //! Code for applying replacement templates for matches that have previously been found.
use crate::{resolving::ResolvedRule, Match, SsrMatches}; use crate::{resolving::ResolvedRule, Match, SsrMatches};
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use syntax::ast::{self, AstToken}; use syntax::ast::{self, AstToken};
use syntax::{SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize}; use syntax::{SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize};
use test_utils::mark;
use text_edit::TextEdit; use text_edit::TextEdit;
/// Returns a text edit that will replace each match in `matches` with its corresponding replacement /// Returns a text edit that will replace each match in `matches` with its corresponding replacement
@ -127,6 +129,7 @@ impl ReplacementRenderer<'_> {
&& (placeholder_value.autoderef_count > 0 && (placeholder_value.autoderef_count > 0
|| placeholder_value.autoref_kind != ast::SelfParamKind::Owned) || placeholder_value.autoref_kind != ast::SelfParamKind::Owned)
{ {
mark::hit!(replace_autoref_autoderef_capture);
let ref_kind = match placeholder_value.autoref_kind { let ref_kind = match placeholder_value.autoref_kind {
ast::SelfParamKind::Owned => "", ast::SelfParamKind::Owned => "",
ast::SelfParamKind::Ref => "&", ast::SelfParamKind::Ref => "&",
@ -206,18 +209,16 @@ fn token_is_method_call_receiver(token: &SyntaxToken) -> bool {
use syntax::ast::AstNode; use syntax::ast::AstNode;
// Find the first method call among the ancestors of `token`, then check if the only token // Find the first method call among the ancestors of `token`, then check if the only token
// within the receiver is `token`. // within the receiver is `token`.
if let Some(receiver) = token if let Some(receiver) =
.ancestors() token.ancestors().find_map(ast::MethodCallExpr::cast).and_then(|call| call.expr())
.find(|node| node.kind() == SyntaxKind::METHOD_CALL_EXPR)
.and_then(|node| ast::MethodCallExpr::cast(node).unwrap().expr())
{ {
let mut tokens = receiver.syntax().descendants_with_tokens().filter_map(|node_or_token| { let tokens = receiver.syntax().descendants_with_tokens().filter_map(|node_or_token| {
match node_or_token { match node_or_token {
SyntaxElement::Token(t) => Some(t), SyntaxElement::Token(t) => Some(t),
_ => None, _ => None,
} }
}); });
if let (Some(only_token), None) = (tokens.next(), tokens.next()) { if let Some((only_token,)) = tokens.collect_tuple() {
return only_token == *token; return only_token == *token;
} }
} }

View file

@ -1179,6 +1179,7 @@ fn replace_autoref_autoderef_capture() {
// second, we already have a reference, so it isn't. When $a is used in a context where autoref // second, we already have a reference, so it isn't. When $a is used in a context where autoref
// doesn't apply, we need to prefix it with `&`. Finally, we have some cases where autoderef // doesn't apply, we need to prefix it with `&`. Finally, we have some cases where autoderef
// needs to be applied. // needs to be applied.
mark::check!(replace_autoref_autoderef_capture);
let code = r#" let code = r#"
struct Foo {} struct Foo {}
impl Foo { impl Foo {