@@ -116,7 +116,8 @@ pub(crate) fn render_field(
116
116
ty : & hir:: Type ,
117
117
) -> CompletionItem {
118
118
let is_deprecated = ctx. is_deprecated ( field) ;
119
- let name = field. name ( ctx. db ( ) ) . to_smol_str ( ) ;
119
+ let name = field. name ( ctx. db ( ) ) ;
120
+ let ( name, escaped_name) = ( name. to_smol_str ( ) , name. escaped ( ) . to_smol_str ( ) ) ;
120
121
let mut item = CompletionItem :: new (
121
122
SymbolKind :: Field ,
122
123
ctx. source_range ( ) ,
@@ -131,10 +132,7 @@ pub(crate) fn render_field(
131
132
. set_documentation ( field. docs ( ctx. db ( ) ) )
132
133
. set_deprecated ( is_deprecated)
133
134
. lookup_by ( name. clone ( ) ) ;
134
- let is_keyword = SyntaxKind :: from_keyword ( name. as_str ( ) ) . is_some ( ) ;
135
- if is_keyword && !matches ! ( name. as_str( ) , "self" | "crate" | "super" | "Self" ) {
136
- item. insert_text ( format ! ( "r#{}" , name) ) ;
137
- }
135
+ item. insert_text ( escaped_name) ;
138
136
if let Some ( receiver) = & dot_access. receiver {
139
137
if let Some ( ref_match) = compute_ref_match ( ctx. completion , ty) {
140
138
item. ref_match ( ref_match, receiver. syntax ( ) . text_range ( ) . start ( ) ) ;
@@ -235,7 +233,7 @@ fn render_resolution_pat(
235
233
_ => ( ) ,
236
234
}
237
235
238
- render_resolution_simple_ ( ctx, local_name, import_to_add, resolution)
236
+ render_resolution_simple_ ( ctx, & local_name, import_to_add, resolution)
239
237
}
240
238
241
239
fn render_resolution_path (
@@ -274,7 +272,10 @@ fn render_resolution_path(
274
272
let config = completion. config ;
275
273
276
274
let name = local_name. to_smol_str ( ) ;
277
- let mut item = render_resolution_simple_ ( ctx, local_name, import_to_add, resolution) ;
275
+ let mut item = render_resolution_simple_ ( ctx, & local_name, import_to_add, resolution) ;
276
+ if local_name. escaped ( ) . is_escaped ( ) {
277
+ item. insert_text ( local_name. escaped ( ) . to_smol_str ( ) ) ;
278
+ }
278
279
// Add `<>` for generic types
279
280
let type_path_no_ty_args = matches ! (
280
281
path_ctx,
@@ -295,7 +296,7 @@ fn render_resolution_path(
295
296
item. lookup_by ( name. clone ( ) )
296
297
. label ( SmolStr :: from_iter ( [ & name, "<…>" ] ) )
297
298
. trigger_call_info ( )
298
- . insert_snippet ( cap, format ! ( "{}<$0>" , name ) ) ;
299
+ . insert_snippet ( cap, format ! ( "{}<$0>" , local_name . escaped ( ) ) ) ;
299
300
}
300
301
}
301
302
}
@@ -321,7 +322,7 @@ fn render_resolution_path(
321
322
322
323
fn render_resolution_simple_ (
323
324
ctx : RenderContext < ' _ > ,
324
- local_name : hir:: Name ,
325
+ local_name : & hir:: Name ,
325
326
import_to_add : Option < LocatedImport > ,
326
327
resolution : ScopeDef ,
327
328
) -> Builder {
@@ -1725,4 +1726,149 @@ fn f() {
1725
1726
"# ] ] ,
1726
1727
) ;
1727
1728
}
1729
+
1730
+ #[ test]
1731
+ fn completes_struct_with_raw_identifier ( ) {
1732
+ check_edit (
1733
+ "type" ,
1734
+ r#"
1735
+ mod m { pub struct r#type {} }
1736
+ fn main() {
1737
+ let r#type = m::t$0;
1738
+ }
1739
+ "# ,
1740
+ r#"
1741
+ mod m { pub struct r#type {} }
1742
+ fn main() {
1743
+ let r#type = m::r#type;
1744
+ }
1745
+ "# ,
1746
+ )
1747
+ }
1748
+
1749
+ #[ test]
1750
+ fn completes_fn_with_raw_identifier ( ) {
1751
+ check_edit (
1752
+ "type" ,
1753
+ r#"
1754
+ mod m { pub fn r#type {} }
1755
+ fn main() {
1756
+ m::t$0
1757
+ }
1758
+ "# ,
1759
+ r#"
1760
+ mod m { pub fn r#type {} }
1761
+ fn main() {
1762
+ m::r#type()$0
1763
+ }
1764
+ "# ,
1765
+ )
1766
+ }
1767
+
1768
+ #[ test]
1769
+ fn completes_macro_with_raw_identifier ( ) {
1770
+ check_edit (
1771
+ "let!" ,
1772
+ r#"
1773
+ macro_rules! r#let { () => {} }
1774
+ fn main() {
1775
+ $0
1776
+ }
1777
+ "# ,
1778
+ r#"
1779
+ macro_rules! r#let { () => {} }
1780
+ fn main() {
1781
+ r#let!($0)
1782
+ }
1783
+ "# ,
1784
+ )
1785
+ }
1786
+
1787
+ #[ test]
1788
+ fn completes_variant_with_raw_identifier ( ) {
1789
+ check_edit (
1790
+ "type" ,
1791
+ r#"
1792
+ enum A { r#type }
1793
+ fn main() {
1794
+ let a = A::t$0
1795
+ }
1796
+ "# ,
1797
+ r#"
1798
+ enum A { r#type }
1799
+ fn main() {
1800
+ let a = A::r#type$0
1801
+ }
1802
+ "# ,
1803
+ )
1804
+ }
1805
+
1806
+ #[ test]
1807
+ fn completes_field_with_raw_identifier ( ) {
1808
+ check_edit (
1809
+ "fn" ,
1810
+ r#"
1811
+ mod r#type {
1812
+ pub struct r#struct {
1813
+ pub r#fn: u32
1814
+ }
1815
+ }
1816
+
1817
+ fn main() {
1818
+ let a = r#type::r#struct {};
1819
+ a.$0
1820
+ }
1821
+ "# ,
1822
+ r#"
1823
+ mod r#type {
1824
+ pub struct r#struct {
1825
+ pub r#fn: u32
1826
+ }
1827
+ }
1828
+
1829
+ fn main() {
1830
+ let a = r#type::r#struct {};
1831
+ a.r#fn
1832
+ }
1833
+ "# ,
1834
+ )
1835
+ }
1836
+
1837
+ #[ test]
1838
+ fn completes_const_with_raw_identifier ( ) {
1839
+ check_edit (
1840
+ "type" ,
1841
+ r#"
1842
+ struct r#struct {}
1843
+ impl r#struct { pub const r#type: u8 = 1; }
1844
+ fn main() {
1845
+ r#struct::t$0
1846
+ }
1847
+ "# ,
1848
+ r#"
1849
+ struct r#struct {}
1850
+ impl r#struct { pub const r#type: u8 = 1; }
1851
+ fn main() {
1852
+ r#struct::r#type
1853
+ }
1854
+ "# ,
1855
+ )
1856
+ }
1857
+
1858
+ #[ test]
1859
+ fn completes_type_alias_with_raw_identifier ( ) {
1860
+ check_edit (
1861
+ "type type" ,
1862
+ r#"
1863
+ struct r#struct {}
1864
+ trait r#trait { type r#type; }
1865
+ impl r#trait for r#struct { type t$0 }
1866
+ "# ,
1867
+ r#"
1868
+ struct r#struct {}
1869
+ trait r#trait { type r#type; }
1870
+ impl r#trait for r#struct { type r#type = $0; }
1871
+ "# ,
1872
+ )
1873
+ }
1728
1874
}
0 commit comments