aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assist_ctx.rs6
-rw-r--r--crates/ra_assists/src/assists/flip_comma.rs2
-rw-r--r--crates/ra_assists/src/assists/raw_string.rs17
-rw-r--r--crates/ra_assists/src/assists/split_import.rs2
4 files changed, 12 insertions, 15 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index e270c5d60..20d33fb16 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -5,7 +5,7 @@ use ra_db::FileRange;
5use ra_fmt::{leading_indent, reindent}; 5use ra_fmt::{leading_indent, reindent};
6use ra_syntax::{ 6use ra_syntax::{
7 algo::{self, find_covering_element, find_node_at_offset}, 7 algo::{self, find_covering_element, find_node_at_offset},
8 AstNode, SourceFile, SyntaxElement, SyntaxNode, SyntaxToken, TextRange, TextUnit, 8 AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextUnit,
9 TokenAtOffset, 9 TokenAtOffset,
10}; 10};
11use ra_text_edit::TextEditBuilder; 11use ra_text_edit::TextEditBuilder;
@@ -111,6 +111,10 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
111 self.source_file.syntax().token_at_offset(self.frange.range.start()) 111 self.source_file.syntax().token_at_offset(self.frange.range.start())
112 } 112 }
113 113
114 pub(crate) fn find_token_at_offset(&self, kind: SyntaxKind) -> Option<SyntaxToken> {
115 self.token_at_offset().find(|it| it.kind() == kind)
116 }
117
114 pub(crate) fn node_at_offset<N: AstNode>(&self) -> Option<N> { 118 pub(crate) fn node_at_offset<N: AstNode>(&self) -> Option<N> {
115 find_node_at_offset(self.source_file.syntax(), self.frange.range.start()) 119 find_node_at_offset(self.source_file.syntax(), self.frange.range.start())
116 } 120 }
diff --git a/crates/ra_assists/src/assists/flip_comma.rs b/crates/ra_assists/src/assists/flip_comma.rs
index d06c5a0e1..53ba8011d 100644
--- a/crates/ra_assists/src/assists/flip_comma.rs
+++ b/crates/ra_assists/src/assists/flip_comma.rs
@@ -19,7 +19,7 @@ use crate::{Assist, AssistCtx, AssistId};
19// } 19// }
20// ``` 20// ```
21pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 21pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
22 let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?; 22 let comma = ctx.find_token_at_offset(T![,])?;
23 let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; 23 let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;
24 let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; 24 let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?;
25 25
diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs
index 9c996c902..ea756d1ca 100644
--- a/crates/ra_assists/src/assists/raw_string.rs
+++ b/crates/ra_assists/src/assists/raw_string.rs
@@ -3,17 +3,14 @@
3use hir::db::HirDatabase; 3use hir::db::HirDatabase;
4use ra_syntax::{ 4use ra_syntax::{
5 SyntaxKind::{RAW_STRING, STRING}, 5 SyntaxKind::{RAW_STRING, STRING},
6 SyntaxToken, TextRange, TextUnit, 6 TextRange, TextUnit,
7}; 7};
8use rustc_lexer; 8use rustc_lexer;
9 9
10use crate::{Assist, AssistCtx, AssistId}; 10use crate::{Assist, AssistCtx, AssistId};
11 11
12pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 12pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
13 let token = ctx.token_at_offset().right_biased()?; 13 let token = ctx.find_token_at_offset(STRING)?;
14 if token.kind() != STRING {
15 return None;
16 }
17 let text = token.text().as_str(); 14 let text = token.text().as_str();
18 let usual_string_range = find_usual_string_range(text)?; 15 let usual_string_range = find_usual_string_range(text)?;
19 let start_of_inside = usual_string_range.start().to_usize() + 1; 16 let start_of_inside = usual_string_range.start().to_usize() + 1;
@@ -44,7 +41,7 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
44} 41}
45 42
46pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 43pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
47 let token = raw_string_token(&ctx)?; 44 let token = ctx.find_token_at_offset(RAW_STRING)?;
48 let text = token.text().as_str(); 45 let text = token.text().as_str();
49 let usual_string_range = find_usual_string_range(text)?; 46 let usual_string_range = find_usual_string_range(text)?;
50 ctx.add_action(AssistId("make_usual_string"), "make usual string", |edit| { 47 ctx.add_action(AssistId("make_usual_string"), "make usual string", |edit| {
@@ -60,7 +57,7 @@ pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<
60} 57}
61 58
62pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 59pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
63 let token = raw_string_token(&ctx)?; 60 let token = ctx.find_token_at_offset(RAW_STRING)?;
64 ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| { 61 ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| {
65 edit.target(token.text_range()); 62 edit.target(token.text_range());
66 edit.insert(token.text_range().start() + TextUnit::of_char('r'), "#"); 63 edit.insert(token.text_range().start() + TextUnit::of_char('r'), "#");
@@ -70,7 +67,7 @@ pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
70} 67}
71 68
72pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 69pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
73 let token = raw_string_token(&ctx)?; 70 let token = ctx.find_token_at_offset(RAW_STRING)?;
74 let text = token.text().as_str(); 71 let text = token.text().as_str();
75 if text.starts_with("r\"") { 72 if text.starts_with("r\"") {
76 // no hash to remove 73 // no hash to remove
@@ -91,10 +88,6 @@ pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
91 ctx.build() 88 ctx.build()
92} 89}
93 90
94fn raw_string_token(ctx: &AssistCtx<impl HirDatabase>) -> Option<SyntaxToken> {
95 ctx.token_at_offset().right_biased().filter(|it| it.kind() == RAW_STRING)
96}
97
98fn count_hashes(s: &str) -> usize { 91fn count_hashes(s: &str) -> usize {
99 let mut max_hash_streak = 0usize; 92 let mut max_hash_streak = 0usize;
100 for idx in s.match_indices("\"#").map(|(i, _)| i) { 93 for idx in s.match_indices("\"#").map(|(i, _)| i) {
diff --git a/crates/ra_assists/src/assists/split_import.rs b/crates/ra_assists/src/assists/split_import.rs
index fe3e64af5..09bde1b72 100644
--- a/crates/ra_assists/src/assists/split_import.rs
+++ b/crates/ra_assists/src/assists/split_import.rs
@@ -8,7 +8,7 @@ use ra_syntax::{ast, AstNode, TextUnit, T};
8use crate::{Assist, AssistCtx, AssistId}; 8use crate::{Assist, AssistCtx, AssistId};
9 9
10pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
11 let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == T![::])?; 11 let colon_colon = ctx.find_token_at_offset(T![::])?;
12 let path = ast::Path::cast(colon_colon.parent())?; 12 let path = ast::Path::cast(colon_colon.parent())?;
13 let top_path = successors(Some(path), |it| it.parent_path()).last()?; 13 let top_path = successors(Some(path), |it| it.parent_path()).last()?;
14 14