Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions crates/ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,33 @@ fn main() { }
);
}

#[test]
fn hover_shows_fn_doc_attr_raw_string() {
check(
r##"
#[doc = r#"Raw string doc attr"#]
pub fn foo<|>(_: &Path) {}

fn main() { }
"##,
expect![[r##"
*foo*

```rust
test
```

```rust
pub fn foo(_: &Path)
```

---

Raw string doc attr
"##]],
);
}

#[test]
fn hover_shows_struct_field_info() {
// Hovering over the field when instantiating
Expand Down
14 changes: 11 additions & 3 deletions crates/syntax/src/ast/node_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use itertools::Itertools;
use parser::SyntaxKind;

use crate::{
ast::{self, support, AstNode, NameOwner, SyntaxNode},
ast::{self, support, token_ext::HasStringValue, AstNode, AstToken, NameOwner, SyntaxNode},
SmolStr, SyntaxElement, SyntaxToken, T,
};

Expand Down Expand Up @@ -53,8 +53,16 @@ impl ast::Attr {
pub fn as_simple_key_value(&self) -> Option<(SmolStr, SmolStr)> {
let lit = self.literal()?;
let key = self.simple_name()?;
// FIXME: escape? raw string?
let value = lit.syntax().first_token()?.text().trim_matches('"').into();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we already have "correct" way to do this implemented: https://github.com/rust-analyzer/rust-analyzer/blob/ea25ae614b21237c4a536304da875bdc29f0c65a/crates/syntax/src/ast/token_ext.rs#L149

We can just ast::String::cast our way to victory here, but, ideally, we should improve the API surrounding string literals somewhat: #6308

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out! I've updated this to cast to ast::String/ast::RawString then use the value method.

I did take a look at #6308 but I think that would take a bit more time than I have right now. But I understand if you'd rather handle this after that, so feel free to close this PR if so.

Thanks for the review!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

bors r+

let value_token = lit.syntax().first_token()?;

let value: SmolStr = if let Some(s) = ast::String::cast(value_token.clone()) {
s.value()?.into()
} else if let Some(s) = ast::RawString::cast(value_token) {
s.value()?.into()
} else {
return None;
};

Some((key, value))
}

Expand Down