diff options
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r-- | crates/ra_ide/src/diagnostics.rs | 10 | ||||
-rw-r--r-- | crates/ra_ide/src/folding_ranges.rs | 23 | ||||
-rw-r--r-- | crates/ra_ide/src/hover.rs | 31 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting.rs | 67 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tests.rs | 21 |
5 files changed, 137 insertions, 15 deletions
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index dd8a7ffd9..73c0b8275 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs | |||
@@ -78,8 +78,10 @@ pub(crate) fn diagnostics( | |||
78 | } else { | 78 | } else { |
79 | let mut field_list = d.ast(db); | 79 | let mut field_list = d.ast(db); |
80 | for f in d.missed_fields.iter() { | 80 | for f in d.missed_fields.iter() { |
81 | let field = | 81 | let field = make::record_expr_field( |
82 | make::record_field(make::name_ref(&f.to_string()), Some(make::expr_unit())); | 82 | make::name_ref(&f.to_string()), |
83 | Some(make::expr_unit()), | ||
84 | ); | ||
83 | field_list = field_list.append_field(&field); | 85 | field_list = field_list.append_field(&field); |
84 | } | 86 | } |
85 | 87 | ||
@@ -178,9 +180,9 @@ fn missing_struct_field_fix( | |||
178 | if new_field_type.is_unknown() { | 180 | if new_field_type.is_unknown() { |
179 | return None; | 181 | return None; |
180 | } | 182 | } |
181 | let new_field = make::record_field_def( | 183 | let new_field = make::record_field( |
182 | record_expr.field_name()?, | 184 | record_expr.field_name()?, |
183 | make::type_ref(&new_field_type.display_source_code(sema.db, module.into()).ok()?), | 185 | make::ty(&new_field_type.display_source_code(sema.db, module.into()).ok()?), |
184 | ); | 186 | ); |
185 | 187 | ||
186 | let last_field = record_fields.fields().last()?; | 188 | let last_field = record_fields.fields().last()?; |
diff --git a/crates/ra_ide/src/folding_ranges.rs b/crates/ra_ide/src/folding_ranges.rs index 903c34996..0fbc9babd 100644 --- a/crates/ra_ide/src/folding_ranges.rs +++ b/crates/ra_ide/src/folding_ranges.rs | |||
@@ -85,7 +85,8 @@ fn fold_kind(kind: SyntaxKind) -> Option<FoldKind> { | |||
85 | COMMENT => Some(FoldKind::Comment), | 85 | COMMENT => Some(FoldKind::Comment), |
86 | USE => Some(FoldKind::Imports), | 86 | USE => Some(FoldKind::Imports), |
87 | ARG_LIST | PARAM_LIST => Some(FoldKind::ArgList), | 87 | ARG_LIST | PARAM_LIST => Some(FoldKind::ArgList), |
88 | RECORD_FIELD_LIST | 88 | ASSOC_ITEM_LIST |
89 | | RECORD_FIELD_LIST | ||
89 | | RECORD_PAT_FIELD_LIST | 90 | | RECORD_PAT_FIELD_LIST |
90 | | RECORD_EXPR_FIELD_LIST | 91 | | RECORD_EXPR_FIELD_LIST |
91 | | ITEM_LIST | 92 | | ITEM_LIST |
@@ -337,6 +338,26 @@ fn main() <fold block>{ | |||
337 | } | 338 | } |
338 | 339 | ||
339 | #[test] | 340 | #[test] |
341 | fn test_folds_structs() { | ||
342 | check( | ||
343 | r#" | ||
344 | struct Foo <fold block>{ | ||
345 | }</fold> | ||
346 | "#, | ||
347 | ); | ||
348 | } | ||
349 | |||
350 | #[test] | ||
351 | fn test_folds_traits() { | ||
352 | check( | ||
353 | r#" | ||
354 | trait Foo <fold block>{ | ||
355 | }</fold> | ||
356 | "#, | ||
357 | ); | ||
358 | } | ||
359 | |||
360 | #[test] | ||
340 | fn test_folds_macros() { | 361 | fn test_folds_macros() { |
341 | check( | 362 | check( |
342 | r#" | 363 | r#" |
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index aa48cb412..385e3e64e 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs | |||
@@ -509,6 +509,37 @@ fn main() { } | |||
509 | } | 509 | } |
510 | 510 | ||
511 | #[test] | 511 | #[test] |
512 | fn hover_shows_fn_doc() { | ||
513 | check( | ||
514 | r#" | ||
515 | /// # Example | ||
516 | /// ``` | ||
517 | /// # use std::path::Path; | ||
518 | /// # | ||
519 | /// foo(Path::new("hello, world!")) | ||
520 | /// ``` | ||
521 | pub fn foo<|>(_: &Path) {} | ||
522 | |||
523 | fn main() { } | ||
524 | "#, | ||
525 | expect![[r#" | ||
526 | *foo* | ||
527 | ```rust | ||
528 | pub fn foo(_: &Path) | ||
529 | ``` | ||
530 | ___ | ||
531 | |||
532 | # Example | ||
533 | ``` | ||
534 | # use std::path::Path; | ||
535 | # | ||
536 | foo(Path::new("hello, world!")) | ||
537 | ``` | ||
538 | "#]], | ||
539 | ); | ||
540 | } | ||
541 | |||
542 | #[test] | ||
512 | fn hover_shows_struct_field_info() { | 543 | fn hover_shows_struct_field_info() { |
513 | // Hovering over the field when instantiating | 544 | // Hovering over the field when instantiating |
514 | check( | 545 | check( |
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index a32ae0165..f71b804fe 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs | |||
@@ -4,7 +4,7 @@ mod injection; | |||
4 | #[cfg(test)] | 4 | #[cfg(test)] |
5 | mod tests; | 5 | mod tests; |
6 | 6 | ||
7 | use hir::{Name, Semantics}; | 7 | use hir::{Name, Semantics, VariantDef}; |
8 | use ra_ide_db::{ | 8 | use ra_ide_db::{ |
9 | defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, | 9 | defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass}, |
10 | RootDatabase, | 10 | RootDatabase, |
@@ -455,6 +455,18 @@ fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> { | |||
455 | Some(TextRange::new(range_start, range_end)) | 455 | Some(TextRange::new(range_start, range_end)) |
456 | } | 456 | } |
457 | 457 | ||
458 | fn is_possibly_unsafe(name_ref: &ast::NameRef) -> bool { | ||
459 | name_ref | ||
460 | .syntax() | ||
461 | .parent() | ||
462 | .and_then(|parent| { | ||
463 | ast::FieldExpr::cast(parent.clone()) | ||
464 | .map(|_| true) | ||
465 | .or_else(|| ast::RecordPatField::cast(parent).map(|_| true)) | ||
466 | }) | ||
467 | .unwrap_or(false) | ||
468 | } | ||
469 | |||
458 | fn highlight_element( | 470 | fn highlight_element( |
459 | sema: &Semantics<RootDatabase>, | 471 | sema: &Semantics<RootDatabase>, |
460 | bindings_shadow_count: &mut FxHashMap<Name, u32>, | 472 | bindings_shadow_count: &mut FxHashMap<Name, u32>, |
@@ -484,10 +496,19 @@ fn highlight_element( | |||
484 | 496 | ||
485 | match name_kind { | 497 | match name_kind { |
486 | Some(NameClass::Definition(def)) => { | 498 | Some(NameClass::Definition(def)) => { |
487 | highlight_name(db, def) | HighlightModifier::Definition | 499 | highlight_name(db, def, false) | HighlightModifier::Definition |
500 | } | ||
501 | Some(NameClass::ConstReference(def)) => highlight_name(db, def, false), | ||
502 | Some(NameClass::FieldShorthand { field, .. }) => { | ||
503 | let mut h = HighlightTag::Field.into(); | ||
504 | if let Definition::Field(field) = field { | ||
505 | if let VariantDef::Union(_) = field.parent_def(db) { | ||
506 | h |= HighlightModifier::Unsafe; | ||
507 | } | ||
508 | } | ||
509 | |||
510 | h | ||
488 | } | 511 | } |
489 | Some(NameClass::ConstReference(def)) => highlight_name(db, def), | ||
490 | Some(NameClass::FieldShorthand { .. }) => HighlightTag::Field.into(), | ||
491 | None => highlight_name_by_syntax(name) | HighlightModifier::Definition, | 512 | None => highlight_name_by_syntax(name) | HighlightModifier::Definition, |
492 | } | 513 | } |
493 | } | 514 | } |
@@ -498,6 +519,7 @@ fn highlight_element( | |||
498 | } | 519 | } |
499 | NAME_REF => { | 520 | NAME_REF => { |
500 | let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); | 521 | let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); |
522 | let possibly_unsafe = is_possibly_unsafe(&name_ref); | ||
501 | match classify_name_ref(sema, &name_ref) { | 523 | match classify_name_ref(sema, &name_ref) { |
502 | Some(name_kind) => match name_kind { | 524 | Some(name_kind) => match name_kind { |
503 | NameRefClass::Definition(def) => { | 525 | NameRefClass::Definition(def) => { |
@@ -508,11 +530,13 @@ fn highlight_element( | |||
508 | binding_hash = Some(calc_binding_hash(&name, *shadow_count)) | 530 | binding_hash = Some(calc_binding_hash(&name, *shadow_count)) |
509 | } | 531 | } |
510 | }; | 532 | }; |
511 | highlight_name(db, def) | 533 | highlight_name(db, def, possibly_unsafe) |
512 | } | 534 | } |
513 | NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(), | 535 | NameRefClass::FieldShorthand { .. } => HighlightTag::Field.into(), |
514 | }, | 536 | }, |
515 | None if syntactic_name_ref_highlighting => highlight_name_ref_by_syntax(name_ref), | 537 | None if syntactic_name_ref_highlighting => { |
538 | highlight_name_ref_by_syntax(name_ref, sema) | ||
539 | } | ||
516 | None => HighlightTag::UnresolvedReference.into(), | 540 | None => HighlightTag::UnresolvedReference.into(), |
517 | } | 541 | } |
518 | } | 542 | } |
@@ -652,10 +676,19 @@ fn is_child_of_impl(element: &SyntaxElement) -> bool { | |||
652 | } | 676 | } |
653 | } | 677 | } |
654 | 678 | ||
655 | fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight { | 679 | fn highlight_name(db: &RootDatabase, def: Definition, possibly_unsafe: bool) -> Highlight { |
656 | match def { | 680 | match def { |
657 | Definition::Macro(_) => HighlightTag::Macro, | 681 | Definition::Macro(_) => HighlightTag::Macro, |
658 | Definition::Field(_) => HighlightTag::Field, | 682 | Definition::Field(field) => { |
683 | let mut h = HighlightTag::Field.into(); | ||
684 | if possibly_unsafe { | ||
685 | if let VariantDef::Union(_) = field.parent_def(db) { | ||
686 | h |= HighlightModifier::Unsafe; | ||
687 | } | ||
688 | } | ||
689 | |||
690 | return h; | ||
691 | } | ||
659 | Definition::ModuleDef(def) => match def { | 692 | Definition::ModuleDef(def) => match def { |
660 | hir::ModuleDef::Module(_) => HighlightTag::Module, | 693 | hir::ModuleDef::Module(_) => HighlightTag::Module, |
661 | hir::ModuleDef::Function(func) => { | 694 | hir::ModuleDef::Function(func) => { |
@@ -677,6 +710,7 @@ fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight { | |||
677 | let mut h = Highlight::new(HighlightTag::Static); | 710 | let mut h = Highlight::new(HighlightTag::Static); |
678 | if s.is_mut(db) { | 711 | if s.is_mut(db) { |
679 | h |= HighlightModifier::Mutable; | 712 | h |= HighlightModifier::Mutable; |
713 | h |= HighlightModifier::Unsafe; | ||
680 | } | 714 | } |
681 | return h; | 715 | return h; |
682 | } | 716 | } |
@@ -724,7 +758,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight { | |||
724 | tag.into() | 758 | tag.into() |
725 | } | 759 | } |
726 | 760 | ||
727 | fn highlight_name_ref_by_syntax(name: ast::NameRef) -> Highlight { | 761 | fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabase>) -> Highlight { |
728 | let default = HighlightTag::UnresolvedReference; | 762 | let default = HighlightTag::UnresolvedReference; |
729 | 763 | ||
730 | let parent = match name.syntax().parent() { | 764 | let parent = match name.syntax().parent() { |
@@ -734,7 +768,20 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef) -> Highlight { | |||
734 | 768 | ||
735 | let tag = match parent.kind() { | 769 | let tag = match parent.kind() { |
736 | METHOD_CALL_EXPR => HighlightTag::Function, | 770 | METHOD_CALL_EXPR => HighlightTag::Function, |
737 | FIELD_EXPR => HighlightTag::Field, | 771 | FIELD_EXPR => { |
772 | let h = HighlightTag::Field; | ||
773 | let is_union = ast::FieldExpr::cast(parent) | ||
774 | .and_then(|field_expr| { | ||
775 | let field = sema.resolve_field(&field_expr)?; | ||
776 | Some(if let VariantDef::Union(_) = field.parent_def(sema.db) { | ||
777 | true | ||
778 | } else { | ||
779 | false | ||
780 | }) | ||
781 | }) | ||
782 | .unwrap_or(false); | ||
783 | return if is_union { h | HighlightModifier::Unsafe } else { h.into() }; | ||
784 | } | ||
738 | PATH_SEGMENT => { | 785 | PATH_SEGMENT => { |
739 | let path = match parent.parent().and_then(ast::Path::cast) { | 786 | let path = match parent.parent().and_then(ast::Path::cast) { |
740 | Some(it) => it, | 787 | Some(it) => it, |
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index 87a6e2523..730efff0d 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs | |||
@@ -9,6 +9,9 @@ use crate::{mock_analysis::single_file, FileRange, TextRange}; | |||
9 | fn test_highlighting() { | 9 | fn test_highlighting() { |
10 | check_highlighting( | 10 | check_highlighting( |
11 | r#" | 11 | r#" |
12 | use inner::{self as inner_mod}; | ||
13 | mod inner {} | ||
14 | |||
12 | #[derive(Clone, Debug)] | 15 | #[derive(Clone, Debug)] |
13 | struct Foo { | 16 | struct Foo { |
14 | pub x: i32, | 17 | pub x: i32, |
@@ -272,19 +275,37 @@ fn test_unsafe_highlighting() { | |||
272 | r#" | 275 | r#" |
273 | unsafe fn unsafe_fn() {} | 276 | unsafe fn unsafe_fn() {} |
274 | 277 | ||
278 | union Union { | ||
279 | a: u32, | ||
280 | b: f32, | ||
281 | } | ||
282 | |||
275 | struct HasUnsafeFn; | 283 | struct HasUnsafeFn; |
276 | 284 | ||
277 | impl HasUnsafeFn { | 285 | impl HasUnsafeFn { |
278 | unsafe fn unsafe_method(&self) {} | 286 | unsafe fn unsafe_method(&self) {} |
279 | } | 287 | } |
280 | 288 | ||
289 | struct TypeForStaticMut { | ||
290 | a: u8 | ||
291 | } | ||
292 | |||
293 | static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 }; | ||
294 | |||
281 | fn main() { | 295 | fn main() { |
282 | let x = &5 as *const usize; | 296 | let x = &5 as *const usize; |
297 | let u = Union { b: 0 }; | ||
283 | unsafe { | 298 | unsafe { |
284 | unsafe_fn(); | 299 | unsafe_fn(); |
300 | let b = u.b; | ||
301 | match u { | ||
302 | Union { b: 0 } => (), | ||
303 | Union { a } => (), | ||
304 | } | ||
285 | HasUnsafeFn.unsafe_method(); | 305 | HasUnsafeFn.unsafe_method(); |
286 | let y = *(x); | 306 | let y = *(x); |
287 | let z = -x; | 307 | let z = -x; |
308 | let a = global_mut.a; | ||
288 | } | 309 | } |
289 | } | 310 | } |
290 | "# | 311 | "# |