From d8ce241cf5645dcdc51e81d3b9c6b5e4bd0cead1 Mon Sep 17 00:00:00 2001 From: Domenic Quirl Date: Sun, 21 Feb 2021 20:18:41 +0100 Subject: [PATCH] derive impl `Display` for `NodeOrToken` --- src/utility_types.rs | 11 +++++++++++ tests/basic.rs | 16 +++++++++++++++- tests/common.rs | 3 +++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/utility_types.rs b/src/utility_types.rs index 41ea079..88ed89e 100644 --- a/src/utility_types.rs +++ b/src/utility_types.rs @@ -1,3 +1,5 @@ +use std::fmt; + /// Convenience type to represent tree elements which may either be a node or a token. /// /// Used for both red and green tree, references to elements, ... @@ -53,6 +55,15 @@ impl NodeOrToken<&N, &T> { } } +impl fmt::Display for NodeOrToken { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + NodeOrToken::Node(node) => node.fmt(f), + NodeOrToken::Token(token) => token.fmt(f), + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Direction { Next, diff --git a/tests/basic.rs b/tests/basic.rs index f9c2fdd..8aeb207 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,6 +1,8 @@ mod common; -use common::{build_recursive, build_tree_with_cache, Element, SyntaxNode}; +use common::{ + build_recursive, build_tree_with_cache, Element, SyntaxElement, SyntaxElementRef, SyntaxNode, SyntaxToken, +}; use cstree::{GreenNodeBuilder, NodeCache, SyntaxKind, TextRange}; use lasso::{Resolver, Rodeo}; @@ -129,3 +131,15 @@ fn inline_resolver() { ); } } + +#[test] +fn assert_debug_display() { + use std::fmt; + fn f() {} + + f::>(); + f::>(); + f::>(); + f::>(); + f::>(); +} diff --git a/tests/common.rs b/tests/common.rs index ffd340a..178c2c9 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -2,6 +2,9 @@ use cstree::{GreenNode, GreenNodeBuilder, Language, NodeCache, SyntaxKind}; use lasso::Interner; pub type SyntaxNode = cstree::SyntaxNode; +pub type SyntaxToken = cstree::SyntaxToken; +pub type SyntaxElement = cstree::SyntaxElement; +pub type SyntaxElementRef<'a, D = (), R = ()> = cstree::SyntaxElementRef<'a, TestLang, D, R>; #[derive(Debug)] pub enum Element<'s> {