aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/raw_string.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/raw_string.rs')
-rw-r--r--crates/ra_assists/src/handlers/raw_string.rs72
1 files changed, 45 insertions, 27 deletions
diff --git a/crates/ra_assists/src/handlers/raw_string.rs b/crates/ra_assists/src/handlers/raw_string.rs
index d22d0aa55..96679e160 100644
--- a/crates/ra_assists/src/handlers/raw_string.rs
+++ b/crates/ra_assists/src/handlers/raw_string.rs
@@ -5,7 +5,7 @@ use ra_syntax::{
5 TextSize, 5 TextSize,
6}; 6};
7 7
8use crate::{AssistContext, AssistId, Assists}; 8use crate::{AssistContext, AssistId, AssistKind, Assists};
9 9
10// Assist: make_raw_string 10// Assist: make_raw_string
11// 11//
@@ -26,14 +26,22 @@ pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<
26 let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?; 26 let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?;
27 let value = token.value()?; 27 let value = token.value()?;
28 let target = token.syntax().text_range(); 28 let target = token.syntax().text_range();
29 acc.add(AssistId("make_raw_string"), "Rewrite as raw string", target, |edit| { 29 acc.add(
30 let max_hash_streak = count_hashes(&value); 30 AssistId("make_raw_string", AssistKind::RefactorRewrite),
31 let mut hashes = String::with_capacity(max_hash_streak + 1); 31 "Rewrite as raw string",
32 for _ in 0..hashes.capacity() { 32 target,
33 hashes.push('#'); 33 |edit| {
34 } 34 let max_hash_streak = count_hashes(&value);
35 edit.replace(token.syntax().text_range(), format!("r{}\"{}\"{}", hashes, value, hashes)); 35 let mut hashes = String::with_capacity(max_hash_streak + 1);
36 }) 36 for _ in 0..hashes.capacity() {
37 hashes.push('#');
38 }
39 edit.replace(
40 token.syntax().text_range(),
41 format!("r{}\"{}\"{}", hashes, value, hashes),
42 );
43 },
44 )
37} 45}
38 46
39// Assist: make_usual_string 47// Assist: make_usual_string
@@ -55,11 +63,16 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Optio
55 let token = ctx.find_token_at_offset(RAW_STRING).and_then(ast::RawString::cast)?; 63 let token = ctx.find_token_at_offset(RAW_STRING).and_then(ast::RawString::cast)?;
56 let value = token.value()?; 64 let value = token.value()?;
57 let target = token.syntax().text_range(); 65 let target = token.syntax().text_range();
58 acc.add(AssistId("make_usual_string"), "Rewrite as regular string", target, |edit| { 66 acc.add(
59 // parse inside string to escape `"` 67 AssistId("make_usual_string", AssistKind::RefactorRewrite),
60 let escaped = value.escape_default().to_string(); 68 "Rewrite as regular string",
61 edit.replace(token.syntax().text_range(), format!("\"{}\"", escaped)); 69 target,
62 }) 70 |edit| {
71 // parse inside string to escape `"`
72 let escaped = value.escape_default().to_string();
73 edit.replace(token.syntax().text_range(), format!("\"{}\"", escaped));
74 },
75 )
63} 76}
64 77
65// Assist: add_hash 78// Assist: add_hash
@@ -80,7 +93,7 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Optio
80pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 93pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
81 let token = ctx.find_token_at_offset(RAW_STRING)?; 94 let token = ctx.find_token_at_offset(RAW_STRING)?;
82 let target = token.text_range(); 95 let target = token.text_range();
83 acc.add(AssistId("add_hash"), "Add # to raw string", target, |edit| { 96 acc.add(AssistId("add_hash", AssistKind::Refactor), "Add # to raw string", target, |edit| {
84 edit.insert(token.text_range().start() + TextSize::of('r'), "#"); 97 edit.insert(token.text_range().start() + TextSize::of('r'), "#");
85 edit.insert(token.text_range().end(), "#"); 98 edit.insert(token.text_range().end(), "#");
86 }) 99 })
@@ -109,18 +122,23 @@ pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
109 return None; 122 return None;
110 } 123 }
111 let target = token.text_range(); 124 let target = token.text_range();
112 acc.add(AssistId("remove_hash"), "Remove hash from raw string", target, |edit| { 125 acc.add(
113 let result = &text[2..text.len() - 1]; 126 AssistId("remove_hash", AssistKind::RefactorRewrite),
114 let result = if result.starts_with('\"') { 127 "Remove hash from raw string",
115 // FIXME: this logic is wrong, not only the last has has to handled specially 128 target,
116 // no more hash, escape 129 |edit| {
117 let internal_str = &result[1..result.len() - 1]; 130 let result = &text[2..text.len() - 1];
118 format!("\"{}\"", internal_str.escape_default().to_string()) 131 let result = if result.starts_with('\"') {
119 } else { 132 // FIXME: this logic is wrong, not only the last has has to handled specially
120 result.to_owned() 133 // no more hash, escape
121 }; 134 let internal_str = &result[1..result.len() - 1];
122 edit.replace(token.text_range(), format!("r{}", result)); 135 format!("\"{}\"", internal_str.escape_default().to_string())
123 }) 136 } else {
137 result.to_owned()
138 };
139 edit.replace(token.text_range(), format!("r{}", result));
140 },
141 )
124} 142}
125 143
126fn count_hashes(s: &str) -> usize { 144fn count_hashes(s: &str) -> usize {