Skip to content

Commit cbc13ae

Browse files
bors[bot]xNul
andauthored
Merge #10152
10152: feat: Add completion for raw identifiers r=matklad a=nabakin ![rust_analyzer_pr](https://user-images.githubusercontent.com/894305/132110362-c21b713d-acaf-4a6d-9749-ff812172cbce.gif) Adds support for valid Rust completion of raw identifiers. Previously, code completion of fields made via raw identifiers would not re-insert those raw identifiers, resulting in invalid Rust code. Now, code completion of fields made via raw identifiers do re-insert those raw identifiers, resulting in valid Rust code. The same is true for all code completion instances for fields and compatible Rust identifiers. Co-authored-by: Blake Wyatt <[email protected]>
2 parents f0b15e2 + 6c51eca commit cbc13ae

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

crates/ide_completion/src/render.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ide_db::{
1616
helpers::{item_name, SnippetCap},
1717
RootDatabase, SymbolKind,
1818
};
19-
use syntax::TextRange;
19+
use syntax::{SyntaxKind, TextRange};
2020

2121
use crate::{
2222
context::{PathCompletionContext, PathKind},
@@ -91,14 +91,18 @@ pub(crate) fn render_field(
9191
);
9292
item.set_relevance(CompletionRelevance {
9393
type_match: compute_type_match(ctx.completion, ty),
94-
exact_name_match: compute_exact_name_match(ctx.completion, &name),
94+
exact_name_match: compute_exact_name_match(ctx.completion, name.as_str()),
9595
..CompletionRelevance::default()
9696
});
9797
item.kind(SymbolKind::Field)
9898
.detail(ty.display(ctx.db()).to_string())
9999
.set_documentation(field.docs(ctx.db()))
100100
.set_deprecated(is_deprecated)
101-
.lookup_by(name);
101+
.lookup_by(name.as_str());
102+
let is_keyword = SyntaxKind::from_keyword(name.as_str()).is_some();
103+
if is_keyword && !matches!(name.as_str(), "self" | "crate" | "super" | "Self") {
104+
item.insert_text(String::from("r#") + name.as_str());
105+
}
102106
if let Some(_ref_match) = compute_ref_match(ctx.completion, ty) {
103107
// FIXME
104108
// For now we don't properly calculate the edits for ref match
@@ -821,6 +825,23 @@ struct ManualVtable { f: fn(u8, u8) }
821825
fn main() -> ManualVtable {
822826
ManualVtable { f: foo }
823827
}
828+
"#,
829+
);
830+
check_edit(
831+
"type",
832+
r#"
833+
struct RawIdentTable { r#type: u32 }
834+
835+
fn main() -> RawIdentTable {
836+
RawIdentTable { t$0: 42 }
837+
}
838+
"#,
839+
r#"
840+
struct RawIdentTable { r#type: u32 }
841+
842+
fn main() -> RawIdentTable {
843+
RawIdentTable { r#type: 42 }
844+
}
824845
"#,
825846
);
826847
}

0 commit comments

Comments
 (0)