aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/hir/src/lib.rs26
-rw-r--r--crates/hir/src/source_analyzer.rs6
-rw-r--r--crates/hir_ty/src/chalk_ext.rs35
-rw-r--r--crates/hir_ty/src/infer/expr.rs5
-rw-r--r--crates/hir_ty/src/infer/pat.rs6
-rw-r--r--crates/hir_ty/src/infer/unify.rs3
-rw-r--r--crates/hir_ty/src/lib.rs60
-rw-r--r--crates/hir_ty/src/walk.rs26
-rw-r--r--crates/ide/src/typing.rs168
-rw-r--r--crates/rust-analyzer/src/caps.rs2
-rw-r--r--crates/rust-analyzer/src/handlers.rs1
-rw-r--r--crates/syntax/Cargo.toml2
12 files changed, 237 insertions, 103 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 86b36c565..13aaa408a 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -1972,9 +1972,9 @@ impl Type {
1972 pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ { 1972 pub fn type_parameters(&self) -> impl Iterator<Item = Type> + '_ {
1973 self.ty 1973 self.ty
1974 .strip_references() 1974 .strip_references()
1975 .substs() 1975 .as_adt()
1976 .into_iter() 1976 .into_iter()
1977 .flat_map(|substs| substs.iter(&Interner)) 1977 .flat_map(|(_, substs)| substs.iter(&Interner))
1978 .filter_map(|arg| arg.ty(&Interner).cloned()) 1978 .filter_map(|arg| arg.ty(&Interner).cloned())
1979 .map(move |ty| self.derived(ty)) 1979 .map(move |ty| self.derived(ty))
1980 } 1980 }
@@ -2115,18 +2115,22 @@ impl Type {
2115 fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) { 2115 fn walk_type(db: &dyn HirDatabase, type_: &Type, cb: &mut impl FnMut(Type)) {
2116 let ty = type_.ty.strip_references(); 2116 let ty = type_.ty.strip_references();
2117 match ty.kind(&Interner) { 2117 match ty.kind(&Interner) {
2118 TyKind::Adt(..) => { 2118 TyKind::Adt(_, substs) => {
2119 cb(type_.derived(ty.clone())); 2119 cb(type_.derived(ty.clone()));
2120 walk_substs(db, type_, &substs, cb);
2120 } 2121 }
2121 TyKind::AssociatedType(..) => { 2122 TyKind::AssociatedType(_, substs) => {
2122 if let Some(_) = ty.associated_type_parent_trait(db) { 2123 if let Some(_) = ty.associated_type_parent_trait(db) {
2123 cb(type_.derived(ty.clone())); 2124 cb(type_.derived(ty.clone()));
2124 } 2125 }
2126 walk_substs(db, type_, &substs, cb);
2125 } 2127 }
2126 TyKind::OpaqueType(..) => { 2128 TyKind::OpaqueType(_, subst) => {
2127 if let Some(bounds) = ty.impl_trait_bounds(db) { 2129 if let Some(bounds) = ty.impl_trait_bounds(db) {
2128 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb); 2130 walk_bounds(db, &type_.derived(ty.clone()), &bounds, cb);
2129 } 2131 }
2132
2133 walk_substs(db, type_, subst, cb);
2130 } 2134 }
2131 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { 2135 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
2132 if let Some(bounds) = ty.impl_trait_bounds(db) { 2136 if let Some(bounds) = ty.impl_trait_bounds(db) {
@@ -2156,11 +2160,17 @@ impl Type {
2156 walk_type(db, &type_.derived(ty.clone()), cb); 2160 walk_type(db, &type_.derived(ty.clone()), cb);
2157 } 2161 }
2158 2162
2163 TyKind::FnDef(_, substs)
2164 | TyKind::Tuple(_, substs)
2165 | TyKind::Closure(.., substs) => {
2166 walk_substs(db, type_, &substs, cb);
2167 }
2168 TyKind::Function(hir_ty::FnPointer { substitution, .. }) => {
2169 walk_substs(db, type_, &substitution.0, cb);
2170 }
2171
2159 _ => {} 2172 _ => {}
2160 } 2173 }
2161 if let Some(substs) = ty.substs() {
2162 walk_substs(db, type_, &substs, cb);
2163 }
2164 } 2174 }
2165 2175
2166 walk_type(db, self, &mut cb); 2176 walk_type(db, self, &mut cb);
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs
index ce6f3c008..847d2537d 100644
--- a/crates/hir/src/source_analyzer.rs
+++ b/crates/hir/src/source_analyzer.rs
@@ -20,7 +20,7 @@ use hir_def::{
20use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; 20use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
21use hir_ty::{ 21use hir_ty::{
22 diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, 22 diagnostics::{record_literal_missing_fields, record_pattern_missing_fields},
23 InferenceResult, Interner, Substitution, TyLoweringContext, 23 InferenceResult, Interner, Substitution, TyExt, TyLoweringContext,
24}; 24};
25use syntax::{ 25use syntax::{
26 ast::{self, AstNode}, 26 ast::{self, AstNode},
@@ -306,7 +306,7 @@ impl SourceAnalyzer {
306 let infer = self.infer.as_ref()?; 306 let infer = self.infer.as_ref()?;
307 307
308 let expr_id = self.expr_id(db, &literal.clone().into())?; 308 let expr_id = self.expr_id(db, &literal.clone().into())?;
309 let substs = infer.type_of_expr[expr_id].substs()?; 309 let substs = infer.type_of_expr[expr_id].as_adt()?.1;
310 310
311 let (variant, missing_fields, _exhaustive) = 311 let (variant, missing_fields, _exhaustive) =
312 record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; 312 record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?;
@@ -324,7 +324,7 @@ impl SourceAnalyzer {
324 let infer = self.infer.as_ref()?; 324 let infer = self.infer.as_ref()?;
325 325
326 let pat_id = self.pat_id(&pattern.clone().into())?; 326 let pat_id = self.pat_id(&pattern.clone().into())?;
327 let substs = infer.type_of_pat[pat_id].substs()?; 327 let substs = infer.type_of_pat[pat_id].as_adt()?.1;
328 328
329 let (variant, missing_fields, _exhaustive) = 329 let (variant, missing_fields, _exhaustive) =
330 record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; 330 record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?;
diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs
index 8e8a1aa48..6a353423a 100644
--- a/crates/hir_ty/src/chalk_ext.rs
+++ b/crates/hir_ty/src/chalk_ext.rs
@@ -8,7 +8,7 @@ use hir_def::{
8use crate::{ 8use crate::{
9 db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, 9 db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id,
10 from_placeholder_idx, to_chalk_trait_id, AdtId, AliasEq, AliasTy, Binders, CallableDefId, 10 from_placeholder_idx, to_chalk_trait_id, AdtId, AliasEq, AliasTy, Binders, CallableDefId,
11 CallableSig, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause, 11 CallableSig, FnPointer, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause,
12 Substitution, TraitRef, Ty, TyBuilder, TyKind, WhereClause, 12 Substitution, TraitRef, Ty, TyBuilder, TyKind, WhereClause,
13}; 13};
14 14
@@ -34,6 +34,9 @@ pub trait TyExt {
34 34
35 fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>>; 35 fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>>;
36 fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId>; 36 fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId>;
37
38 /// FIXME: Get rid of this, it's not a good abstraction
39 fn equals_ctor(&self, other: &Ty) -> bool;
37} 40}
38 41
39impl TyExt for Ty { 42impl TyExt for Ty {
@@ -238,6 +241,36 @@ impl TyExt for Ty {
238 _ => None, 241 _ => None,
239 } 242 }
240 } 243 }
244
245 fn equals_ctor(&self, other: &Ty) -> bool {
246 match (self.kind(&Interner), other.kind(&Interner)) {
247 (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2,
248 (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_, _), TyKind::Array(_, _)) => {
249 true
250 }
251 (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2,
252 (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2,
253 (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => {
254 ty_id == ty_id2
255 }
256 (TyKind::Foreign(ty_id, ..), TyKind::Foreign(ty_id2, ..)) => ty_id == ty_id2,
257 (TyKind::Closure(id1, _), TyKind::Closure(id2, _)) => id1 == id2,
258 (TyKind::Ref(mutability, ..), TyKind::Ref(mutability2, ..))
259 | (TyKind::Raw(mutability, ..), TyKind::Raw(mutability2, ..)) => {
260 mutability == mutability2
261 }
262 (
263 TyKind::Function(FnPointer { num_binders, sig, .. }),
264 TyKind::Function(FnPointer { num_binders: num_binders2, sig: sig2, .. }),
265 ) => num_binders == num_binders2 && sig == sig2,
266 (TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => {
267 cardinality == cardinality2
268 }
269 (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true,
270 (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2,
271 _ => false,
272 }
273 }
241} 274}
242 275
243pub trait ProjectionTyExt { 276pub trait ProjectionTyExt {
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index 9ab0fa212..9841988c5 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -412,7 +412,10 @@ impl<'a> InferenceContext<'a> {
412 412
413 self.unify(&ty, &expected.ty); 413 self.unify(&ty, &expected.ty);
414 414
415 let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); 415 let substs = ty
416 .as_adt()
417 .map(|(_, s)| s.clone())
418 .unwrap_or_else(|| Substitution::empty(&Interner));
416 let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default(); 419 let field_types = def_id.map(|it| self.db.field_types(it)).unwrap_or_default();
417 let variant_data = def_id.map(|it| it.variant_data(self.db.upcast())); 420 let variant_data = def_id.map(|it| it.variant_data(self.db.upcast()));
418 for field in fields.iter() { 421 for field in fields.iter() {
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs
index e4813c87c..f88d5c5d3 100644
--- a/crates/hir_ty/src/infer/pat.rs
+++ b/crates/hir_ty/src/infer/pat.rs
@@ -33,7 +33,8 @@ impl<'a> InferenceContext<'a> {
33 } 33 }
34 self.unify(&ty, expected); 34 self.unify(&ty, expected);
35 35
36 let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); 36 let substs =
37 ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner));
37 38
38 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); 39 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
39 let (pre, post) = match ellipsis { 40 let (pre, post) = match ellipsis {
@@ -74,7 +75,8 @@ impl<'a> InferenceContext<'a> {
74 75
75 self.unify(&ty, expected); 76 self.unify(&ty, expected);
76 77
77 let substs = ty.substs().cloned().unwrap_or_else(|| Substitution::empty(&Interner)); 78 let substs =
79 ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(&Interner));
78 80
79 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); 81 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
80 for subpat in subpats { 82 for subpat in subpats {
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs
index d717e3375..2ea9dd920 100644
--- a/crates/hir_ty/src/infer/unify.rs
+++ b/crates/hir_ty/src/infer/unify.rs
@@ -8,7 +8,8 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue};
8use super::{DomainGoal, InferenceContext}; 8use super::{DomainGoal, InferenceContext};
9use crate::{ 9use crate::{
10 AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSubst, 10 AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSubst,
11 InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyKind, TypeWalk, WhereClause, 11 InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyExt, TyKind, TypeWalk,
12 WhereClause,
12}; 13};
13 14
14impl<'a> InferenceContext<'a> { 15impl<'a> InferenceContext<'a> {
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index ae3987752..2e851d3e0 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -168,65 +168,7 @@ impl CallableSig {
168 } 168 }
169} 169}
170 170
171impl Ty { 171impl Ty {}
172 pub fn equals_ctor(&self, other: &Ty) -> bool {
173 match (self.kind(&Interner), other.kind(&Interner)) {
174 (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2,
175 (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_, _), TyKind::Array(_, _)) => {
176 true
177 }
178 (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2,
179 (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2,
180 (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => {
181 ty_id == ty_id2
182 }
183 (TyKind::Foreign(ty_id, ..), TyKind::Foreign(ty_id2, ..)) => ty_id == ty_id2,
184 (TyKind::Closure(id1, _), TyKind::Closure(id2, _)) => id1 == id2,
185 (TyKind::Ref(mutability, ..), TyKind::Ref(mutability2, ..))
186 | (TyKind::Raw(mutability, ..), TyKind::Raw(mutability2, ..)) => {
187 mutability == mutability2
188 }
189 (
190 TyKind::Function(FnPointer { num_binders, sig, .. }),
191 TyKind::Function(FnPointer { num_binders: num_binders2, sig: sig2, .. }),
192 ) => num_binders == num_binders2 && sig == sig2,
193 (TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => {
194 cardinality == cardinality2
195 }
196 (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true,
197 (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2,
198 _ => false,
199 }
200 }
201
202 /// Returns the type parameters of this type if it has some (i.e. is an ADT
203 /// or function); so if `self` is `Option<u32>`, this returns the `u32`.
204 pub fn substs(&self) -> Option<&Substitution> {
205 match self.kind(&Interner) {
206 TyKind::Adt(_, substs)
207 | TyKind::FnDef(_, substs)
208 | TyKind::Tuple(_, substs)
209 | TyKind::OpaqueType(_, substs)
210 | TyKind::AssociatedType(_, substs)
211 | TyKind::Closure(.., substs) => Some(substs),
212 TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&substs.0),
213 _ => None,
214 }
215 }
216
217 fn substs_mut(&mut self) -> Option<&mut Substitution> {
218 match self.interned_mut() {
219 TyKind::Adt(_, substs)
220 | TyKind::FnDef(_, substs)
221 | TyKind::Tuple(_, substs)
222 | TyKind::OpaqueType(_, substs)
223 | TyKind::AssociatedType(_, substs)
224 | TyKind::Closure(.., substs) => Some(substs),
225 TyKind::Function(FnPointer { substitution: substs, .. }) => Some(&mut substs.0),
226 _ => None,
227 }
228 }
229}
230 172
231#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] 173#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
232pub enum ImplTraitId { 174pub enum ImplTraitId {
diff --git a/crates/hir_ty/src/walk.rs b/crates/hir_ty/src/walk.rs
index 41ebf6137..91116dcda 100644
--- a/crates/hir_ty/src/walk.rs
+++ b/crates/hir_ty/src/walk.rs
@@ -162,13 +162,15 @@ impl TypeWalk for Ty {
162 TyKind::Function(fn_pointer) => { 162 TyKind::Function(fn_pointer) => {
163 fn_pointer.substitution.0.walk(f); 163 fn_pointer.substitution.0.walk(f);
164 } 164 }
165 _ => { 165 TyKind::Adt(_, substs)
166 if let Some(substs) = self.substs() { 166 | TyKind::FnDef(_, substs)
167 for t in substs.iter(&Interner) { 167 | TyKind::Tuple(_, substs)
168 t.walk(f); 168 | TyKind::OpaqueType(_, substs)
169 } 169 | TyKind::AssociatedType(_, substs)
170 } 170 | TyKind::Closure(.., substs) => {
171 substs.walk(f);
171 } 172 }
173 _ => {}
172 } 174 }
173 f(self); 175 f(self);
174 } 176 }
@@ -199,11 +201,15 @@ impl TypeWalk for Ty {
199 TyKind::Function(fn_pointer) => { 201 TyKind::Function(fn_pointer) => {
200 fn_pointer.substitution.0.walk_mut_binders(f, binders.shifted_in()); 202 fn_pointer.substitution.0.walk_mut_binders(f, binders.shifted_in());
201 } 203 }
202 _ => { 204 TyKind::Adt(_, substs)
203 if let Some(substs) = self.substs_mut() { 205 | TyKind::FnDef(_, substs)
204 substs.walk_mut_binders(f, binders); 206 | TyKind::Tuple(_, substs)
205 } 207 | TyKind::OpaqueType(_, substs)
208 | TyKind::AssociatedType(_, substs)
209 | TyKind::Closure(.., substs) => {
210 substs.walk_mut_binders(f, binders);
206 } 211 }
212 _ => {}
207 } 213 }
208 f(self, binders); 214 f(self, binders);
209 } 215 }
diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs
index 11408d445..1378048e5 100644
--- a/crates/ide/src/typing.rs
+++ b/crates/ide/src/typing.rs
@@ -22,18 +22,19 @@ use ide_db::{
22use syntax::{ 22use syntax::{
23 algo::find_node_at_offset, 23 algo::find_node_at_offset,
24 ast::{self, edit::IndentLevel, AstToken}, 24 ast::{self, edit::IndentLevel, AstToken},
25 AstNode, SourceFile, 25 AstNode, Parse, SourceFile,
26 SyntaxKind::{FIELD_EXPR, METHOD_CALL_EXPR}, 26 SyntaxKind::{FIELD_EXPR, METHOD_CALL_EXPR},
27 TextRange, TextSize, 27 TextRange, TextSize,
28}; 28};
29 29
30use text_edit::TextEdit; 30use text_edit::{Indel, TextEdit};
31 31
32use crate::SourceChange; 32use crate::SourceChange;
33 33
34pub(crate) use on_enter::on_enter; 34pub(crate) use on_enter::on_enter;
35 35
36pub(crate) const TRIGGER_CHARS: &str = ".=>"; 36// Don't forget to add new trigger characters to `server_capabilities` in `caps.rs`.
37pub(crate) const TRIGGER_CHARS: &str = ".=>{";
37 38
38// Feature: On Typing Assists 39// Feature: On Typing Assists
39// 40//
@@ -41,6 +42,7 @@ pub(crate) const TRIGGER_CHARS: &str = ".=>";
41// 42//
42// - typing `let =` tries to smartly add `;` if `=` is followed by an existing expression 43// - typing `let =` tries to smartly add `;` if `=` is followed by an existing expression
43// - typing `.` in a chain method call auto-indents 44// - typing `.` in a chain method call auto-indents
45// - typing `{` in front of an expression inserts a closing `}` after the expression
44// 46//
45// VS Code:: 47// VS Code::
46// 48//
@@ -57,28 +59,79 @@ pub(crate) fn on_char_typed(
57 position: FilePosition, 59 position: FilePosition,
58 char_typed: char, 60 char_typed: char,
59) -> Option<SourceChange> { 61) -> Option<SourceChange> {
60 assert!(TRIGGER_CHARS.contains(char_typed)); 62 if !stdx::always!(TRIGGER_CHARS.contains(char_typed)) {
61 let file = &db.parse(position.file_id).tree(); 63 return None;
62 assert_eq!(file.syntax().text().char_at(position.offset), Some(char_typed)); 64 }
65 let file = &db.parse(position.file_id);
66 if !stdx::always!(file.tree().syntax().text().char_at(position.offset) == Some(char_typed)) {
67 return None;
68 }
63 let edit = on_char_typed_inner(file, position.offset, char_typed)?; 69 let edit = on_char_typed_inner(file, position.offset, char_typed)?;
64 Some(SourceChange::from_text_edit(position.file_id, edit)) 70 Some(SourceChange::from_text_edit(position.file_id, edit))
65} 71}
66 72
67fn on_char_typed_inner(file: &SourceFile, offset: TextSize, char_typed: char) -> Option<TextEdit> { 73fn on_char_typed_inner(
68 assert!(TRIGGER_CHARS.contains(char_typed)); 74 file: &Parse<SourceFile>,
75 offset: TextSize,
76 char_typed: char,
77) -> Option<TextEdit> {
78 if !stdx::always!(TRIGGER_CHARS.contains(char_typed)) {
79 return None;
80 }
69 match char_typed { 81 match char_typed {
70 '.' => on_dot_typed(file, offset), 82 '.' => on_dot_typed(&file.tree(), offset),
71 '=' => on_eq_typed(file, offset), 83 '=' => on_eq_typed(&file.tree(), offset),
72 '>' => on_arrow_typed(file, offset), 84 '>' => on_arrow_typed(&file.tree(), offset),
85 '{' => on_opening_brace_typed(file, offset),
73 _ => unreachable!(), 86 _ => unreachable!(),
74 } 87 }
75} 88}
76 89
90/// Inserts a closing `}` when the user types an opening `{`, wrapping an existing expression in a
91/// block.
92fn on_opening_brace_typed(file: &Parse<SourceFile>, offset: TextSize) -> Option<TextEdit> {
93 if !stdx::always!(file.tree().syntax().text().char_at(offset) == Some('{')) {
94 return None;
95 }
96
97 let brace_token = file.tree().syntax().token_at_offset(offset).right_biased()?;
98
99 // Remove the `{` to get a better parse tree, and reparse
100 let file = file.reparse(&Indel::delete(brace_token.text_range()));
101
102 let mut expr: ast::Expr = find_node_at_offset(file.tree().syntax(), offset)?;
103 if expr.syntax().text_range().start() != offset {
104 return None;
105 }
106
107 // Enclose the outermost expression starting at `offset`
108 while let Some(parent) = expr.syntax().parent() {
109 if parent.text_range().start() != expr.syntax().text_range().start() {
110 break;
111 }
112
113 match ast::Expr::cast(parent) {
114 Some(parent) => expr = parent,
115 None => break,
116 }
117 }
118
119 // If it's a statement in a block, we don't know how many statements should be included
120 if ast::ExprStmt::can_cast(expr.syntax().parent()?.kind()) {
121 return None;
122 }
123
124 // Insert `}` right after the expression.
125 Some(TextEdit::insert(expr.syntax().text_range().end() + TextSize::of("{"), "}".to_string()))
126}
127
77/// Returns an edit which should be applied after `=` was typed. Primarily, 128/// Returns an edit which should be applied after `=` was typed. Primarily,
78/// this works when adding `let =`. 129/// this works when adding `let =`.
79// FIXME: use a snippet completion instead of this hack here. 130// FIXME: use a snippet completion instead of this hack here.
80fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> { 131fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
81 assert_eq!(file.syntax().text().char_at(offset), Some('=')); 132 if !stdx::always!(file.syntax().text().char_at(offset) == Some('=')) {
133 return None;
134 }
82 let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?; 135 let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?;
83 if let_stmt.semicolon_token().is_some() { 136 if let_stmt.semicolon_token().is_some() {
84 return None; 137 return None;
@@ -100,7 +153,9 @@ fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
100 153
101/// Returns an edit which should be applied when a dot ('.') is typed on a blank line, indenting the line appropriately. 154/// Returns an edit which should be applied when a dot ('.') is typed on a blank line, indenting the line appropriately.
102fn on_dot_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> { 155fn on_dot_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
103 assert_eq!(file.syntax().text().char_at(offset), Some('.')); 156 if !stdx::always!(file.syntax().text().char_at(offset) == Some('.')) {
157 return None;
158 }
104 let whitespace = 159 let whitespace =
105 file.syntax().token_at_offset(offset).left_biased().and_then(ast::Whitespace::cast)?; 160 file.syntax().token_at_offset(offset).left_biased().and_then(ast::Whitespace::cast)?;
106 161
@@ -129,7 +184,9 @@ fn on_dot_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
129/// Adds a space after an arrow when `fn foo() { ... }` is turned into `fn foo() -> { ... }` 184/// Adds a space after an arrow when `fn foo() { ... }` is turned into `fn foo() -> { ... }`
130fn on_arrow_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> { 185fn on_arrow_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
131 let file_text = file.syntax().text(); 186 let file_text = file.syntax().text();
132 assert_eq!(file_text.char_at(offset), Some('>')); 187 if !stdx::always!(file_text.char_at(offset) == Some('>')) {
188 return None;
189 }
133 let after_arrow = offset + TextSize::of('>'); 190 let after_arrow = offset + TextSize::of('>');
134 if file_text.char_at(after_arrow) != Some('{') { 191 if file_text.char_at(after_arrow) != Some('{') {
135 return None; 192 return None;
@@ -152,7 +209,7 @@ mod tests {
152 let edit = TextEdit::insert(offset, char_typed.to_string()); 209 let edit = TextEdit::insert(offset, char_typed.to_string());
153 edit.apply(&mut before); 210 edit.apply(&mut before);
154 let parse = SourceFile::parse(&before); 211 let parse = SourceFile::parse(&before);
155 on_char_typed_inner(&parse.tree(), offset, char_typed).map(|it| { 212 on_char_typed_inner(&parse, offset, char_typed).map(|it| {
156 it.apply(&mut before); 213 it.apply(&mut before);
157 before.to_string() 214 before.to_string()
158 }) 215 })
@@ -373,4 +430,85 @@ fn main() {
373 fn adds_space_after_return_type() { 430 fn adds_space_after_return_type() {
374 type_char('>', "fn foo() -$0{ 92 }", "fn foo() -> { 92 }") 431 type_char('>', "fn foo() -$0{ 92 }", "fn foo() -> { 92 }")
375 } 432 }
433
434 #[test]
435 fn adds_closing_brace() {
436 type_char(
437 '{',
438 r#"
439fn f() { match () { _ => $0() } }
440 "#,
441 r#"
442fn f() { match () { _ => {()} } }
443 "#,
444 );
445 type_char(
446 '{',
447 r#"
448fn f() { $0() }
449 "#,
450 r#"
451fn f() { {()} }
452 "#,
453 );
454 type_char(
455 '{',
456 r#"
457fn f() { let x = $0(); }
458 "#,
459 r#"
460fn f() { let x = {()}; }
461 "#,
462 );
463 type_char(
464 '{',
465 r#"
466fn f() { let x = $0a.b(); }
467 "#,
468 r#"
469fn f() { let x = {a.b()}; }
470 "#,
471 );
472 type_char(
473 '{',
474 r#"
475const S: () = $0();
476fn f() {}
477 "#,
478 r#"
479const S: () = {()};
480fn f() {}
481 "#,
482 );
483 type_char(
484 '{',
485 r#"
486const S: () = $0a.b();
487fn f() {}
488 "#,
489 r#"
490const S: () = {a.b()};
491fn f() {}
492 "#,
493 );
494 type_char(
495 '{',
496 r#"
497fn f() {
498 match x {
499 0 => $0(),
500 1 => (),
501 }
502}
503 "#,
504 r#"
505fn f() {
506 match x {
507 0 => {()},
508 1 => (),
509 }
510}
511 "#,
512 );
513 }
376} 514}
diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs
index 7a5bcb8c7..3c87782f2 100644
--- a/crates/rust-analyzer/src/caps.rs
+++ b/crates/rust-analyzer/src/caps.rs
@@ -57,7 +57,7 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti
57 document_range_formatting_provider: None, 57 document_range_formatting_provider: None,
58 document_on_type_formatting_provider: Some(DocumentOnTypeFormattingOptions { 58 document_on_type_formatting_provider: Some(DocumentOnTypeFormattingOptions {
59 first_trigger_character: "=".to_string(), 59 first_trigger_character: "=".to_string(),
60 more_trigger_character: Some(vec![".".to_string(), ">".to_string()]), 60 more_trigger_character: Some(vec![".".to_string(), ">".to_string(), "{".to_string()]),
61 }), 61 }),
62 selection_range_provider: Some(SelectionRangeProviderCapability::Simple(true)), 62 selection_range_provider: Some(SelectionRangeProviderCapability::Simple(true)),
63 folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)), 63 folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)),
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index 4d10a2ead..31d8c487b 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -231,7 +231,6 @@ pub(crate) fn handle_on_enter(
231 Ok(Some(edit)) 231 Ok(Some(edit))
232} 232}
233 233
234// Don't forget to add new trigger characters to `ServerCapabilities` in `caps.rs`.
235pub(crate) fn handle_on_type_formatting( 234pub(crate) fn handle_on_type_formatting(
236 snap: GlobalStateSnapshot, 235 snap: GlobalStateSnapshot,
237 params: lsp_types::DocumentOnTypeFormattingParams, 236 params: lsp_types::DocumentOnTypeFormattingParams,
diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml
index 10024366e..f09cdf28a 100644
--- a/crates/syntax/Cargo.toml
+++ b/crates/syntax/Cargo.toml
@@ -14,7 +14,7 @@ doctest = false
14cov-mark = { version = "1.1", features = ["thread-local"] } 14cov-mark = { version = "1.1", features = ["thread-local"] }
15itertools = "0.10.0" 15itertools = "0.10.0"
16rowan = "0.13.0-pre.3" 16rowan = "0.13.0-pre.3"
17rustc_lexer = { version = "710.0.0", package = "rustc-ap-rustc_lexer" } 17rustc_lexer = { version = "714.0.0", package = "rustc-ap-rustc_lexer" }
18rustc-hash = "1.1.0" 18rustc-hash = "1.1.0"
19arrayvec = "0.7" 19arrayvec = "0.7"
20once_cell = "1.3.1" 20once_cell = "1.3.1"