Skip to content

Commit 388acf0

Browse files
authored
feat: add --indent option (#75)
1 parent c42d58c commit 388acf0

File tree

6 files changed

+84
-54
lines changed

6 files changed

+84
-54
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ FLAGS:
6565
--expand-opt Expand '?' (optional) to 'nil' type
6666
6767
OPTIONS:
68+
-i, --indent <u8> Controls the indent width [default: 4]
6869
-l, --layout <layout> Vimdoc text layout [default: 'default']
6970
- "default" : Default layout
7071
- "compact[:n=0]" : Aligns [desc] with <type>

src/cli.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use lemmy_help::{vimdoc::VimDoc, FromEmmy, Layout, LemmyHelp, Settings};
22

33
use lexopt::{
44
Arg::{Long, Short, Value},
5-
Parser,
5+
Parser, ValueExt,
66
};
77
use std::{ffi::OsString, fs::read_to_string, path::PathBuf, str::FromStr};
88

@@ -55,6 +55,9 @@ impl Cli {
5555
value: l.into(),
5656
})?;
5757
}
58+
Short('i') | Long("indent") => {
59+
c.settings.indent_width = parser.value()?.parse()?;
60+
}
5861
Short('M') | Long("no-modeline") => c.modeline = false,
5962
Short('f') | Long("prefix-func") => c.settings.prefix_func = true,
6063
Short('a') | Long("prefix-alias") => c.settings.prefix_alias = true,
@@ -117,6 +120,7 @@ FLAGS:
117120
--expand-opt Expand '?' (optional) to 'nil' type
118121
119122
OPTIONS:
123+
-i, --indent <u8> Controls the indent width [default: 4]
120124
-l, --layout <layout> Vimdoc text layout [default: 'default']
121125
- "default" : Default layout
122126
- "compact[:n=0]" : Aligns [desc] with <type>

src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl FromStr for Layout {
7979
}
8080
}
8181

