diff options
Diffstat (limited to 'crates/assists/src/handlers/raw_string.rs')
-rw-r--r-- | crates/assists/src/handlers/raw_string.rs | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/crates/assists/src/handlers/raw_string.rs b/crates/assists/src/handlers/raw_string.rs index 9ddd116e0..4c759cc25 100644 --- a/crates/assists/src/handlers/raw_string.rs +++ b/crates/assists/src/handlers/raw_string.rs | |||
@@ -1,11 +1,6 @@ | |||
1 | use std::borrow::Cow; | 1 | use std::borrow::Cow; |
2 | 2 | ||
3 | use syntax::{ | 3 | use syntax::{ast, AstToken, TextRange, TextSize}; |
4 | ast::{self, HasQuotes, HasStringValue}, | ||
5 | AstToken, | ||
6 | SyntaxKind::{RAW_STRING, STRING}, | ||
7 | TextRange, TextSize, | ||
8 | }; | ||
9 | use test_utils::mark; | 4 | use test_utils::mark; |
10 | 5 | ||
11 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 6 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
@@ -26,7 +21,10 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; | |||
26 | // } | 21 | // } |
27 | // ``` | 22 | // ``` |
28 | pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 23 | 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 | } | ||
30 | let value = token.value()?; | 28 | let value = token.value()?; |
31 | let target = token.syntax().text_range(); | 29 | let target = token.syntax().text_range(); |
32 | acc.add( | 30 | acc.add( |
@@ -65,7 +63,10 @@ pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option< | |||
65 | // } | 63 | // } |
66 | // ``` | 64 | // ``` |
67 | pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 65 | 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 | } | ||
69 | let value = token.value()?; | 70 | let value = token.value()?; |
70 | let target = token.syntax().text_range(); | 71 | let target = token.syntax().text_range(); |
71 | acc.add( | 72 | acc.add( |
@@ -104,11 +105,15 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Optio | |||
104 | // } | 105 | // } |
105 | // ``` | 106 | // ``` |
106 | pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 107 | pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
107 | let token = ctx.find_token_at_offset(RAW_STRING)?; | 108 | let token = ctx.find_token_at_offset::<ast::String>()?; |
108 | let target = token.text_range(); | 109 | if !token.is_raw() { |
110 | return None; | ||
111 | } | ||
112 | let text_range = token.syntax().text_range(); | ||
113 | let target = text_range; | ||
109 | acc.add(AssistId("add_hash", AssistKind::Refactor), "Add #", target, |edit| { | 114 | acc.add(AssistId("add_hash", AssistKind::Refactor), "Add #", target, |edit| { |
110 | edit.insert(token.text_range().start() + TextSize::of('r'), "#"); | 115 | edit.insert(text_range.start() + TextSize::of('r'), "#"); |
111 | edit.insert(token.text_range().end(), "#"); | 116 | edit.insert(text_range.end(), "#"); |
112 | }) | 117 | }) |
113 | } | 118 | } |
114 | 119 | ||
@@ -128,7 +133,10 @@ pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | |||
128 | // } | 133 | // } |
129 | // ``` | 134 | // ``` |
130 | pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 135 | 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 | } | ||
132 | 140 | ||
133 | let text = token.text().as_str(); | 141 | let text = token.text().as_str(); |
134 | if !text.starts_with("r#") && text.ends_with('#') { | 142 | if !text.starts_with("r#") && text.ends_with('#') { |