mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-06 04:08:40 +00:00
extract assist helper for getting a specific token
This commit is contained in:
parent
3840324429
commit
61349a3d18
@ -5,7 +5,7 @@ use ra_db::FileRange;
|
|||||||
use ra_fmt::{leading_indent, reindent};
|
use ra_fmt::{leading_indent, reindent};
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
algo::{self, find_covering_element, find_node_at_offset},
|
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,
|
TokenAtOffset,
|
||||||
};
|
};
|
||||||
use ra_text_edit::TextEditBuilder;
|
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())
|
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> {
|
pub(crate) fn node_at_offset<N: AstNode>(&self) -> Option<N> {
|
||||||
find_node_at_offset(self.source_file.syntax(), self.frange.range.start())
|
find_node_at_offset(self.source_file.syntax(), self.frange.range.start())
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ use crate::{Assist, AssistCtx, AssistId};
|
|||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
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 prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;
|
||||||
let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?;
|
let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?;
|
||||||
|
|
||||||
|
@ -3,17 +3,14 @@
|
|||||||
use hir::db::HirDatabase;
|
use hir::db::HirDatabase;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
SyntaxKind::{RAW_STRING, STRING},
|
SyntaxKind::{RAW_STRING, STRING},
|
||||||
SyntaxToken, TextRange, TextUnit,
|
TextRange, TextUnit,
|
||||||
};
|
};
|
||||||
use rustc_lexer;
|
use rustc_lexer;
|
||||||
|
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||||
let token = ctx.token_at_offset().right_biased()?;
|
let token = ctx.find_token_at_offset(STRING)?;
|
||||||
if token.kind() != STRING {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let text = token.text().as_str();
|
let text = token.text().as_str();
|
||||||
let usual_string_range = find_usual_string_range(text)?;
|
let usual_string_range = find_usual_string_range(text)?;
|
||||||
let start_of_inside = usual_string_range.start().to_usize() + 1;
|
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> {
|
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 text = token.text().as_str();
|
||||||
let usual_string_range = find_usual_string_range(text)?;
|
let usual_string_range = find_usual_string_range(text)?;
|
||||||
ctx.add_action(AssistId("make_usual_string"), "make usual string", |edit| {
|
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> {
|
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| {
|
ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| {
|
||||||
edit.target(token.text_range());
|
edit.target(token.text_range());
|
||||||
edit.insert(token.text_range().start() + TextUnit::of_char('r'), "#");
|
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> {
|
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();
|
let text = token.text().as_str();
|
||||||
if text.starts_with("r\"") {
|
if text.starts_with("r\"") {
|
||||||
// no hash to remove
|
// no hash to remove
|
||||||
@ -91,10 +88,6 @@ pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
|
|||||||
ctx.build()
|
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 {
|
fn count_hashes(s: &str) -> usize {
|
||||||
let mut max_hash_streak = 0usize;
|
let mut max_hash_streak = 0usize;
|
||||||
for idx in s.match_indices("\"#").map(|(i, _)| i) {
|
for idx in s.match_indices("\"#").map(|(i, _)| i) {
|
||||||
|
@ -8,7 +8,7 @@ use ra_syntax::{ast, AstNode, TextUnit, T};
|
|||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
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 path = ast::Path::cast(colon_colon.parent())?;
|
||||||
let top_path = successors(Some(path), |it| it.parent_path()).last()?;
|
let top_path = successors(Some(path), |it| it.parent_path()).last()?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user