Skip to content

Commit 5d17b6a

Browse files
committed
Implement hover for lints
1 parent cd46255 commit 5d17b6a

File tree

5 files changed

+3566
-3375
lines changed

5 files changed

+3566
-3375
lines changed

crates/ide/src/hover.rs

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use hir::{
33
AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, InFile, Module,
44
ModuleDef, Semantics,
55
};
6+
use ide_completion::generated_lint_completions::{CLIPPY_LINTS, FEATURES};
67
use ide_db::{
78
base_db::SourceDatabase,
89
defs::{Definition, NameClass, NameRefClass},
@@ -11,7 +12,10 @@ use ide_db::{
1112
};
1213
use itertools::Itertools;
1314
use stdx::format_to;
14-
use syntax::{ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TokenAtOffset, T};
15+
use syntax::{
16+
algo, ast, match_ast, AstNode, AstToken, Direction, SyntaxKind::*, SyntaxToken, TokenAtOffset,
17+
T,
18+
};
1519

1620
use crate::{
1721
display::{macro_label, TryToNav},
@@ -115,8 +119,8 @@ pub(crate) fn hover(
115119
|d| d.defined(db),
116120
),
117121

118-
_ => ast::Comment::cast(token.clone())
119-
.and_then(|_| {
122+
_ => {
123+
if ast::Comment::cast(token.clone()).is_some() {
120124
let (attributes, def) = doc_attributes(&sema, &node)?;
121125
let (docs, doc_mapping) = attributes.docs_with_rangemap(db)?;
122126
let (idl_range, link, ns) =
@@ -129,9 +133,11 @@ pub(crate) fn hover(
129133
}
130134
})?;
131135
range = Some(idl_range);
132-
resolve_doc_path_for_def(db, def, &link, ns)
133-
})
134-
.map(Definition::ModuleDef),
136+
resolve_doc_path_for_def(db, def, &link, ns).map(Definition::ModuleDef)
137+
} else {
138+
return try_hover_for_attribute(&token);
139+
}
140+
},
135141
}
136142
};
137143

@@ -194,6 +200,40 @@ pub(crate) fn hover(
194200
Some(RangeInfo::new(range, res))
195201
}
196202

203+
fn try_hover_for_attribute(token: &SyntaxToken) -> Option<RangeInfo<HoverResult>> {
204+
let attr = token.ancestors().nth(1).and_then(ast::Attr::cast)?;
205+
let (path, tt) = attr.as_simple_call()?;
206+
if !tt.syntax().text_range().contains(token.text_range().start()) {
207+
return None;
208+
}
209+
let lints = match &*path {
210+
"feature" => FEATURES,
211+
"allow" | "warn" | "forbid" | "error" => {
212+
let is_clippy = algo::skip_trivia_token(token.clone(), Direction::Prev)
213+
.filter(|t| t.kind() == T![::])
214+
.and_then(|t| algo::skip_trivia_token(t, Direction::Prev))
215+
.map_or(false, |t| t.kind() == T![ident] && t.text() == "clippy");
216+
if is_clippy {
217+
CLIPPY_LINTS
218+
} else {
219+
&[]
220+
}
221+
}
222+
_ => return None,
223+
};
224+
let lint = lints
225+
.binary_search_by_key(&token.text(), |lint| lint.label)
226+
.ok()
227+
.map(|idx| &FEATURES[idx])?;
228+
Some(RangeInfo::new(
229+
token.text_range(),
230+
HoverResult {
231+
markup: Markup::from(format!("```\n{}\n```\n___\n\n{}", lint.label, lint.description)),
232+
..Default::default()
233+
},
234+
))
235+
}
236+
197237
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
198238
fn to_action(nav_target: NavigationTarget) -> HoverAction {
199239
HoverAction::Implementation(FilePosition {
@@ -3977,4 +4017,42 @@ pub fn foo() {}
39774017
"#]],
39784018
)
39794019
}
4020+
4021+
#[test]
4022+
fn hover_feature() {
4023+
check(
4024+
r#"#![feature(box_syntax$0)]"#,
4025+
expect![[r##"
4026+
*box_syntax*
4027+
```
4028+
box_syntax
4029+
```
4030+
___
4031+
4032+
# `box_syntax`
4033+
4034+
The tracking issue for this feature is: [#49733]
4035+
4036+
[#49733]: https://github.com/rust-lang/rust/issues/49733
4037+
4038+
See also [`box_patterns`](box-patterns.md)
4039+
4040+
------------------------
4041+
4042+
Currently the only stable way to create a `Box` is via the `Box::new` method.
4043+
Also it is not possible in stable Rust to destructure a `Box` in a match
4044+
pattern. The unstable `box` keyword can be used to create a `Box`. An example
4045+
usage would be:
4046+
4047+
```rust
4048+
#![feature(box_syntax)]
4049+
4050+
fn main() {
4051+
let b = box 5;
4052+
}
4053+
```
4054+
4055+
"##]],
4056+
)
4057+
}
39804058
}

0 commit comments

Comments
 (0)