diff options
Diffstat (limited to 'crates/ide')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 65 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/highlight.rs | 23 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/test_data/highlighting.html | 2 | ||||
-rw-r--r-- | crates/ide/src/syntax_highlighting/tests.rs | 8 |
4 files changed, 70 insertions, 28 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 27d347dbd..dcac7c76d 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs | |||
@@ -299,10 +299,10 @@ fn unresolved_fix(id: &'static str, label: &str, target: TextRange) -> Assist { | |||
299 | 299 | ||
300 | #[cfg(test)] | 300 | #[cfg(test)] |
301 | mod tests { | 301 | mod tests { |
302 | use expect_test::{expect, Expect}; | 302 | use expect_test::Expect; |
303 | use ide_assists::AssistResolveStrategy; | 303 | use ide_assists::AssistResolveStrategy; |
304 | use stdx::trim_indent; | 304 | use stdx::trim_indent; |
305 | use test_utils::assert_eq_text; | 305 | use test_utils::{assert_eq_text, extract_annotations}; |
306 | 306 | ||
307 | use crate::{fixture, DiagnosticsConfig}; | 307 | use crate::{fixture, DiagnosticsConfig}; |
308 | 308 | ||
@@ -396,26 +396,51 @@ mod tests { | |||
396 | expect.assert_debug_eq(&diagnostics) | 396 | expect.assert_debug_eq(&diagnostics) |
397 | } | 397 | } |
398 | 398 | ||
399 | pub(crate) fn check_diagnostics(ra_fixture: &str) { | ||
400 | let (analysis, file_id) = fixture::file(ra_fixture); | ||
401 | let diagnostics = analysis | ||
402 | .diagnostics(&DiagnosticsConfig::default(), AssistResolveStrategy::All, file_id) | ||
403 | .unwrap(); | ||
404 | |||
405 | let expected = extract_annotations(&*analysis.file_text(file_id).unwrap()); | ||
406 | let actual = diagnostics.into_iter().map(|d| (d.range, d.message)).collect::<Vec<_>>(); | ||
407 | assert_eq!(expected, actual); | ||
408 | } | ||
409 | |||
399 | #[test] | 410 | #[test] |
400 | fn test_unresolved_macro_range() { | 411 | fn test_unresolved_macro_range() { |
401 | check_expect( | 412 | check_diagnostics( |
402 | r#"foo::bar!(92);"#, | 413 | r#" |
403 | expect![[r#" | 414 | foo::bar!(92); |
404 | [ | 415 | //^^^ unresolved macro `foo::bar!` |
405 | Diagnostic { | 416 | "#, |
406 | message: "unresolved macro `foo::bar!`", | 417 | ); |
407 | range: 5..8, | 418 | } |
408 | severity: Error, | 419 | |
409 | fixes: None, | 420 | #[test] |
410 | unused: false, | 421 | fn unresolved_import_in_use_tree() { |
411 | code: Some( | 422 | // Only the relevant part of a nested `use` item should be highlighted. |
412 | DiagnosticCode( | 423 | check_diagnostics( |
413 | "unresolved-macro-call", | 424 | r#" |
414 | ), | 425 | use does_exist::{Exists, DoesntExist}; |
415 | ), | 426 | //^^^^^^^^^^^ unresolved import |
416 | }, | 427 | |
417 | ] | 428 | use {does_not_exist::*, does_exist}; |
418 | "#]], | 429 | //^^^^^^^^^^^^^^^^^ unresolved import |
430 | |||
431 | use does_not_exist::{ | ||
432 | a, | ||
433 | //^ unresolved import | ||
434 | b, | ||
435 | //^ unresolved import | ||
436 | c, | ||
437 | //^ unresolved import | ||
438 | }; | ||
439 | |||
440 | mod does_exist { | ||
441 | pub struct Exists; | ||
442 | } | ||
443 | "#, | ||
419 | ); | 444 | ); |
420 | } | 445 | } |
421 | 446 | ||
diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index b4a3d39c9..9503c936d 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs | |||
@@ -71,7 +71,7 @@ pub(super) fn element( | |||
71 | } | 71 | } |
72 | NAME_REF => { | 72 | NAME_REF => { |
73 | let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); | 73 | let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap(); |
74 | highlight_func_by_name_ref(sema, &name_ref).unwrap_or_else(|| { | 74 | highlight_func_by_name_ref(sema, krate, &name_ref).unwrap_or_else(|| { |
75 | let is_self = name_ref.self_token().is_some(); | 75 | let is_self = name_ref.self_token().is_some(); |
76 | let h = match NameRefClass::classify(sema, &name_ref) { | 76 | let h = match NameRefClass::classify(sema, &name_ref) { |
77 | Some(name_kind) => match name_kind { | 77 | Some(name_kind) => match name_kind { |
@@ -108,7 +108,7 @@ pub(super) fn element( | |||
108 | NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(), | 108 | NameRefClass::FieldShorthand { .. } => SymbolKind::Field.into(), |
109 | }, | 109 | }, |
110 | None if syntactic_name_ref_highlighting => { | 110 | None if syntactic_name_ref_highlighting => { |
111 | highlight_name_ref_by_syntax(name_ref, sema) | 111 | highlight_name_ref_by_syntax(name_ref, sema, krate) |
112 | } | 112 | } |
113 | None => HlTag::UnresolvedReference.into(), | 113 | None => HlTag::UnresolvedReference.into(), |
114 | }; | 114 | }; |
@@ -434,19 +434,23 @@ fn highlight_def(db: &RootDatabase, krate: Option<hir::Crate>, def: Definition) | |||
434 | 434 | ||
435 | fn highlight_func_by_name_ref( | 435 | fn highlight_func_by_name_ref( |
436 | sema: &Semantics<RootDatabase>, | 436 | sema: &Semantics<RootDatabase>, |
437 | krate: Option<hir::Crate>, | ||
437 | name_ref: &ast::NameRef, | 438 | name_ref: &ast::NameRef, |
438 | ) -> Option<Highlight> { | 439 | ) -> Option<Highlight> { |
439 | let mc = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?; | 440 | let mc = name_ref.syntax().parent().and_then(ast::MethodCallExpr::cast)?; |
440 | highlight_method_call(sema, &mc) | 441 | highlight_method_call(sema, krate, &mc) |
441 | } | 442 | } |
442 | 443 | ||
443 | fn highlight_method_call( | 444 | fn highlight_method_call( |
444 | sema: &Semantics<RootDatabase>, | 445 | sema: &Semantics<RootDatabase>, |
446 | krate: Option<hir::Crate>, | ||
445 | method_call: &ast::MethodCallExpr, | 447 | method_call: &ast::MethodCallExpr, |
446 | ) -> Option<Highlight> { | 448 | ) -> Option<Highlight> { |
447 | let func = sema.resolve_method_call(&method_call)?; | 449 | let func = sema.resolve_method_call(&method_call)?; |
450 | |||
448 | let mut h = SymbolKind::Function.into(); | 451 | let mut h = SymbolKind::Function.into(); |
449 | h |= HlMod::Associated; | 452 | h |= HlMod::Associated; |
453 | |||
450 | if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { | 454 | if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) { |
451 | h |= HlMod::Unsafe; | 455 | h |= HlMod::Unsafe; |
452 | } | 456 | } |
@@ -454,7 +458,10 @@ fn highlight_method_call( | |||
454 | h |= HlMod::Async; | 458 | h |= HlMod::Async; |
455 | } | 459 | } |
456 | if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() { | 460 | if func.as_assoc_item(sema.db).and_then(|it| it.containing_trait(sema.db)).is_some() { |
457 | h |= HlMod::Trait | 461 | h |= HlMod::Trait; |
462 | } | ||
463 | if Some(func.module(sema.db).krate()) != krate { | ||
464 | h |= HlMod::Library; | ||
458 | } | 465 | } |
459 | 466 | ||
460 | if let Some(self_param) = func.self_param(sema.db) { | 467 | if let Some(self_param) = func.self_param(sema.db) { |
@@ -503,7 +510,11 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight { | |||
503 | tag.into() | 510 | tag.into() |
504 | } | 511 | } |
505 | 512 | ||
506 | fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabase>) -> Highlight { | 513 | fn highlight_name_ref_by_syntax( |
514 | name: ast::NameRef, | ||
515 | sema: &Semantics<RootDatabase>, | ||
516 | krate: Option<hir::Crate>, | ||
517 | ) -> Highlight { | ||
507 | let default = HlTag::UnresolvedReference; | 518 | let default = HlTag::UnresolvedReference; |
508 | 519 | ||
509 | let parent = match name.syntax().parent() { | 520 | let parent = match name.syntax().parent() { |
@@ -514,7 +525,7 @@ fn highlight_name_ref_by_syntax(name: ast::NameRef, sema: &Semantics<RootDatabas | |||
514 | match parent.kind() { | 525 | match parent.kind() { |
515 | METHOD_CALL_EXPR => { | 526 | METHOD_CALL_EXPR => { |
516 | return ast::MethodCallExpr::cast(parent) | 527 | return ast::MethodCallExpr::cast(parent) |
517 | .and_then(|it| highlight_method_call(sema, &it)) | 528 | .and_then(|it| highlight_method_call(sema, krate, &it)) |
518 | .unwrap_or_else(|| SymbolKind::Function.into()); | 529 | .unwrap_or_else(|| SymbolKind::Function.into()); |
519 | } | 530 | } |
520 | FIELD_EXPR => { | 531 | FIELD_EXPR => { |
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html index 055d21109..0264e39a3 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html | |||
@@ -258,7 +258,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
258 | 258 | ||
259 | <span class="keyword">let</span> <span class="variable declaration">control_flow</span> <span class="operator">=</span> <span class="module library">foo</span><span class="operator">::</span><span class="function library">identity</span><span class="parenthesis">(</span><span class="module library">foo</span><span class="operator">::</span><span class="enum library">ControlFlow</span><span class="operator">::</span><span class="enum_variant library">Continue</span><span class="parenthesis">)</span><span class="semicolon">;</span> | 259 | <span class="keyword">let</span> <span class="variable declaration">control_flow</span> <span class="operator">=</span> <span class="module library">foo</span><span class="operator">::</span><span class="function library">identity</span><span class="parenthesis">(</span><span class="module library">foo</span><span class="operator">::</span><span class="enum library">ControlFlow</span><span class="operator">::</span><span class="enum_variant library">Continue</span><span class="parenthesis">)</span><span class="semicolon">;</span> |
260 | 260 | ||
261 | <span class="keyword control">if</span> <span class="keyword">let</span> <span class="module library">foo</span><span class="operator">::</span><span class="enum library">ControlFlow</span><span class="operator">::</span><span class="enum_variant library">Die</span> <span class="operator">=</span> <span class="variable">control_flow</span> <span class="brace">{</span> | 261 | <span class="keyword control">if</span> <span class="variable">control_flow</span><span class="operator">.</span><span class="function associated consuming library">should_die</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span> |
262 | foo::<span class="macro">die!</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span> | 262 | foo::<span class="macro">die!</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span> |
263 | <span class="brace">}</span> | 263 | <span class="brace">}</span> |
264 | <span class="brace">}</span> | 264 | <span class="brace">}</span> |
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index be4447ebb..662b53481 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs | |||
@@ -232,7 +232,7 @@ fn use_foo_items() { | |||
232 | 232 | ||
233 | let control_flow = foo::identity(foo::ControlFlow::Continue); | 233 | let control_flow = foo::identity(foo::ControlFlow::Continue); |
234 | 234 | ||
235 | if let foo::ControlFlow::Die = control_flow { | 235 | if control_flow.should_die() { |
236 | foo::die!(); | 236 | foo::die!(); |
237 | } | 237 | } |
238 | } | 238 | } |
@@ -249,6 +249,12 @@ pub enum ControlFlow { | |||
249 | Die, | 249 | Die, |
250 | } | 250 | } |
251 | 251 | ||
252 | impl ControlFlow { | ||
253 | pub fn should_die(self) -> bool { | ||
254 | matches!(self, ControlFlow::Die) | ||
255 | } | ||
256 | } | ||
257 | |||
252 | pub fn identity<T>(x: T) -> T { x } | 258 | pub fn identity<T>(x: T) -> T { x } |
253 | 259 | ||
254 | pub mod consts { | 260 | pub mod consts { |