Skip to content

Commit

Permalink
feat: add --indent option
Browse files Browse the repository at this point in the history
  • Loading branch information
numToStr committed Dec 30, 2022
1 parent c42d58c commit cec4400
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 54 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ FLAGS:
--expand-opt Expand '?' (optional) to 'nil' type
OPTIONS:
-i, --indent <u8> Controls the indent width [default: 4]
-l, --layout <layout> Vimdoc text layout [default: 'default']
- "default" : Default layout
- "compact[:n=0]" : Aligns [desc] with <type>
Expand Down
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use lemmy_help::{vimdoc::VimDoc, FromEmmy, Layout, LemmyHelp, Settings};

use lexopt::{
Arg::{Long, Short, Value},
Parser,
Parser, ValueExt,
};
use std::{ffi::OsString, fs::read_to_string, path::PathBuf, str::FromStr};

Expand Down Expand Up @@ -55,6 +55,9 @@ impl Cli {
value: l.into(),
})?;
}
Short('i') | Long("indent") => {
c.settings.indent_width = parser.value()?.parse()?;
}
Short('M') | Long("no-modeline") => c.modeline = false,
Short('f') | Long("prefix-func") => c.settings.prefix_func = true,
Short('a') | Long("prefix-alias") => c.settings.prefix_alias = true,
Expand Down Expand Up @@ -117,6 +120,7 @@ FLAGS:
--expand-opt Expand '?' (optional) to 'nil' type
OPTIONS:
-i, --indent <u8> Controls the indent width [default: 4]
-l, --layout <layout> Vimdoc text layout [default: 'default']
- "default" : Default layout
- "compact[:n=0]" : Aligns [desc] with <type>
Expand Down
18 changes: 17 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl FromStr for Layout {
}
}

#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Settings {
/// Prefix `function` name with `---@mod` name
pub prefix_func: bool,
Expand All @@ -93,6 +93,22 @@ pub struct Settings {
pub expand_opt: bool,
/// Vimdoc text layout
pub layout: Layout,
/// Controls the indent width
pub indent_width: usize,
}

impl Default for Settings {
fn default() -> Self {
Self {
prefix_func: false,
prefix_alias: false,
prefix_class: false,
prefix_type: false,
expand_opt: false,
layout: Layout::default(),
indent_width: 4,
}
}
}

#[derive(Debug, Default)]
Expand Down
85 changes: 47 additions & 38 deletions src/vimdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::{
Accept, FromEmmy, Layout, Settings, Visitor,
};

/// Text Width
const TW: usize = 80;

#[derive(Debug)]
pub struct VimDoc(String);

Expand All @@ -21,15 +24,15 @@ impl Visitor for VimDoc {
doc.push_str(&format!(
"{:>w$}",
format!("*{}*", n.name),
w = 80 - desc.len()
w = TW - desc.len()
));
doc.push('\n');
doc
}

