From 61349a3d18353ddfbd992a3bf51a88f5f0cfcddd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 11:45:59 +0300 Subject: extract assist helper for getting a specific token --- crates/ra_assists/src/assist_ctx.rs | 6 +++++- crates/ra_assists/src/assists/flip_comma.rs | 2 +- crates/ra_assists/src/assists/raw_string.rs | 17 +++++------------ crates/ra_assists/src/assists/split_import.rs | 2 +- 4 files changed, 12 insertions(+), 15 deletions(-) (limited to 'crates') 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; 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 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { self.source_file.syntax().token_at_offset(self.frange.range.start()) } + pub(crate) fn find_token_at_offset(&self, kind: SyntaxKind) -> Option { + self.token_at_offset().find(|it| it.kind() == kind) + } + pub(crate) fn node_at_offset(&self) -> Option { find_node_at_offset(self.source_file.syntax(), self.frange.range.start()) } 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}; // } // ``` pub(crate) fn flip_comma(mut ctx: AssistCtx) -> Option { - 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)?; 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 @@ 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) -> Option { - 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) -> Option) -> Option { - 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) -> Option< } pub(crate) fn add_hash(mut ctx: AssistCtx) -> Option { - 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) -> Option { } pub(crate) fn remove_hash(mut ctx: AssistCtx) -> Option { - 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) -> Option) -> Option { - 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) { 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}; use crate::{Assist, AssistCtx, AssistId}; pub(crate) fn split_import(mut ctx: AssistCtx) -> Option { - 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()?; -- cgit v1.2.3