mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Replace some String usages with SmolStr in completions
This commit is contained in:
parent
439a8194b0
commit
2f5afba9f8
18 changed files with 99 additions and 111 deletions
|
@ -2,10 +2,7 @@
|
|||
|
||||
use hir::{AsAssocItem, HasSource};
|
||||
use ide_db::SymbolKind;
|
||||
use syntax::{
|
||||
ast::{Const, HasName},
|
||||
display::const_label,
|
||||
};
|
||||
use syntax::{ast::Const, display::const_label};
|
||||
|
||||
use crate::{item::CompletionItem, render::RenderContext};
|
||||
|
||||
|
@ -27,7 +24,7 @@ impl<'a> ConstRender<'a> {
|
|||
}
|
||||
|
||||
fn render(self) -> Option<CompletionItem> {
|
||||
let name = self.name()?;
|
||||
let name = self.const_.name(self.ctx.db())?.to_smol_str();
|
||||
let detail = self.detail();
|
||||
|
||||
let mut item =
|
||||
|
@ -42,7 +39,7 @@ impl<'a> ConstRender<'a> {
|
|||
let db = self.ctx.db();
|
||||
if let Some(actm) = self.const_.as_assoc_item(db) {
|
||||
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
|
||||
item.trait_name(trt.name(db).to_string());
|
||||
item.trait_name(trt.name(db).to_smol_str());
|
||||
item.insert_text(name);
|
||||
}
|
||||
}
|
||||
|
@ -50,10 +47,6 @@ impl<'a> ConstRender<'a> {
|
|||
Some(item.build())
|
||||
}
|
||||
|
||||
fn name(&self) -> Option<String> {
|
||||
self.ast_node.name().map(|name| name.text().to_string())
|
||||
}
|
||||
|
||||
fn detail(&self) -> String {
|
||||
const_label(&self.ast_node)
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ pub(crate) fn render_method(
|
|||
#[derive(Debug)]
|
||||
struct FunctionRender<'a> {
|
||||
ctx: RenderContext<'a>,
|
||||
name: String,
|
||||
name: hir::Name,
|
||||
receiver: Option<hir::Name>,
|
||||
func: hir::Function,
|
||||
/// NB: having `ast::Fn` here might or might not be a good idea. The problem
|
||||
|
@ -67,7 +67,7 @@ impl<'a> FunctionRender<'a> {
|
|||
fn_: hir::Function,
|
||||
is_method: bool,
|
||||
) -> Option<FunctionRender<'a>> {
|
||||
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db())).to_string();
|
||||
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()));
|
||||
let ast_node = fn_.source(ctx.db())?.value;
|
||||
|
||||
Some(FunctionRender { ctx, name, receiver, func: fn_, ast_node, is_method })
|
||||
|
@ -77,7 +77,7 @@ impl<'a> FunctionRender<'a> {
|
|||
let params = self.params();
|
||||
let call = match &self.receiver {
|
||||
Some(receiver) => format!("{}.{}", receiver, &self.name),
|
||||
None => self.name.clone(),
|
||||
None => self.name.to_string(),
|
||||
};
|
||||
let mut item = CompletionItem::new(self.kind(), self.ctx.source_range(), call.clone());
|
||||
item.set_documentation(self.ctx.docs(self.func))
|
||||
|
@ -91,7 +91,7 @@ impl<'a> FunctionRender<'a> {
|
|||
let db = self.ctx.db();
|
||||
if let Some(actm) = self.func.as_assoc_item(db) {
|
||||
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
|
||||
item.trait_name(trt.name(db).to_string());
|
||||
item.trait_name(trt.name(db).to_smol_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ impl<'a> FunctionRender<'a> {
|
|||
if let Some(import_to_add) = import_to_add {
|
||||
item.add_import(import_to_add);
|
||||
}
|
||||
item.lookup_by(self.name);
|
||||
item.lookup_by(self.name.to_smol_str());
|
||||
|
||||
let ret_type = self.func.ret_type(self.ctx.db());
|
||||
item.set_relevance(CompletionRelevance {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use hir::HasSource;
|
||||
use ide_db::SymbolKind;
|
||||
use syntax::display::macro_label;
|
||||
use syntax::{display::macro_label, SmolStr};
|
||||
|
||||
use crate::{
|
||||
context::CallKind,
|
||||
|
@ -23,7 +23,7 @@ pub(crate) fn render_macro(
|
|||
#[derive(Debug)]
|
||||
struct MacroRender<'a> {
|
||||
ctx: RenderContext<'a>,
|
||||
name: String,
|
||||
name: SmolStr,
|
||||
macro_: hir::MacroDef,
|
||||
docs: Option<hir::Documentation>,
|
||||
bra: &'static str,
|
||||
|
@ -32,7 +32,7 @@ struct MacroRender<'a> {
|
|||
|
||||
impl<'a> MacroRender<'a> {
|
||||
fn new(ctx: RenderContext<'a>, name: hir::Name, macro_: hir::MacroDef) -> MacroRender<'a> {
|
||||
let name = name.to_string();
|
||||
let name = name.to_smol_str();
|
||||
let docs = ctx.docs(macro_);
|
||||
let docs_str = docs.as_ref().map_or("", |s| s.as_str());
|
||||
let (bra, ket) = guess_macro_braces(&name, docs_str);
|
||||
|
@ -47,7 +47,7 @@ impl<'a> MacroRender<'a> {
|
|||
} else {
|
||||
Some(self.ctx.source_range())
|
||||
}?;
|
||||
let mut item = CompletionItem::new(SymbolKind::Macro, source_range, &self.label());
|
||||
let mut item = CompletionItem::new(SymbolKind::Macro, source_range, self.label());
|
||||
item.set_documentation(self.docs.clone())
|
||||
.set_deprecated(self.ctx.is_deprecated(self.macro_))
|
||||
.set_detail(self.detail());
|
||||
|
@ -72,7 +72,7 @@ impl<'a> MacroRender<'a> {
|
|||
}
|
||||
_ => {
|
||||
cov_mark::hit!(dont_insert_macro_call_parens_unncessary);
|
||||
item.insert_text(&self.name);
|
||||
item.insert_text(&*self.name);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -84,18 +84,18 @@ impl<'a> MacroRender<'a> {
|
|||
&& !matches!(self.ctx.completion.path_call_kind(), Some(CallKind::Mac))
|
||||
}
|
||||
|
||||
fn label(&self) -> String {
|
||||
fn label(&self) -> SmolStr {
|
||||
if self.needs_bang() && self.ctx.snippet_cap().is_some() {
|
||||
format!("{}!{}…{}", self.name, self.bra, self.ket)
|
||||
SmolStr::from_iter([&*self.name, "!", self.bra, "…", self.ket])
|
||||
} else if self.macro_.kind() == hir::MacroKind::Derive {
|
||||
self.name.to_string()
|
||||
self.name.clone()
|
||||
} else {
|
||||
self.banged_name()
|
||||
}
|
||||
}
|
||||
|
||||
fn banged_name(&self) -> String {
|
||||
format!("{}!", self.name)
|
||||
fn banged_name(&self) -> SmolStr {
|
||||
SmolStr::from_iter([&*self.name, "!"])
|
||||
}
|
||||
|
||||
fn detail(&self) -> Option<String> {
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
use hir::{db::HirDatabase, HasAttrs, HasVisibility, Name, StructKind};
|
||||
use ide_db::helpers::SnippetCap;
|
||||
use itertools::Itertools;
|
||||
use syntax::SmolStr;
|
||||
|
||||
use crate::{
|
||||
context::{ParamKind, PatternContext},
|
||||
|
@ -25,7 +26,7 @@ pub(crate) fn render_struct_pat(
|
|||
return None;
|
||||
}
|
||||
|
||||
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_string();
|
||||
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_smol_str();
|
||||
let pat = render_pat(&ctx, &name, strukt.kind(ctx.db()), &visible_fields, fields_omitted)?;
|
||||
|
||||
Some(build_completion(ctx, name, pat, strukt))
|
||||
|
@ -43,8 +44,8 @@ pub(crate) fn render_variant_pat(
|
|||
let (visible_fields, fields_omitted) = visible_fields(&ctx, &fields, variant)?;
|
||||
|
||||
let name = match &path {
|
||||
Some(path) => path.to_string(),
|
||||
None => local_name.unwrap_or_else(|| variant.name(ctx.db())).to_string(),
|
||||
Some(path) => path.to_string().into(),
|
||||
None => local_name.unwrap_or_else(|| variant.name(ctx.db())).to_smol_str(),
|
||||
};
|
||||
let pat = render_pat(&ctx, &name, variant.kind(ctx.db()), &visible_fields, fields_omitted)?;
|
||||
|
||||
|
@ -53,7 +54,7 @@ pub(crate) fn render_variant_pat(
|
|||
|
||||
fn build_completion(
|
||||
ctx: RenderContext<'_>,
|
||||
name: String,
|
||||
name: SmolStr,
|
||||
pat: String,
|
||||
def: impl HasAttrs + Copy,
|
||||
) -> CompletionItem {
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
use hir::{db::HirDatabase, HasAttrs, HasVisibility, Name, StructKind};
|
||||
use ide_db::helpers::SnippetCap;
|
||||
use itertools::Itertools;
|
||||
use syntax::SmolStr;
|
||||
|
||||
use crate::{render::RenderContext, CompletionItem, CompletionItemKind};
|
||||
|
||||
|
@ -21,7 +22,7 @@ pub(crate) fn render_struct_literal(
|
|||
return None;
|
||||
}
|
||||
|
||||
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_string();
|
||||
let name = local_name.unwrap_or_else(|| strukt.name(ctx.db())).to_smol_str();
|
||||
let literal = render_literal(&ctx, &name, strukt.kind(ctx.db()), &visible_fields)?;
|
||||
|
||||
Some(build_completion(ctx, name, literal, strukt))
|
||||
|
@ -29,12 +30,15 @@ pub(crate) fn render_struct_literal(
|
|||
|
||||
fn build_completion(
|
||||
ctx: RenderContext<'_>,
|
||||
name: String,
|
||||
name: SmolStr,
|
||||
literal: String,
|
||||
def: impl HasAttrs + Copy,
|
||||
) -> CompletionItem {
|
||||
let mut item =
|
||||
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), name + " {…}");
|
||||
let mut item = CompletionItem::new(
|
||||
CompletionItemKind::Snippet,
|
||||
ctx.source_range(),
|
||||
SmolStr::from_iter([&name, " {…}"]),
|
||||
);
|
||||
item.set_documentation(ctx.docs(def)).set_deprecated(ctx.is_deprecated(def)).detail(&literal);
|
||||
match ctx.snippet_cap() {
|
||||
Some(snippet_cap) => item.insert_snippet(snippet_cap, literal),
|
||||
|
|
|
@ -58,7 +58,7 @@ impl<'a> TypeAliasRender<'a> {
|
|||
let db = self.ctx.db();
|
||||
if let Some(actm) = self.type_alias.as_assoc_item(db) {
|
||||
if let Some(trt) = actm.containing_trait_or_trait_impl(db) {
|
||||
item.trait_name(trt.name(db).to_string());
|
||||
item.trait_name(trt.name(db).to_smol_str());
|
||||
item.insert_text(name);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue