From 5d17b6a6873d530eda89d271807dcb70a811a200 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 4 Jun 2021 17:03:18 +0200 Subject: Implement hover for lints --- crates/ide/src/hover.rs | 90 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 6 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 04598cd06..4d91c5c4f 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3,6 +3,7 @@ use hir::{ AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, InFile, Module, ModuleDef, Semantics, }; +use ide_completion::generated_lint_completions::{CLIPPY_LINTS, FEATURES}; use ide_db::{ base_db::SourceDatabase, defs::{Definition, NameClass, NameRefClass}, @@ -11,7 +12,10 @@ use ide_db::{ }; use itertools::Itertools; use stdx::format_to; -use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; +use syntax::{ + algo, ast, match_ast, AstNode, AstToken, Direction, SyntaxKind::*, SyntaxToken, TokenAtOffset, + T, +}; use crate::{ display::{macro_label, TryToNav}, @@ -115,8 +119,8 @@ pub(crate) fn hover( |d| d.defined(db), ), - _ => ast::Comment::cast(token.clone()) - .and_then(|_| { + _ => { + if ast::Comment::cast(token.clone()).is_some() { let (attributes, def) = doc_attributes(&sema, &node)?; let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?; let (idl_range, link, ns) = @@ -129,9 +133,11 @@ pub(crate) fn hover( } })?; range = Some(idl_range); - resolve_doc_path_for_def(db, def, &link, ns) - }) - .map(Definition::ModuleDef), + resolve_doc_path_for_def(db, def, &link, ns).map(Definition::ModuleDef) + } else { + return try_hover_for_attribute(&token); + } + }, } }; @@ -194,6 +200,40 @@ pub(crate) fn hover( Some(RangeInfo::new(range, res)) } +fn try_hover_for_attribute(token: &SyntaxToken) -> Option> { + let attr = token.ancestors().nth(1).and_then(ast::Attr::cast)?; + let (path, tt) = attr.as_simple_call()?; + if !tt.syntax().text_range().contains(token.text_range().start()) { + return None; + } + let lints = match &*path { + "feature" => FEATURES, + "allow" | "warn" | "forbid" | "error" => { + let is_clippy = algo::skip_trivia_token(token.clone(), Direction::Prev) + .filter(|t| t.kind() == T![::]) + .and_then(|t| algo::skip_trivia_token(t, Direction::Prev)) + .map_or(false, |t| t.kind() == T![ident] && t.text() == "clippy"); + if is_clippy { + CLIPPY_LINTS + } else { + &[] + } + } + _ => return None, + }; + let lint = lints + .binary_search_by_key(&token.text(), |lint| lint.label) + .ok() + .map(|idx| &FEATURES[idx])?; + Some(RangeInfo::new( + token.text_range(), + HoverResult { + markup: Markup::from(format!("```\n{}\n```\n___\n\n{}", lint.label, lint.description)), + ..Default::default() + }, + )) +} + fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option { fn to_action(nav_target: NavigationTarget) -> HoverAction { HoverAction::Implementation(FilePosition { @@ -3977,4 +4017,42 @@ pub fn foo() {} "#]], ) } + + #[test] + fn hover_feature() { + check( + r#"#![feature(box_syntax$0)]"#, + expect![[r##" + *box_syntax* + ``` + box_syntax + ``` + ___ + + # `box_syntax` + + The tracking issue for this feature is: [#49733] + + [#49733]: https://github.com/rust-lang/rust/issues/49733 + + See also [`box_patterns`](box-patterns.md) + + ------------------------ + + Currently the only stable way to create a `Box` is via the `Box::new` method. + Also it is not possible in stable Rust to destructure a `Box` in a match + pattern. The unstable `box` keyword can be used to create a `Box`. An example + usage would be: + + ```rust + #![feature(box_syntax)] + + fn main() { + let b = box 5; + } + ``` + + "##]], + ) + } } -- cgit v1.2.3 From 343df88ac7579316a5500fa7f4a07602809af669 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 4 Jun 2021 18:35:19 +0200 Subject: Generate default lint completions --- crates/ide/src/hover.rs | 75 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 15 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 4d91c5c4f..f3f6f749c 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3,11 +3,13 @@ use hir::{ AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, InFile, Module, ModuleDef, Semantics, }; -use ide_completion::generated_lint_completions::{CLIPPY_LINTS, FEATURES}; use ide_db::{ base_db::SourceDatabase, defs::{Definition, NameClass, NameRefClass}, - helpers::FamousDefs, + helpers::{ + generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES}, + FamousDefs, + }, RootDatabase, }; use itertools::Itertools; @@ -206,25 +208,36 @@ fn try_hover_for_attribute(token: &SyntaxToken) -> Option if !tt.syntax().text_range().contains(token.text_range().start()) { return None; } - let lints = match &*path { - "feature" => FEATURES, - "allow" | "warn" | "forbid" | "error" => { - let is_clippy = algo::skip_trivia_token(token.clone(), Direction::Prev) - .filter(|t| t.kind() == T![::]) - .and_then(|t| algo::skip_trivia_token(t, Direction::Prev)) - .map_or(false, |t| t.kind() == T![ident] && t.text() == "clippy"); + let (is_clippy, lints) = match &*path { + "feature" => (false, FEATURES), + "allow" | "deny" | "forbid" | "warn" => { + let is_clippy = algo::non_trivia_sibling(token.clone().into(), Direction::Prev) + .filter(|t| t.kind() == T![:]) + .and_then(|t| algo::non_trivia_sibling(t, Direction::Prev)) + .filter(|t| t.kind() == T![:]) + .and_then(|t| algo::non_trivia_sibling(t, Direction::Prev)) + .map_or(false, |t| { + t.kind() == T![ident] && t.into_token().map_or(false, |t| t.text() == "clippy") + }); if is_clippy { - CLIPPY_LINTS + (true, CLIPPY_LINTS) } else { - &[] + (false, DEFAULT_LINTS) } } _ => return None, }; - let lint = lints - .binary_search_by_key(&token.text(), |lint| lint.label) - .ok() - .map(|idx| &FEATURES[idx])?; + + let tmp; + let needle = if is_clippy { + tmp = format!("clippy::{}", token.text()); + &tmp + } else { + &*token.text() + }; + + let lint = + lints.binary_search_by_key(&needle, |lint| lint.label).ok().map(|idx| &lints[idx])?; Some(RangeInfo::new( token.text_range(), HoverResult { @@ -4055,4 +4068,36 @@ pub fn foo() {} "##]], ) } + + #[test] + fn hover_lint() { + check( + r#"#![allow(arithmetic_overflow$0)]"#, + expect![[r#" + *arithmetic_overflow* + ``` + arithmetic_overflow + ``` + ___ + + arithmetic operation overflows + "#]], + ) + } + + #[test] + fn hover_clippy_lint() { + check( + r#"#![allow(clippy::almost_swapped$0)]"#, + expect![[r#" + *almost_swapped* + ``` + clippy::almost_swapped + ``` + ___ + + Checks for `foo = bar; bar = foo` sequences. + "#]], + ) + } } -- cgit v1.2.3 From 0c89f38378c2110cf6c080a5dc837bf7731fef5c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 4 Jun 2021 19:03:45 +0200 Subject: Replace `-` with `_` in generated lint names --- crates/ide/src/hover.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'crates/ide/src/hover.rs') diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index f3f6f749c..573ffd10d 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -123,6 +123,7 @@ pub(crate) fn hover( _ => { if ast::Comment::cast(token.clone()).is_some() { + cov_mark::hit!(no_highlight_on_comment_hover); let (attributes, def) = doc_attributes(&sema, &node)?; let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?; let (idl_range, link, ns) = @@ -136,8 +137,10 @@ pub(crate) fn hover( })?; range = Some(idl_range); resolve_doc_path_for_def(db, def, &link, ns).map(Definition::ModuleDef) + } else if let res@Some(_) = try_hover_for_attribute(&token) { + return res; } else { - return try_hover_for_attribute(&token); + None } }, } @@ -169,11 +172,6 @@ pub(crate) fn hover( } } - if token.kind() == syntax::SyntaxKind::COMMENT { - cov_mark::hit!(no_highlight_on_comment_hover); - return None; - } - if let res @ Some(_) = hover_for_keyword(&sema, links_in_hover, markdown, &token) { return res; } -- cgit v1.2.3