Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/assists/src/assist_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ide_db::{
};
use syntax::{
algo::{self, find_node_at_offset, SyntaxRewriter},
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxToken, TextRange, TextSize,
AstNode, AstToken, SourceFile, SyntaxElement, SyntaxKind, SyntaxToken, TextRange, TextSize,
TokenAtOffset,
};
use text_edit::{TextEdit, TextEditBuilder};
Expand Down Expand Up @@ -81,9 +81,12 @@ impl<'a> AssistContext<'a> {
pub(crate) fn token_at_offset(&self) -> TokenAtOffset<SyntaxToken> {
self.source_file.syntax().token_at_offset(self.offset())
}
pub(crate) fn find_token_at_offset(&self, kind: SyntaxKind) -> Option<SyntaxToken> {
pub(crate) fn find_token_syntax_at_offset(&self, kind: SyntaxKind) -> Option<SyntaxToken> {
self.token_at_offset().find(|it| it.kind() == kind)
}
pub(crate) fn find_token_at_offset<T: AstToken>(&self) -> Option<T> {
self.token_at_offset().find_map(T::cast)
}
pub(crate) fn find_node_at_offset<N: AstNode>(&self) -> Option<N> {
find_node_at_offset(self.source_file.syntax(), self.offset())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/add_turbo_fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
// }
// ```
pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let ident = ctx.find_token_at_offset(SyntaxKind::IDENT).or_else(|| {
let ident = ctx.find_token_syntax_at_offset(SyntaxKind::IDENT).or_else(|| {
let arg_list = ctx.find_node_at_offset::<ast::ArgList>()?;
if arg_list.args().count() > 0 {
return None;
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/expand_glob_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::{
// fn qux(bar: Bar, baz: Baz) {}
// ```
pub(crate) fn expand_glob_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let star = ctx.find_token_at_offset(T![*])?;
let star = ctx.find_token_syntax_at_offset(T![*])?;
let (parent, mod_path) = find_parent_and_path(&star)?;
let target_module = match ctx.sema.resolve_path(&mod_path)? {
PathResolution::Def(ModuleDef::Module(it)) => it,
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/flip_comma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// }
// ```
pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let comma = ctx.find_token_at_offset(T![,])?;
let comma = ctx.find_token_syntax_at_offset(T![,])?;
let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?;
let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/flip_trait_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
pub(crate) fn flip_trait_bound(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
// We want to replicate the behavior of `flip_binexpr` by only suggesting
// the assist when the cursor is on a `+`
let plus = ctx.find_token_at_offset(T![+])?;
let plus = ctx.find_token_syntax_at_offset(T![+])?;

// Make sure we're in a `TypeBoundList`
if ast::TypeBoundList::cast(plus.parent()).is_none() {
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/introduce_named_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static ASSIST_LABEL: &str = "Introduce named lifetime";
// FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo
pub(crate) fn introduce_named_lifetime(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let lifetime_token = ctx
.find_token_at_offset(SyntaxKind::LIFETIME)
.find_token_syntax_at_offset(SyntaxKind::LIFETIME)
.filter(|lifetime| lifetime.text() == "'_")?;
if let Some(fn_def) = lifetime_token.ancestors().find_map(ast::Fn::cast) {
generate_fn_def_assist(acc, &fn_def, lifetime_token.text_range())
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/invert_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
// ```

pub(crate) fn invert_if(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let if_keyword = ctx.find_token_at_offset(T![if])?;
let if_keyword = ctx.find_token_syntax_at_offset(T![if])?;
let expr = ast::IfExpr::cast(if_keyword.parent())?;
let if_range = if_keyword.text_range();
let cursor_in_range = if_range.contains_range(ctx.frange.range);
Expand Down
34 changes: 21 additions & 13 deletions crates/assists/src/handlers/raw_string.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use std::borrow::Cow;

use syntax::{
ast::{self, HasQuotes, HasStringValue},
AstToken,
SyntaxKind::{RAW_STRING, STRING},
TextRange, TextSize,
};
use syntax::{ast, AstToken, TextRange, TextSize};
use test_utils::mark;

use crate::{AssistContext, AssistId, AssistKind, Assists};
Expand All @@ -26,7 +21,10 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// }
// ```
pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?;
let token = ctx.find_token_at_offset::<ast::String>()?;
if token.is_raw() {
return None;
}
let value = token.value()?;
let target = token.syntax().text_range();
acc.add(
Expand Down Expand Up @@ -65,7 +63,10 @@ pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext) -> Option<
// }
// ```
pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let token = ctx.find_token_at_offset(RAW_STRING).and_then(ast::RawString::cast)?;
let token = ctx.find_token_at_offset::<ast::String>()?;
if !token.is_raw() {
return None;
}
let value = token.value()?;
let target = token.syntax().text_range();
acc.add(
Expand Down Expand Up @@ -104,11 +105,15 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext) -> Optio
// }
// ```
pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let token = ctx.find_token_at_offset(RAW_STRING)?;
let target = token.text_range();
let token = ctx.find_token_at_offset::<ast::String>()?;
if !token.is_raw() {
return None;
}
let text_range = token.syntax().text_range();
let target = text_range;
acc.add(AssistId("add_hash", AssistKind::Refactor), "Add #", target, |edit| {
edit.insert(token.text_range().start() + TextSize::of('r'), "#");
edit.insert(token.text_range().end(), "#");
edit.insert(text_range.start() + TextSize::of('r'), "#");
edit.insert(text_range.end(), "#");
})
}

Expand All @@ -128,7 +133,10 @@ pub(crate) fn add_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
// }
// ```
pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let token = ctx.find_token_at_offset(RAW_STRING).and_then(ast::RawString::cast)?;
let token = ctx.find_token_at_offset::<ast::String>()?;
if !token.is_raw() {
return None;
}

let text = token.text().as_str();
if !text.starts_with("r#") && text.ends_with('#') {
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/remove_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// }
// ```
pub(crate) fn remove_mut(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let mut_token = ctx.find_token_at_offset(T![mut])?;
let mut_token = ctx.find_token_syntax_at_offset(T![mut])?;
let delete_from = mut_token.text_range().start();
let delete_to = match mut_token.next_token() {
Some(it) if it.kind() == SyntaxKind::WHITESPACE => it.text_range().end(),
Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/replace_let_with_if_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use ide_db::ty_filter::TryEnum;
// fn compute() -> Option<i32> { None }
// ```
pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let let_kw = ctx.find_token_at_offset(T![let])?;
let let_kw = ctx.find_token_syntax_at_offset(T![let])?;
let let_stmt = let_kw.ancestors().find_map(ast::LetStmt::cast)?;
let init = let_stmt.initializer()?;
let original_pat = let_stmt.pat()?;
Expand Down
8 changes: 2 additions & 6 deletions crates/assists/src/handlers/replace_string_with_char.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use syntax::{
ast::{self, HasStringValue},
AstToken,
SyntaxKind::STRING,
};
use syntax::{ast, AstToken, SyntaxKind::STRING};

use crate::{AssistContext, AssistId, AssistKind, Assists};

Expand All @@ -22,7 +18,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// }
// ```
pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?;
let token = ctx.find_token_syntax_at_offset(STRING).and_then(ast::String::cast)?;
let value = token.value()?;
let target = token.syntax().text_range();

Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/split_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// use std::{collections::HashMap};
// ```
pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let colon_colon = ctx.find_token_at_offset(T![::])?;
let colon_colon = ctx.find_token_syntax_at_offset(T![::])?;
let path = ast::Path::cast(colon_colon.parent())?.qualifier()?;
let top_path = successors(Some(path.clone()), |it| it.parent_path()).last()?;

Expand Down
2 changes: 1 addition & 1 deletion crates/assists/src/handlers/unwrap_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
let assist_id = AssistId("unwrap_block", AssistKind::RefactorRewrite);
let assist_label = "Unwrap block";

let l_curly_token = ctx.find_token_at_offset(T!['{'])?;
let l_curly_token = ctx.find_token_syntax_at_offset(T!['{'])?;
let mut block = ast::BlockExpr::cast(l_curly_token.parent())?;
let mut parent = block.syntax().parent()?;
if ast::MatchArm::can_cast(parent.kind()) {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir_expand/src/builtin_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use base_db::FileId;
use either::Either;
use mbe::parse_to_token_tree;
use parser::FragmentKind;
use syntax::ast::{self, AstToken, HasStringValue};
use syntax::ast::{self, AstToken};

macro_rules! register_builtin {
( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/extend_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn try_extend_selection(
) -> Option<TextRange> {
let range = frange.range;

let string_kinds = [COMMENT, STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING];
let string_kinds = [COMMENT, STRING, BYTE_STRING];
let list_kinds = [
RECORD_PAT_FIELD_LIST,
MATCH_ARM_LIST,
Expand Down
16 changes: 7 additions & 9 deletions crates/ide/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,12 @@ pub(crate) fn highlight(
element.clone()
};

if let Some(token) = element.as_token().cloned().and_then(ast::RawString::cast) {
let expanded = element_to_highlight.as_token().unwrap().clone();
if injection::highlight_injection(&mut stack, &sema, token, expanded).is_some() {
continue;
if let Some(token) = element.as_token().cloned().and_then(ast::String::cast) {
if token.is_raw() {
let expanded = element_to_highlight.as_token().unwrap().clone();
if injection::highlight_injection(&mut stack, &sema, token, expanded).is_some() {
continue;
}
}
}

Expand Down Expand Up @@ -214,10 +216,6 @@ pub(crate) fn highlight(
}
stack.pop_and_inject(None);
}
} else if let Some(string) =
element_to_highlight.as_token().cloned().and_then(ast::RawString::cast)
{
format_string_highlighter.highlight_format_string(&mut stack, &string, range);
}
}
}
Expand Down Expand Up @@ -532,7 +530,7 @@ fn highlight_element(
None => h.into(),
}
}
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => HighlightTag::StringLiteral.into(),
STRING | BYTE_STRING => HighlightTag::StringLiteral.into(),
ATTR => HighlightTag::Attribute.into(),
INT_NUMBER | FLOAT_NUMBER => HighlightTag::NumericLiteral.into(),
BYTE => HighlightTag::ByteLiteral.into(),
Expand Down
4 changes: 1 addition & 3 deletions crates/ide/src/syntax_highlighting/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ impl FormatStringHighlighter {
.children_with_tokens()
.filter(|t| t.kind() != SyntaxKind::WHITESPACE)
.nth(1)
.filter(|e| {
ast::String::can_cast(e.kind()) || ast::RawString::can_cast(e.kind())
})
.filter(|e| ast::String::can_cast(e.kind()))
}
_ => {}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/ide/src/syntax_highlighting/injection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::{collections::BTreeMap, convert::TryFrom};

use ast::{HasQuotes, HasStringValue};
use hir::Semantics;
use ide_db::call_info::ActiveParameter;
use itertools::Itertools;
Expand All @@ -15,7 +14,7 @@ use super::HighlightedRangeStack;
pub(super) fn highlight_injection(
acc: &mut HighlightedRangeStack,
sema: &Semantics<RootDatabase>,
literal: ast::RawString,
literal: ast::String,
expanded: SyntaxToken,
) -> Option<()> {
let active_parameter = ActiveParameter::at_token(&sema, expanded)?;
Expand Down
6 changes: 2 additions & 4 deletions crates/ide/src/syntax_tree.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use ide_db::base_db::{FileId, SourceDatabase};
use ide_db::RootDatabase;
use syntax::{
algo, AstNode, NodeOrToken, SourceFile,
SyntaxKind::{RAW_STRING, STRING},
SyntaxToken, TextRange, TextSize,
algo, AstNode, NodeOrToken, SourceFile, SyntaxKind::STRING, SyntaxToken, TextRange, TextSize,
};

// Feature: Show Syntax Tree
Expand Down Expand Up @@ -46,7 +44,7 @@ fn syntax_tree_for_string(token: &SyntaxToken, text_range: TextRange) -> Option<
// we'll attempt parsing it as rust syntax
// to provide the syntax tree of the contents of the string
match token.kind() {
STRING | RAW_STRING => syntax_tree_for_token(token, text_range),
STRING => syntax_tree_for_token(token, text_range),
_ => None,
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,7 @@ fn abi(p: &mut Parser) {
assert!(p.at(T![extern]));
let abi = p.start();
p.bump(T![extern]);
match p.current() {
STRING | RAW_STRING => p.bump_any(),
_ => (),
}
p.eat(STRING);
abi.complete(p, ABI);
}

Expand Down
14 changes: 2 additions & 12 deletions crates/parser/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,8 @@ use super::*;
// let _ = b"e";
// let _ = br"f";
// }
pub(crate) const LITERAL_FIRST: TokenSet = TokenSet::new(&[
TRUE_KW,
FALSE_KW,
INT_NUMBER,
FLOAT_NUMBER,
BYTE,
CHAR,
STRING,
RAW_STRING,
BYTE_STRING,
RAW_BYTE_STRING,
]);
pub(crate) const LITERAL_FIRST: TokenSet =
TokenSet::new(&[TRUE_KW, FALSE_KW, INT_NUMBER, FLOAT_NUMBER, BYTE, CHAR, STRING, BYTE_STRING]);

pub(crate) fn literal(p: &mut Parser) -> Option<CompletedMarker> {
if !p.at_ts(LITERAL_FIRST) {
Expand Down
4 changes: 1 addition & 3 deletions crates/parser/src/grammar/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
T![static] => consts::static_(p, m),
// test extern_block
// extern {}
T![extern]
if la == T!['{'] || ((la == STRING || la == RAW_STRING) && p.nth(2) == T!['{']) =>
{
T![extern] if la == T!['{'] || (la == STRING && p.nth(2) == T!['{']) => {
abi(p);
extern_item_list(p);
m.complete(p, EXTERN_BLOCK);
Expand Down
5 changes: 1 addition & 4 deletions crates/parser/src/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ pub enum SyntaxKind {
CHAR,
BYTE,
STRING,
RAW_STRING,
BYTE_STRING,
RAW_BYTE_STRING,
ERROR,
IDENT,
WHITESPACE,
Expand Down Expand Up @@ -277,8 +275,7 @@ impl SyntaxKind {
}
pub fn is_literal(self) -> bool {
match self {
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE | STRING | RAW_STRING | BYTE_STRING
| RAW_BYTE_STRING => true,
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE | STRING | BYTE_STRING => true,
_ => false,
}
}
Expand Down
11 changes: 9 additions & 2 deletions crates/syntax/src/ast/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ impl ast::Literal {
ast::IntNumber::cast(self.token())
}

pub fn as_string(&self) -> Option<ast::String> {
ast::String::cast(self.token())
}
pub fn as_byte_string(&self) -> Option<ast::ByteString> {
ast::ByteString::cast(self.token())
}

fn find_suffix(text: &str, possible_suffixes: &[&str]) -> Option<SmolStr> {
possible_suffixes
.iter()
Expand Down Expand Up @@ -351,10 +358,10 @@ impl ast::Literal {
suffix: Self::find_suffix(&text, &ast::FloatNumber::SUFFIXES),
}
}
STRING | RAW_STRING => LiteralKind::String,
STRING => LiteralKind::String,
T![true] => LiteralKind::Bool(true),
T![false] => LiteralKind::Bool(false),
BYTE_STRING | RAW_BYTE_STRING => LiteralKind::ByteString,
BYTE_STRING => LiteralKind::ByteString,
CHAR => LiteralKind::Char,
BYTE => LiteralKind::Byte,
_ => unreachable!(),
Expand Down
Loading