Skip to content

Commit 5ba4f94

Browse files
committed
Kill RAW_ literals
Syntactically, they are indistinguishable from non-raw versions, so it doesn't make sense to separate then *at the syntax* level.
1 parent 6725dcf commit 5ba4f94

File tree

53 files changed

+141
-164
lines changed

Some content is hidden

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

53 files changed

+141
-164
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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use std::borrow::Cow;
22

33
use syntax::{
44
ast::{self, HasQuotes, HasStringValue},
5-
AstToken,
6-
SyntaxKind::{RAW_STRING, STRING},
7-
TextRange, TextSize,
5+
AstToken, TextRange, TextSize,
86
};
97
use test_utils::mark;
108

@@ -26,7 +24,10 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2624
// }
2725
// ```
2826
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)?;
27+
let token = ctx.find_token_at_offset::<ast::String>()?;
28+
if token.is_raw() {
29+
return None;
30+
}
3031
let value = token.value()?;
3132
let target = token.syntax().text_range();
3233
acc.add(
@@ -65,7 +66,10 @@ pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<
6566
// }
6667
// ```
6768
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)?;
69+
let token = ctx.find_token_at_offset::<ast::String>()?;
70+
if !token.is_raw() {
71+
return None;
72+
}
6973
let value = token.value()?;
7074
let target = token.syntax().text_range();
7175
acc.add(
@@ -104,11 +108,15 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Optio
104108
// }
105109
// ```
106110
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();
111+
let token = ctx.find_token_at_offset::<ast::String>()?;
112+
if !token.is_raw() {
113+
return None;
114+
}
115+
let text_range = token.syntax().text_range();
116+
let target = text_range;
109117
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(), "#");
118+
edit.insert(text_range.start() + TextSize::of('r'), "#");
119+
edit.insert(text_range.end(), "#");
112120
})
113121
}
114122

@@ -128,7 +136,10 @@ pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
128136
// }
129137
// ```
130138
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)?;
139+
let token = ctx.find_token_at_offset::<ast::String>()?;
140+
if !token.is_raw() {
141+
return None;
142+
}
132143

133144
let text = token.text().as_str();
134145
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)