rust/crates/ra_ide/src/display.rs

66 lines
2.0 KiB
Rust
Raw Normal View History

2019-04-08 08:04:58 -05:00
//! This module contains utilities for turning SyntaxNodes and HIR types
2019-04-09 08:08:24 -05:00
//! into types that may be used to render in a UI.
2019-04-08 08:07:50 -05:00
2020-07-16 11:07:53 -05:00
pub(crate) mod function_signature;
2019-04-08 08:07:50 -05:00
mod navigation_target;
2019-06-09 14:28:53 -05:00
mod short_label;
2019-04-08 08:07:50 -05:00
use ra_syntax::{
2019-09-10 00:32:47 -05:00
ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
SyntaxKind::{ATTR, COMMENT},
};
2020-02-22 09:57:29 -06:00
pub(crate) use navigation_target::{ToNav, TryToNav};
2019-06-09 14:28:53 -05:00
pub(crate) use short_label::ShortLabel;
2019-06-09 10:59:59 -05:00
2020-07-16 11:13:43 -05:00
pub use navigation_target::NavigationTarget;
pub(crate) fn function_label(node: &ast::FnDef) -> String {
2020-07-16 11:07:53 -05:00
function_signature::FunctionSignature::from(node).to_string()
}
pub(crate) fn const_label(node: &ast::ConstDef) -> String {
let label: String = node
.syntax()
.children_with_tokens()
.filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
.map(|node| node.to_string())
.collect();
label.trim().to_owned()
}
pub(crate) fn type_label(node: &ast::TypeAliasDef) -> String {
let label: String = node
.syntax()
.children_with_tokens()
.filter(|child| !(child.kind() == COMMENT || child.kind() == ATTR))
.map(|node| node.to_string())
.collect();
label.trim().to_owned()
}
pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> {
let mut res = vec![];
if let Some(type_params) = node.type_param_list() {
res.extend(type_params.lifetime_params().map(|p| p.syntax().text().to_string()));
res.extend(type_params.type_params().map(|p| p.syntax().text().to_string()));
}
res
}
pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> {
let mut res = vec![];
if let Some(clause) = node.where_clause() {
res.extend(clause.predicates().map(|p| p.syntax().text().to_string()));
}
res
}
2019-09-10 00:32:47 -05:00
pub(crate) fn macro_label(node: &ast::MacroCall) -> String {
let name = node.name().map(|name| name.syntax().text().to_string()).unwrap_or_default();
let vis = if node.has_atom_attr("macro_export") { "#[macro_export]\n" } else { "" };
format!("{}macro_rules! {}", vis, name)
}