From 46b4f89c920c314caf1a8af2abdb09732d100d67 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Jan 2021 01:56:11 +0300 Subject: . --- crates/assists/src/handlers/generate_impl.rs | 11 +++++++---- crates/assists/src/handlers/generate_new.rs | 8 ++++---- crates/assists/src/handlers/raw_string.rs | 2 +- .../src/handlers/replace_derive_with_manual_impl.rs | 19 ++++++++++--------- crates/assists/src/utils.rs | 2 +- crates/hir_expand/src/builtin_derive.rs | 2 +- crates/hir_expand/src/name.rs | 8 ++++---- crates/ide/src/display/navigation_target.rs | 3 +-- crates/ide/src/display/short_label.rs | 4 ++-- crates/ide/src/extend_selection.rs | 4 ++-- crates/ide/src/inlay_hints.rs | 2 +- crates/ide/src/join_lines.rs | 2 +- crates/ide/src/syntax_highlighting/format.rs | 2 +- crates/ide/src/syntax_highlighting/inject.rs | 2 +- crates/ide_db/src/defs.rs | 2 +- crates/ide_db/src/helpers/insert_use.rs | 2 +- crates/ide_db/src/symbol_index.rs | 4 ++-- crates/mbe/src/syntax_bridge.rs | 2 +- crates/ssr/src/matching.rs | 13 +++++++------ crates/ssr/src/replacing.rs | 2 +- crates/ssr/src/resolving.rs | 2 +- crates/syntax/Cargo.toml | 2 +- crates/syntax/src/ast.rs | 4 ++-- crates/syntax/src/ast/make.rs | 6 +++--- crates/syntax/src/ast/node_ext.rs | 10 +++++----- crates/syntax/src/ast/token_ext.rs | 12 ++++++------ crates/syntax/src/validation.rs | 2 +- 27 files changed, 69 insertions(+), 65 deletions(-) (limited to 'crates') diff --git a/crates/assists/src/handlers/generate_impl.rs b/crates/assists/src/handlers/generate_impl.rs index 9af45192b..827477272 100644 --- a/crates/assists/src/handlers/generate_impl.rs +++ b/crates/assists/src/handlers/generate_impl.rs @@ -1,6 +1,9 @@ use itertools::Itertools; use stdx::format_to; -use syntax::ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner}; +use syntax::{ + ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner}, + SmolStr, +}; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -49,16 +52,16 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<() format_to!(buf, "{}", type_params.syntax()); } buf.push_str(" "); - buf.push_str(name.text().as_str()); + buf.push_str(name.text()); if let Some(type_params) = type_params { let lifetime_params = type_params .lifetime_params() .filter_map(|it| it.lifetime()) - .map(|it| it.text().clone()); + .map(|it| SmolStr::from(it.text())); let type_params = type_params .type_params() .filter_map(|it| it.name()) - .map(|it| it.text().clone()); + .map(|it| SmolStr::from(it.text())); let generic_params = lifetime_params.chain(type_params).format(", "); format_to!(buf, "<{}>", generic_params) diff --git a/crates/assists/src/handlers/generate_new.rs b/crates/assists/src/handlers/generate_new.rs index 5c52b2bc8..b7390855a 100644 --- a/crates/assists/src/handlers/generate_new.rs +++ b/crates/assists/src/handlers/generate_new.rs @@ -3,7 +3,7 @@ use itertools::Itertools; use stdx::format_to; use syntax::{ ast::{self, AstNode, GenericParamsOwner, NameOwner, StructKind, VisibilityOwner}, - T, + SmolStr, T, }; use crate::{AssistContext, AssistId, AssistKind, Assists}; @@ -95,14 +95,14 @@ fn generate_impl_text(strukt: &ast::Struct, code: &str) -> String { format_to!(buf, "{}", type_params.syntax()); } buf.push_str(" "); - buf.push_str(strukt.name().unwrap().text().as_str()); + buf.push_str(strukt.name().unwrap().text()); if let Some(type_params) = type_params { let lifetime_params = type_params .lifetime_params() .filter_map(|it| it.lifetime()) - .map(|it| it.text().clone()); + .map(|it| SmolStr::from(it.text())); let type_params = - type_params.type_params().filter_map(|it| it.name()).map(|it| it.text().clone()); + type_params.type_params().filter_map(|it| it.name()).map(|it| SmolStr::from(it.text())); format_to!(buf, "<{}>", lifetime_params.chain(type_params).format(", ")) } diff --git a/crates/assists/src/handlers/raw_string.rs b/crates/assists/src/handlers/raw_string.rs index be963f162..d95267607 100644 --- a/crates/assists/src/handlers/raw_string.rs +++ b/crates/assists/src/handlers/raw_string.rs @@ -138,7 +138,7 @@ pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext) -> Option<()> return None; } - let text = token.text().as_str(); + let text = token.text(); if !text.starts_with("r#") && text.ends_with('#') { return None; } diff --git a/crates/assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/assists/src/handlers/replace_derive_with_manual_impl.rs index bd4c1c806..6aa9d2f2c 100644 --- a/crates/assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/assists/src/handlers/replace_derive_with_manual_impl.rs @@ -3,7 +3,7 @@ use ide_db::imports_locator; use itertools::Itertools; use syntax::{ ast::{self, make, AstNode}, - Direction, SmolStr, + Direction, SyntaxKind::{IDENT, WHITESPACE}, TextSize, }; @@ -43,17 +43,18 @@ pub(crate) fn replace_derive_with_manual_impl( ) -> Option<()> { let attr = ctx.find_node_at_offset::()?; - let attr_name = attr + let has_derive = attr .syntax() .descendants_with_tokens() .filter(|t| t.kind() == IDENT) .find_map(syntax::NodeOrToken::into_token) - .filter(|t| t.text() == "derive")? - .text() - .clone(); + .filter(|t| t.text() == "derive") + .is_some(); + if !has_derive { + return None; + } - let trait_token = - ctx.token_at_offset().find(|t| t.kind() == IDENT && *t.text() != attr_name)?; + let trait_token = ctx.token_at_offset().find(|t| t.kind() == IDENT && t.text() != "derive")?; let trait_path = make::path_unqualified(make::path_segment(make::name_ref(trait_token.text()))); let annotated_name = attr.syntax().siblings(Direction::Next).find_map(ast::Name::cast)?; @@ -176,9 +177,9 @@ fn update_attribute( .syntax() .descendants_with_tokens() .filter(|t| t.kind() == IDENT) - .filter_map(|t| t.into_token().map(|t| t.text().clone())) + .filter_map(|t| t.into_token().map(|t| t.text().to_string())) .filter(|t| t != trait_name.text()) - .collect::>(); + .collect::>(); let has_more_derives = !new_attr_input.is_empty(); if has_more_derives { diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index fc9f83bab..44c35bafa 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -223,7 +223,7 @@ fn invert_special_case(expr: &ast::Expr) -> Option { let method = mce.name_ref()?; let arg_list = mce.arg_list()?; - let method = match method.text().as_str() { + let method = match method.text() { "is_some" => "is_none", "is_none" => "is_some", "is_ok" => "is_err", diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs index eb257579f..b7f1aae8f 100644 --- a/crates/hir_expand/src/builtin_derive.rs +++ b/crates/hir_expand/src/builtin_derive.rs @@ -102,7 +102,7 @@ fn parse_adt(tt: &tt::Subtree) -> Result { debug!("name token not found"); mbe::ExpandError::ConversionError })?; - let name_token = tt::Ident { id: name_token_id, text: name.text().clone() }; + let name_token = tt::Ident { id: name_token_id, text: name.text().into() }; let type_params = params.map_or(0, |type_param_list| type_param_list.type_params().count()); Ok(BasicAdtInfo { name: name_token, type_params }) } diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs index 95d853b6d..d692cec14 100644 --- a/crates/hir_expand/src/name.rs +++ b/crates/hir_expand/src/name.rs @@ -38,7 +38,7 @@ impl Name { } pub fn new_lifetime(lt: &ast::Lifetime) -> Name { - Self::new_text(lt.text().clone()) + Self::new_text(lt.text().into()) } /// Shortcut to create inline plain text name @@ -47,12 +47,12 @@ impl Name { } /// Resolve a name from the text of token. - fn resolve(raw_text: &SmolStr) -> Name { + fn resolve(raw_text: &str) -> Name { let raw_start = "r#"; - if raw_text.as_str().starts_with(raw_start) { + if raw_text.starts_with(raw_start) { Name::new_text(SmolStr::new(&raw_text[raw_start.len()..])) } else { - Name::new_text(raw_text.clone()) + Name::new_text(raw_text.into()) } } diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 00e601244..671aa1373 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -153,8 +153,7 @@ impl NavigationTarget { node: InFile<&dyn ast::NameOwner>, kind: SymbolKind, ) -> NavigationTarget { - let name = - node.value.name().map(|it| it.text().clone()).unwrap_or_else(|| SmolStr::new("_")); + let name = node.value.name().map(|it| it.text().into()).unwrap_or_else(|| "_".into()); let focus_range = node.value.name().map(|it| node.with_value(it.syntax()).original_file_range(db).range); let frange = node.map(|it| it.syntax()).original_file_range(db); diff --git a/crates/ide/src/display/short_label.rs b/crates/ide/src/display/short_label.rs index 990f740b8..b8e4cc181 100644 --- a/crates/ide/src/display/short_label.rs +++ b/crates/ide/src/display/short_label.rs @@ -90,7 +90,7 @@ impl ShortLabel for ast::Variant { impl ShortLabel for ast::ConstParam { fn short_label(&self) -> Option { let mut buf = "const ".to_owned(); - buf.push_str(self.name()?.text().as_str()); + buf.push_str(self.name()?.text()); if let Some(type_ref) = self.ty() { format_to!(buf, ": {}", type_ref.syntax()); } @@ -117,6 +117,6 @@ where { let mut buf = node.visibility().map(|v| format!("{} ", v.syntax())).unwrap_or_default(); buf.push_str(label); - buf.push_str(node.name()?.text().as_str()); + buf.push_str(node.name()?.text()); Some(buf) } diff --git a/crates/ide/src/extend_selection.rs b/crates/ide/src/extend_selection.rs index 17a540972..2d722dee0 100644 --- a/crates/ide/src/extend_selection.rs +++ b/crates/ide/src/extend_selection.rs @@ -213,8 +213,8 @@ fn extend_ws(root: &SyntaxNode, ws: SyntaxToken, offset: TextSize) -> TextRange let ws_text = ws.text(); let suffix = TextRange::new(offset, ws.text_range().end()) - ws.text_range().start(); let prefix = TextRange::new(ws.text_range().start(), offset) - ws.text_range().start(); - let ws_suffix = &ws_text.as_str()[suffix]; - let ws_prefix = &ws_text.as_str()[prefix]; + let ws_suffix = &ws_text[suffix]; + let ws_prefix = &ws_text[prefix]; if ws_text.contains('\n') && !ws_suffix.contains('\n') { if let Some(node) = ws.next_sibling_or_token() { let start = match ws_prefix.rfind('\n') { diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index a2039fcc7..54485fd30 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -411,7 +411,7 @@ fn get_string_representation(expr: &ast::Expr) -> Option { match expr { ast::Expr::MethodCallExpr(method_call_expr) => { let name_ref = method_call_expr.name_ref()?; - match name_ref.text().as_str() { + match name_ref.text() { "clone" => method_call_expr.receiver().map(|rec| rec.to_string()), name_ref => Some(name_ref.to_owned()), } diff --git a/crates/ide/src/join_lines.rs b/crates/ide/src/join_lines.rs index 981467c8d..631bde0f1 100644 --- a/crates/ide/src/join_lines.rs +++ b/crates/ide/src/join_lines.rs @@ -59,7 +59,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextS // The node is either the first or the last in the file let suff = &token.text()[TextRange::new( offset - token.text_range().start() + TextSize::of('\n'), - TextSize::of(token.text().as_str()), + TextSize::of(token.text()), )]; let spaces = suff.bytes().take_while(|&b| b == b' ').count(); diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs index a74ca844b..8a9b5ca8c 100644 --- a/crates/ide/src/syntax_highlighting/format.rs +++ b/crates/ide/src/syntax_highlighting/format.rs @@ -30,7 +30,7 @@ fn is_format_string(string: &ast::String) -> Option<()> { let parent = string.syntax().parent(); let name = parent.parent().and_then(ast::MacroCall::cast)?.path()?.segment()?.name_ref()?; - if !matches!(name.text().as_str(), "format_args" | "format_args_nl") { + if !matches!(name.text(), "format_args" | "format_args_nl") { return None; } diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs index 281461493..8cdc3688f 100644 --- a/crates/ide/src/syntax_highlighting/inject.rs +++ b/crates/ide/src/syntax_highlighting/inject.rs @@ -116,7 +116,7 @@ pub(super) fn doc_comment(hl: &mut Highlights, node: &SyntaxNode) { None => (), } - let line: &str = comment.text().as_str(); + let line: &str = comment.text(); let range = comment.syntax().text_range(); let mut pos = TextSize::of(comment.prefix()); diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index d9875ffef..a8091dbee 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -343,7 +343,7 @@ impl NameRefClass { hir::AssocItem::TypeAlias(it) => Some(*it), _ => None, }) - .find(|alias| alias.name(sema.db).to_string() == **name_ref.text()) + .find(|alias| &alias.name(sema.db).to_string() == name_ref.text()) { return Some(NameRefClass::Definition(Definition::ModuleDef( ModuleDef::TypeAlias(ty), diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs index 877d4f1c7..fd4035198 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs @@ -507,7 +507,7 @@ impl ImportGroup { PathSegmentKind::SelfKw => ImportGroup::ThisModule, PathSegmentKind::SuperKw => ImportGroup::SuperModule, PathSegmentKind::CrateKw => ImportGroup::ThisCrate, - PathSegmentKind::Name(name) => match name.text().as_str() { + PathSegmentKind::Name(name) => match name.text() { "std" => ImportGroup::Std, "core" => ImportGroup::Std, _ => ImportGroup::ExternCrate, diff --git a/crates/ide_db/src/symbol_index.rs b/crates/ide_db/src/symbol_index.rs index 0aa6a0765..500bdfd6b 100644 --- a/crates/ide_db/src/symbol_index.rs +++ b/crates/ide_db/src/symbol_index.rs @@ -209,7 +209,7 @@ pub fn crate_symbols(db: &RootDatabase, krate: CrateId, query: Query) -> Vec Vec { +pub fn index_resolve(db: &RootDatabase, name: &str) -> Vec { let mut query = Query::new(name.to_string()); query.exact(); query.limit(4); @@ -409,7 +409,7 @@ fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> { fn decl(node: N) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> { let name = node.name()?; let name_range = name.syntax().text_range(); - let name = name.text().clone(); + let name = name.text().into(); let ptr = SyntaxNodePtr::new(node.syntax()); Some((name, ptr, name_range)) diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 51002e7b8..1e1123889 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -507,7 +507,7 @@ impl SrcToken for SynToken { } } fn to_text(&self) -> SmolStr { - self.token().text().clone() + self.token().text().into() } } diff --git a/crates/ssr/src/matching.rs b/crates/ssr/src/matching.rs index 42d313f91..df013bae9 100644 --- a/crates/ssr/src/matching.rs +++ b/crates/ssr/src/matching.rs @@ -10,8 +10,11 @@ use hir::Semantics; use ide_db::base_db::FileRange; use rustc_hash::FxHashMap; use std::{cell::Cell, iter::Peekable}; -use syntax::ast::{AstNode, AstToken}; use syntax::{ast, SyntaxElement, SyntaxElementChildren, SyntaxKind, SyntaxNode, SyntaxToken}; +use syntax::{ + ast::{AstNode, AstToken}, + SmolStr, +}; use test_utils::mark; // Creates a match error. If we're currently attempting to match some code that we thought we were @@ -398,11 +401,11 @@ impl<'db, 'sema> Matcher<'db, 'sema> { code: &SyntaxNode, ) -> Result<(), MatchFailed> { // Build a map keyed by field name. - let mut fields_by_name = FxHashMap::default(); + let mut fields_by_name: FxHashMap = FxHashMap::default(); for child in code.children() { if let Some(record) = ast::RecordExprField::cast(child.clone()) { if let Some(name) = record.field_name() { - fields_by_name.insert(name.text().clone(), child.clone()); + fields_by_name.insert(name.text().into(), child.clone()); } } } @@ -473,9 +476,7 @@ impl<'db, 'sema> Matcher<'db, 'sema> { } SyntaxElement::Node(n) => { if let Some(first_token) = n.first_token() { - if Some(first_token.text().as_str()) - == next_pattern_token.as_deref() - { + if Some(first_token.text()) == next_pattern_token.as_deref() { if let Some(SyntaxElement::Node(p)) = pattern.next() { // We have a subtree that starts with the next token in our pattern. self.attempt_match_token_tree(phase, &p, &n)?; diff --git a/crates/ssr/src/replacing.rs b/crates/ssr/src/replacing.rs index 7e7ce37bd..06a94a46c 100644 --- a/crates/ssr/src/replacing.rs +++ b/crates/ssr/src/replacing.rs @@ -173,7 +173,7 @@ impl ReplacementRenderer<'_> { ); } } else { - self.out.push_str(token.text().as_str()); + self.out.push_str(token.text()); } } diff --git a/crates/ssr/src/resolving.rs b/crates/ssr/src/resolving.rs index f5ceb5729..14e5a3b69 100644 --- a/crates/ssr/src/resolving.rs +++ b/crates/ssr/src/resolving.rs @@ -228,7 +228,7 @@ impl<'db> ResolutionScope<'db> { None, |_ty, assoc_item| { let item_name = assoc_item.name(self.scope.db)?; - if item_name.to_string().as_str() == name.text().as_str() { + if item_name.to_string().as_str() == name.text() { Some(hir::PathResolution::AssocItem(assoc_item)) } else { None diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index 37d3faa03..165533388 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml @@ -12,7 +12,7 @@ doctest = false [dependencies] itertools = "0.10.0" -rowan = "0.11" +rowan = { path="../../../rowan" } rustc_lexer = { version = "697.0.0", package = "rustc-ap-rustc_lexer" } rustc-hash = "1.1.0" arrayvec = "0.5.1" diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs index 83de067d9..a25ff655e 100644 --- a/crates/syntax/src/ast.rs +++ b/crates/syntax/src/ast.rs @@ -12,7 +12,7 @@ use std::marker::PhantomData; use crate::{ syntax_node::{SyntaxNode, SyntaxNodeChildren, SyntaxToken}, - SmolStr, SyntaxKind, + SyntaxKind, }; pub use self::{ @@ -54,7 +54,7 @@ pub trait AstToken { fn syntax(&self) -> &SyntaxToken; - fn text(&self) -> &SmolStr { + fn text(&self) -> &str { self.syntax().text() } } diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index cc1717237..b755c9692 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -495,7 +495,7 @@ pub mod tokens { .syntax() .descendants_with_tokens() .filter_map(|it| it.into_token()) - .find(|it| it.kind() == WHITESPACE && it.text().as_str() == " ") + .find(|it| it.kind() == WHITESPACE && it.text() == " ") .unwrap() } @@ -523,7 +523,7 @@ pub mod tokens { .syntax() .descendants_with_tokens() .filter_map(|it| it.into_token()) - .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n") + .find(|it| it.kind() == WHITESPACE && it.text() == "\n") .unwrap() } @@ -533,7 +533,7 @@ pub mod tokens { .syntax() .descendants_with_tokens() .filter_map(|it| it.into_token()) - .find(|it| it.kind() == WHITESPACE && it.text().as_str() == "\n\n") + .find(|it| it.kind() == WHITESPACE && it.text() == "\n\n") .unwrap() } diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 738c92a5b..5c8cf900f 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -13,19 +13,19 @@ use crate::{ }; impl ast::Lifetime { - pub fn text(&self) -> &SmolStr { + pub fn text(&self) -> &str { text_of_first_token(self.syntax()) } } impl ast::Name { - pub fn text(&self) -> &SmolStr { + pub fn text(&self) -> &str { text_of_first_token(self.syntax()) } } impl ast::NameRef { - pub fn text(&self) -> &SmolStr { + pub fn text(&self) -> &str { text_of_first_token(self.syntax()) } @@ -34,7 +34,7 @@ impl ast::NameRef { } } -fn text_of_first_token(node: &SyntaxNode) -> &SmolStr { +fn text_of_first_token(node: &SyntaxNode) -> &str { node.green().children().next().and_then(|it| it.into_token()).unwrap().text() } @@ -121,7 +121,7 @@ impl ast::Attr { pub fn simple_name(&self) -> Option { let path = self.path()?; match (path.segment(), path.qualifier()) { - (Some(segment), None) => Some(segment.syntax().first_token()?.text().clone()), + (Some(segment), None) => Some(segment.syntax().first_token()?.text().into()), _ => None, } } diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 5e9620a40..5e07ec7d1 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs @@ -41,7 +41,7 @@ impl ast::Comment { match kind { CommentKind { shape, doc: Some(_) } => { let prefix = kind.prefix(); - let text = &self.text().as_str()[prefix.len()..]; + let text = &self.text()[prefix.len()..]; let ws = text.chars().next().filter(|c| c.is_whitespace()); let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]); match shape { @@ -156,13 +156,13 @@ impl ast::String { pub fn value(&self) -> Option> { if self.is_raw() { - let text = self.text().as_str(); + let text = self.text(); let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; return Some(Cow::Borrowed(text)); } - let text = self.text().as_str(); + let text = self.text(); let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; let mut buf = String::new(); @@ -190,7 +190,7 @@ impl ast::String { } pub fn quote_offsets(&self) -> Option { - let text = self.text().as_str(); + let text = self.text(); let offsets = QuoteOffsets::new(text)?; let o = self.syntax().text_range().start(); let offsets = QuoteOffsets { @@ -560,7 +560,7 @@ impl HasFormatSpecifier for ast::String { fn char_ranges( &self, ) -> Option)>> { - let text = self.text().as_str(); + let text = self.text(); let text = &text[self.text_range_between_quotes()? - self.syntax().text_range().start()]; let offset = self.text_range_between_quotes()?.start() - self.syntax().text_range().start(); @@ -590,7 +590,7 @@ impl ast::IntNumber { pub fn value(&self) -> Option { let token = self.syntax(); - let mut text = token.text().as_str(); + let mut text = token.text(); if let Some(suffix) = self.suffix() { text = &text[..text.len() - suffix.len()] } diff --git a/crates/syntax/src/validation.rs b/crates/syntax/src/validation.rs index 7901580ee..7694e8834 100644 --- a/crates/syntax/src/validation.rs +++ b/crates/syntax/src/validation.rs @@ -116,7 +116,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec) { } let token = literal.token(); - let text = token.text().as_str(); + let text = token.text(); // FIXME: lift this lambda refactor to `fn` (https://github.com/rust-analyzer/rust-analyzer/pull/2834#discussion_r366199205) let mut push_err = |prefix_len, (off, err): (usize, unescape::EscapeError)| { -- cgit v1.2.3 From 3429b32ad119756985e1a7bfa5e9e53042671f8b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Jan 2021 14:04:53 +0300 Subject: :arrow_up: rowan It now stores text inline with tokens --- crates/mbe/src/syntax_bridge.rs | 4 +--- crates/syntax/Cargo.toml | 5 +---- crates/syntax/src/lib.rs | 4 ++-- crates/syntax/src/parsing/reparsing.rs | 3 +-- crates/syntax/src/parsing/text_tree_sink.rs | 4 ++-- crates/syntax/src/syntax_node.rs | 4 ++-- 6 files changed, 9 insertions(+), 15 deletions(-) (limited to 'crates') diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 1e1123889..0cdc175be 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -682,10 +682,8 @@ impl<'a> TreeSink for TtTreeSink<'a> { self.text_pos += TextSize::of(text); } - let text = SmolStr::new(self.buf.as_str()); + self.inner.token(kind, self.buf.as_str()); self.buf.clear(); - self.inner.token(kind, text); - // Add whitespace between adjoint puncts let next = last.bump(); if let ( diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index 165533388..55b437a3a 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml @@ -12,15 +12,12 @@ doctest = false [dependencies] itertools = "0.10.0" -rowan = { path="../../../rowan" } +rowan = "0.12" rustc_lexer = { version = "697.0.0", package = "rustc-ap-rustc_lexer" } rustc-hash = "1.1.0" arrayvec = "0.5.1" once_cell = "1.3.1" indexmap = "1.4.0" -# This crate transitively depends on `smol_str` via `rowan`. -# ideally, `serde` should be enabled by `rust-analyzer`, but we enable it here -# to reduce number of compilations smol_str = { version = "0.1.15", features = ["serde"] } serde = { version = "1.0.106", features = ["derive"] } diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index ea7482bb1..11294c5b2 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -56,9 +56,9 @@ pub use crate::{ }; pub use parser::{SyntaxKind, T}; pub use rowan::{ - Direction, GreenNode, NodeOrToken, SmolStr, SyntaxText, TextRange, TextSize, TokenAtOffset, - WalkEvent, + Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize, TokenAtOffset, WalkEvent, }; +pub use smol_str::SmolStr; /// `Parse` is the result of the parsing: a syntax tree and a collection of /// errors. diff --git a/crates/syntax/src/parsing/reparsing.rs b/crates/syntax/src/parsing/reparsing.rs index 76f01084c..3d637bf91 100644 --- a/crates/syntax/src/parsing/reparsing.rs +++ b/crates/syntax/src/parsing/reparsing.rs @@ -73,8 +73,7 @@ fn reparse_token<'node>( new_text.pop(); } - let new_token = - GreenToken::new(rowan::SyntaxKind(prev_token_kind.into()), new_text.into()); + let new_token = GreenToken::new(rowan::SyntaxKind(prev_token_kind.into()), &new_text); Some(( prev_token.replace_with(new_token), new_err.into_iter().collect(), diff --git a/crates/syntax/src/parsing/text_tree_sink.rs b/crates/syntax/src/parsing/text_tree_sink.rs index ce27c3dd9..d5ddc076f 100644 --- a/crates/syntax/src/parsing/text_tree_sink.rs +++ b/crates/syntax/src/parsing/text_tree_sink.rs @@ -8,7 +8,7 @@ use crate::{ ast, parsing::Token, syntax_node::GreenNode, - SmolStr, SyntaxError, + SyntaxError, SyntaxKind::{self, *}, SyntaxTreeBuilder, TextRange, TextSize, }; @@ -135,7 +135,7 @@ impl<'a> TextTreeSink<'a> { fn do_token(&mut self, kind: SyntaxKind, len: TextSize, n_tokens: usize) { let range = TextRange::at(self.text_pos, len); - let text: SmolStr = self.text[range].into(); + let text = &self.text[range]; self.text_pos += len; self.token_pos += n_tokens; self.inner.token(kind, text); diff --git a/crates/syntax/src/syntax_node.rs b/crates/syntax/src/syntax_node.rs index cc30138fa..8f643b228 100644 --- a/crates/syntax/src/syntax_node.rs +++ b/crates/syntax/src/syntax_node.rs @@ -8,7 +8,7 @@ use rowan::{GreenNodeBuilder, Language}; -use crate::{Parse, SmolStr, SyntaxError, SyntaxKind, TextSize}; +use crate::{Parse, SyntaxError, SyntaxKind, TextSize}; pub(crate) use rowan::{GreenNode, GreenToken, NodeOrToken}; @@ -53,7 +53,7 @@ impl SyntaxTreeBuilder { Parse::new(green, errors) } - pub fn token(&mut self, kind: SyntaxKind, text: SmolStr) { + pub fn token(&mut self, kind: SyntaxKind, text: &str) { let kind = RustLanguage::kind_to_raw(kind); self.inner.token(kind, text) } -- cgit v1.2.3