fn divider(&self, n: &crate::parser::Divider, _: &Self::S) -> Self::R {
let mut doc = String::with_capacity(81);
for _ in 0..80 {
let mut doc = String::with_capacity(TW - 1);
for _ in 0..TW - 2 {
doc.push(n.0);
}
doc.push('\n');
Expand All @@ -43,7 +46,7 @@ impl Visitor for VimDoc {
}

fn tag(&self, n: &crate::parser::Tag, _: &Self::S) -> Self::R {
format!("{:>80}", format!("*{}*", n.0))
format!("{:>w$}", format!("*{}*", n.0), w = TW)
}

fn func(&self, n: &crate::parser::Func, s: &Self::S) -> Self::R {
Expand All @@ -68,16 +71,16 @@ impl Visitor for VimDoc {
&format!("{}{}", n.prefix.right.as_deref().unwrap_or_default(), n.op),
));
if !n.desc.is_empty() {
doc.push_str(&description(&n.desc.join("\n")))
doc.push_str(&description(&n.desc.join("\n"), s.indent_width))
}
doc.push('\n');
if !n.params.is_empty() {
doc.push_str(&description("Parameters: ~"));
doc.push_str(&description("Parameters: ~", s.indent_width));
doc.push_str(&self.params(&n.params, s));
doc.push('\n');
}
if !n.returns.is_empty() {
doc.push_str(&description("Returns: ~"));
doc.push_str(&description("Returns: ~", s.indent_width));
doc.push_str(&self.returns(&n.returns, s));
doc.push('\n');
}
Expand All @@ -91,7 +94,7 @@ impl Visitor for VimDoc {
}

fn params(&self, n: &[crate::parser::Param], s: &Self::S) -> Self::R {
let mut table = Table::new();
let mut table = Table::new(s.indent_width);
for param in n {
let (name, ty) = match (s.expand_opt, &param.name) {
(true, Name::Opt(n)) => (format!("{{{n}}}"), format!("(nil|{})", param.ty)),
Expand Down Expand Up @@ -122,7 +125,7 @@ impl Visitor for VimDoc {
}

fn r#returns(&self, n: &[crate::parser::Return], s: &Self::S) -> Self::R {
let mut table = Table::new();
let mut table = Table::new(s.indent_width);
for entry in n {
if let Layout::Mini(n) = s.layout {
table.add_row([format!(
Expand Down Expand Up @@ -163,11 +166,11 @@ impl Visitor for VimDoc {
doc.push_str(&header(&name, &n.name));
}
if !n.desc.is_empty() {
doc.push_str(&description(&n.desc.join("\n")));
doc.push_str(&description(&n.desc.join("\n"), s.indent_width));
}
doc.push('\n');
if !n.fields.is_empty() {
doc.push_str(&description("Fields: ~"));
doc.push_str(&description("Fields: ~", s.indent_width));
doc.push_str(&self.fields(&n.fields, s));
doc.push('\n');
}
Expand All @@ -178,7 +181,7 @@ impl Visitor for VimDoc {
}

fn fields(&self, n: &[crate::parser::Field], s: &Self::S) -> Self::R {
let mut table = Table::new();
let mut table = Table::new(s.indent_width);
for field in n {
let (name, ty) = match (s.expand_opt, &field.name) {
(true, Name::Opt(n)) => (format!("{{{n}}}"), format!("(nil|{})", field.ty)),
Expand Down Expand Up @@ -210,27 +213,27 @@ impl Visitor for VimDoc {
table.to_string()
}

fn alias(&self, n: &crate::parser::Alias, _: &Self::S) -> Self::R {
fn alias(&self, n: &crate::parser::Alias, s: &Self::S) -> Self::R {
let mut doc = String::new();
if let Some(prefix) = &n.prefix.right {
doc.push_str(&header(&n.name, &format!("{prefix}.{}", n.name)));
} else {
doc.push_str(&header(&n.name, &n.name));
}
if !n.desc.is_empty() {
doc.push_str(&description(&n.desc.join("\n")));
doc.push_str(&description(&n.desc.join("\n"), s.indent_width));
}
doc.push('\n');
match &n.kind {
AliasKind::Type(ty) => {
doc.push_str(&description("Type: ~"));
let ty = ty.to_string();
doc.push_str(&format!("{:>w$}", ty, w = 8 + ty.len()));
doc.push_str(&description("Type: ~", s.indent_width));
doc.push_str(&(" ").repeat(s.indent_width * 2));
doc.push_str(&ty.to_string());
doc.push('\n');
}
AliasKind::Enum(variants) => {
doc.push_str(&description("Variants: ~"));
let mut table = Table::new();
doc.push_str(&description("Variants: ~", s.indent_width));
let mut table = Table::new(s.indent_width);
for (ty, desc) in variants {
table.add_row([&format!("({})", ty), desc.as_deref().unwrap_or_default()]);
}
Expand All @@ -249,11 +252,11 @@ impl Visitor for VimDoc {
));
let (extract, desc) = &n.desc;
if !extract.is_empty() {
doc.push_str(&description(&extract.join("\n")));
doc.push_str(&description(&extract.join("\n"), s.indent_width));
}
doc.push('\n');
doc.push_str(&description("Type: ~"));
let mut table = Table::new();
doc.push_str(&description("Type: ~", s.indent_width));
let mut table = Table::new(s.indent_width);
table.add_row([&format!("({})", n.ty), desc.as_deref().unwrap_or_default()]);
doc.push_str(&table.to_string());
doc.push('\n');
Expand All @@ -266,23 +269,27 @@ impl Visitor for VimDoc {
doc
}

fn see(&self, n: &crate::parser::See, _: &Self::S) -> Self::R {
fn see(&self, n: &crate::parser::See, s: &Self::S) -> Self::R {
let mut doc = String::new();
doc.push_str(&description("See: ~"));
for s in &n.refs {
doc.push_str(&format!(" |{s}|\n"));
doc.push_str(&description("See: ~", s.indent_width));
for reff in &n.refs {
doc.push_str(&(" ").repeat(s.indent_width * 2));
doc.push_str(&format!("|{reff}|\n"));
}
doc.push('\n');
doc
}

fn usage(&self, n: &crate::parser::Usage, _: &Self::S) -> Self::R {
fn usage(&self, n: &crate::parser::Usage, s: &Self::S) -> Self::R {
let mut doc = String::new();
doc.push_str(&description("Usage: ~"));
doc.push_str(&description("Usage: ~", s.indent_width));
doc.push('>');
doc.push_str(n.lang.as_deref().unwrap_or("lua"));
doc.push('\n');
doc.push_str(&textwrap::indent(&n.code, " "));
doc.push_str(&textwrap::indent(
&n.code,
&(" ").repeat(s.indent_width * 2),
));
doc.push_str("\n<\n\n");
doc
}
Expand Down Expand Up @@ -337,15 +344,14 @@ impl Display for VimDoc {

// #################

struct Table(comfy_table::Table);
struct Table(comfy_table::Table, usize);

impl Table {
pub fn new() -> Self {
pub fn new(ident: usize) -> Self {
let mut tbl = comfy_table::Table::new();
tbl.load_preset(comfy_table::presets::NOTHING);
// tbl.column_iter_mut().map(|c| c.set_padding((0, 0)));

Self(tbl)
Self(tbl, ident)
}

pub fn add_row<T: Into<comfy_table::Row>>(&mut self, row: T) -> &Self {
Expand All @@ -356,14 +362,17 @@ impl Table {

impl std::fmt::Display for Table {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&textwrap::indent(&self.0.trim_fmt(), " "))?;
f.write_str(&textwrap::indent(
&self.0.trim_fmt(),
&(" ").repeat((self.1 * 2) - 1),
))?;
f.write_str("\n")
}
}

#[inline]
fn description(desc: &str) -> String {
let mut d = textwrap::indent(desc, " ");
fn description(desc: &str, indent: usize) -> String {
let mut d = textwrap::indent(desc, &(" ").repeat(indent));
d.push('\n');
d
}
Expand All @@ -372,7 +381,7 @@ fn description(desc: &str) -> String {
fn header(name: &str, tag: &str) -> String {
let len = name.len();
if len > 40 || tag.len() > 40 {
return format!("{:>80}\n{}\n", format!("*{}*", tag), name);
return format!("{:>w$}\n{}\n", format!("*{}*", tag), name, w = TW);
}
format!("{}{:>w$}\n", name, format!("*{}*", tag), w = 80 - len)
format!("{}{:>w$}\n", name, format!("*{}*", tag), w = TW - len)
}
26 changes: 13 additions & 13 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ fn divider_and_tag() {
assert_eq!(
lemmy!(src),
"\
================================================================================
==============================================================================
*kinda.module*
--------------------------------------------------------------------------------
------------------------------------------------------------------------------
*kinda.section*
"
Expand Down Expand Up @@ -470,7 +470,7 @@ fn module() {
assert_eq!(
lemmy!(src, src2),
"\
================================================================================
==============================================================================
Introduction *mod.intro*
Expand All @@ -479,7 +479,7 @@ This is for the cases where you want bunch of block only just for text
and does not contains any code.
================================================================================
==============================================================================
Human module *mod.Human*
Human *Human*
Expand Down Expand Up @@ -513,7 +513,7 @@ U:create() *U:create*
<
================================================================================
==============================================================================
*wo.desc*
"
Expand All @@ -539,20 +539,20 @@ fn table_of_contents() {
assert_eq!(
lemmy!(src),
"\
================================================================================
==============================================================================
Table of Contents *my-plugin.contents*
First Module······················································|first.module|
Second Module····················································|second.module|
Third Module······················································|third.module|
First Module ···················································· |first.module|
Second Module ·················································· |second.module|
Third Module ···················································· |third.module|
================================================================================
==============================================================================
First Module *first.module*
================================================================================
==============================================================================
Second Module *second.module*
================================================================================
==============================================================================
Third Module *third.module*
"
Expand Down Expand Up @@ -777,7 +777,7 @@ fn export() {
assert_eq!(
lemmy!(src),
"\
================================================================================
==============================================================================
Configuration *module.config*
Config:get() *Config:get*
Expand Down
2 changes: 1 addition & 1 deletion tests/with_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn rename_with_mod() {
assert_eq!(
VimDoc::from_emmy(&lemmy, &s).to_string(),
"\
================================================================================
==============================================================================
This is working *awesome*
ID *awesome.ID*
Expand Down

0 comments on commit cec4400

Please sign in to comment.