82-
#[derive(Debug, Default)]
82+
#[derive(Debug)]
8383
pub struct Settings {
8484
/// Prefix `function` name with `---@mod` name
8585
pub prefix_func: bool,
@@ -93,6 +93,22 @@ pub struct Settings {
9393
pub expand_opt: bool,
9494
/// Vimdoc text layout
9595
pub layout: Layout,
96+
/// Controls the indent width
97+
pub indent_width: usize,
98+
}
99+
100+
impl Default for Settings {
101+
fn default() -> Self {
102+
Self {
103+
prefix_func: false,
104+
prefix_alias: false,
105+
prefix_class: false,
106+
prefix_type: false,
107+
expand_opt: false,
108+
layout: Layout::default(),
109+
indent_width: 4,
110+
}
111+
}
96112
}
97113

98114
#[derive(Debug, Default)]

src/vimdoc.rs

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use crate::{
66
Accept, FromEmmy, Layout, Settings, Visitor,
77
};
88

9+
/// Text Width
10+
const TW: usize = 80;
11+
912
#[derive(Debug)]
1013
pub struct VimDoc(String);
1114

@@ -21,15 +24,15 @@ impl Visitor for VimDoc {
2124
doc.push_str(&format!(
2225
"{:>w$}",
2326
format!("*{}*", n.name),
24-
w = 80 - desc.len()
27+
w = TW - desc.len()
2528
));
2629
doc.push('\n');
2730
doc
2831
}
2932

3033
fn divider(&self, n: &crate::parser::Divider, _: &Self::S) -> Self::R {
31-
let mut doc = String::with_capacity(81);
32-
for _ in 0..80 {
34+
let mut doc = String::with_capacity(TW - 1);
35+
for _ in 0..TW - 2 {
3336
doc.push(n.0);
3437
}
3538
doc.push('\n');
@@ -43,7 +46,7 @@ impl Visitor for VimDoc {
4346
}
4447

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

4952
fn func(&self, n: &crate::parser::Func, s: &Self::S) -> Self::R {
@@ -68,16 +71,16 @@ impl Visitor for VimDoc {
6871
&format!("{}{}", n.prefix.right.as_deref().unwrap_or_default(), n.op),
6972
));
7073
if !n.desc.is_empty() {
71-
doc.push_str(&description(&n.desc.join("\n")))
74+
doc.push_str(&description(&n.desc.join("\n"), s.indent_width))
7275
}
7376
doc.push('\n');
7477
if !n.params.is_empty() {
75-
doc.push_str(&description("Parameters: ~"));
78+
doc.push_str(&description("Parameters: ~", s.indent_width));
7679
doc.push_str(&self.params(&n.params, s));
7780
doc.push('\n');
7881
}
7982
if !n.returns.is_empty() {
80-
doc.push_str(&description("Returns: ~"));
83+
doc.push_str(&description("Returns: ~", s.indent_width));
8184
doc.push_str(&self.returns(&n.returns, s));
8285
doc.push('\n');
8386
}
@@ -91,7 +94,7 @@ impl Visitor for VimDoc {
9194
}
9295

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

124127
fn r#returns(&self, n: &[crate::parser::Return], s: &Self::S) -> Self::R {
125-
let mut table = Table::new();
128+
let mut table = Table::new(s.indent_width);
126129
for entry in n {
127130
if let Layout::Mini(n) = s.layout {
128131
table.add_row([format!(
@@ -163,11 +166,11 @@ impl Visitor for VimDoc {
163166
doc.push_str(&header(&name, &n.name));
164167
}
165168
if !n.desc.is_empty() {
166-
doc.push_str(&description(&n.desc.join("\n")));
169+
doc.push_str(&description(&n.desc.join("\n"), s.indent_width));
167170
}
168171
doc.push('\n');
169172
if !n.fields.is_empty() {
170-
doc.push_str(&description("Fields: ~"));
173+
doc.push_str(&description("Fields: ~", s.indent_width));
171174
doc.push_str(&self.fields(&n.fields, s));
172175
doc.push('\n');
173176
}
@@ -178,7 +181,7 @@ impl Visitor for VimDoc {
178181
}
179182

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

213-
fn alias(&self, n: &crate::parser::Alias, _: &Self::S) -> Self::R {
216+
fn alias(&self, n: &crate::parser::Alias, s: &Self::S) -> Self::R {
214217
let mut doc = String::new();
215218
if let Some(prefix) = &n.prefix.right {
216219
doc.push_str(&header(&n.name, &format!("{prefix}.{}", n.name)));
217220
} else {
218221
doc.push_str(&header(&n.name, &n.name));
219222
}
220223
if !n.desc.is_empty() {
221-
doc.push_str(&description(&n.desc.join("\n")));
224+
doc.push_str(&description(&n.desc.join("\n"), s.indent_width));
222225
}
223226
doc.push('\n');
224227
match &n.kind {
225228
AliasKind::Type(ty) => {
226-
doc.push_str(&description("Type: ~"));
227-
let ty = ty.to_string();
228-
doc.push_str(&format!("{:>w$}", ty, w = 8 + ty.len()));
229+
doc.push_str(&description("Type: ~", s.indent_width));
230+
doc.push_str(&(" ").repeat(s.indent_width * 2));
231+
doc.push_str(&ty.to_string());
229232
doc.push('\n');
230233
}
231234
AliasKind::Enum(variants) => {
232-
doc.push_str(&description("Variants: ~"));
233-
let mut table = Table::new();
235+
doc.push_str(&description("Variants: ~", s.indent_width));
236+
let mut table = Table::new(s.indent_width);
234237
for (ty, desc) in variants {
235238
table.add_row([&format!("({})", ty), desc.as_deref().unwrap_or_default()]);
236239
}
@@ -249,11 +252,11 @@ impl Visitor for VimDoc {
249252
));
250253
let (extract, desc) = &n.desc;
251254
if !extract.is_empty() {
252-
doc.push_str(&description(&extract.join("\n")));
255+
doc.push_str(&description(&extract.join("\n"), s.indent_width));
253256
}
254257
doc.push('\n');
255-
doc.push_str(&description("Type: ~"));
256-
let mut table = Table::new();
258+
doc.push_str(&description("Type: ~", s.indent_width));
259+
let mut table = Table::new(s.indent_width);
257260
table.add_row([&format!("({})", n.ty), desc.as_deref().unwrap_or_default()]);
258261
doc.push_str(&table.to_string());
259262
doc.push('\n');
@@ -266,23 +269,27 @@ impl Visitor for VimDoc {
266269
doc
267270
}
268271

269-
fn see(&self, n: &crate::parser::See, _: &Self::S) -> Self::R {
272+
fn see(&self, n: &crate::parser::See, s: &Self::S) -> Self::R {
270273
let mut doc = String::new();
271-
doc.push_str(&description("See: ~"));
272-
for s in &n.refs {
273-
doc.push_str(&format!(" |{s}|\n"));
274+
doc.push_str(&description("See: ~", s.indent_width));
275+
for reff in &n.refs {
276+
doc.push_str(&(" ").repeat(s.indent_width * 2));
277+
doc.push_str(&format!("|{reff}|\n"));
274278
}
275279
doc.push('\n');
276280
doc
277281
}
278282

279-
fn usage(&self, n: &crate::parser::Usage, _: &Self::S) -> Self::R {
283+
fn usage(&self, n: &crate::parser::Usage, s: &Self::S) -> Self::R {
280284
let mut doc = String::new();
281-
doc.push_str(&description("Usage: ~"));
285+
doc.push_str(&description("Usage: ~", s.indent_width));
282286
doc.push('>');
283287
doc.push_str(n.lang.as_deref().unwrap_or("lua"));
284288
doc.push('\n');
285-
doc.push_str(&textwrap::indent(&n.code, " "));
289+
doc.push_str(&textwrap::indent(
290+
&n.code,
291+
&(" ").repeat(s.indent_width * 2),
292+
));
286293
doc.push_str("\n<\n\n");
287294
doc
288295
}
@@ -337,15 +344,14 @@ impl Display for VimDoc {
337344

338345
// #################
339346

340-
struct Table(comfy_table::Table);
347+
struct Table(comfy_table::Table, usize);
341348

342349
impl Table {
343-
pub fn new() -> Self {
350+
pub fn new(ident: usize) -> Self {
344351
let mut tbl = comfy_table::Table::new();
345352
tbl.load_preset(comfy_table::presets::NOTHING);
346353
// tbl.column_iter_mut().map(|c| c.set_padding((0, 0)));
347-
348-
Self(tbl)
354+
Self(tbl, ident)
349355
}
350356

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

357363
impl std::fmt::Display for Table {
358364
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
359-
f.write_str(&textwrap::indent(&self.0.trim_fmt(), " "))?;
365+
f.write_str(&textwrap::indent(
366+
&self.0.trim_fmt(),
367+
&(" ").repeat((self.1 * 2) - 1),
368+
))?;
360369
f.write_str("\n")
361370
}
362371
}
363372

364373
#[inline]
365-
fn description(desc: &str) -> String {
366-
let mut d = textwrap::indent(desc, " ");
374+
fn description(desc: &str, indent: usize) -> String {
375+
let mut d = textwrap::indent(desc, &(" ").repeat(indent));
367376
d.push('\n');
368377
d
369378
}
@@ -372,7 +381,7 @@ fn description(desc: &str) -> String {
372381
fn header(name: &str, tag: &str) -> String {
373382
let len = name.len();
374383
if len > 40 || tag.len() > 40 {
375-
return format!("{:>80}\n{}\n", format!("*{}*", tag), name);
384+
return format!("{:>w$}\n{}\n", format!("*{}*", tag), name, w = TW);
376385
}
377-
format!("{}{:>w$}\n", name, format!("*{}*", tag), w = 80 - len)
386+
format!("{}{:>w$}\n", name, format!("*{}*", tag), w = TW - len)
378387
}

tests/basic.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ fn divider_and_tag() {
8888
assert_eq!(
8989
lemmy!(src),
9090
"\
91-
================================================================================
91+
==============================================================================
9292
9393
*kinda.module*
94-
--------------------------------------------------------------------------------
94+
------------------------------------------------------------------------------
9595
9696
*kinda.section*
9797
"
@@ -470,7 +470,7 @@ fn module() {
470470
assert_eq!(
471471
lemmy!(src, src2),
472472
"\
473-
================================================================================
473+
==============================================================================
474474
Introduction *mod.intro*
475475
476476
@@ -479,7 +479,7 @@ This is for the cases where you want bunch of block only just for text
479479
and does not contains any code.
480480
481481
482-
================================================================================
482+
==============================================================================
483483
Human module *mod.Human*
484484
485485
Human *Human*
@@ -513,7 +513,7 @@ U:create() *U:create*
513513
<
514514
515515
516-
================================================================================
516+
==============================================================================
517517
*wo.desc*
518518
519519
"
@@ -539,20 +539,20 @@ fn table_of_contents() {
539539
assert_eq!(
540540
lemmy!(src),
541541
"\
542-
================================================================================
542+
==============================================================================
543543
Table of Contents *my-plugin.contents*
544544
545-
First Module······················································|first.module|
546-
Second Module····················································|second.module|
547-
Third Module······················································|third.module|
545+
First Module ···················································· |first.module|
546+
Second Module ·················································· |second.module|
547+
Third Module ···················································· |third.module|
548548
549-
================================================================================
549+
==============================================================================
550550
First Module *first.module*
551551
552-
================================================================================
552+
==============================================================================
553553
Second Module *second.module*
554554
555-
================================================================================
555+
==============================================================================
556556
Third Module *third.module*
557557
558558
"
@@ -777,7 +777,7 @@ fn export() {
777777
assert_eq!(
778778
lemmy!(src),
779779
"\
780-
================================================================================
780+
==============================================================================
781781
Configuration *module.config*
782782
783783
Config:get() *Config:get*

tests/with_settings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ fn rename_with_mod() {
9595
assert_eq!(
9696
VimDoc::from_emmy(&lemmy, &s).to_string(),
9797
"\
98-
================================================================================
98+
==============================================================================
9999
This is working *awesome*
100100
101101
ID *awesome.ID*

0 commit comments

Comments
 (0)