Skip to content

Commit 7f12a1f

Browse files
bors[bot]matklad
andauthored
Merge #6485
6485: Remove RAW literals r=matklad a=matklad bors r+ 🤖 closes #6308 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents cdddcae + 6158304 commit 7f12a1f

File tree

54 files changed

+157
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+157
-200
lines changed

crates/assists/src/assist_context.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ide_db::{
1212
};
1313
use syntax::{
1414
algo::{self, find_node_at_offset, SyntaxRewriter},
15-
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxToken, TextRange, TextSize,
15+
AstNode, AstToken, SourceFile, SyntaxElement, SyntaxKind, SyntaxToken, TextRange, TextSize,
1616
TokenAtOffset,
1717
};
1818
use text_edit::{TextEdit, TextEditBuilder};
@@ -81,9 +81,12 @@ impl<'a> AssistContext<'a> {
8181
pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
8282
self.source_file.syntax().token_at_offset(self.offset())
8383
}
84-
pub(crate) fn find_token_at_offset(&self, kind: SyntaxKind) -> Option<SyntaxToken> {
84+
pub(crate) fn find_token_syntax_at_offset(&self, kind: SyntaxKind) -> Option<SyntaxToken> {
8585
self.token_at_offset().find(|it| it.kind() == kind)
8686
}
87+
pub(crate) fn find_token_at_offset<T: AstToken>(&self) -> Option<T> {
88+
self.token_at_offset().find_map(T::cast)
89+
}
8790
pub(crate) fn find_node_at_offset<N: AstNode>(&self) -> Option<N> {
8891
find_node_at_offset(self.source_file.syntax(), self.offset())
8992
}

crates/assists/src/handlers/add_turbo_fish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
// }
2626
// ```
2727
pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
28-
let ident = ctx.find_token_at_offset(SyntaxKind::IDENT).or_else(|| {
28+
let ident = ctx.find_token_syntax_at_offset(SyntaxKind::IDENT).or_else(|| {
2929
let arg_list = ctx.find_node_at_offset::<ast::ArgList>()?;
3030
if arg_list.args().count() > 0 {
3131
return None;

crates/assists/src/handlers/expand_glob_import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
// fn qux(bar: Bar, baz: Baz) {}
4242
// ```
4343
pub(crate) fn expand_glob_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
44-
let star = ctx.find_token_at_offset(T![*])?;
44+
let star = ctx.find_token_syntax_at_offset(T![*])?;
4545
let (parent, mod_path) = find_parent_and_path(&star)?;
4646
let target_module = match ctx.sema.resolve_path(&mod_path)? {
4747
PathResolution::Def(ModuleDef::Module(it)) => it,

crates/assists/src/handlers/flip_comma.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
1818
// }
1919
// ```
2020
pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
21-
let comma = ctx.find_token_at_offset(T![,])?;
21+
let comma = ctx.find_token_syntax_at_offset(T![,])?;
2222
let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;
2323
let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?;
2424

crates/assists/src/handlers/flip_trait_bound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2020
pub(crate) fn flip_trait_bound(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
2121
// We want to replicate the behavior of `flip_binexpr` by only suggesting
2222
// the assist when the cursor is on a `+`
23-
let plus = ctx.find_token_at_offset(T![+])?;
23+
let plus = ctx.find_token_syntax_at_offset(T![+])?;
2424

2525
// Make sure we're in a `TypeBoundList`
2626
if ast::TypeBoundList::cast(plus.parent()).is_none() {

crates/assists/src/handlers/introduce_named_lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static ASSIST_LABEL: &str = "Introduce named lifetime";
3636
// FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo
3737
pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
3838
let lifetime_token = ctx
39-
.find_token_at_offset(SyntaxKind::LIFETIME)
39+
.find_token_syntax_at_offset(SyntaxKind::LIFETIME)
4040
.filter(|lifetime| lifetime.text() == "'_")?;
4141
if let Some(fn_def) = lifetime_token.ancestors().find_map(ast::Fn::cast) {
4242
generate_fn_def_assist(acc, &fn_def, lifetime_token.text_range())

crates/assists/src/handlers/invert_if.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
// ```
3030

3131
pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
32-
let if_keyword = ctx.find_token_at_offset(T![if])?;
32+
let if_keyword = ctx.find_token_syntax_at_offset(T![if])?;
3333
let expr = ast::IfExpr::cast(if_keyword.parent())?;
3434
let if_range = if_keyword.text_range();
3535
let cursor_in_range = if_range.contains_range(ctx.frange.range);

crates/assists/src/handlers/raw_string.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
use std::borrow::Cow;
22

3-
use syntax::{
4-
ast::{self, HasQuotes, HasStringValue},
5-
AstToken,
6-
SyntaxKind::{RAW_STRING, STRING},
7-
TextRange, TextSize,
8-
};
3+
use syntax::{ast, AstToken, TextRange, TextSize};
94
use test_utils::mark;
105

116
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -26,7 +21,10 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2621
// }
2722
// ```
2823
pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
29-
let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?;
24+
let token = ctx.find_token_at_offset::<ast::String>()?;
25+
if token.is_raw() {
26+
return None;
27+
}
3028
let value = token.value()?;
3129
let target = token.syntax().text_range();
3230
acc.add(
@@ -65,7 +63,10 @@ pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<
6563
// }
6664
// ```
6765
pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
68-
let token = ctx.find_token_at_offset(RAW_STRING).and_then(ast::RawString::cast)?;
66+
let token = ctx.find_token_at_offset::<ast::String>()?;
67+
if !token.is_raw() {
68+
return None;
69+
}
6970
let value = token.value()?;
7071
let target = token.syntax().text_range();
7172
acc.add(
@@ -104,11 +105,15 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Optio
104105
// }
105106
// ```
106107
pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
107-
let token = ctx.find_token_at_offset(RAW_STRING)?;
108-
let target = token.text_range();
108+
let token = ctx.find_token_at_offset::<ast::String>()?;
109+
if !token.is_raw() {
110+
return None;
111+
}
112+
let text_range = token.syntax().text_range();
113+
let target = text_range;
109114
acc.add(AssistId("add_hash", AssistKind::Refactor), "Add #", target, |edit| {
110-
edit.insert(token.text_range().start() + TextSize::of('r'), "#");
111-
edit.insert(token.text_range().end(), "#");
115+
edit.insert(text_range.start() + TextSize::of('r'), "#");
116+
edit.insert(text_range.end(), "#");
112117
})
113118
}
114119

@@ -128,7 +133,10 @@ pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
128133
// }
129134
// ```
130135
pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
131-
let token = ctx.find_token_at_offset(RAW_STRING).and_then(ast::RawString::cast)?;
136+
let token = ctx.find_token_at_offset::<ast::String>()?;
137+
if !token.is_raw() {
138+
return None;
139+
}
132140

133141
let text = token.text().as_str();
134142
if !text.starts_with("r#") && text.ends_with('#') {

crates/assists/src/handlers/remove_mut.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
1818
// }
1919
// ```
2020
pub(crate) fn remove_mut(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
21-
let mut_token = ctx.find_token_at_offset(T![mut])?;
21+
let mut_token = ctx.find_token_syntax_at_offset(T![mut])?;
2222
let delete_from = mut_token.text_range().start();
2323
let delete_to = match mut_token.next_token() {
2424
Some(it) if it.kind() == SyntaxKind::WHITESPACE => it.text_range().end(),

crates/assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use ide_db::ty_filter::TryEnum;
3737
// fn compute() -> Option<i32> { None }
3838
// ```
3939
pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
40-
let let_kw = ctx.find_token_at_offset(T![let])?;
40+
let let_kw = ctx.find_token_syntax_at_offset(T![let])?;
4141
let let_stmt = let_kw.ancestors().find_map(ast::LetStmt::cast)?;
4242
let init = let_stmt.initializer()?;
4343
let original_pat = let_stmt.pat()?;

0 commit comments

Comments
 (0)