extract assist helper for getting a specific token

This commit is contained in:
Aleksey Kladov 2019-10-27 11:45:59 +03:00
parent 3840324429
commit 61349a3d18
4 changed files with 12 additions and 15 deletions

View File

@ -5,7 +5,7 @@
use ra_fmt::{leading_indent, reindent};
use ra_syntax::{
algo::{self, find_covering_element, find_node_at_offset},
AstNode, SourceFile, SyntaxElement, SyntaxNode, SyntaxToken, TextRange, TextUnit,
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextUnit,
TokenAtOffset,
};
use ra_text_edit::TextEditBuilder;
@ -111,6 +111,10 @@ pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
self.source_file.syntax().token_at_offset(self.frange.range.start())
}
pub(crate) fn find_token_at_offset(&self, kind: SyntaxKind) -> Option<SyntaxToken> {
self.token_at_offset().find(|it| it.kind() == kind)
}
pub(crate) fn node_at_offset<N: AstNode>(&self) -> Option<N> {
find_node_at_offset(self.source_file.syntax(), self.frange.range.start())
}

View File

@ -19,7 +19,7 @@
// }
// ```
pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let comma = ctx.token_at_offset().find(|leaf| leaf.kind() == T![,])?;
let comma = ctx.find_token_at_offset(T![,])?;
let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;
let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?;

View File

@ -3,17 +3,14 @@
use hir::db::HirDatabase;
use ra_syntax::{
SyntaxKind::{RAW_STRING, STRING},
SyntaxToken, TextRange, TextUnit,
TextRange, TextUnit,
};
use rustc_lexer;
use crate::{Assist, AssistCtx, AssistId};
pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let token = ctx.token_at_offset().right_biased()?;
if token.kind() != STRING {
return None;
}
let token = ctx.find_token_at_offset(STRING)?;
let text = token.text().as_str();
let usual_string_range = find_usual_string_range(text)?;
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
}
pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let token = raw_string_token(&ctx)?;
let token = ctx.find_token_at_offset(RAW_STRING)?;
let text = token.text().as_str();
let usual_string_range = find_usual_string_range(text)?;
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<
}
pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let token = raw_string_token(&ctx)?;
let token = ctx.find_token_at_offset(RAW_STRING)?;
ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| {
edit.target(token.text_range());
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> {
}
pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let token = raw_string_token(&ctx)?;
let token = ctx.find_token_at_offset(RAW_STRING)?;
let text = token.text().as_str();
if text.starts_with("r\"") {
// no hash to remove
@ -91,10 +88,6 @@ pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
ctx.build()
}
fn raw_string_token(ctx: &AssistCtx<impl HirDatabase>) -> Option<SyntaxToken> {
ctx.token_at_offset().right_biased().filter(|it| it.kind() == RAW_STRING)
}
fn count_hashes(s: &str) -> usize {
let mut max_hash_streak = 0usize;
for idx in s.match_indices("\"#").map(|(i, _)| i) {

View File

@ -8,7 +8,7 @@
use crate::{Assist, AssistCtx, AssistId};
pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let colon_colon = ctx.token_at_offset().find(|leaf| leaf.kind() == T![::])?;
let colon_colon = ctx.find_token_at_offset(T![::])?;
let path = ast::Path::cast(colon_colon.parent())?;
let top_path = successors(Some(path), |it| it.parent_path()).last()?;