Skip to content

Commit

Permalink
chore: no public Tag and related items; reexport StyledType etc
Browse files Browse the repository at this point in the history
  • Loading branch information
zjp-CN committed Mar 22, 2024
1 parent 94fa408 commit 1ab5f69
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 32 deletions.
7 changes: 2 additions & 5 deletions src/bin/page/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use ratatui::{
};
use term_rustdoc::{
tree::{CrateDoc, IDMap},
type_name::{
render::{DeclarationLine, DeclarationLines},
style::item_styled,
},
type_name::{DeclarationLine, DeclarationLines},
};

#[derive(Default)]
Expand Down Expand Up @@ -145,7 +142,7 @@ impl LineState for DeclarationLine {

impl Declaration {
fn update_decl(&mut self, id: &str, map: &IDMap, width: u16) {
let lines = item_styled(id, map).to_declaration_lines();
let lines = DeclarationLines::new(id, map);
if lines.is_empty() {
self.display.scroll_text().lines = Default::default();
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/type_name/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod render;
pub mod style;
mod render;
pub(crate) mod style;

pub use render::{DeclarationLine, DeclarationLines, TextTag};
pub use style::StyledType;
14 changes: 12 additions & 2 deletions src/type_name/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::style::{StyledType, Tag};
use crate::{
tree::ID,
tree::{IDMap, ID},
type_name::style::{Punctuation, Symbol},
util::XString,
};
Expand Down Expand Up @@ -28,8 +28,18 @@ impl std::ops::Deref for DeclarationLines {
}
}

impl From<&StyledType> for DeclarationLines {
fn from(value: &StyledType) -> Self {
Self::new_(value)
}
}

impl DeclarationLines {
pub fn new(styled_type: &StyledType) -> Self {
pub fn new(id: &str, map: &IDMap) -> Self {
Self::new_(&StyledType::new(id, map))
}

fn new_(styled_type: &StyledType) -> Self {
let tags = styled_type.tags();
let mut lines = Vec::with_capacity(8);
let mut line = Vec::with_capacity(8);
Expand Down
8 changes: 7 additions & 1 deletion src/type_name/style/decl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
use crate::tree::IDMap;
use rustdoc_types::{ItemEnum, Visibility};

pub fn item_styled(id: &str, map: &IDMap) -> StyledType {
fn item_styled(id: &str, map: &IDMap) -> StyledType {
if let Some(item) = map.get_item(id) {
let vis_name_map = VisNameMap {
name: item.name.as_deref().unwrap_or(""),
Expand All @@ -31,6 +31,12 @@ pub fn item_styled(id: &str, map: &IDMap) -> StyledType {
StyledType::default()
}

impl StyledType {
pub fn new(id: &str, map: &IDMap) -> Self {
item_styled(id, map)
}
}

impl Format for Visibility {
fn format<Kind: FindName>(&self, buf: &mut StyledType) {
buf.write(match self {
Expand Down
14 changes: 5 additions & 9 deletions src/type_name/style/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(unused)]
mod decl;
mod function;
mod generics;
Expand All @@ -12,7 +13,6 @@ use crate::{
};
use std::fmt;

pub use decl::item_styled;
pub use path::{long, long_path};

#[derive(Default, Clone, Debug)]
Expand All @@ -30,13 +30,13 @@ impl fmt::Display for StyledType {
}

impl StyledType {
pub fn with_capacity(cap: usize) -> Self {
fn with_capacity(cap: usize) -> Self {
StyledType {
inner: Vec::with_capacity(cap),
}
}

pub fn tags(&self) -> &[Tag] {
pub(crate) fn tags(&self) -> &[Tag] {
&self.inner
}

Expand All @@ -55,10 +55,6 @@ impl StyledType {
buf
}

pub fn to_declaration_lines(&self) -> DeclarationLines {
DeclarationLines::new(self)
}

fn write_id_name(&mut self, id: impl IdToID, name: &str) {
self.write_span_path_name(|buf| {
buf.write(Tag::Path(id.to_ID()));
Expand Down Expand Up @@ -93,7 +89,7 @@ macro_rules! impl_write_tag {
/// Write stuff in enclosing [`Punctuation`]s.
impl StyledType { $(
#[doc = concat!("Write `", $s, "` where x is written from the callback.")]
pub fn $fname(&mut self, f: impl FnOnce(&mut StyledType)) {
fn $fname(&mut self, f: impl FnOnce(&mut StyledType)) {
let start = Tag::Symbol(Symbol::Punctuation(Punctuation::$start));
let end = Tag::Symbol(Symbol::Punctuation(Punctuation::$end));
self.write_enclosing_tag(start, end, f);
Expand All @@ -105,7 +101,7 @@ macro_rules! impl_write_tag {
impl StyledType { $(
#[doc = concat!("Write `", $s, "` as [`Span::", stringify!($span),
"`] between [`Tag::Start`] and [`Tag::End`] with the callback.")]
pub fn $fname(&mut self, f: impl FnOnce(&mut StyledType)) {
fn $fname(&mut self, f: impl FnOnce(&mut StyledType)) {
let start = Tag::Start(Span::$span);
let end = Tag::End(Span::$span);
self.write_enclosing_tag(start, end, f);
Expand Down
23 changes: 10 additions & 13 deletions tests/parse-json-docs/fn_item_decl.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{doc, shot, snap};
use term_rustdoc::{
tree::{DModule, IDMap},
type_name::{
render::DeclarationLines,
style::{item_styled, StyledType},
},
type_name::{DeclarationLines, StyledType},
};

#[test]
Expand All @@ -13,7 +10,7 @@ fn fn_items() {
let dmod = map.dmodule();
let mut styled_fn = Vec::with_capacity(24);
for fn_ in &dmod.functions {
styled_fn.push(item_styled(&fn_.id, map));
styled_fn.push(StyledType::new(&fn_.id, map));
}
shot!(DisplaySlice(&styled_fn), @r###"
pub fn func_dyn_trait(d: &(dyn ATrait + Send + Sync)) -> &dyn ATrait
Expand Down Expand Up @@ -50,7 +47,7 @@ fn fn_items() {
)
"###);

let lines = Vec::from_iter(styled_fn.iter().map(DeclarationLines::new));
let lines = Vec::from_iter(styled_fn.iter().map(DeclarationLines::from));
snap!("DeclarationLines-fn-items", DisplaySlice(&lines));
}

Expand All @@ -62,22 +59,22 @@ fn methods() {
let mut fns_str = Vec::with_capacity(16);
for struct_ in &dmod.structs {
for inh in &*struct_.impls.merged_inherent.functions {
fns_str.push(item_styled(&inh.id, map));
fns_str.push(StyledType::new(&inh.id, map));
}
}
for enum_ in &dmod.enums {
for inh in &*enum_.impls.merged_inherent.functions {
fns_str.push(item_styled(&inh.id, map));
fns_str.push(StyledType::new(&inh.id, map));
}
}
for union_ in &dmod.unions {
for inh in &*union_.impls.merged_inherent.functions {
fns_str.push(item_styled(&inh.id, map));
fns_str.push(StyledType::new(&inh.id, map));
}
}
for trait_ in &dmod.traits {
for fn_ in &*trait_.functions {
fns_str.push(item_styled(&fn_.id, map));
fns_str.push(StyledType::new(&fn_.id, map));
}
}
shot!(DisplaySlice(&fns_str), @r###"
Expand All @@ -89,7 +86,7 @@ fn methods() {
fn return_assoc(&self) -> Self::Assoc<'_>;
"###);

let lines = Vec::from_iter(fns_str.iter().map(DeclarationLines::new));
let lines = Vec::from_iter(fns_str.iter().map(DeclarationLines::from));
snap!("DeclarationLines-methods", DisplaySlice(&lines));
}

Expand Down Expand Up @@ -163,13 +160,13 @@ fn structs() {
pub struct AUnitStruct;
"###);

let lines = Vec::from_iter(structs_str.iter().map(DeclarationLines::new));
let lines = Vec::from_iter(structs_str.iter().map(DeclarationLines::from));
snap!("DeclarationLines-structs", DisplaySlice(&lines));
}

fn recursive_struct_str(dmod: &DModule, structs_str: &mut Vec<StyledType>, map: &IDMap) {
for struct_ in &dmod.structs {
structs_str.push(item_styled(&struct_.id, map));
structs_str.push(StyledType::new(&struct_.id, map));
}
for m in &dmod.modules {
recursive_struct_str(m, structs_str, map);
Expand Down

0 comments on commit 1ab5f69

Please sign in to comment.