Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/display.rs')
| -rw-r--r-- | crates/syntax/src/display.rs | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/crates/syntax/src/display.rs b/crates/syntax/src/display.rs index 95e37944cc..e2115cbd2b 100644 --- a/crates/syntax/src/display.rs +++ b/crates/syntax/src/display.rs @@ -1,10 +1,6 @@ -//! This module contains utilities for turning SyntaxNodes and HIR types -//! into types that may be used to render in a UI. +//! This module contains utilities for rendering syntax nodes into a string representing their signature. -use crate::{ - ast::{self, AstNode, HasAttrs, HasGenericParams, HasName}, - SyntaxKind::{ATTR, COMMENT}, -}; +use crate::ast::{self, HasAttrs, HasGenericParams, HasName}; use ast::HasVisibility; use stdx::format_to; @@ -55,25 +51,39 @@ pub fn function_declaration(node: &ast::Fn) -> String { } pub fn const_label(node: &ast::Const) -> 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() + let mut s = String::new(); + if let Some(vis) = node.visibility() { + format_to!(s, "{} ", vis); + } + format_to!(s, "const "); + if let Some(name) = node.name() { + format_to!(s, "{}", name); + } else { + format_to!(s, "?"); + } + format_to!(s, ": "); + if let Some(ty) = node.ty() { + format_to!(s, "{}", ty); + } else { + format_to!(s, "?"); + } + format_to!(s, ";"); + s } pub fn type_label(node: &ast::TypeAlias) -> 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() + let mut s = String::new(); + if let Some(vis) = node.visibility() { + format_to!(s, "{} ", vis); + } + format_to!(s, "type "); + if let Some(name) = node.name() { + format_to!(s, "{}", name); + } else { + format_to!(s, "?"); + } + format_to!(s, ";"); + s } pub fn macro_label(node: &ast::Macro) -> String { |