diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-07-31 03:12:44 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-07-31 03:12:44 +0100 |
commit | f05d7b41a719d848844b054a16477b29d0f063c6 (patch) | |
tree | 0a8a0946e8aef2ce64d4c13d0035ba41cce2daf3 /crates/ra_ide/src/display.rs | |
parent | 73ff610e41959e3e7c78a2b4b25b086883132956 (diff) | |
parent | 6b7cb8b5ab539fc4333ce34bc29bf77c976f232a (diff) |
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Hasn't fixed tests yet.
Diffstat (limited to 'crates/ra_ide/src/display.rs')
-rw-r--r-- | crates/ra_ide/src/display.rs | 96 |
1 files changed, 41 insertions, 55 deletions
diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs index 827c094e7..fd42aa435 100644 --- a/crates/ra_ide/src/display.rs +++ b/crates/ra_ide/src/display.rs | |||
@@ -1,31 +1,60 @@ | |||
1 | //! This module contains utilities for turning SyntaxNodes and HIR types | 1 | //! This module contains utilities for turning SyntaxNodes and HIR types |
2 | //! into types that may be used to render in a UI. | 2 | //! into types that may be used to render in a UI. |
3 | 3 | ||
4 | mod function_signature; | ||
5 | mod navigation_target; | 4 | mod navigation_target; |
6 | mod structure; | ||
7 | mod short_label; | 5 | mod short_label; |
8 | 6 | ||
9 | use std::fmt::Display; | ||
10 | |||
11 | use ra_syntax::{ | 7 | use ra_syntax::{ |
12 | ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner}, | 8 | ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner}, |
13 | SyntaxKind::{ATTR, COMMENT}, | 9 | SyntaxKind::{ATTR, COMMENT}, |
14 | }; | 10 | }; |
11 | |||
12 | use ast::VisibilityOwner; | ||
15 | use stdx::format_to; | 13 | use stdx::format_to; |
16 | 14 | ||
17 | pub use function_signature::FunctionSignature; | ||
18 | pub use navigation_target::NavigationTarget; | 15 | pub use navigation_target::NavigationTarget; |
19 | pub use structure::{file_structure, StructureNode}; | ||
20 | |||
21 | pub(crate) use navigation_target::{ToNav, TryToNav}; | 16 | pub(crate) use navigation_target::{ToNav, TryToNav}; |
22 | pub(crate) use short_label::ShortLabel; | 17 | pub(crate) use short_label::ShortLabel; |
23 | 18 | ||
24 | pub(crate) fn function_label(node: &ast::FnDef) -> String { | 19 | pub(crate) fn function_declaration(node: &ast::Fn) -> String { |
25 | FunctionSignature::from(node).to_string() | 20 | let mut buf = String::new(); |
21 | if let Some(vis) = node.visibility() { | ||
22 | format_to!(buf, "{} ", vis); | ||
23 | } | ||
24 | if node.async_token().is_some() { | ||
25 | format_to!(buf, "async "); | ||
26 | } | ||
27 | if node.const_token().is_some() { | ||
28 | format_to!(buf, "const "); | ||
29 | } | ||
30 | if node.unsafe_token().is_some() { | ||
31 | format_to!(buf, "unsafe "); | ||
32 | } | ||
33 | if let Some(abi) = node.abi() { | ||
34 | // Keyword `extern` is included in the string. | ||
35 | format_to!(buf, "{} ", abi); | ||
36 | } | ||
37 | if let Some(name) = node.name() { | ||
38 | format_to!(buf, "fn {}", name) | ||
39 | } | ||
40 | if let Some(type_params) = node.generic_param_list() { | ||
41 | format_to!(buf, "{}", type_params); | ||
42 | } | ||
43 | if let Some(param_list) = node.param_list() { | ||
44 | format_to!(buf, "{}", param_list); | ||
45 | } | ||
46 | if let Some(ret_type) = node.ret_type() { | ||
47 | if ret_type.ty().is_some() { | ||
48 | format_to!(buf, " {}", ret_type); | ||
49 | } | ||
50 | } | ||
51 | if let Some(where_clause) = node.where_clause() { | ||
52 | format_to!(buf, "\n{}", where_clause); | ||
53 | } | ||
54 | buf | ||
26 | } | 55 | } |
27 | 56 | ||
28 | pub(crate) fn const_label(node: &ast::ConstDef) -> String { | 57 | pub(crate) fn const_label(node: &ast::Const) -> String { |
29 | let label: String = node | 58 | let label: String = node |
30 | .syntax() | 59 | .syntax() |
31 | .children_with_tokens() | 60 | .children_with_tokens() |
@@ -36,7 +65,7 @@ pub(crate) fn const_label(node: &ast::ConstDef) -> String { | |||
36 | label.trim().to_owned() | 65 | label.trim().to_owned() |
37 | } | 66 | } |
38 | 67 | ||
39 | pub(crate) fn type_label(node: &ast::TypeAliasDef) -> String { | 68 | pub(crate) fn type_label(node: &ast::TypeAlias) -> String { |
40 | let label: String = node | 69 | let label: String = node |
41 | .syntax() | 70 | .syntax() |
42 | .children_with_tokens() | 71 | .children_with_tokens() |
@@ -47,51 +76,8 @@ pub(crate) fn type_label(node: &ast::TypeAliasDef) -> String { | |||
47 | label.trim().to_owned() | 76 | label.trim().to_owned() |
48 | } | 77 | } |
49 | 78 | ||
50 | pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> { | ||
51 | let mut res = vec![]; | ||
52 | if let Some(type_params) = node.type_param_list() { | ||
53 | res.extend(type_params.lifetime_params().map(|p| p.syntax().text().to_string())); | ||
54 | res.extend(type_params.type_params().map(|p| p.syntax().text().to_string())); | ||
55 | } | ||
56 | res | ||
57 | } | ||
58 | |||
59 | pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> { | ||
60 | let mut res = vec![]; | ||
61 | if let Some(clause) = node.where_clause() { | ||
62 | res.extend(clause.predicates().map(|p| p.syntax().text().to_string())); | ||
63 | } | ||
64 | res | ||
65 | } | ||
66 | |||
67 | pub(crate) fn macro_label(node: &ast::MacroCall) -> String { | 79 | pub(crate) fn macro_label(node: &ast::MacroCall) -> String { |
68 | let name = node.name().map(|name| name.syntax().text().to_string()).unwrap_or_default(); | 80 | let name = node.name().map(|name| name.syntax().text().to_string()).unwrap_or_default(); |
69 | let vis = if node.has_atom_attr("macro_export") { "#[macro_export]\n" } else { "" }; | 81 | let vis = if node.has_atom_attr("macro_export") { "#[macro_export]\n" } else { "" }; |
70 | format!("{}macro_rules! {}", vis, name) | 82 | format!("{}macro_rules! {}", vis, name) |
71 | } | 83 | } |
72 | |||
73 | pub(crate) fn rust_code_markup(code: &impl Display) -> String { | ||
74 | rust_code_markup_with_doc(code, None, None) | ||
75 | } | ||
76 | |||
77 | pub(crate) fn rust_code_markup_with_doc( | ||
78 | code: &impl Display, | ||
79 | doc: Option<&str>, | ||
80 | mod_path: Option<&str>, | ||
81 | ) -> String { | ||
82 | let mut buf = String::new(); | ||
83 | |||
84 | if let Some(mod_path) = mod_path { | ||
85 | if !mod_path.is_empty() { | ||
86 | format_to!(buf, "```rust\n{}\n```\n\n", mod_path); | ||
87 | } | ||
88 | } | ||
89 | format_to!(buf, "```rust\n{}\n```", code); | ||
90 | |||
91 | if let Some(doc) = doc { | ||
92 | format_to!(buf, "\n___"); | ||
93 | format_to!(buf, "\n\n{}", doc); | ||
94 | } | ||
95 | |||
96 | buf | ||
97 | } | ||